离散化+并查集。。n==0输出1 。CNMCNMCNMCNMCNM
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
int f[200005],A[200005],B[200005],k[200005],t[200005];
int find(int x)
{
if(x==f[x])
return x;
else
return f[x]=find(f[x]);
}
bool same(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return true;
else
return false;
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
f[x]=y;
}
int main()
{ map<int ,int> m,v;
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,x,i,a,b,p,q;
while(scanf("%d",&n)!=EOF){
m.clear();
v.clear();
if(n==0){
printf("1\n");
continue;
}
memset(t,0,sizeof(t));
p=0,q=0,x=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
A[p++]=a;
B[q++]=b;
if(!v[a])
{
k[x++]=a;
v[a]=1;
}
if(!v[b])
{
k[x++]=b;
v[b]=1;
}
}
sort(k,k+x);
for(i=0;i<x;i++){
f[i]=i;
m[k[i]]=i;
}
for(i=0;i<p;i++)
{ // printf("%d %d\n",m[A[i]],m[B[i]]);
unite(m[A[i]],m[B[i]]);
}
int max=0;
for(i=0;i<x;i++)
{ // printf("%d ",f[i]);
int l=find(i);
t[l]++;
if(t[l]>max)
max=t[l];
}
printf("%d\n",max);
}
return 0;
}