Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
2017-03-17 21:13
429 查看
题目链接:http://codeforces.com/contest/742/problem/E
题意:
有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜。
现在让你输出一种方案,满足以下要求:
情侣间吃不同的菜
相邻的三个人不能都吃同一种菜
输出任意一个解:
先将相邻的两个人连边,这样就满足了3个人不吃同样一种菜。
情侣间连边。
View Code
题意:
有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜。
现在让你输出一种方案,满足以下要求:
情侣间吃不同的菜
相邻的三个人不能都吃同一种菜
输出任意一个解:
先将相邻的两个人连边,这样就满足了3个人不吃同样一种菜。
情侣间连边。
#include <bits/stdc++.h> using namespace std; const int maxn = 2*100010; vector <int> G[maxn]; int color[maxn]; int b[maxn]; int g[maxn]; bool dfs(int u) { for(int i=0; i<G[u].size(); i++) { int v = G[u][i]; if(color[u]==color[v]) return false; if(!color[v]) { color[v] = 3 - color[u]; if(!dfs(v)) return false; } } return true; } int main() { int n; scanf("%d",&n); for(int i=1; i<=n; i++) { G[i*2-1].push_back(2*i); G[i*2].push_back(2*i-1); } for(int i=0; i<n; i++) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); b[i] = u; g[i] = v; } for(int i=1; i<=2*n; i++) { if(color[i]==0) { color[i] = 1; dfs(i); } } for(int i=0; i<n; i++) { printf("%d %d\n",color[b[i]],color[g[i]]); } return 0; }
View Code
相关文章推荐
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- AIM Tech Round (Div. 2) C. Graph and String(二分图染色 | 贪心)
- 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C (dfs+vector)
- CF - 782C. Andryusha and Colored Balloons - DFS染色问题
- Codeforces Round #197 (Div. 2) C. Xenia and Weights(DFS)
- CF 297 div2 E. Anya and Cubes (hash+dfs)
- Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
- CF#272 (Div. 2) B.(dfs)
- Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2 dfs
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
- (一般)POJ-1129 染色,DFS技巧
- Codeforces 586D Phillip and Trains dfs —— Round #325 (Div. 2)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- UVALive 6663 Count the Regions --离散化+DFS染色
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序 + 线段树)
- Codeforces Round #311 (Div. 2) C(技巧) *D(二分图染色)
- SGU 177. Square dfs 漂浮法倒序染色
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1(DFS好题)
- Codeforces Round #224 (Div. 2)(数学、dfs)