题目链接:传送门
题目大意:有n个昆虫,有m组关系,接下来m行表示两个昆虫性别不同,问是否有矛盾情况(同男同女)
题目思路:并查集的高级应用,开两倍数组大小,后n个数组表示和当前昆虫不同性别的集合
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <cctype> #include <queue> #include <string> #include <vector> #include <set> #include <map> #include <climits> #define lson root<<1,l,mid #define rson root<<1|1,mid+1,r #define fi first #define se second #define seg int root,int l,int r #define ping(x,y) ((x-y)*(x-y)) #define mst(x,y) memset(x,y,sizeof(x)) #define mcp(x,y) memcpy(x,y,sizeof(y)) #define Min(x,y) (x<y?x:y) #define Max(x,y) (x>y?x:y) using namespace std; #define gamma 0.5772156649015328606065120 #define MOD 100000007 #define inf 0x3f3f3f3f #define N 100005 #define maxn 10001000 typedef long long LL; typedef pair<int,int> PII; int fp[N]; int findp(int x){return fp[x]==x?x:fp[x]=findp(fp[x]);} int main(){ int i,j,group,k,v,x,y,Case=0,n,m; scanf("%d",&group); while(group--){ int flag=0; scanf("%d%d",&n,&m); for(i=0;i<=n<<1;++i) fp[i]=i; for(i=0;i<m;++i){ scanf("%d%d",&x,&y); if(!flag){ int xa=findp(x); int ya=findp(y); if(xa==ya) flag=1; else{ fp[xa]=findp(y+n); fp[ya]=findp(x+n); } } } printf("Scenario #%d:\n",++Case); if(flag) printf("Suspicious bugs found!\n\n"); else printf("No suspicious bugs found!\n\n"); } return 0; }
hdu1829(A Bug's Life)
时间: 2024-12-21 18:14:36