博客
关于我
1907: 树的路径覆盖
阅读量:395 次
发布时间: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/

你可能感兴趣的文章
SNMP介绍及使用,超有用,建议收藏!
查看>>
SDUT2161:Simple Game(NIM博弈+巴什博弈)
查看>>
51nod 1596 搬货物(二进制处理)
查看>>
来自星星的祝福(容斥+排列组合)
查看>>
Hmz 的女装(递推)
查看>>
HDU5589:Tree(莫队+01字典树)
查看>>
Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
查看>>
不停机替换线上代码? 你没听错,Arthas它能做到
查看>>
sharding-jdbc 分库分表的 4种分片策略,还蛮简单的
查看>>
分库分表的 9种分布式主键ID 生成方案,挺全乎的
查看>>
MySQL不会丢失数据的秘密,就藏在它的 7种日志里
查看>>
Python开发之序列化与反序列化:pickle、json模块使用详解
查看>>
回顾-生成 vs 判别模型-和图
查看>>
采坑 - 字符串的 "" 与 pd.isnull()
查看>>
无序列表 - 链表
查看>>
SQL 查询强化 - 数据准备
查看>>
SQL 强化练习 (四)
查看>>
SQL 强化练习 (八)
查看>>
Excel 拼接为 SQL 并打包 exe
查看>>
Pandas数据分析从放弃到入门
查看>>