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

你可能感兴趣的文章
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>