1129. Door Painting
Time limit: 0.25 second Memory limit: 64 MB
There are many rooms, corridors and doors between them in the kindergarten. Some repairs are planned to be made soon. The doors are agreed to be painted in bright cheerful colors: green and yellow. The matron of the kindergarten wants the doors to satisfy the following condition: the sides of an arbitrary door must have the different colors. The number of green doors in each of the lodgings must differ from the number of yellow doors not more than by one. Given the plan of the kindergarten suggest your scheme of door painting.
Input
The first line contains the number of lodgings N ≤ 100 in the kindergarten. The next N lines contain description of the door configuration (k+1-st line contains a description of the k-th lodging). Each of the N lines starts with the number of doors that connect this lodging with adjacent ones. Then there are numbers of adjacent lodgings separated with a space (these numbers follow in ascending order).
Output
should contain a required painting scheme or the word “Impossible” if it is impossible to satisfy the requirements. The colors of the K-th room doors should be put in the K-th line in the same order as they were in the input data. The green color is denoted by G, yellow — by Y.
Sample
input | output |
---|---|
5 3 2 3 4 3 1 3 5 4 1 2 4 5 3 1 3 5 3 2 3 4 |
G Y G Y G Y G Y Y G Y G G G Y Y |
Problem Author: Magaz Asanov Problem Source: VI Ural State University Collegiate Programming Contest (21.10.2001)
***************************************************************************************
将从房子出去的门染红色,进去的门染绿色..也就转换成出度和入度关系;
若每个房子有偶数个门,这整个图就是个欧拉回路,故一定能行
若有奇数个房子有奇数个门,则一定不行(不能构成欧拉路或欧拉回路)
则对于有偶数个房子有奇数个门,则将这偶数个门两两匹配,形成一条虚拟的走廊...则样就形成了欧拉回路了!!最后去掉加入的边即可
这题有重边,可能不联通有多条欧拉路。
******************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<stack> 7 #include<cmath> 8 using namespace std; 9 const int maxn=1001; 10 vector<int>A[maxn][maxn]; 11 vector<int>odd; 12 int n,G[maxn][maxn]; 13 int mat[maxn][maxn]; 14 int vis[maxn]; 15 int out[maxn]; 16 int i,j,k,a; 17 void dfs(int u)//dfs求欧拉回路或欧拉路 18 { 19 vis[u]=1; 20 for(int it=1;it<=n;it++) 21 { 22 while(G[u][it]) 23 { 24 G[u][it]--; 25 G[it][u]--; 26 dfs(it); 27 A[u][it].push_back(1); 28 A[it][u].push_back(2); 29 } 30 } 31 } 32 int main() 33 { 34 memset(out,0,sizeof(out)); 35 memset(G,0,sizeof(G)); 36 odd.clear(); 37 cin>>n; 38 for(i=1;i<=n;i++) 39 { 40 cin>>out[i]; 41 if(out[i]%2) 42 odd.push_back(i); 43 for(j=1;j<=out[i];j++) 44 { 45 cin>>a; 46 G[i][a]++; 47 } 48 } 49 memcpy(mat,G,sizeof(G)); 50 if(odd.size()%2)//度为奇数的点的个数为奇数时情况,不存在 51 { 52 cout<<"Impossible"<<endl; 53 return 0; 54 } 55 for(i=0;i<odd.size();i+=2) 56 { 57 int u=odd[i];//出度为奇点的边度两两加1 58 int v=odd[i+1]; 59 G[u][v]++; 60 G[v][u]++; 61 } 62 memset(vis,0,sizeof(vis)); 63 memset(A,0,sizeof(A)); 64 for(i=1;i<=n;i++) 65 if(!vis[i]) 66 dfs(i); 67 for(i=1;i<=n;i++) 68 { 69 bool gs=true; 70 for(j=1;j<=n;j++) 71 { 72 int k=0; 73 while(mat[i][j]) 74 { 75 mat[i][j]--; 76 if(!gs) 77 cout<<' '; 78 else 79 gs=false; 80 if(A[i][j][k]==1)//输出记录的路径 81 cout<<'G'; 82 else 83 cout<<'Y'; 84 } 85 } 86 cout<<endl; 87 } 88 return 0; 89 90 }