C. Cut 'em all!+子树大小

C. Cut 'em all!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You're given a tree with n

vertices.

Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.

Input

The first line contains an integer n

( 1n105

) denoting the size of the tree.

The next n1

lines contain two integers u, v ( 1u,vn) each, describing the vertices connected by the i

-th edge.

It's guaranteed that the given edges form a tree.

Output

Output a single integer k

— the maximum number of edges that can be removed to leave all connected components with even size, or 1

if it is impossible to remove edges in order to satisfy this property.

Examples
Input
Copy
4
2 4
4 1
3 1
Output
Copy
1
Input
Copy
3
1 2
1 3
Output
Copy
-1
Input
Copy
10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5
Output
Copy
4
Input
Copy
2
1 2
Output
Copy
0
Note

In the first example you can remove the edge between vertices 1

and 4

. The graph after that will have two connected components with two vertices in each.

In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is 1

.

#define happy

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int N=1e5+10;

vi mp[N];
int sz[N];

void dfs(int p,int u){
    sz[u]=1;
    for(int v:mp[u]){
        if(v==p)continue;
        dfs(u,v);
        sz[u]+=sz[v];
    }
}


int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    int n=rd();
    if(n%2)return puts("-1"),0;
    rep(i,1,n-1){
        int x=rd(),y=rd();
        mp[x].pb(y);
        mp[y].pb(x);
    }
    dfs(-1,1);
    int ans=-1;
    rep(i,1,n)if(sz[i]%2==0)ans++;
    printf("%d\n",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值