[SGU]121. Bridges painting
来源:互联网 发布:知乎话题重复 编辑:程序博客网 时间:2024/06/18 16:05
Analysis
刚拿到这道题的时候也想不出什么算法,然后去搜了一下才发现原来是简单的DFS构造……从任意奇数点开始DFS,对遍历到的边交替染色,如果这么做无解的话那么就No solution,否则输出解。证明似乎不那么容易,不过自己在纸上画一画应当能感觉出来吧……
Accepted Code
var map,color:array[0..110,0..110] of longint; deg:array[0..110] of longint; n,i,j,t:longint;procedure dfs(u,c:longint);var i,j:longint;begin c:=3-c; for i:=1 to deg[u] do begin j:=map[u,i]; if color[u,j]=0 then begin color[u,j]:=c; color[j,u]:=c; dfs(j,c); c:=3-c; end; end;end;begin readln(n); fillchar(map,sizeof(map),0); fillchar(deg,sizeof(deg),0); fillchar(color,sizeof(color),0); for i:=1 to n do begin read(t); while t<>0 do begin inc(deg[i]); map[i,deg[i]]:=t; read(t); end; end; for i:=1 to n do if odd(deg[i]) then dfs(i,1); for i:=1 to n do dfs(i,1); for i:=1 to n do if deg[i]>1 then begin t:=0; for j:=1 to deg[i] do t:=t or color[i,map[i,j]]; if t<>3 then begin writeln('No solution'); exit; end; end; for i:=1 to n do begin for j:=1 to deg[i] do write(color[i,map[i,j]],' '); writeln(0); end;end.
- [SGU]121. Bridges painting
- SGU 121 Bridges painting
- SGU 121 Bridges painting
- SGU 121. Bridges painting 构造 数据略水有漏洞
- SGU 121 Bridges painting(构造)
- sgu121:Bridges painting
- SGU121 Bridges painting
- SGU 183 Painting the balls (DP优化)
- SGU - 355 - Numbers Painting (贪心)
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- (SGU
- SGU
- SGU
- chrome extension(2)
- UVa 532 - Dungeon Master
- 背景建模
- 用MinGW编译boost的方法
- 求两个正整数的最大公约数
- [SGU]121. Bridges painting
- 求1000以内所有偶数的和(不包含1000)
- 千内偶数解法一
- HTTP协议及其request.setAttribute()用法
- Java中16进制字符串提交到C中转中英文字符串
- Howto enable flash support for chromium browser
- 【wp7】简易的语音报时懒人闹钟
- ajax 提交 報錯 參數有問題
- JS改变图片src代码