Acwing 681. 疏散人群(dfs)(记录根节点下有几个子节点)

输入样例:
6
2 1
3 2
4 3
5 2
6 1
输出样例:
4
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL N=100200,M=2020;
const LL mod=998244353;
vector<LL> g[N];
LL sum[N];
LL dfs(LL idx,LL fa)
{
    LL res=1;
    for(int i=0;i<g[idx].size();i++)
    {
        LL flag=g[idx][i];
        if(flag==fa) continue;
        res+=dfs(flag,idx);
        //cout<<idx<<" "<<res<<endl;
    }
    return res;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        for(int i=1;i<n;i++)
        {
            LL x,y;
            cin>>x>>y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        LL maxn=0;
        for(int i=0;i<g[1].size();i++)
        {
            maxn=max(maxn,dfs(g[1][i],1));
        }
        cout<<maxn<<endl;
    }
    return 0;
}
posted @ 2024-04-08 16:13  高尔赛凡尔娟  阅读(3)  评论(0编辑  收藏  举报