POJ 2492-A Bug's Life(帶權並查集)
A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 36820 | Accepted: 12027 |
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
2 3 3 1 2 2 3 1 3 4 2 1 2 3 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個飛蛾之間的異性關係,判斷是否存在同性戀。
解題思路:
每隻飛蛾看作一個節點,0/1標記當前飛蛾節點和根節點的同異性關係。
每次輸入一組就進行處理,同一棵樹中的兩個點根據和根節點的關係判斷同異性關係,否則合併後更新同異性關係。
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define MAXN 10010
int fa[MAXN];
int a[MAXN],b[MAXN];
bool rel[MAXN];//0/1標記和樹根的同異性關係
int setfind(int x)
{
int temp=fa[x];
if(temp==x) return x;
fa[x]=setfind(temp);
rel[x]=(rel[x]+rel[temp])%2;//更新同異性關係
return fa[x];
}
void join(int p,int q)
{
int x=setfind(p);
int y=setfind(q);
if(x!=y) fa[x]=y;
rel[x]=((rel[p]-rel[q]+1)%2);//確定同異性關係,如果之前是同性則改為異性
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t,ca=0;
cin>>t;
while(t--)
{
for(int i=0; i<MAXN; ++i) fa[i]=i;
memset(rel,false,sizeof(rel));
int n,m;
bool flag=false;
cin>>n>>m;
for(int i=0; i<m; ++i)
{
int a,b;
cin>>a>>b;
if(setfind(a)==setfind(b))
{
if(rel[a]==rel[b]%2)
flag=true;//出現同性戀
}
else join(a,b);
}
if(flag) cout<<"Scenario #"<<++ca<<":"<<endl<<"Suspicious bugs found!"<<endl<<endl;
else cout<<"Scenario #"<<++ca<<":"<<endl<<"No suspicious bugs found!"<<endl<<endl;
}
return 0;
}
相關文章
- POJ 2492 A bug's life【擴充套件域 | 邊帶權並查集】套件並查集
- POJ 2492 A Bug's Life(關係並查集)並查集
- 並查集到帶權並查集並查集
- POJ 1182 食物鏈【擴充套件域 | 邊帶權並查集】套件並查集
- 【資料結構】帶權並查集資料結構並查集
- 樹(tree) - 題解(帶權並查集)並查集
- 【帶權並查集】理論和應用並查集
- POJ2492(種類並查集)並查集
- 【並查集】【帶偏移的並查集】食物鏈並查集
- HDU 3038 How Many Answers Are Wrong (帶權並查集)並查集
- POJ 1308-Is It A Tree?(並查集)並查集
- 【POJ 1182】食物鏈(並查集)並查集
- bzoj4690: Never Wait for Weights(帶權並查集)AI並查集
- 關押罪犯 擴充套件域並查集 帶權並查集 二分圖+二分套件並查集
- POJ1797 Heavy Transportation【並查集+貪心】並查集
- POJ 1703-Find them, Catch them(並查集)並查集
- POJ 1733 Parity【擴充套件域並查集】套件並查集
- POJ2253 Frogger【並查集+貪心】並查集
- POJ 2236-Wireless Network(並查集)並查集
- POJ 2524-Ubiquitous Religions(入門並查集)UI並查集
- POJ 1703 Find them, Catch them (關係並查集)並查集
- poj 1182 並查集經典問題並查集
- CHOJ 4101 銀河英雄傳說【邊帶權並查集】並查集
- POJ-1182-食物鏈(並查集種類)並查集
- POJ 1611-The Suspects(並查集-同一集合)並查集
- POJ 1182(食物鏈-另類做法【拆點】)[Template:並查集]並查集
- POJ 2513-Colored Sticks(連線木棍-trie樹+並查集+尤拉通路)並查集
- It's my life
- It s my life
- 並查集(一)並查集的幾種實現並查集
- 並查集(小白)並查集
- 3.1並查集並查集
- [kuangbin帶你飛]專題五 並查集 題解並查集
- Luogu P1196 [NOI2002]銀河英雄傳說:帶權並查集並查集
- 並查集(Union Find)並查集
- 並查集應用並查集
- The Door Problem 並查集並查集
- 並查集練習並查集