ural 1080 Map Coloring DFS染色
2012-09-10 22:39
225 查看
1080. Map Coloring
Time Limit: 1.0 secondMemory Limit: 16 MB
We consider a geographical map with N countries numbered from 1 to N (0 < N < 99). For every country we know the numbers of other countries which are connected with its border. From every country we can reach to any other one, eventually crossing some borders. Write a program which determines whether it is possible to color the map only in two colors — red and blue in such a way that if two countries are connected their colors are different. The color of the first country is red. Your program must output one possible coloring for the other countries, or show, that such coloring is impossible.
Input
On the first line is written the number N. On the following N lines, the i-th line contains the countries to which the i-th country is connected. Every integer on this line is bigger than i, except the last one which is 0 and marks that no more countries are listed for country i. If a line contains 0, that means that the i-th country is not connected to any other country, which number is larger than i.Output
The output contains exactly one line. If the coloring is possible, this line must contain a list of zeros and ones, without any separators between them. The i-th digit in this sequence is the color of the i-th country. 0 corresponds to red color, and one — to blue color. If a coloring is not possible, output the integer −1.Sample
input | output |
---|---|
3 2 0 3 0 0 | 010 |
只用0,1对图进行染色,并且相邻的不能是相同颜色
分析:
直接DFS染色,对于相邻的染成相反的颜色即可
View Code
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int X = 105; int po[X],tol,n; int use[X]; struct node{ int y,next; }edge[X*X]; void add(int x,int y){ edge[++tol].y = y; edge[tol].next = po[x]; po[x] = tol; } bool dfs(int x){ int y; bool ok = true; for(int i=po[x];i;i=edge[i].next){ y = edge[i].y; if(use[x]==use[y]) return false; if(use[y]!=-1) continue; use[y] = !use[x]; ok = ok&&dfs(y); if(!ok) return false; } return true; } int main(){ //freopen("sum.in","r",stdin); int x; while(cin>>n){ memset(po,0,sizeof(po)); tol = 0; for(int i=1;i<=n;i++){ while(scanf("%d",&x),x){ add(i,x); add(x,i); } } bool ok = true; memset(use,-1,sizeof(use)); for(int i=1;i<=n;i++) if(use[i]==-1){ use[i] = 0; if(!dfs(i)){ ok = false; break; } } if(ok){ for(int i=1;i<=n;i++) printf("%d",use[i]); puts(""); } else puts("-1"); } return 0; }
相关文章推荐
- ural 1080. Map Coloring(二分染色bfs)
- URAL 1080 Map Coloring(染色)
- URAL 1080 Map Coloring (BFS)
- Map Coloring - URAL 1080 涂色问题
- URAL 1156 Two Rounds (DFS二分染色 + 分组背包)
- uva 193 Graph Coloring( 图染色 ) DFS+回溯
- UVA 193 Graph Coloring 图染色 DFS 数据
- 1080. Map Coloring
- CodeForces - 149D Coloring Brackets 详细题解(递归区间DP+dfs染色,好题)
- uva 193 Graph Coloring(图染色 dfs回溯)
- UVA 193 Graph Coloring 图染色 DFS 数据
- 1080. Map Coloring
- Codeforces 663C Graph Coloring(图染色dfs)
- URAL 1080 Map Colouring [简单DFS]
- E. Ice cream coloring (图论 染色 DFS)
- ural1156. Two Rounds(二分染色+dfs)
- 【CodeForces 149D】 【dp+dfs好题】D. Coloring Brackets【在限制条件下括号染色问题】
- URAL 1156 Two Rounds (DFS二分染色 + DFS枚举 + 剪枝)
- ural 1106,二分图染色,DFS
- poj 1419 Graph Coloring uva 193 - Graph Coloring (图着色问题,dfs)