米哈游笔试D卷编程C题
#include <iostream>
#include <vector>
using namespace std;
const int N = 2e5 + 5;
using ll = long long;
struct node {
int n;
ll deep;
ll ans;
};
vector<node> ans;
vector<vector<int>> g;
void dfs(int root, int fa = -1) {
for (auto &to : g[root]) {
if (to == fa) continue;
dfs(to, root);
ans[root].n += ans[to].n;
ans[root].deep += ans[to].deep + ans[to].n;
}
ans[root].n += 1;
for(auto &to : g[root]) {
if (to == fa) continue;
ans[root].ans += (ans[to].n + ans[to].deep) * (ans[root].n - ans[to].n) + ans[to].ans;
}
}
int main() {
int n, m;
cin >> n >> m;
g.resize(n + 1);
ans.resize(n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
while(m--) {
int x;
cin >> x;
cout << ans[x].ans << endl;
}
return 0;
}
笔试的时候 ans[root].ans += (ans[to].n + ans[to].deep) * (ans[root].n - ans[to].n) + ans[to].ans;忘记加ans[to].ans了,人麻了。
#米哈游笔试#
#include <vector>
using namespace std;
const int N = 2e5 + 5;
using ll = long long;
struct node {
int n;
ll deep;
ll ans;
};
vector<node> ans;
vector<vector<int>> g;
void dfs(int root, int fa = -1) {
for (auto &to : g[root]) {
if (to == fa) continue;
dfs(to, root);
ans[root].n += ans[to].n;
ans[root].deep += ans[to].deep + ans[to].n;
}
ans[root].n += 1;
for(auto &to : g[root]) {
if (to == fa) continue;
ans[root].ans += (ans[to].n + ans[to].deep) * (ans[root].n - ans[to].n) + ans[to].ans;
}
}
int main() {
int n, m;
cin >> n >> m;
g.resize(n + 1);
ans.resize(n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
while(m--) {
int x;
cin >> x;
cout << ans[x].ans << endl;
}
return 0;
}
笔试的时候 ans[root].ans += (ans[to].n + ans[to].deep) * (ans[root].n - ans[to].n) + ans[to].ans;忘记加ans[to].ans了,人麻了。
#米哈游笔试#
全部评论
相关推荐
03-01 21:45
中北大学 Python
孤蓝长空:请你说一下为什么你用websocket而不是http,请你说一下什么是rpc,为什么用rpc,你的rpc的传输协议是JSON,xml还是什么 请你描述一下你的鉴权流程(完整的) 我问的是第二个项目,随便问的哈哈哈 点赞 评论 收藏
分享
查看6道真题和解析 点赞 评论 收藏
分享
