hduoj - 6567 Cotree【树DP】

Cotree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 33    Accepted Submission(s): 13


 

Problem Description

Avin has two trees which are not connected. He asks you to add an edge between them to make them connected while minimizing the function ∑ni=1∑nj=i+1dis(i,j), where dis(i,j) represents the number of edges of the path from i to j. He is happy with only the function value.

 

 

Input

The first line contains a number n (2<=n<=100000). In each of the following n−2 lines, there are two numbers u and v, meaning that there is an edge between uand v. The input is guaranteed to contain exactly two trees.

 

 

Output

Just print the minimum function value.

 

 

Sample Input

 

3 1 2

 

 

Sample Output

 

4

 

 

分析:对每棵树分别求出每个节点到其他节点的距离之和,显而易见,两棵树上这个值最小的点连起来就是最优解。

然后再跑一次整棵树答案就出来了。可以先跑出第一个节点到其他节点的距离之和,然后维护一个子树的节点数不断的更新,随便推一下结果就出来了。

#include

using namespace std;
int n;
vector v[100004];
long long son[100004];
bool vis[100004];
long long size[100004];
int fa[100004];
int num[100004];

int Find(int a) {
    return fa[a] == a ? a : fa[a] = Find(fa[a]);
}

void dfs1(int u, int pre) {
    son[u] = 1;
    for (int i = 0; i < v[u].size(); ++i) {
        if (v[u][i] == pre)continue;
        dfs1(v[u][i], u);
        son[u] += son[v[u][i]];
    }
}

long long sum, res;

void dfs2(int u, int pre, int n) {
    size[u] = res;
    sum++;
    for (int i = 0; i < v[u].size(); ++i) {
        if (v[u][i] == pre)continue;
        res += sum;
        res += -2 * son[v[u][i]] + n - sum;
        dfs2(v[u][i], u, n);
    }
    res += -sum + 2 * son[u];
    res += -(n - sum);
}

void dfs3(int u, int pre, int dis) {
    res += dis;
    for (int i = 0; i < v[u].size(); ++i) {
        if (v[u][i] == pre)continue;
        dfs3(v[u][i], u, dis + 1);
    }

}

int main() {
    cin >> n;
    memset(son, 0, sizeof(son));
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) {
        fa[i] = i;
        num[i] = 1;
    }
    for (int i = 0; i < n - 2; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
        int fx = Find(x);
        int fy = Find(y);
        if (fx != fy) {
            fa[fx] = fy;
            num[fy] += num[fx];
        }
    }
    vector a;
    for (int i = 1; i <= n; ++i) {
        if (fa[i] == i) {
            a.push_back(i);
            sum = res = 0;
            dfs1(i, -1);
            dfs3(i, -1, 0);
            dfs2(i, -1, num[i]);
        }
    }
    long long mini1 = 1e18, mini2 = 1e18;
    int cnt1, cnt2;
    for (int i = 1; i <= n; ++i) {
        if (Find(i) == a[0]) {
            if (size[i] < mini1) {
                cnt1 = i;
                mini1 = size[i];
            }
        } else {
            if (size[i] < mini2) {
                cnt2 = i;
                mini2 = size[i];
            }
        }
    }
    v[cnt1].push_back(cnt2);
    v[cnt2].push_back(cnt1);
    sum = res = 0;
    dfs1(1, -1);
    dfs3(1, -1, 0);
    dfs2(1, -1, n);
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans += size[i];
    }
    ans /= 2;
    cout << ans << endl;
}

 

你可能感兴趣的:(hduoj - 6567 Cotree【树DP】)