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. updateByPrimaryKeySelective更新失败

    问题:使用Mybatis中Mapper内置方法updateByPrimaryKeySelective更新失败. 发现:控制台打印出来的sql语句发现where条件出现所有属性. 解决:映射的实体类没有 ...

  2. List集合实现简易学生管理

    题目: 代码: package org.wlgzs; import java.util.ArrayList; import java.util.List; import java.util.Scann ...

  3. 网站精准查询IP

    分享一些网站能精准查询IP的 https://www.chaidu.com/App/Web/IP/ http://www.ipip.net/ip.html http://www.hao7188.com ...

  4. git如何列出最简短的commit(tag和head名都不显示)

    答:git log --oneline --no-decorate --oneline: 将commit显示成一行 --no-decorate: 将tag和head名隐藏掉

  5. P4013 数字梯形问题 网络流二十四题

    P4013 数字梯形问题 题目描述 给定一个由 nn 行数字组成的数字梯形如下图所示. 梯形的第一行有 m 个数字.从梯形的顶部的 m 个数字开始,在每个数字处可以沿左下或右下方向移动,形成一条从梯形 ...

  6. 【第二章】 第二个spring-boot程序

    上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序.这一节会引入spring-boot官方文档推荐的方式来开发代码,并引入我们在spring开发中service层等的调用. 1. ...

  7. Unity3D学习笔记(十三):委托、考试复习

    委托:比较什么时候用委托好   下课案例:不用下课铃 1.ClassManager需要拿到所有教室的引用,课堂管理者应该只负责计时并告知每间教室 2.每间教室应该是由当班老师负责是否需要下课,而课堂管 ...

  8. redis nginx session tomcat

    Redis解决session共享 http://lyl-zsu.iteye.com/blog/2408292 http://zx10103326.iteye.com/blog/2244195 http ...

  9. WPF基础学习笔记整理 (六) RoutedEvent路由事件

    基础知识: 传统的事件模型中,会在消息触发时将消息通过事件传给事件的订阅者(显式的事件订阅),事件订阅者使用事件处理程序来做出响应.事件订阅者必须能够直接访问到事件的宿主(拥有者). 路由事件的事件的 ...

  10. canvas 实现鼠标画出矩形

    <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...