标签:round tput gif from 技术分享 struct poj pac wrong
A Bug‘s Life POJ - 2492
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<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<sstream> #include<cmath> #include<stack> #include<map> #include<cstdlib> #include<vector> #include<string> #include<queue> using namespace std; #define ll long long #define llu unsigned long long #define INF 0x3f3f3f3f const double PI = acos(-1.0); const int maxn = 5e4+10; const int mod = 1e9+7; struct node { int pre; int relation; //0:同 1:异 }p[maxn]; int find(int x) { int temp; if(x == p[x].pre) return x; temp = p[x].pre; p[x].pre = find(temp); p[x].relation = (p[x].relation + p[temp].relation + 1) % 2; return p[x].pre; } void combine(int x,int y) { int xx = find(x); int yy = find(y); if(xx != yy) { p[xx].pre = yy; p[xx].relation = (p[y].relation - p[x].relation) % 2; } } int main() { int t; scanf("%d",&t); int ca=1; while(t--) { int n,m; int flag = 0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { p[i].pre = i; p[i].relation = 1; } for(int i=0;i<m;i++) { int a,b; scanf("%d %d",&a,&b); if(find(a) == find(b)) { if(p[a].relation == p[b].relation) flag = 1; } else combine(a,b); } if(flag) printf("Scenario #%d:\nSuspicious bugs found!\n\n",ca++); else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",ca++); } }
A Bug's Life POJ - 2492 (带权并查集)
标签:round tput gif from 技术分享 struct poj pac wrong
原文地址:https://www.cnblogs.com/smallhester/p/10331303.html