Wrestling Match
题目大意:
有n名球员,m场比赛。其中有x名好球员,y名坏球员。
好球员必定赢过坏球员,问能否把球员明确的区分为好、坏球员。
题解:
思路是二分图染色,当然并查集也可以做。后续有空的话,会补上并查集做法。
利用比赛建图。
先把好、坏球员的分别染色 color[i] = 1 和 color[i] = -1。
①然后依次深搜已染色球员,遇到相同颜色的就输出 “NO”;
②若还有未染色球员,单个点输出 “NO”,否则设为好球员 color[i] = 1进行深搜,重复①。
代码:
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 1e4+5;
vector <int > G[MAXN];
int color[MAXN];
int n, m, x, y;
int u, v;
bool dfs(int v, int c){
color[v] = c;
for(int i = 0; i < G[v].size(); ++i){
if(color[G[v][i]] == c)
return false;
if(color[G[v][i]] == 0 && !dfs(G[v][i], -c))
return false;
}
return true;
}
void solve(){
for(int i = 1; i <= n; ++i){
if(color[i] != 0){
if(!dfs(i, color[i])){
printf("NO\n");
return ;
}
}
}
for(int i = 1; i <= n; ++i){
if(color[i] == 0){
if(G[i].size()==0){
printf("NO\n");
return ;
}
if(!dfs(i, 1)){
printf("NO\n");
return ;
}
}
}
printf("YES\n");
}
int main(){
while(scanf("%d %d %d %d", &n, &m, &x, &y) != EOF){
for(int i = 0; i <= MAXN; ++i){
G[i].clear();
}
memset(color, 0, sizeof(color));
for(int i = 0; i < m; ++i){
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = 0; i < x; ++i){
scanf("%d", &u);
color[u] = 1;
}
for(int i = 0; i < y; ++i){
scanf("%d", &v);
color[v] = -1;
}
solve();
}
return 0;
}