• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

1080. Map Coloring

题意:N个城市,给定一连通图,问相邻的城市间能否染不同颜色,能则用0、1表示红蓝色输出,否则输出-1。

思路:双向建图,广搜~

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1080

 

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <queue>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 const int N=110;
10 
11 queue<int>Q;
12 int c[N],map[N][N],pay[N];
13 int n;
14 
15 void Init(){
16     memset(pay,-1,sizeof(pay));
17     memset(map,0,sizeof(map));
18     scanf("%d",&n);
19     int k,i;
20     for(i=1;i<n;i++){
21         while(scanf("%d",&k),k){
22             map[i][k]=1;
23             map[k][i]=1;
24         }
25     }
26     while(!Q.empty())
27         Q.pop();
28 }
29 
30 int BFS(int x){
31     pay[x]=0;
32     Q.push(x);
33     int i,v;
34     while(!Q.empty()){
35         v=Q.front();
36         Q.pop();
37         for(i=1;i<=n;i++){
38             if(map[v][i]){
39                 if(pay[i]==-1){
40                     pay[i]=1-pay[v];
41                     Q.push(i);
42                 }
43                 else{
44                     if(pay[i]==pay[v]) return 0;
45                 }
46             }    
47         }
48     }
49     return 1;
50 }
51 
52 int main(){
53     
54 //    freopen("data.in","r",stdin);
55 //    freopen("data.out","w",stdout);
56     
57     Init();
58     int k=BFS(1);
59     if(k==0) puts("-1");
60     else{
61         for(int i=1;i<=n;i++)
62             printf("%d",pay[i]);
63         puts("");
64     }
65     return 0;
66 }
posted @ 2012-05-05 22:57  Hug_Sea  阅读(166)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3