hdu-3314-并查集
来源:互联网 发布:验证码滑动加速算法 编辑:程序博客网 时间:2024/05/16 05:09
http://acm.hdu.edu.cn/showproblem.php?pid=3314
如果形成环,则标记一下,最后统计一遍每个人得多少票,看是否唯一即可
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <map>#include <set>#include <vector>#include <iostream>using namespace std;const double pi=acos(-1.0);double eps=0.000001;int fa[123456];int find(int x){ if (fa[x]==x) return x; else return fa[x]=find(fa[x]);}int num[123456];int main(){ int n,m; while(scanf("%d",&n)!=EOF) { set<int > del; del.clear(); for (int i=0; i<n; i++) fa[i]=i ,num[i]=0; for (int i=0; i<n; i++) { int x; scanf("%d",&x); int fx=find(x); int fy=find(i); if (x==i) continue; if (fx!=fy) fa[fy]=x; else del.insert(fx); } int maxx=0; int maxi=-1; for (int i=0; i<n; i++) { int fx=find(i); if (del.find(fx)!=del.end()) //找到 continue; else num[fx]++; } for (int i=0; i<n; i++) { if (num[i]>maxx) maxx=num[i],maxi=i; } int cun=0; for (int i=0; i<n; i++) if (num[i]==maxx) cun++; if (cun==1) printf("%d\n",maxi); else printf("Trouble\n"); } return 0;}
0 0
- hdu-3314-并查集
- HDU 3371 并查集
- HDU 2473 并查集
- hdu 1116 并查集
- HDU 1856 并查集
- HDU 1598 并查集
- HDU-1232 并查集
- 【并查集】hdu 2818
- hdu 1232 并查集
- hdu 1856(并查集)
- HDU 1598 并查集
- HDU-1232(并查集)
- hdu 1856 并查集
- hdu 1829 并查集
- HDU-1272(并查集)
- hdu 1213 并查集
- HDU 1272 并查集
- hdu 1232(并查集)
- Java调用Telnet(windows版和Linux版)
- find
- PAT乙级练习题B1054. 求平均值
- hdu_1280_前m大的数
- 微服务设计-思维导图笔记
- hdu-3314-并查集
- js打开app应用或进入下载页面
- MySQL 调优和使用必读
- HData——ETL 数据导入/导出工具
- 【Java线程】锁机制:synchronized、Lock、Condition
- mysql explain中的using filesort
- 关于iOS 的手势滑动返回功能
- Spark性能优化指南(一)(开发调优)
- 【整理】Session和Cookie的区别,关系和应用