sgu 121. Bridges painting 列举情况 难度:1

121. Bridges painting

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

 

New Berland consists of N (1£ N£ 100) islands, some of them are connected by bridges. There can be no more than one bridge between any pair of islands. Mr. President issued a law to paint all bridges. A bridge can be painted white or black. Any island must have at least one white bridge and at least one black (of course if an island has more than one bridge).

 

Input

There is N on the fisrt line of input. Next N lines contain a list of islands connected with given island. Every list is finished by 0.

 

Output

If needed painting exists then write N lines. Write “1” and “2” in each line. Write “1” if bridge is painted white and “2” in other case. Write 0 at the end of any list. If needed painting does not exist then write “No solution”.

 

Sample Input

6
2 3 0
1 3 0
1 2 5 0
5 0
4 6 3 0
5 0

Sample Output

1 2 0
1 2 0
2 2 1 0
2 0
2 2 1 0
2 0

错误原因:!!用first的邻接表会出错会反过来,不想写网络流跑最优....

#include <cstdio>
#include <cstring>
using namespace std;
const int maxm=20000;
const int maxn=101;
int n,e;
bool vis[maxn],black[maxn],white[maxn];
int  G[maxn][maxn],color[maxn][maxn],len[maxm];
void dye(int x,int c){
    if(c==2)black[x]=true;
    if(c==1)white[x]=true;
}
bool hasdye(int x,int c){
    if(c==2)return black[x];
    if(c==1)return white[x];
    return true;
}
void dfs(int s,int c){
  //printf("dfs %d %d\n",s,c);
  vis[s]=true;
  if(c==0){
    c=1;
    for(int p=0;p<len[s];p++){
        int to=G[s][p];
        if(color[s][to]==0){
           color[s][to]=color[to][s]=c;
            dye(to,color[s][to]);
            dye(s,color[s][to]);
    //     printf("dye1 f:%d t:%d c:%d\n",s,to,color[s][to]);
            dfs(to,color[s][to]);
            c=3-c;
        }
    }
  }
  int undo=0;
    for(int p=0;p<len[s];p++){
        int to=G[s][p];
        if(color[s][to]==0&&(!hasdye(to,3-c)||hasdye(to,c))){
           color[s][to]=color[to][s]=3-c;
            dye(to,color[s][to]);
            dye(s,color[s][to]);
       //  printf("dye1 f:%d t:%d c:%d\n",s,to,color[s][to]);
            dfs(to,color[s][to]);
        }
        else if(color[s][to]==0)undo++;
    }

    if(undo<len[s]||hasdye(s,3-c))
        for(int p=0;p<len[s];p++){
            int to=G[s][p];
            if(color[s][to]==0){
            color[s][to]=color[to][s]=c;
                dye(to,color[s][to]);
                dye(s,color[s][to]);
          //       printf("dye2 f:%d t:%d c:%d\n",s,to,color[s][to]);
                dfs(to,color[s][to]);
            }
        }
    else if(undo==1){
        for(int p=0;p<len[s];p++){
            int to=G[s][p];
            if(color[s][to]==0){
                color[s][to]=color[to][s]=3-c;
                dye(to,color[s][to]);
                dye(s,color[s][to]);
      //        printf("dye2 f:%d t:%d c:%d\n",s,to,color[s][to]);
                dfs(to,color[s][to]);
            }
        }
    }
    else {
        for(int p=0;p<len[s];p++){
            int to=G[s][p];
            if(color[s][to]==0){
                if(undo)color[s][to]=color[to][s]=c;
                else color[s][to]=color[to][s]=3-c;
                undo=0;
                dye(to,color[s][to]);
                dye(s,color[s][to]);
                //  printf("dye1 f:%d t:%d c:%d\n",s,to,color[s][to]);
                dfs(to,color[s][to]);
            }
        }
    }
}
void addedge(int f,int t){
    G[f][len[f]++]=t;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int tmpt;
        while(scanf("%d",&tmpt)==1&&tmpt){
           addedge(i,tmpt);
        }
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            dfs(i,0);
        }
    }
    for(int i=1;i<=n;i++){
        if(len[i]>1&&(!black[i]||!white[i])){puts("No solution");return 0;}
    }
    for(int i=1;i<=n;i++){
        for(int p=0;p<len[i];p++){
            int to=G[i][p];
            printf("%d ",color[i][to]);
        }
        puts("0");
    }
    return 0;
}

  这是解题报告学习的

#include <cstdio>
#include <cstring>
using namespace std;
const int maxm=20000;
const int maxn=101;
int n,e;
bool vis[maxn],black[maxn],white[maxn];
int  G[maxn][maxn],color[maxn][maxn],len[maxm];
void dye(int x,int c){
    if(c==2)black[x]=true;
    if(c==1)white[x]=true;
}
bool hasdye(int x,int c){
    if(c==2)return black[x];
    if(c==1)return white[x];
    return true;
}
void dfs(int s,int c){
 // printf("dfs %d %d\n",s,c);
  vis[s]=true;
    for(int p=0;p<len[s];p++){
        int to=G[s][p];
        if(color[s][to]==0){
           color[s][to]=color[to][s]=3-c;
            dye(to,color[s][to]);
            dye(s,color[s][to]);
         //  printf("dye1 f:%d t:%d c:%d\n",s,to,color[s][to]);
            dfs(to,color[s][to]);
            c=3-c;
        }
    }
}
void addedge(int f,int t){
    G[f][len[f]++]=t;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int tmpt;
        while(scanf("%d",&tmpt)==1&&tmpt){
           addedge(i,tmpt);
        }
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]&&len[i]&1){
            dfs(i,1);
        }
    }
    for(int i=1;i<=n;i++){
           if(!vis[i]) dfs(i,1);
    }
    for(int i=1;i<=n;i++){
        if(len[i]>1&&(!black[i]||!white[i])){puts("No solution");return 0;}
    }
    for(int i=1;i<=n;i++){
        for(int p=0;p<len[i];p++){
            int to=G[i][p];
            printf("%d ",color[i][to]);
        }
        puts("0");
    }
    return 0;
}

  

posted @ 2014-10-06 02:01  雪溯  阅读(272)  评论(0编辑  收藏  举报