sgu121

121. Bridges painting

time limit per test: 0.5 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
/*
 *NAME:Bridge painting
 *LANG:C++
 *SOURCE:sgu121
 *TIMES:3
 *METHOD:
 *找到一个度为奇数的点(如果没有那么从度为偶数的点开始)
 *对于从度为奇数的点进行扩展,
 *假设我们扩展出的第一个条边的颜色为1,
 *1、如果我们可以沿着这条边,经过一个环,回到这个点(此时我们利用了该点的两条边),
 *那么我们可以通过该点的另一条边,继续扩展边并且颜色为2
 *2、如果我们沿着这条边,只能访问一条路径,不能访问贿赂,
 *那么同理,可以扩展另一条路径,并且颜色为2
 *如果该点为偶数点,显然可以同时扩展颜色为1和颜色为2的边,不多说了
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int mn = 110;
struct node{
    int v;
    node * next;
};
node * g[mn];
int num[mn],l,now[mn],n,k;
int cc[mn][mn];
bool visit[mn][mn] = {0};
void insert(int x,int y){
    node *z = new node();
    z->v = x;z->next = g[y];g[y]=z;
}
void dfs(int p,int color){
    int c = 3-color;
    node *x = g[p];
    while (x){
	if (!visit[p][x->v] && !visit[x->v][p]) {
	    visit[p][x->v]=visit[x->v][p]=true;
	    cc[p][x->v] = cc[x->v][p] = c;
	    dfs(x->v,c);c = 3-c;
	}
	x = x->next;
    }
}
void print(node *x,int pos){
    if (x!=0) print(x->next,pos);
    else return ;
    cout << cc[pos][x->v] << " ";
}
int main(){
    cin >> n;memset(g,0,sizeof(g));
    for (int i=1;i<=n;++i){
	cin >> k;
	while (k){
	    insert(k,i);num[i]++;cin >> k;
	}now[i] = 1;
    }
    for (int i=1;i<=n;++i)
	if (num[i] & 1) dfs(i,1);
    for (int i=1;i<=n;++i)
	dfs(i,1);
    for (int i=1;i<=n;++i){
	node * x= g[i];int tmp=0;
	while (x!=0){
	    tmp = tmp | cc[i][x->v];
	    x=x->next;
	}
	if (tmp!=3 && num[i]>1){
	    cout << "No solution" << endl;
	    return 0;
	}
    }
    for (int i =1;i<=n;++i){
	print(g[i],i);
	cout << "0" << endl;
    }return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值