1080. Map Coloring
题意:N个城市,给定一连通图,问相邻的城市间能否染不同颜色,能则用0、1表示红蓝色输出,否则输出-1。
思路:双向建图,广搜~
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1080
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }