The Largest Generation (25) (dfs)

题目大意:给你一颗树但节点序号是两位数表示,01也是允许的(事实上直接scanf就可以了)

让你求其拥有最大节点数的那一层和其节点数.

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define ull unsigned long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)

#define base 2333
const int mod=1e9+7;
const int maxn=2e2+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int n,m;
vector<int> g[maxn];
int dep[maxn],cnt1=0,cnt2;
void dfs(int u,int pre,int d){
    dep[d]++;
    if(cnt1<dep[d]) cnt1=dep[d],cnt2=d;
    rep(i,0,g[u].size()){
        int v=g[u][i];
        if(v==pre) continue;
        dfs(v,u,d+1);
    }
}
int main(){
    scanf("%d%d",&n,&m);
    rep(i,0,m){
        int x,t,y;
        scanf("%d%d",&x,&t);
        rep(j,0,t){
            int y;scanf("%d",&y);
            g[x].push_back(y);
            g[y].push_back(x);
        }
    }
    dfs(1,0,1);
    printf("%d %d\n",cnt1,cnt2);
    return 0;
}

 

上一篇:什么是混合app?


下一篇:关于github在mac 10.10上无法提交代码的解决办法---备用