题意:

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

思路:

简单并查集

代码:

int n;
struct node{
int x,y;
}
Pair[100005]; int cn;
int a[200005];
int ct[200005];
int fa[200005]; int findFa(int x){
return fa[x]==x?fa[x]:fa[x]=findFa(fa[x]);
} void Union(int x,int y){
int fx=findFa(x);
int fy=findFa(y);
if(fx!=fy){
fa[fx]=fy;
}
} int main(){
map<int,int> mp; while(scanf("%d",&n)!=EOF){
mp.clear();
cn=0;
rep(i,1,n){
scanf("%d%d",&Pair[i].x,&Pair[i].y);
if(mp[Pair[i].x]==0){
mp[Pair[i].x]=1;
a[++cn]=Pair[i].x;
}
if(mp[Pair[i].y]==0){
mp[Pair[i].y]=1;
a[++cn]=Pair[i].y;
}
}
sort(a+1,a+1+cn);
rep(i,1,cn) fa[i]=i;
rep(i,1,n){
int px=lower_bound(a+1,a+1+cn,Pair[i].x)-a;
int py=lower_bound(a+1,a+1+cn,Pair[i].y)-a;
Union(px,py);
}
mem(ct,0);
int ans=1;
rep(i,1,cn){
int t=findFa(i);
ct[t]++;
ans=max( ans,ct[t] );
}
printf("%d\n",ans);
} return 0;
}

最新文章

  1. lua 时间戳和时间互转
  2. Essential controls for web app
  3. js中this的绑定
  4. 直接用&lt;img&gt; 的src属性显示base64转码后的字符串成图片
  5. 价值1400美元的CEH(道德黑客)认证培训课程长啥样?(3)工具集
  6. asp.net 修改/删除站内目录操作后会导致Session丢失
  7. tomcat重启session不过期的处理
  8. 【C++】动态内存与智能指针
  9. Q3: Linked List Cycle II
  10. 威胁远胜“心脏出血”?国外新爆Bash高危安全漏洞
  11. $1200元 设计数据挖掘模型及对应RESTful Web Service
  12. CSharp Algorithm - How to traverse binary tree by breadth (Part II)
  13. vim setting
  14. Java中间件
  15. SNMP配置和常用命令OID(转)
  16. 《码农周刊》干货精选(Python 篇)
  17. ABP官方文档翻译 3.1 实体
  18. Acitivity(活动)
  19. vue的渐进式理解
  20. Some beautiful Progress Bars in WPF

热门文章

  1. 2021ICPC网络赛第一场部分题解-The 2021 ICPC Asia Regionals Online Contest (I)
  2. Jmeter系类(32) - JSR223(2) | Groovy常见内置函数及调用
  3. Gitee自动化部署python脚本
  4. 浅聊Linux的五种IO模型
  5. 关于国密HTTPS 的那些事(一)
  6. Unity——基于UGUI的UI框架
  7. vue 快速入门 系列 —— vue-router
  8. 数据库的高可用MHA实验步骤
  9. 从零入门 Serverless | 函数计算的可观测性
  10. Lamport时间戳论文笔记