sgu121

来源:互联网 发布:绝对萌域淘宝店名 编辑:程序博客网 时间:2024/05/26 02:51

题目:

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

62 3 01 3 01 2 5 05 04 6 3 05 0

Sample Output

1 2 01 2 02 2 1 02 02 2 1 02 0
题解:

1.先找度为奇的点,再找度为偶的点

2.搜索到的每条边依次赋值为1和2,同一条点连出去的每一条边也依次赋值为1和2

3.如果有一个度不为一的点连出去的边只有1或只有2则无解


先是WA在test8,然后就莫名其妙的WA在test6,最后发现将同一个点连出去的边全部赋成了同一个值,更正后就AC了。。。话说这样也能过到test5???

PS:中间WA了一次test15,然后将num[i] & 1 && !visited[i]改成!visited[i] && num[i] & 1就过了。。。莫名其妙。。。


代码:

#include <cstdio>bool visited[105] = {false};int n, print[105][105], map[105][105], num[105];void work(int now, int k){visited[now] = true;for (int i = 1; i <= num[now]; i++)if (print[now][map[now][i]] == 0){print[now][map[now][i]] = print[map[now][i]][now] = k + 1;work(map[now][i], 1 - k);k = 1 - k;}}int main(){int j, done;scanf("%d", &n);for (int i = 1; i <= n; i++)for (;scanf("%d", &j), j;)map[i][++num[i]] = j;for (int i = 1; i <= n; i++)if (!visited[i] && num[i] & 1)work(i, 0);for (int i = 1; i <= n; i++)if (!visited[i])work(i, 0);for (int i = 1; i <= n; i++)if (num[i] > 1){done = 0;for (j = 1; j <= num[i] && done != 3; j++)done |= print[i][map[i][j]];if (done != 3){puts("No solution");return 0;}}for (int i = 1; i <= n; i++){for (j = 1; j <= num[i]; j++)printf("%d ", print[i][map[i][j]]);printf("0\n");}return 0;}

0 0
原创粉丝点击