您的位置:首页 > 其它

HDU 5971 Wrestling Match(染色+暴力)

2016-11-10 18:52 351 查看

题目分析

主要的策略就是如果有已经确定的点,那么很明显沿着这条边直接跑就可以了,判断其相连的所有点时good player 还是 bad player,如果所有已经明确了的所有已经明确了的点都没有相邻边,那么直接找一个没有被标记过的点跑一下就可以了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1005;

struct Edge{
int to, next;
}e[maxn*20];

int head[maxn], color[maxn], tot;

void addedge(int from, int to){
e[tot].to = to;
e[tot].next = head[from];
head[from] = tot++;
}

bool dfs(int u, int fa){
for(int i = head[u]; i != -1; i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
if(color[v] == color[u]) return false;
if(!color[v]){
color[v] = 3 - color[u];
if(!dfs(v, u)) return false;
}
}
return true;
}

void init(){
tot = 0;
memset(head, -1, sizeof(head));
memset(color, 0, sizeof(color));
}

int main(){
int n, m, x, y;
while(scanf("%d%d%d%d", &n, &m, &x, &y) != EOF){
init();
int from, to;
for(int i = 0; i < m; i++){
scanf("%d%d", &from, &to);
addedge(from, to);
addedge(to, from);
}
int xx;
int flag = true, cnt = 1;
for(int i = 0; i < x; i++){
scanf("%d", &xx);
if(!color[xx]){
color[xx] = 1;
if(head[xx] != -1) cnt = 0;
flag = flag&&dfs(xx, -1);
}
else if(color[xx] == 2) flag = false;
}

for(int i = 0; i < y; i++){
scanf("%d", &xx);
if(!color[xx]){
color[xx] = 2;
if(head[xx] != -1) cnt = 0;
flag = flag&&dfs(xx, -1);
}
else if(color[xx] == 1) flag = false;
}
if(!flag){
printf("NO\n");
continue;
}
else{
if(cnt){
for(int i = 1; i <= n; i++)
if(!color[i]){
color[i] = 1;
flag = flag&&dfs(i, -1);
break;
}

for(int i = 1; i <= n; i++)
if(!color[i]){ flag = false; break; }
}
else{
for(int i = 1; i <= n; i++)
if(!color[i]){ flag = false; break;}
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: