POJ 2528(并查集)
来源:互联网 发布:手机能改淘宝密码吗 编辑:程序博客网 时间:2024/05/16 23:59
A Bug's Life
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 33086 Accepted: 10840
Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
23 31 22 31 34 21 23 4
Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!
Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
题意:有n个人,每个人都有一个编号,现在有m组输入(x,y),表示x喜欢y(y喜欢x),问通过这些关系能否找出同性恋?
题解:很容易想到使用并查集,但是并不是很好写呀Orz,我开始是考虑使用2个根来表示关系,突然有发现这些数字关系有可能会形成森林,这时我们需要使用数组,记录每个根的对立面的集合,每次添加到他们对立的集合,即男女集合,这样就不用考虑它是否会形成森林,直接合并即可,这里注意在一些元素根本不知道属性的情况下是不去要合并它的对立面的
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<string> #include<bitset> #include<utility> #include<functional> #include<iomanip> #include<sstream> #include<ctime> using namespace std; #define N int(1e6+10) #define inf int(0x3f3f3f3f) #define mod int(1e9+7) typedef long long LL; pair<int,int>pat[N];int fa[N],Rank[N];void init(int n){for(int i=1;i<=n;i++){pat[i].first=0;Rank[i]=1;fa[i]=i;}}int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}void unio(int x,int y){x=find(x);y=find(y);if(x!=y){if(Rank[x]>Rank[y]){Rank[x]+=Rank[y];fa[y]=fa[x];}else{Rank[y]+=Rank[x];fa[x]=fa[y];}}}int main() { #ifdef CDZSC freopen("i.txt", "r", stdin); //freopen("o.txt","w",stdout); int _time_jc = clock(); #endif int t,n,m,cas=0,x,y;scanf("%d",&t);while(t--){int ok=1;printf("Scenario #%d:\n",++cas);scanf("%d%d",&n,&m);init(n);while(m--){scanf("%d%d",&x,&y);if(pat[x].first==0&&pat[y].first==0){pat[x].first=y;pat[y].first=x;continue;}else{int xx=find(x);int yy=find(y);if(xx==yy){ok=0;}else{if(pat[xx].first!=0){unio(y,pat[xx].first);}else{pat[xx].first=xx;}if(pat[yy].first!=0)unio(x,pat[yy].first);else{pat[yy].first=yy;}}}}puts(ok?"No suspicious bugs found!\n":"Suspicious bugs found!\n");} return 0; }
0 0
- POJ 2528(并查集)
- Poj 并查集
- poj并查集
- POJ 并查集
- POJ 2588(解析几何+并查集)
- POJ 1611(并查集)
- poj 1611 (并查集)
- 种类并查集(POJ 1703)
- poj 1182食物链(并查集)
- POJ - 1182 食物链(并查集)
- POJ 1182食物链(并查集)
- POJ-2524(并查集)
- POJ 1182 :食物链(并查集)
- POJ-1611(并查集)
- poj 1182(并查集)
- POJ 1838 Banana (并查集)
- poj 1182(食物链)并查集
- POJ Ubiquitous Religions (并查集)
- 机器学习1
- java的poi技术读写excel数据
- C++学习之旅-结构struct
- sqlite3安装使用
- Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory AndroidStudio
- POJ 2528(并查集)
- NYOJ_1241_Distribution
- leetcode-09-Palindrome Number
- Java简介
- LightOj 1220 Fantasy of a Summation(快速幂)
- 前端开发学习笔记02----HTML基础
- 人见人爱A^B
- objective-c之block 总结
- 在Eclipse中打开工程中的drawable里面的PNG格式的图片报错,找不到应用程序