PAT1021 Deepest Root

传送

给定N个点和N-1条边构成的无向图,若超过一个连通分量,输出连通分量的个数,否则输出能构成的树的最大深度

判断连通分量个数考虑并查集

因为数据很小,考虑暴力枚举每个点为根的情况的深度,取最大值。

用邻接表存储

#include <iostream>
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define prs(x) printf("%s\n",(x))
#define prl(x) printf("%lld\n",x)
#define ll long long
#define PII pair<int,int>
#define eps 1e-6
#define inf 1e17
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1e5+5;
struct node{
    int to,next;
}E[maxn];
int head[maxn],tot;
vector<int>ans;
int n,mx;
int pre[maxn];

int find(int x){
  return x==pre[x] ? x : pre[x]=find(pre[x]);
}

void add(int a,int b){
    E[tot].to=b,E[tot].next=head[a],head[a]=tot++;
}
int dfs(int u,int father){
    int depth=0;
    for(int i=head[u];~i;i=E[i].next){
        int v=E[i].to;
        if(v!=father) depth=max(depth,dfs(v,u)+1);
    }
    return depth;
}

int main(){
    sca(n);
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)pre[i]=i;
    int a,b;
    int k=n;
    rep(i,0,n){
        sca2(a,b);
        if(find(a)!=find(b)){
          k--;
          pre[find(a)]=find(b);
        }
        add(a,b),add(b,a);
    }
    if(k>1) printf("Error: %d components",k);
    else{
        mx=-1;
        rep(i,1,n+1){
            int depth=dfs(i,-1);
            if(depth>mx){
                mx=depth;
                ans.clear();
                ans.push_back(i);
            }
            else if(depth==mx){
                ans.push_back(i);
            }
        }
        for(auto v:ans) cout<<v<<endl;
    }
}
发布了95 篇原创文章 · 获赞 6 · 访问量 8430

猜你喜欢

转载自blog.csdn.net/kl782636177/article/details/105605485