HDU 4714 Tree2Cycle
来源:互联网 发布:js发送http请求 cookie 编辑:程序博客网 时间:2024/06/03 20:15
简单的DFS 有些大神有树形DP做的,效率要高很多。
若degree[i] > 2 ,则断开 i 与其父节点之间的边 和 degree[i] - 3 个子节点之间的边,其父节点 度 减一。
此时总消费增加 sum += (degree[i] - 2)*2;
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <queue> 7 #include <vector> 8 #pragma comment(linker,"/STACK:102400000,102400000") 9 10 using namespace std;11 12 struct E13 {14 int v,next;15 } edge[2001000];16 17 int head[1001000];18 19 int top,sum;20 21 void link(int u,int v)22 {23 edge[top].v = v;24 edge[top].next = head[u];25 head[u] = top++;26 }27 28 bool visit[1000100];29 30 int dfs(int st)31 {32 int i;33 34 i = head[st];35 if(i == -1)//此时st点为叶子36 {37 return 1;38 }39 40 int ans = 0;41 42 visit[st] = true;43 44 while(i != -1)45 {46 if(visit[edge[i].v] == false)47 {48 visit[edge[i].v] = true;49 ans += dfs(edge[i].v);50 }51 i = edge[i].next;52 }53 54 55 if(ans <= 1)56 return 1;57 if(st == 1)58 sum += (ans -2)*2;59 else60 sum += (ans-1)*2;61 return 0;62 63 }64 65 int main()66 {67 int T;68 int n,i,u,v;69 70 scanf("%d",&T);71 72 while(T--)73 {74 top = 0;75 sum = 0;76 77 scanf("%d",&n);78 79 memset(head,-1,(n+2)*sizeof(int));80 memset(visit,false,(n+2)*sizeof(bool));81 82 for(i = 1; i < n; ++i)83 {84 scanf("%d %d",&u,&v);85 link(u,v);86 link(v,u);87 }88 89 dfs(1);90 printf("%d\n",++sum);91 }92 return 0;93 }
- HDU 4714 Tree2cycle
- hdu 4714 Tree2cycle dp
- hdu 4714 Tree2cycle
- HDU 4714 Tree2cycle
- hdu 4714 Tree2cycle
- hdu 4714 Tree2cycle
- Hdu 4714 Tree2cycle - DFS
- HDU 4714 Tree2cycle
- HDU 4714 Tree2Cycle
- HDU 4714 Tree2cycle
- HDU 4714 Tree2cycle
- HDU 4714 Tree2cycle 解题报告
- 树形dp-hdu-4714-Tree2cycle
- hdu 4714 Tree2cycle树上乱搞
- hdu 4714 Tree2cycle (树形dp)
- HDU 4714 Tree2cycle(树型DP)
- HDU 4714 Tree2cycle(树形dp)
- HDU 4714 Tree2cycle (构造,树形DP)
- POJ 1850 Code
- gcc链接,去掉不用的函数和data
- POJ 1942 Paths on a Grid 组合数的优化
- Documentation-filesystems/sysfs
- HDU 1009 The Shortest Path in Nya Graph
- HDU 4714 Tree2Cycle
- Nginx的基本操作
- 随机抽样一致性算法(RANSAC)
- iOS百度地图——用户当前位置
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
- MTK6589编译出错
- 基于App developer的开发
- Documentation_driver-model_bus.txt
- 调试突然报 Error Domain=NSURLErrorDomain Code=-1003