Tree Modification

You are given a tree with n n n vertices. You are allowed to modify the structure of the tree through the following multi-step operation:

Choose three vertices a a a, b b b, and c c c such that b b b is adjacent to both a a a and c c c.
For every vertex d d d other than b b b that is adjacent to a a a, remove the edge connecting d d d and a a a and add the edge connecting d d d and c c c.
Delete the edge connecting a a a and b b b and add the edge connecting a a a and c c c.
As an example, consider the following tree:
在这里插入图片描述

The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices 2 2 2, 4 4 4, and 5 5 5:

在这里插入图片描述
It can be proven that after each operation, the resulting graph is still a tree.

Find the minimum number of operations that must be performed to transform the tree into a star. A star is a tree with one vertex of degree n − 1 n−1 n1, called its center, and n − 1 n−1 n1 vertices of degree 1 1 1.

Input
The first line contains an integer n n n ( 3 ≤ n ≤ 2 ⋅ 1 0 5 ) (3≤n≤2⋅10^5) (3n2105) — the number of vertices in the tree.

The i i i-th of the following n − 1 n−1 n1 lines contains two integers u i u_i ui and v i v_i vi ( 1 ≤ u i , v i ≤ n , u i ≠ v i ) (1≤u_i,v_i≤n, u_i≠v_i) (1ui,vin,ui=vi) denoting that there exists an edge connecting vertices u i u_i ui and v i v_i vi. It is guaranteed that the given edges form a tree.

Output
Print a single integer — the minimum number of operations needed to transform the tree into a star.

It can be proven that under the given constraints, it is always possible to transform the tree into a star using at most 1 0 18 10^{18} 1018 operations.

Examples

input
6
4 5
2 6
3 2
1 2
2 4
output
1
input
4
2 4
4 1
3 4
output
0

Note
The first test case corresponds to the tree shown in the statement. As we have seen before, we can transform the tree into a star with center at vertex 5 5 5 by applying a single operation to vertices 2 2 2, 4 4 4, and 5 5 5.

In the second test case, the given tree is already a star with the center at vertex 4 4 4, so no operations have to be performed.
假设合并方式类似拓扑排序,先从叶子节点及其父节点开始。
这里合并的含义是:基于这种合并方式,每次建立新的边时一定有一个端点会变成叶子结点,可以看做是这个叶子节点合并到另一个节点上。
根据描述,对树进行黑白染色后合并时的节点 a a a c c c颜色总是相同的。显然只有所有参与合并的 a a a c c c颜色都相同是才是最优解,因为假如不相同,最后合并不到同一个点上,还要把一个点上的叶子节点一个个移到另一个点上,这样的总次数等于一边的黑色点数加上另一边白色点数加上最后合并时较少一边的点数,这些点数一定包括了两种颜色的点数,因此总操作数等于某种颜色点数加上某一边另一种颜色的点数,一定不是最优解。
综上,最优解是两种颜色中数量较小的一种减一。

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e5 + 10;
int head[N], ver[N << 1], Next[N << 1], tot;
int n, d[N], cnt[2];

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

void dfs(int x, int f) {
    d[x] = d[f] + 1;
    reps(x) {
        int y = ver[i];
        if (y == f)continue;
        dfs(y, x);
    }
}

int main() {
    n = qr();
    repi(i, 1, n - 1) {
        int x = qr(), y = qr();
        add(x, y), add(y, x);
    }
    dfs(1, 0);
    repi(i, 1, n)cnt[d[i] & 1]++;
    pi(min(cnt[0], cnt[1]) - 1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值