当前位置 : 主页 > 编程语言 > python >

hdu 4714 Tree2cycle(简单图或树DP,4级)

来源:互联网 收集:自由互联 发布时间:2023-03-22
Tree2cycle Time Limit: 15000/8000 MS (Java/Others)Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 354Accepted Submission(s): 65 Problem Description A tree with N nodes and N-1 edges is given. To connect or disconnect one ed

Tree2cycle

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 354    Accepted Submission(s): 65

Problem Description

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.

 

Input

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).

 

Output

For each test case, please output one integer representing minimal cost to transform the tree to a cycle.

 

Sample Input

1 4 1 2 2 3 2 4

 

Sample Output

Hint

 

Source

​​2013 ACM/ICPC Asia Regional Online —— Warmup​​

 

Recommend

liuyiding

 思路:居然还真是像我想的那样,凡是碰到多于两个分支的全砍掉,砍掉的分支*2+1就是答案。

#include<cstdio>#include<cstring>#include<iostream>#pragma comment(linker,"/STACK:102400000,102400000")#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=2e6+9;int head[mm],edge,n;class Edge{ public:int v,next;}e[mm];void data(){ clr(head,-1);edge=0;}void add(int u,int v){ e[edge].v=v;e[edge].next=head[u];head[u]=edge++;}int ans;int dfs(int u,int fa){ int v; int id=0; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(v==fa)continue; id+=dfs(v,u); } if(id>=2) { if(u==1)//root; ans+=id-2; else ans+=id-1; return 0; } else return 1;//不切}void find_bcc(){ ans=0; dfs(1,-1); printf("%d\n",ans+ans+1);}int main(){ int cas; while(~scanf("%d",&cas)) { int a,b; while(cas--) { data(); scanf("%d",&n); FOR(i,2,n) { scanf("%d%d",&a,&b);add(a,b);add(b,a); } find_bcc(); } }}

dp

#include<cstdio>#include<cstring>#include<iostream>#pragma comment(linker,"/STACK:102400000,102400000")#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=2e6+9;int head[mm],edge,n;int dp[mm][2];class Edge{ public:int v,next;}e[mm];void data(){ clr(head,-1);edge=0;}void add(int u,int v){ e[edge].v=v;e[edge].next=head[u];head[u]=edge++;}int ans;void dfs(int u,int fa){ int v; dp[u][1]=dp[u][0]=0;//1 切父节点变成链的最小花费,0不切 int sum1 = 0; int maxn = 0, maxid = -1; int smaxn = 0, smaxid = -1; for(int i =head[u];~i;i=e[i].next) { int v = e[i].v; if(v == fa)continue; dfs(v,u); sum1 += dp[v][1]+2; int tmp = dp[v][0] - (dp[v][1] + 2); tmp = -tmp; if(tmp > smaxn) { smaxn = tmp; smaxid = v; if(smaxn > maxn) { swap(smaxn,maxn); swap(smaxid,maxid); } } } dp[u][0] = sum1 - maxn; dp[u][1] = sum1 - maxn - smaxn;}void find_bcc(){ ans=0; dfs(1,-1); printf("%d\n",dp[1][1]+1);}int main(){ int cas; while(~scanf("%d",&cas)) { int a,b; while(cas--) { data(); scanf("%d",&n); FOR(i,2,n) { scanf("%d%d",&a,&b);add(a,b);add(b,a); } find_bcc(); } }}

上一篇:hdu 4598 Difference(spfa+差分约束,4级)
下一篇:没有了
网友评论