Wrestling Match (dfs亂搞染色)
Nowadays, at least one wrestling match is held every year in our country. There are a lot of people in the game is “good player”, the rest is “bad player”. Now, Xiao Ming is referee of the wrestling match and he has a list of the matches in his hand. At the same time, he knows some people are good players,some are bad players. He believes that every game is a battle between the good and the bad player. Now he wants to know whether all the people can be divided into “good player“ and “bad player“. Input Input contains multiple sets of data. For each set of data,there are four numbers in the first line: N (1 ≤ N ≤ 1000), M (1 ≤ M ≤ 10000), X, Y (X +Y ≤ N),in order to show the number of players (numbered 1 to N), the number of matches, the number of known “good players“ and the number of known “bad players“. In the next M lines, each line has two numbers a, b (a ̸= b),said there is a game between a and b. The next line has X different numbers. Each number is known as a “good player“ number. The last line contains Y different numbers. Each number represents a known “bad player“ number. Data guarantees there will not be a player number is a good player and also a bad player. Output If all the people can be divided into “good players“ and “bad players”, output ‘YES’, otherwise output ‘NO’. Sample Input 5 4 0 0 1 3 1 4 3 5 4 5 5 4 1 0 1 3 1 4 3 5 4 5 2 Sample Output NO YES
題目大概:
給你n個人,m組關係,還有X,Y這兩個陣營的人的編號。每個人必須屬於一個陣營。問是否能分成這兩個陣營。輸出YES或NO。
思路:
直接利用dfs順著m組關係,染色,有衝突則直接輸出NO。
然後利用X,Y兩個陣營的人員名單染色,分兩種情況染色,X染0,Y染1,或者X染1,Y染0.
如果有衝突,則有衝突那種情況返回NO。
如果這兩種情況有一種是YES的,就輸出YES。否則NO。
程式碼:
#include<bits/stdc++.h> using namespace std; #define ll long long const int mod=1e9+7; const int INF=0x3f3f3f3f; const int maxn=1100; const int maxm=110000; const double pi=acos(-1); int head[maxm]; int edgenum=0; int n,m,X,Y; struct node { int to,next; } edge[maxm]; void addedge(int a,int b) { edge[edgenum].to=b; edge[edgenum].next=head[a]; head[a]=edgenum++; } int work(int w,int a[],int b[],int vis[]) { //0 for(int i=1;i<=X;i++) { int u=a[i]; if(vis[u]==-1) { vis[u]=w; } else { if(vis[u]!=w) { return 0; } } } //1 for(int i=1;i<=Y;i++) { int u=b[i]; if(vis[u]==-1) { vis[u]=w^1; } else { if(vis[u]!=(w^1)) { return 0; } } } for(int i=1;i<=n;i++) { if(vis[i]==-1)return 0; } return 1; } int a[maxn],b[maxn]; int vis[maxn]; void debug() { for(int i=1; i<=n; i++) { cout<<i<<" "<<vis[i]<<endl; } } int dfs(int u,int fa,int w) { if(head[u]!=-1) { if(vis[u]==-1) { vis[u]=w; } else { if(vis[u]!=w) { return -1; } else return 1; } } int flag=1; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; int f1=dfs(v,u,w^1); if(f1==-1)flag=-1; } return flag; } void init() { memset(head,-1,sizeof(head)); memset(vis,-1,sizeof(vis)); edgenum=0; } int main() { while(~scanf("%d%d%d%d",&n,&m,&X,&Y)) { int u,v; init(); for(int i=1; i<=m; i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } for(int i=1; i<=X; i++) { scanf("%d",&a[i]); } for(int i=1; i<=Y; i++) { scanf("%d",&b[i]); } int flag_1=0; for(int i=1; i<=n; i++) { int p=0; if(vis[i]==-1)p=dfs(i,-1,0); if(p==-1) { flag_1=1; break; } } //debug(); if(flag_1) { printf("NO\n"); continue; } int flag1=work(0,a,b,vis); int flag2=work(1,a,b,vis); if(flag1||flag2) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }