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
#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 Life(HDU-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 Bug‘s 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>