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:偶尔在电视上看到一些被称为“神童”的孩子们背诵小数点以后几万位的圆周率.背诵这么长的数字,可利用分割数字的方法.我们用这种方法将数字按照位数不等的大小分割后再背诵. 分割形式如下: 所有数字都相 ...
随机推荐
- 关于fragment点击穿透的问题
当一个activity有多个fragment的时候,点击当前显示的fragment,如果点击处在其他fragment中也有相应的控件,那么可能会点击穿透,有响应另外fragment事件的趋势.但是这个 ...
- 论文笔记之:Continuous Deep Q-Learning with Model-based Acceleration
Continuous Deep Q-Learning with Model-based Acceleration 本文提出了连续动作空间的深度强化学习算法. 开始正文之前,首先要弄清楚两个概念:Mod ...
- C#学习笔记(十二):构造函数、属性和静态类
面向对象 简写重载的方法:重载中如果逻辑重复的情况下,用参数少的调用参数多 参数空缺,可以用null填补 using System; using System.Collections.Generic; ...
- javaweb项目运行时生成的Servers项目作用
在javaweb项目中,看到有一个Servers的项目,发现每新增一个项目,就会在Servers项目中新生成一些对应的项目文件. 如图所示: 每个项目都有对应的文件.文件的结构图如下: 解释一:Ser ...
- python 将16进制转化为2进制
>>> x='123abc' >>> b=bin())[:] >>> print(b)
- linux下带有空格的文件怎么删除
如:hello world文件 第一种方式 先用 ls -i 得到 hello world 的inod(就是最前面的数字)假设这个数字是123,然后find . -inum -exec rm {} \ ...
- AttributeError: 'Request' object has no attribute 'json', cherrypy 无法接收到json字符串,解决方法
@cherrypy.expose @cherrypy.tools.accept(media="application/json") #加入这个装饰器 @cherrypy.too ...
- Ubuntu 14.04 定时任务
如何在Ubuntu上启动一个定时任务,使得可以定时删除机器上的日志 首先, #查看cron状态 service cron status 如果提示没有安装 #安装cron服务 apt-get ins ...
- Oracle数据库常见版本
Oracle数据库常见版本 在Oracle数据库的发展中,数据库一直处于不断升级状态,有以下几个版本: Oracle 8,Oracle 8i:Oracle 8i表示Oracle正式向Internet上 ...
- Codeforces 101628A - Arthur's Language
101628A - Arthur's Language 思路:dp,状态转移见代码. 代码: #include<bits/stdc++.h> using namespace std; #d ...