sgu 121. Bridges painting 列举情况 难度:1
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的更多相关文章
- SGU 121.Bridges painting
原题地址 题意: 新百慕大由N个岛屿组成,在岛屿之间有一些连接它们的桥.在任意两个岛屿之间,最多只有一座桥连接它们.总统先生下达命令,要求给所有桥上色. 每一座桥能被染成 白色 或者 黑色. 每一个岛 ...
- Bridges painting - SGU 121(构造)
题目大意:有个一无向图,给所有的边染色,如果一个点连接的边超过两个,那么最少要染一个白色和一个黑色,能否给整个图染色?不能输出“No solution”. 分析:引用连接 http://edward- ...
- 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 ...
- hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1
F - Rotational Painting Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 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 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- 今日SGU 5.28
SGU 121 题意:给你一张图,问你每个顶点必须有黑白两条边(如果它的边数>=2),问你怎么染色,不行就输出no 收获:你会发现不行的情况只有一个单纯的奇数环的时候,反之我们交替染色即可 #i ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 《算法问题实战策略》-chaper8-动态规划法
Q1:偶尔在电视上看到一些被称为“神童”的孩子们背诵小数点以后几万位的圆周率.背诵这么长的数字,可利用分割数字的方法.我们用这种方法将数字按照位数不等的大小分割后再背诵. 分割形式如下: 所有数字都相 ...
随机推荐
- Eclipse中离线安装ADT插件详细教程
在搭建Android开发环境的时候,我们需要为Eclipse安装ADT(Android Development Tools)插件,这个插件可以为用户提供一个强大的Android集成开发环境.通过给Ec ...
- 【镜像地址】Maven地址列表
1.国内OSChina提供的镜像,非常不错 <mirror> <id>CN</id> <name>OSChina Central</name> ...
- BZOJ4415: [Shoi2013]发牌 树状数组+二分
Description 假设一开始,荷官拿出了一副新牌,这副牌有N张不同的牌,编号依次为1到N.由于是新牌,所以牌是按照顺序排好的,从牌库顶开始,依次为1, 2,……直到N,N号牌在牌库底.为了发完所 ...
- 【Coursera】Fourth Week(2)
Netscape JavaScript and Firefox 当Microsoft收购Netscape失败之后: JavaScript 创造并用于与 Visual Basic 竞争(1995). N ...
- python 判断一个数字是否为4的幂
def is_Power_of_four(n): while n and not (n & 0b11): n >>= ) print(is_Power_of_four()) pri ...
- JavaScript 基础知识入门
js3种弹出框 alert消息对话框 var mychar=I love JavaScript;alert(mychar); confirm消息对话框 返回值:bool var mymessage ...
- notification后,程序应该如何响应
一般来讲,点击一个notification后,都会打开一个Activity做为对点击事件的响应,这个Activity是之前在PendingIntent中设置好的. 经常玩Android手机的应该都有印 ...
- angular5补漏知识点
1.属性行指令 attr.** 2.ngfor循环优化 trackBy func 3.aot编译 4.tree shaking 5.脏检测方法 6.管道的 pure和impure 7.asyncpip ...
- C++STL2--map
C++STL2--map 一.心得 本质上就是数组,关联数组,map就是键到值得一个映射,并且重载了[]符号,所以可以像数组一样用. map<string,int> cnt;//前键后值, ...
- Myeclipse2016安装Aptana
Myeclipse2016安装Aptana 想装个Aptana,装了半天,网上说的什么links方式啊,在线方式啊,都是什么的浮云. 所以自己来写个安装教程. 一.Aptana简要介绍 Aptana有 ...