JZOJ 7.9 B组第三题 treecut

1301. treecut (Standard IO)Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Description有一个N个节点的无根树,各节点编号为1..N,现在要求你删除其中的一个点,使分割开的连通块中节点个数都不超过原来的一半多。Input第一行:一个整数N (1 <= N <= 10,000)。   后面有N-1行:每行两个整数 X 和 Y,表示一个边连接的两个节点号。Output输出所有可能选择的点。如果有多个节点,按编号从小到大输出,每个一行。 如果找不到这样的点,输出一行:"NONE".Sample Input10 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10 3 8Sample Output3 8Data ConstraintHint样例说明:删除3号或8号节点,则分枝最多有5个节点分析:正解应该是选一个点建立有根树,然后维护一个数组来表示以I为根的树的节点个数。如果一个节点的所有儿子节点的 S 值不超过总结点数的一半,以及总结点数减去该节点的 S 值也不超过总节点数的一半,则该节点符合题目条件。但是用水dfs强行过了。。。代码:const maxn=10000;var flag:array [0..maxn,0..maxn] of boolean; mark,root:array [0..maxn] of boolean; follow:array [0..maxn] of longint; n:longint;procedure dfs(dep:longint);var i:longint; v:boolean;begin mark[dep]:=true; v:=true; for i:=1 to n do if (flag[i,dep]) and (not mark[i]) then begin dfs(i); inc(follow[dep],follow[i]); if follow[i]>n div 2 then begin v:=false; break; end; end; if n-follow[dep]>n div 2 then v:=false; if v then root[dep]:=true;end;procedure init;var i,x,y:longint;begin readln(n); for i:=1 to n-1 do begin readln(x,y); flag[x,y]:=true; flag[y,x]:=true; end;end;procedure main;var i:longint;begin for i:=1 to n do follow[i]:=1; dfs(1); for i:=1 to n do if root[i] then writeln(i);end;begin assign(input,'treecut.in');reset(input); assign(output,'treecut.out');rewrite(output); init; main; close(input);close(output);end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值