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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章