博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1856 More is better 并查集(基础模板题)
阅读量:4610 次
发布时间:2019-06-09

本文共 1336 字,大约阅读时间需要 4 分钟。

题意:先输入n,然后输入n行,同一行的两个数表示的互为朋友,要求找到最多的直接朋友或间接朋友。比如

41 23 45 61 6 其中1、2、5、6是朋友,3、4是朋友,所以输出4 测试样例:
41 23 45 61 641 23 45 67 8
输出 4 2 注意:init()写在while循环里面,注意n=0的特殊情况,注意数据范围!

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 int n,p,q; 7 int f[10000010],num[10000010]; 8 void init() 9 {10 for(int i=1;i<10000010;i++)11 {12 f[i]=i;13 num[i]=1;14 }15 }16 int find(int x)17 {18 if(f[x]!=x)19 return f[x]=find(f[x]);20 }21 void merge(int x,int y)22 {23 int tx=find(x);24 int ty=find(y);25 if(tx!=ty)26 {27 f[tx]=ty;28 num[ty]+=num[tx];29 }30 return ;31 }32 int main()33 {34 while(~scanf("%d",&n))35 {36 init();37 if(n==0)38 {39 printf("1\n");40 continue; 41 }42 int maxn=0;43 while(n--)44 {45 cin>>p>>q;46 if(p>maxn)47 {48 maxn=p;49 } 50 if(q>maxn)51 {52 maxn=q;53 }54 merge(p,q); 55 }56 int Max=0; 57 for(int i=1;i<=maxn;i++)58 {59 // cout<
<
Max)61 {62 Max=num[i];63 } 64 }65 cout<
<

 

转载于:https://www.cnblogs.com/1013star/p/9267940.html

你可能感兴趣的文章
MySQL 索引
查看>>
HDU 5104 Primes Problem(素数打表)
查看>>
C#特性类的使用
查看>>
Entity Framework DbSet<T>之Include方法与IQueryable<T>扩展方法Include的使用
查看>>
vue 静态资源 压缩提交自动化
查看>>
[爬虫学习笔记]DNS解析服务增加缓存机制
查看>>
noip模拟赛 黑骑士
查看>>
【Python web 开发】用户收藏功能
查看>>
POJ2524+并查集
查看>>
前端性能优化方法
查看>>
Docker镜像拉不下来?试试这些
查看>>
实例11 加密可以这样简单(位运算)
查看>>
[告知]在评论中发布广告者必删!
查看>>
判断一个js对象是不是数组
查看>>
Vue-详解设置路由导航的两种方法: <router-link :to="..."> 和router.push(...)
查看>>
c# 实现电脑系统音量的增加,减少,静音等。
查看>>
Block那些事儿
查看>>
突然的感触,关乎技术上的
查看>>
再说单例模式的线程安全问题
查看>>
<<、>>、>>>移位操作
查看>>