HDU 5973 Wrestling Match
Description:
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 1toN ),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 numbersa, 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個關係,描述a,b身份不同。X個good身份的人和Y個bad身份的人並給出編號。
思路:
看完題立馬想到上半年西安邀請賽熱身的一道題,瞬間開始一頓胡寫。。。 嗯WA了兩發之後開始整理這道題的幾個條件還是很好過的【據說是大連賽銅題。
核心是並查集沒什麼說的,用dfs也是可以的。注意點有幾個, 對於已經確定關係的編號要注意他們的身份一定要是對立的。對於沒有提及的人算不確定身份。對於已經確定身份【注意不是關係】的人要判斷他們的關係是否符合他們的身份。資料量很小水水的寫一下就好了。
程式碼:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[9][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0 };
const ll ll_inf = 1e18;
const double PI=acos(-1.0);
const int inf = 0x3f3f3f;
const int mod = (int) 1e9 + 7;
const int Max = (int) 1e3+11;
const int N = (int) 40000+5;
const int maxn = (int) 120;
stringstream ss;
/**********************************************Head-----Template****************************************/
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
/**********************************************Head-----Temlate*****************************************/
//
// tools:
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// ios::sync_with_stdio(false);
//
int n ,m, x, y;
int rela[Max], Fa[Max];
vector < int> good, bad;
bool flag;
bool vis[Max];
void Init() {
flag = true;
for (int i = 1; i <= n; ++i) {
// relative
rela[i] = 0;
Fa[i] = i;
}
while (!good.empty()) good.clear();
while (!bad.empty()) bad.clear();
memset(vis, false, sizeof(vis));
}
int Pa(int x) {
if (x == Fa[x]) return x;
int t = Fa[x];
Fa[x] = Pa(Fa[x]);
rela[x] = (rela[x] + rela[t]) % 2;
return Fa[x];
}
bool Merge(int x, int y) {
int fx = Pa(x);
int fy = Pa(y);
if (fx == fy) {
if (rela[x] == rela[y])
return false;
}
Fa[fx] = fy;
rela[fx] = (rela[x] + rela[y] + 1) % 2;
return true;
}
bool check() {
if (good.size() && bad.size()) {
for (int i = 0; i < good.size(); ++i) {
for (int j = 0; j < bad.size(); ++j) {
int x = good[i];
int y = bad[j];
vis[x] = vis[y] = true;
int fx = Pa(x);
int fy = Pa(y);
if (fx == fy && rela[x] == rela[y]) return false;
}
}
}
if (good.size()) {
for (int i = 0; i < good.size(); ++i) {
for (int j = i; j < good.size(); ++j) {
int x = good[i];
int y = good[j];
vis[x] = vis[y] = true;
int fx = Pa(x);
int fy = Pa(y);
if (fx == fy && rela[x] != rela[y]) return false;
}
}
}
if (bad.size()) {
for (int i = 0; i < bad.size(); ++i) {
for (int j = i; j < bad.size(); ++j) {
int x = bad[i];
int y = bad[j];
vis[x] = vis[y] = true;
int fx = Pa(x);
int fy = Pa(y);
if (fx == fy && rela[x] != rela[y]) return false;
}
}
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) return false;
}
return true;
}
int main() {
while (~scanf("%d %d %d %d", &n, &m, &x, &y)) {
Init();
int a, b;
while (m--) {
scanf("%d %d", &a, &b);
vis[a] = vis[b] = true;
if (flag == false) {
continue;
}
flag = Merge(a, b);
}
int num;
for (int i = 0; i < x; ++i) {
scanf("%d", &num);
good.push_back(num);
}
for (int i = 0; i < y; ++i) {
scanf("%d", &num);
bad.push_back(num);
}
if (flag) flag = check();
printf("%s\n", flag == true ? "YES" : "NO");
}
return 0;
}