Rebuilding Roads
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 10441 | Accepted: 4778 |
Description
The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
Sample Input
11 6 1 2 1 3 1 4 1 5 2 6 2 7 2 8 4 9 4 10 4 11
Sample Output
2
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]
Source
题大意是:给你一棵节点为n的树,问至少砍几刀可以孤立出一棵节点为m的子树。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson ind<<1,le,mid
#define rson ind<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
#define ysk(x) (1<<(x))
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 150+10 ;
//const int maxm= ;
int n,p;
int ans;
vector<int >G[maxn];
void init()
{
for(int i=1;i<=n;i++) G[i].clear();
ans=INF;
}
int dp[maxn][maxn];
void dfs(int x)
{
memset(dp[x],0x3f,sizeof dp[x]);
// dp[x][0]=1;
dp[x][1]=0;
for(int i=0;i<G[x].size();i++)
{
int y=G[x][i];
dfs(y);
for(int v=n;v>=1;v--)
{
dp[x][v]++;
for(int v2=1;v2<v;v2++)
{
dp[x][v]=min(dp[x][v],dp[x][v-v2]+dp[y][v2]);
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&p))
{
init();
int x,y;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
G[x].push_back(y);
}
dfs(1);
for(int i=2;i<=n;i++)
{
ans=min(ans,++dp[i][p]);
}
ans=min(dp[1][p],ans);
printf("%d\n",ans);
}
return 0;
}
AC之后看了看别人的代码:
转移的思路稍稍与我不同。
总结一些小知识:
对于类似dp[]=min(dp[],dp[1]+/-dp[2]...)类似的状态转移方程,
如果把不存在设为INF(一般是0x3f)
那么dp[]是不会超过INF的
如果想把代码写的更精细些,可以在状态转移前面加上判断条件:
if(dp[1]<INF&&dp[2]<INF.... )
if(dp[1]!=INF&&dp[2]!=INF.... )
助于理解
如果有多个dp[]相加,小心爆int,最后和成了负数!!!成了最小值min