博客
关于我
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/

你可能感兴趣的文章
MySQL 的mysql_secure_installation安全脚本执行过程介绍
查看>>
MySQL 的Rename Table语句
查看>>
MySQL 的全局锁、表锁和行锁
查看>>
mysql 的存储引擎介绍
查看>>
MySQL 的存储引擎有哪些?为什么常用InnoDB?
查看>>
Mysql 知识回顾总结-索引
查看>>
Mysql 笔记
查看>>
MySQL 精选 60 道面试题(含答案)
查看>>
mysql 索引
查看>>
MySQL 索引失效的 15 种场景!
查看>>
MySQL 索引深入解析及优化策略
查看>>
MySQL 索引的面试题总结
查看>>
mysql 索引类型以及创建
查看>>
MySQL 索引连环问题,你能答对几个?
查看>>
Mysql 索引问题集锦
查看>>
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>