題目大意:給定一棵n棵節點的樹,求刪去某條邊後兩個分支的最小差異值。
解題思路:樹形DP.深搜兩次,第一次深搜記錄從當前節點的子孫節點總數(包括自己),第一次算預處理,復雜度為O(N),第二次利用第一次的結果找去掉某條邊後的最小差異值,答案需用__int64。其實一切皆模擬,這題只是模擬地比較有規律而已。
測試數據:
7 6
2 2 2 2 2 2 2
1 2
2 7
3 7
4 6
6 2
5 7
0 0
代碼:
[cpp]
#include <stdio.h>
#include <string.h>
#define MAX 110000
#define min(a,b) (a)<(b)?(a):(b)
#define Rabs(a) (a<0?-a:a)
#define int64 __int64
#define INF 21474836470000000
struct node {
int v;
node *next;
}*head[MAX],tree[MAX*2];
int n,m,ptr,val[MAX],vis[MAX];
int64 dp[MAX],ans,cnt,sum; www.2cto.com
void Initial() {
ans = INF;
sum = ptr = 0;
memset(vis,0,sizeof(vis));
memset(head,NULL,sizeof(head));
}
void AddEdge(int x,int y) {
tree[ptr].v = y;
tree[ptr].next = head[x],head[x] = &tree[ptr++];
tree[ptr].v = x;
tree[ptr].next = head[y],head[y] = &tree[ptr++];
}
void Dfs_Ini(int s,int pa) {
//自底向上計算每個節點的子孫個數
dp[s] = val[s];
vis[s] = 1;
node *p = head[s];
while (p != NULL) {
if (vis[p->v] == 0) {
Dfs_Ini(p->v,s);
dp[s] += dp[p->v];
}
p = p->next;
}
}
void Dfs_Solve() {
for (int i = 1; i <= n; ++i) {
//枚舉每一條邊
node *p = head[i];
while (p != NULL) {
int64 tp = dp[p->v];
ans = min(ans,Rabs((2 * tp - sum)));
p = p->next;
}
}
}
int main()
{
int i,j,k,a,b,cas = 0;
while (scanf("%d%d",&n,&m),n+m) {
Initial();
for (i = 1; i <= n; ++i)
scanf("%d",&val[i]),sum += val[i];
for (i = 1; i <= m; ++i) {
scanf("%d%d",&a,&b);
AddEdge(a,b);
}
Dfs_Ini(1,0); //第一次深搜,記錄當前節點的子孫總數
memset(vis,0,sizeof(vis));
Dfs_Solve(); //更新答案
printf("Case %d: %I64d\n",++cas,ans);
}
}