1. 程式人生 > >HDU 1829 A Bug's Life (分組並查集)

HDU 1829 A Bug's Life (分組並查集)

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7507    Accepted Submission(s): 2417


Problem 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. 題目意思:給定一系列數對,例如a和b,表示a和b不是同一種性別,然後不斷的給出這樣的數對,問有沒有性別不對的情況。 例如給定: 1    2 3    4 1    3 那這裡就是說1和2不是同種性別,3和4也不是同種性別,1和3不是同種性別,那這樣就說明1和3是同一種性別,2和4是同一種性別,所以沒有任何歧義,這時候輸出No suspicious bugs found! 但是例如,(本文作者:CSDN:凌風) 1 2 2 3 1 3 1和2不同性別,2和3不同性別,那麼1和3同一性別的,但是第三組數對又表明1和3不同性別,所以這裡就出現了3或者1的性別出現了歧義,也就是說條件矛盾,這時候輸出Suspicious bugs found!.
很明顯的分組並查集的題目,可以考慮用兩種方法來做: (本文作者:CSDN:凌風) 1.開兩個並查集,然後合併的時候要合併兩次,這樣在合併之前判斷是否衝突,如果不衝突就進行合併,否則不需要繼續合併。 2.開一個並查集,但是要加個偏移向量陣列,來記錄每個節點距離根節點的距離,但這裡最好取一下餘,因為畢竟只有兩個分組。 在這裡我用兩種方法分別實現了一下: 方法一的實現:
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 2000;

int pre[2*MAX+5];

bool mark;

void init(int n){
	int i;
	////(author:CSDN:凌風)
	for(i=1;i<=MAX+n;++i)pre[i] = i;
	mark = true;
}

int root(int x){
	if(x!=pre[x]){
		pre[x] = root(pre[x]);
	}
	return pre[x];
}

void merge(int x,int y){
	int fx,fy;
	fx = root(x);
	fy = root(y-MAX);
	
	if(fx==fy){
		mark = false;
		return;
	}
	
	fy = root(y);
	if(fx!=fy){
		pre[fx] = pre[fy];
	}
}

int main(){
	//freopen("in.txt","r",stdin);
	//(author:CSDN:凌風)
	int t,i,n,m,x,y,k;
	scanf("%d",&t);
	for(i=1;i<=t;++i){
		scanf("%d %d",&n,&m);
		init(n);
		for(k=1;k<=m;++k){
			scanf("%d %d",&x,&y);
			if(mark){
				merge(x,y+MAX);
				merge(y,x+MAX);
			}
		}
		printf("Scenario #%d:\n",i);
		if(mark){
			printf("No suspicious bugs found!\n");
		}else{
			printf("Suspicious bugs found!\n");
		}
		printf("\n");
	}
	return 0;
}


方法二的程式碼:
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 2000;

int pre[MAX+5];
int offset[MAX+5];

bool mark;

void init(int n){
	int i;

	//(author:CSDN:凌風)
	for(i=1;i<=n;++i){
		pre[i] = i;
		offset[i] = 0;
	}
	mark = true;
}

int root(int x){
	int px;
	if(x!=pre[x]){
		px = pre[x];
		pre[x] = root(pre[x]);
		offset[x] = (offset[px] + offset[x])%2;
	}
	return pre[x];
}

void merge(int x,int y){
	int fx = root(x);
	int fy = root(y);
	
	if(fx!=fy){
		pre[fx] = fy;
		offset[fx] = (1 + offset[y] - offset[x])%2;
	}
	root(x);
}

int main(){
	//freopen("in.txt","r",stdin);
	//(author:CSDN:凌風)
	int t,i,n,m,x,y,k;
	scanf("%d",&t);
	for(i=1;i<=t;++i){
		scanf("%d %d",&n,&m);
		init(n);
		for(k=1;k<=m;++k){
			scanf("%d %d",&x,&y);
			if(mark){
				merge(x,y);
			}
			if(offset[x]==offset[y]){
				mark = false;
			}
		}
		printf("Scenario #%d:\n",i);
		if(mark){
			printf("No suspicious bugs found!\n");
		}else{
			printf("Suspicious bugs found!\n");
		}
		printf("\n");
	}
	return 0;
}


相關推薦

HDU 1829 A Bug's Life 分組

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7507    Accepted

【ZCMU1437】A Bug's Life種類

題目連結 1437: A Bug's Life Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 113  Solved: 50 [Submit][Status][Web Board] Description Pr

A Bug's Life加權

Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature t

A Bug's Life 分類

Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different gend

POJ-2492 A Bug's Life 種類

A Bug’s Life Background Professor Hopper is researching th

HDU 1829 A Bug's Life

問題描述: Problem Description Background Professor Hopper is researching the sexual behavior of a rar

【ZCMU1437】A Bug's Life種類

題目連結 1437: A Bug's Life Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 113  Solved: 50 [Submit][Status][Web Board] Descript

A Bug's Life加權

滴答滴答---題目連結  A Bug's Life(加權並查集) Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs

POJ 2492 - A Bug's Life 種類

Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that

HDU 1829 A Bug's Life

Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

HDU 6326 Problem H. Monster Hunter 貪心+*

Problem H. Monster Hunter Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submissi

hdu 6326 Problem H. Monster Hunter貪心+

Problem H. Monster Hunter Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 663 

A Bug's LifeHDU-1829

Problem Description Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two diffe

zcmu 1437 A Bug's Life

【題目】 1437: A Bug's Life Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 112  Solved: 49 [Submit][Status][Web Board] Description Prof

A Bugs life POJ 2492 解題報告 種類

   這也算是種類並查集的一道經典例題了吧,題意就不多解釋了,先寫一些我對種類並查集的一些理解。    種類並查集比普通的並查集多一個relation陣列,relation[i] 記錄了 i 和 其直接父親節點的關係,這個關係的表示因題目而異,種類並查集的重點和難點就是

POJ2492---A Bug's Life做完食物鏈,再秒這個

和食物鏈那個是一種型別的,直接程式碼。 #include<iostream> #include<algorithm> #include<cstdio> usin

A Bug's Life拓展

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12358    Accepted

A Bug's Life

Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

A Bug's Life帶權

hdu1829 這是一道比較簡單 好理解的並查集 #include <iostream> #include <stdio.h> #include <math.h> #include <algorithm> #include &

2492  A Bug's Life 帶權

解題思路:d[i]=0表示父節點同性 d[i]==1 表示與父節點異性  #include<iostream> #include<cstdio> #include<vector> #include<algorithm>