标签:并查集
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28882 | Accepted: 9423 |
Description
Input
Output
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 <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; const int maxn = 1100000; int n , m; int f[maxn]; int rea[maxn]; void init() { for(int i = 0 ; i < maxn ; i++) { f[i] = i; rea[i] = 0; } } int find(int x) { return x == f[x] ? x : find(f[x]); } void unin(int a , int b) { int x = find(a); int y = find(b); if(x != y) f[x] = y; } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int T; scanf("%d",&T); int cas = 1; while(T--) { scanf("%d%d",&n,&m); init(); int a , b; int flag = 0; for(int i = 0 ; i < m ; i ++) { scanf("%d%d" , &a , &b); int x = find(a); int y = find(b); if(x == y) flag = 1; if(rea[x] == 0 && rea[y] == 0) // x和y没有建立性别关系 { rea[x] = y; // x的异性是y rea[y] = x; // y的异性是x } else if(rea[x] == 0) // y建立起了性别关系,x没建立 { rea[x] = y; // x的异性是y unin(x , rea[y]); // 根据假设,那么x一定应该和y的原有异性进行合并,下面同理 } else if(rea[y] == 0) { rea[y] = x; unin(y , rea[x]); } else { unin(x , rea[y]); unin(y , rea[x]); } } printf("Scenario #%d:\n" , cas++); if(flag) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); printf("\n"); } }
标签:并查集
原文地址:http://blog.csdn.net/u013447865/article/details/40920235