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

你可能感兴趣的文章
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>