HDU 4714 Tree2cycle (树形DP)
2013-09-08 19:51
531 查看
Tree2cycle
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 324 Accepted Submission(s): 54
[align=left]Problem Description[/align]
A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of cost respectively. The nodes are labeled from 1 to N. Your job is to transform the tree to a cycle(without superfluous edges) using minimal cost.
A cycle of n nodes is defined as follows: (1)a graph with n nodes and n edges (2)the degree of every node is 2 (3) each node can reach every other node with these N edges.
[align=left]Input[/align]
The first line contains the number of test cases T( T<=10 ). Following lines are the scenarios of each test case.
In the first line of each test case, there is a single integer N( 3<=N<=1000000 ) - the number of nodes in the tree. The following N-1 lines describe the N-1 edges of the tree. Each line has a pair of integer U, V ( 1<=U,V<=N ), describing a bidirectional edge (U, V).
[align=left]Output[/align]
For each test case, please output one integer representing minimal cost to transform the tree to a cycle.
[align=left]Sample Input[/align]
1
4
1 2
2 3
2 4
[align=left]Sample Output[/align]
3
Hint
In the sample above, you can disconnect (2,4) and then connect (1, 4) and
(3, 4), and the total cost is 3.
[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Online —— Warmup
[align=left]Recommend[/align]
liuyiding
简单树形DP来一发
用dp1表示形成一颗链,而且该点在端点。
dp2表示形成一颗链需要的最少步数
/* ******************************************* Author : kuangbin Created Time : 2013年09月08日 星期日 12时00分01秒 File Name : 1009.cpp ******************************************* */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MAXN = 1000010; vector<int>vec[MAXN]; int dp1[MAXN]; int dp2[MAXN]; void dfs(int u,int pre) { int sz = vec[u].size(); int sum1 = 0; int maxn = 0, maxid = -1; int smaxn = 0, smaxid = -1; for(int i = 0;i < sz;i++) { int v = vec[u][i]; if(v == pre)continue; dfs(v,u); sum1 += dp2[v]+2; int tmp = dp1[v] - (dp2[v] + 2); tmp = -tmp; if(tmp > smaxn) { smaxn = tmp; smaxid = v; if(smaxn > maxn) { swap(smaxn,maxn); swap(smaxid,maxid); } } } dp1[u] = sum1 - maxn; dp2[u] = sum1 - maxn - smaxn; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int n; scanf("%d",&T); while(T--) { scanf("%d",&n); int u,v; for(int i = 1;i <= n;i++) vec[i].clear(); for(int i = 1;i < n;i++) { scanf("%d%d",&u,&v); vec[u].push_back(v); vec[v].push_back(u); } dfs(1,-1); cout<<dp2[1]+1<<endl; } return 0; }
相关文章推荐
- HDU 4714 Tree2cycle (构造,树形DP)
- 树形dp-hdu-4714-Tree2cycle
- HDU 4714 Tree2cycle(树形dp)
- HDU 4714 Tree2cycle (树形DP)
- 树形dp-hdu-4714-Tree2cycle
- hdu_4714_Tree2cycle(树形DP)
- hdu_4714_Tree2cycle(树形DP)
- hdu 4714(树形dp)
- HDOJ 题目4714 Tree2cycle(树形DP)
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
- hdu 4714 树形DP
- hdu4714 树形dp
- hdu 4714 Tree2cycle 树形经典问题
- HDU-4714-树形dp
- hdu 4714 (树形dp)
- HDU 4714 Treecycle(树形dp)
- hdu 4714 Tree2cycle(简单图或树DP,4级)
- hdu 4714 (树形dp)
- hdu 4714 Tree2cycle(简单图或树DP,4级)
- hdu 4714(树形dp)