码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1856

时间:2020-03-02 10:44:17      阅读:45      评论:0      收藏:0      [点我收藏+]

标签:class   inline   最大   ++   namespace   std   hellip   ace   oid   

求最大连通块……

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+10;
const int maxm=2*1e5+10;
int vis[maxn];
int beg[maxn],nex[maxn],to[maxn],e;
inline void add(int x,int y){
    e++;nex[e]=beg[x];
    beg[x]=e;to[e]=y;
}
inline int dfs(int x){
    vis[x]=1;
    int cnt=1;
    for(int i=beg[x];i;i=nex[i]){
        int t=to[i];
        if(!vis[t])cnt+=dfs(t);
    }
    return cnt;
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int x,y;
        e=0;
        memset(vis,0,sizeof(vis));
        memset(beg,0,sizeof(beg));
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            add(x,y),add(y,x);
        }
        int ans=0;
        for(int i=1;i<=10000000;i++)
            if(!vis[i])
                ans=max(ans,dfs(i));
        printf("%d\n",ans);
    }
    return 0;
}

 

hdu1856

标签:class   inline   最大   ++   namespace   std   hellip   ace   oid   

原文地址:https://www.cnblogs.com/syzf2222/p/12393932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!