CF 272E Dima and Horses 染色,dfs 难度:2
2015-03-11 00:35
218 查看
http://codeforces.com/problemset/problem/272/E
把仇恨关系想象为边,
因为度只能为0,1,2,3,所以有以下几种
0,1 直接放即可
2: 有(1,1),(0,2)两种情况,第一种随便放,第二种放0那里
3:有(1,2),(0,3)两种情况,第一种放1,第二种放0那里
也就是说,怎样都有解
dfs寻找即可
不过一开始觉得这样不靠谱,因为时间复杂度很高,不过没想到过了
把仇恨关系想象为边,
因为度只能为0,1,2,3,所以有以下几种
0,1 直接放即可
2: 有(1,1),(0,2)两种情况,第一种随便放,第二种放0那里
3:有(1,2),(0,3)两种情况,第一种放1,第二种放0那里
也就是说,怎样都有解
dfs寻找即可
不过一开始觉得这样不靠谱,因为时间复杂度很高,不过没想到过了
#include <cstdio> #include <vector> using namespace std; const int maxn=3e5+5; bool other[maxn]; vector<int> e[maxn]; int n,m; void print(){ for(int i=1;i<=n;i++){ if(other[i])printf("1"); else printf("0"); } puts(""); } void judge(int s){ int o=0; for(int i=0;i<(int)e[s].size();i++){ int t=e[s][i]; if(other[t]==other[s])o++; } if(o>1){ other[s]=!other[s]; for(int i=0;i<(int)e[s].size();i++){ int t=e[s][i]; if(other[t]==other[s])judge(t); } } } int main(){ scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ int f,t; scanf("%d%d",&f,&t); e[f].push_back(t); e[t].push_back(f); } for(int i=1;i<=n;i++){ judge(i); } print(); return 0; }
相关文章推荐
- [Codeforces 272E] Dima and Horses (图染色构造)
- CF 272E Dima and Horses(2-SAT变形)
- CF 659 A方向 B排序 C暴力 D方向 E并查集/无向环的dfs/无向环的染色bfs F并查集+dfs/bfs G:递推
- Codeforces 272E Dima and Horses【玄学暴力】
- CF 782C 贪心,DFS染色,水题
- CF - 782C. Andryusha and Colored Balloons - DFS染色问题
- CF 161 (Div. 2) D 题 Cycle in Graph 【dfs找环】
- CF - Profact(DFS + 剪枝)
- CF Dima and Salad 01背包
- URAL 1557 Network Attack 图论,连通性,tarjain,dfs建树,分类讨论 难度:2
- CF 241E flights 最短路,重复迭代直到稳定 难度:3
- CF 37 C Old Berland Language【模拟+dfs】
- <cf>Solitaire(DFS or DP)
- dfs之地图染色
- cf 167.div2 D.Dima and Two Sequences
- HDU 5285 wyh2000 and pupil (DFS染色判二分图 + 贪心)
- cf B. Dima and Text Messages
- Codeforces Round #167 (Div. 1) C. Dima and Horses(BFS+贪心)
- [DFS][BFS][种子染色]拯救OIBH总部
- UVA 193 Graph Coloring 图染色 DFS 数据