CSU-ACM2017暑假集训比赛7 - D - Bicoloring - UVA - 10004
2017-08-23 19:40
489 查看
D - Bicoloring
一边深度优先搜索,一边染色,一边检查是否有相邻节点染上了相同颜色即可。也可以理解为染色法判断二分图是否成立。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstring> #include <queue> using namespace std; const int maxn = 204; int N, L, cnt; vector<int> g[maxn]; struct node{ int color; bool vis; }point[maxn]; void init(){ for(int i = 0; i <= N; i++) g[i].clear(); memset(point, 0, sizeof(point)); } bool dfs(int u){ for(int i = 0; i < g[u].size(); i++){ int to = g[u][i]; if(!point[to].vis){ point[to].vis = true; point[to].color = -point[u].color; return dfs(to); } else{ if(point[to].color + point[u].color == 0) continue; else return false; } } return true; } int main(){ #ifdef TEST freopen("test.txt", "r", stdin); #endif // TEST while(scanf("%d", &N) && N){ init(); scanf("%d", &L); for(int i = 0; i < L; i++){ int a, b; scanf("%d%d", &a, &b); g[a].push_back(b); } point[0].color = point[0].vis = 1; bool ok = dfs(0); if(ok) printf("BICOLORABLE.\n"); else printf("NOT BICOLORABLE.\n"); } return 0; }
相关文章推荐
- CSU-ACM2017暑假集训比赛2 C - (╯°口°)╯(┴—┴
- CSU-ACM2017暑假集训比赛1 A - I Can Guess the Data Structure! -uva111995
- CSU-ACM2017暑假集训比赛2 E - ( ̄▽ ̄)/
- CSU-ACM2017暑假集训比赛7 C - Pasha and Tea - CodeForces - 557B
- CSU-ACM2017暑假集训比赛2 D - ^(●゚∀゚○)ノ
- CSU-ACM2017暑假集训比赛3D - D CodeForces - 557C
- R2D2 and Droid Army--CSU-ACM2017暑假集训比赛1
- CSU-ACM2017暑假集训比赛2 HDU - 1597 find the nth digit
- CSU-ACM2017暑假集训比赛8 - B - The Two Routes - CodeForces - 601A
- CSU-ACM2017暑假集训比赛7 - E - Courses - HDU - 1083
- CSU-ACM2017暑假集训比赛2 B - : )
- CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet CodeForces - 589F
- CSU-ACM2017暑假集训比赛2 CodeForces - 724D
- CSU-ACM2017暑假集训比赛1 TD POJ3111
- CSU-ACM2017暑假集训比赛7 A - Equivalent Strings - CodeForces - 560D
- CSU-ACM2017暑假集训比赛8 - E - Escape - HDU - 3605
- CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet
- CSU-ACM2017暑假集训比赛2 A - _(:з」∠)_
- CSU-ACM2017暑假集训比赛7 - F - 凑数字 - 51Nod - 1385
- CSU-ACM2017暑假集训比赛1 B - R2D2 and Droid Army