SGU 121.Bridges painting

原题地址

题意:

      新百慕大由N个岛屿组成,在岛屿之间有一些连接它们的桥。在任意两个岛屿之间,最多只有一座桥连接它们。总统先生下达命令,要求给所有桥上色。

      每一座桥能被染成 白色 或者 黑色。

      每一个岛屿至少有一座白色的桥和一座黑色的桥(当然,如果只有一座桥就不存在这些问题)

 


 

 

Solution:

               很简单的dfs,水水就能过。

 

代码

         

#include <iostream>
#include <cstring>
#define INF 111
using namespace std;
int edge[INF][INF], color[INF][INF], sed[INF];
int n, x, sum, t;
void dfs (int x, int k) {
	for (int i = 1, j; i <= sed[x]; i++) {
		j = edge[x][i];
		if (!color[x][j]) {
			color[x][j] = color[j][x] = k == 0 ? 2 : 1;
			dfs (j, k ^ 1);
			k ^= 1;
		}
	}
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)
		while (cin >> x && x != 0)
			edge[i][++sed[i]] = x;
	for (int i = 1; i <= n; i++)
		if (sed[i] & 1) dfs (i, 1);
	for (int i = 1; i <= n; i++)
		dfs (i, 1);
	for (int i = 1, k; i <= n; i++) {
		if (sed[i] <= 1) continue;
		k = 0;
		for (int j = 1; j <= sed[i]; j++) {
			k |= color[i][edge[i][j]];
		}
		if (k != 3) {t = 0;cout << "No solution"; return 0;}
	}
       for (int i = 1; i <= n; i++) {
              for (int j = 1; j <= sed[i]; j++) {
                     int p = edge[i][j];
                     cout << color[i][p] << ' ';
              }
              cout << 0 << endl;
       }
}

  

 

posted @ 2014-07-06 20:32  keambar  阅读(167)  评论(0编辑  收藏  举报