A.二叉树的的根

题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模拟赛Day2)/二叉树的根

题解:自己yy一下就出来了。。。

如果有度数超过3的节点,则不可能成为2叉树,直接输出0即可

否则,树中度数为1和2的点都可以作为根

代码:

 1 var i,n,x,y,tot:longint;
 2     a,d:array[0..150000] of longint;
 3 procedure init;
 4  begin
 5    readln(n);
 6    for i:=1 to n-1 do
 7     begin
 8       readln(x,y);inc(d[x]);inc(d[y]);
 9     end;
10  end;
11 procedure main;
12  begin
13    tot:=0;
14    for i:=1 to n do
15     if d[i]>=4 then begin writeln(0);exit;end
16     else if d[i]<=2 then begin inc(tot);a[tot]:=i;end;
17    writeln(tot);
18    write(a[1]);
19    for i:=2 to tot do write(' ',a[i]);
20  end;
21 
22 begin
23   init;
24   main;
25 end.     
View Code

相关文章: