博客
关于我
1907: 树的路径覆盖
阅读量:410 次
发布时间:2019-03-05

本文共 2127 字,大约阅读时间需要 7 分钟。

1907: 树的路径覆盖

Time Limit: 5 Sec  Memory Limit: 259 MB
Submit: 506  Solved: 219
[][][]

Description

Input

Output

Sample Input

1
7
1 2
2 3
2 4
4 6
5 6
6 7

Sample Output

3

HINT

Source

 

题解:贪心题么么哒,直接DFS一遍就好啦

1 /************************************************************** 2     Problem: 1907 3     User: HansBug 4     Language: Pascal 5     Result: Accepted 6     Time:392 ms 7     Memory:3480 kb 8 ****************************************************************/ 9  10 type11     point=^node;12     node=record13                g:longint;14                next:point;15     end;16 var17    i,j,k,l,m,n,tot,t:longint;18    a:array[0..100000] of point;19    b:array[0..100000] of longint;20    c:array[0..100000] of boolean;21 procedure add(x,y:longint);22           var p:point;23           begin24                new(p);p^.g:=y;p^.next:=a[x];a[x]:=p;25           end;26 procedure dfs(y,x:longint);27           var p:point;tot:longint;28           begin29                b[x]:=1;tot:=0;p:=a[x];30                while p<>nil do31                      begin32                           if p^.g<>y then33                              begin34                                   dfs(x,p^.g);35                                   inc(b[x],b[p^.g]);36                                   if not(c[p^.g]) then inc(tot);37                              end;38                           p:=p^.next;39                      end;40                if tot>=2 then41                   begin42                        dec(b[x],2);43                        c[x]:=true;44                   end45                else if tot=1 then dec(b[x]);46           end;47 begin48      readln(t);49      while t>0 do50            begin51                 readln(n);52                 fillchar(b,sizeof(b),0);53                 fillchar(c,sizeof(c),false);54                 for i:=1 to n do a[i]:=nil;55                 for i:=1 to n-1 do56                     begin57                          readln(j,k);58                          add(j,k);add(k,j);59                     end;60                 dfs(0,1);dec(t);61                 writeln(b[1]);62            end;63 end.

 

 

转载地址:http://kpezz.baihongyu.com/

你可能感兴趣的文章
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>