POJ 2492-A Bug's Life(带权并查集)

xiaoxiao2021-02-27  509

A Bug's Life Time Limit: 10000MS Memory Limit: 65536KTotal 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.

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; }
转载请注明原文地址: https://www.6miu.com/read-762.html

最新回复(0)