- 子树节点个数
为什么错了
- 2024-6-23 11:44:53 @
有没有大佬帮我看看,这个真的好奇怪。就是过不了
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
int dfs(vector<vector<int>>&tree,vector<int>&ans,int node)
{
ans[node]=1;
if(tree[node].empty())
{
return ans[node];
}
for(int i=0;i<(int)tree[node].size();i++)
{
ans[node]+=dfs(tree,ans,tree[node][i]);
}
return ans[node];
}
int main()
{
int n;
cin>>n;
vector<vector<int>> tree(n+1);
vector<int> ans(n+1,0);
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
tree[a].push_back(b);
}
dfs(tree,ans,1);
for(int i=1;i<ans.size();i++)
{
cout<<ans[i]<<" ";
}
}
0 comments
No comments so far...
Information
- ID
- 58
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 4
- Tags
- # Submissions
- 415
- Accepted
- 25
- Uploaded By