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. MIME协议(详解范例)

    转载一:http://blog.csdn.net/bripengandre/article/details/2192982 转载二:http://blog.csdn.net/flfna/article ...

  2. idata,xdata,pdata,code

    data       --->   可寻址片内ram bdata     --->  可位寻址的片内ram idata      --->   可寻址片内ram,允许访问全部内部ra ...

  3. Python3基础 while 阶乘

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. python学习笔记比较全

    注:本笔记基于python2.6而编辑,尽量的偏向3.x的语法 Python的特色 1.简单 2.易学 3.免费.开源 4.高层语言: 封装内存管理等 5.可移植性: 程序如果避免使用依赖于系统的特性 ...

  5. JavaScript replaceAll

    网上的: String.prototype.replaceAll = function(str1, str2) { var str = this; var result = str.replace(e ...

  6. UVa 3349 Snowflake Snow Snowflakes(Hash)

    http://poj.org/problem?id=3349 题意: 给出n片雪花留个角的长度,要求判断是否有一样的雪花. 思路: Hash表的应用. 首先将每个雪花所有角的总长计算出来,如果两片雪花 ...

  7. MVC ---- Linq查询

    Linq查询:编译后,会生成对应的标准查询运算符!所以说,Linq只是类似与Sql的一种更加友好的语法而已: public class LinqDemo{ public static void Tes ...

  8. linux之cut用法--转载

    cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut  [-bn] [file] 或 cut ...

  9. Yii中的CComponent应用实例

    首先我们先了解一下如何创建一个CComponent,手册讲述如下: CComponent 是所有组件类的基类. CComponent 实现了定义.使用属性和事件的协议. 属性是通过getter方法或/ ...

  10. jsp动作之 setProperty

    setProperty用来设置useBean实例的属性. 如useBean实例化了一个类,类中有nickname属性,那么,我们可以用setProperty来重新定义他的值. setProperty有 ...