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; }
相关文章推荐
- 【HDU】5971-Wrestling Match 二分染色
- HDU 5971 Wrestling Match(二分图染色)
- -----染色问题 hdu 5971-Wrestling Match
- 【HDU 5971 Wrestling Match】+ 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学) + 二分图染色
- HDU 5971 icpc 大连 A Wrestling Match
- HDU 5971 Wrestling Match 2016大连区域赛
- HDU 5971 Wrestling Match(dfs)
- HDU 5971 Wrestling Match(染色体法判断二分图)
- hdu 5971 Wrestling Match
- hdu 5971 Wrestling Match(队列)
- HDU 5971 Wrestling Match(dfs)2016 icpc 大连 A题
- 文章标题 HDU 5971 : Wrestling Match (2-sat)
- hdu5917——Wrestling Match(二分图染色)
- HDU 5971 Wrestling Match(深搜)
- hdu 5971 Wrestling Match 判断能否构成二分图
- hdu 5971 Wrestling Match(二分图判断)
- HDU 1814 Peaceful Commission (2-sat暴力染色)
- HDU 5971 Wrestling Match (二分图)
- HDU 5971 Wrestling Match(染色法二分图 OR 并查集)
- HDU 5971 && 2016ICPC大连 A: Wrestling Match(二分图判断)