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;
}

  

sgu 121. Bridges painting 列举情况 难度:1的更多相关文章

  1. SGU 121.Bridges painting

    原题地址 题意: 新百慕大由N个岛屿组成,在岛屿之间有一些连接它们的桥.在任意两个岛屿之间,最多只有一座桥连接它们.总统先生下达命令,要求给所有桥上色. 每一座桥能被染成 白色 或者 黑色. 每一个岛 ...

  2. Bridges painting - SGU 121(构造)

    题目大意:有个一无向图,给所有的边染色,如果一个点连接的边超过两个,那么最少要染一个白色和一个黑色,能否给整个图染色?不能输出“No solution”. 分析:引用连接 http://edward- ...

  3. SGU 138. Games of Chess 构造 难度:2

    138. Games of Chess time limit per test: 0.25 sec. memory limit per test: 4096 KB N friends gathered ...

  4. hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1

    F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  5. sgu 147. Black-white king 思路 坑 难度:1

    147. Black-white king time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard i ...

  6. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  7. 今日SGU 5.28

    SGU 121 题意:给你一张图,问你每个顶点必须有黑白两条边(如果它的边数>=2),问你怎么染色,不行就输出no 收获:你会发现不行的情况只有一个单纯的奇数环的时候,反之我们交替染色即可 #i ...

  8. leetcode 179. Largest Number 求最大组合数 ---------- java

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. 《算法问题实战策略》-chaper8-动态规划法

    Q1:偶尔在电视上看到一些被称为“神童”的孩子们背诵小数点以后几万位的圆周率.背诵这么长的数字,可利用分割数字的方法.我们用这种方法将数字按照位数不等的大小分割后再背诵. 分割形式如下: 所有数字都相 ...

随机推荐

  1. Cocos 开发笔记

    经发现: cocos creator 提供的hello world 模版中.只有HelloWorkd.js中 properties 属性 text的值不是'hello world!' Label 组件 ...

  2. [c/c++]指针(2)

    首先呢,讲讲数组 数组就是一连串的地址对不对?所以它们的地址是紧挨着的 1 | 2 | 3 | 4 | 2 | 0 1 2 3 4 那我们把一个数组的首地址赋给一个指针变量 ] = {, , , , ...

  3. JQuery插件模板

    (function($){ $.fn.插件名 = function(settings){ var defaultSettings = { } /* 合并默认参数和用户自定义参数 */settings ...

  4. Sizeof与Strlen的区别【转】

    本文转载自:http://www.cnblogs.com/carekee/articles/1630789.html Sizeof与Strlen的区别与联系 一.sizeof    sizeof(.. ...

  5. 一种斐波那契博弈(Fibonacci Nim)

    事实上我也不知道这算是哪个类型的博弈 是在复习$NOIP$初赛的时候看到的一个挺有趣的博弈 所以就写出来分享一下 $upd \ on \ 2018.10.12$忽然发现这个其实就是$Fibonacci ...

  6. HDU 3549 Flow Problem(最大流模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...

  7. UVa 11572 唯一的雪花(滑动窗口)

    https://vjudge.net/problem/UVA-11572 题意:输入一个长度为n的序列A,找到一个尽量长的连续子序列,使得该序列中没有相同的元素. 思路:很简单的题,也没啥好解释的了. ...

  8. Java zip解压,并遍历zip中的配置文件 .cfg或.properties

    1.解析cfg或properties配置文件 讲配置文件,读取,并封装成为map类型数据 /** * 解析cfg文件 * * @param cfgFile * @return */ public st ...

  9. 关于C++中的友元函数的总结

    1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率.如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数.具 ...

  10. H5图片预览、压缩、上传

    目标实现: 1.选择图片, 前端预览效果 2.图片大于1.2M的时候, 对图片进行压缩 3.以表单的形式上传图片 4.图片删除 预览效果图: 代码说明: 1.input:file选择图片 <!- ...