别人用的并查集。。我这又钻牛角尖了,硬是要用dfs,TLE了好几次才AC。。已经越来越依赖STL了。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#define ll long long
#define MP make_pair
#define PB push_back
#define SZ(x) ((int)(x).size())
#define REP(i,n) for (int i=0; (i)<(int)n; ++(i))
#define FOR(it,c) for ( __typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it )
using namespace std;
map<int,int> to;
vector<int> g[100010];
int ans,cnt;
bool vis[100010];
void dfs(int u){
FOR(it,g[u])
if(!vis[*it]){
++cnt;
vis[*it]=true;
dfs(*it);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
int m;
while(cin>>m){
int ind=0;
memset(vis,false,sizeof(vis));
while(m--){
int a,b;
scanf("%d%d",&a,&b);
if(!to.count(a)) to[a]=ind++;
if(!to.count(b)) to[b]=ind++;
g[to[a]].PB(to[b]);g[to[b]].PB(to[a]);
}
ans=1;
FOR(it,to){
int tmp=it->second;
if(!vis[tmp]){
cnt=1;
vis[tmp]=true;
dfs(tmp);
if(cnt>ans) ans=cnt;
}
}
cout<<ans<<endl;
to.clear();
REP(i,ind) g[i].clear();
}
return 0;
}