Codeforces Round #484 (Div. 2) C. Cut 'em all!(简单树形dp)

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 nn 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 nn (1≤n≤1051≤n≤105) denoting the size of the tree.

The next n−1n−1 lines contain two integers uu, vv (1≤u,v≤n1≤u,v≤n) each, describing the vertices connected by the ii-th edge.

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

Output

Output a single integer kk — the maximum number of edges that can be removed to leave all connected components with even size, or −1−1if 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 11 and 44. 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−1.

有一颗树,然后他想要删除一些边,使得每一块联通块中的点的个数是偶数,问你最多可以删除多少条边,不能的话就是-1

显然,如果点的个数为奇数的时候就是-1

然后首先dfs记录一下以i为根的子树的节点个数,接着再dfs一下,从底往上搜,如果出现num[i]为偶数的节点考虑删去,因为你是从下一点点遍历的,所以肯定会删除最少的2,除非出现一种情况,就是一颗小的子树,而非联通,这样可能就会要分开4个节点作为连通块(类似下图,这种结构不管你怎么分,都是不能再删除的),然后找到一次,记录ans++即可

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp(db a,db b){ return sign(a - b);}
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
int fa[maxn];
int Find(int x) {   if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
ll c,n,k;
vector<int>v[maxn];
int cnt = 0;
int f[maxn];
int num[maxn];
void dfs(int x,int fa){
    for(auto d:v[x]){
        if(d == fa) continue;
        dfs(d,x);
        num[x] += num[d];
    }
    return ;
}
int ans = 0;
void dfs1(int x,int fa){
    for(auto d:v[x]){
        if(d == fa) continue;
        if(num[d] % 2 == 0) ans++;
        dfs1(d,x);
    }
    return ;
}
int main() {
    ios::sync_with_stdio(false);
    while(cin >> n){
         for(int i = 1;i < n;i++){
            int u,vv;
            cin >> u >> vv;
            v[u].push_back(vv);
            v[vv].push_back(u);
         }
         if(n & 1){
            cout << -1 << endl;
            continue;
         }
         for(int i = 1;i <= n;i++) num[i] = 1;
         dfs(1,0);
//         for(int i = 1;i <= n;i++){
//            cout << num[i] << endl;
//         }
         dfs1(1,0);
         cout << ans << endl;
    }
    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给定一棵 $n$ 个节点的树,每个节点都有一个权值 $w_i$。你需要删去树上的一些边,使得剩下的每个连通块中,所有节点的权值之和不超过 $k$。 求最多能删去多少条边。 输入格式 第一行包含两个整数 $n,k$。 第二行包含 $n$ 个整数 $w_1,w_2,\cdots,w_n$。 接下来 $n-1$ 行,每行包含两个整数 $a,b$,表示节点 $a$ 和节点 $b$ 之间有一条边。 输出格式 输出一个整数,表示最多能删去的边数。 数据范围 $1\le n \le 10^5$ $1 \le k,w_i \le 10^9$ 输入样例1: 5 6 2 3 1 5 4 1 2 1 3 2 4 2 5 输出样例1: 2 输入样例2: 5 3 2 3 1 5 4 1 2 1 3 2 4 2 5 输出样例2: 0 算法1 (dfs) $O(n)$ 首先我们可以想到暴力的做法,即对于每个连通块,暴力枚举删去哪些边,尝试得到最多的删边数。那么如何求解一个连通块内的所有节点权值之和呢?我们可以使用 DFS 遍历树,对于每个节点,求出其子树内所有节点的权值之和,那么以该节点为根的子树内的所有节点权值之和就是该节点自身的权值加上其所有子节点的权值之和。 对于每个连通块,我们枚举该连通块内任意两个节点 $x,y$。如果 $x$ 与 $y$ 之间的路径上的所有边都被删去了,那么 $x$ 和 $y$ 就会分别成为两个新的连通块,这两个新的连通块内所有节点的权值之和都不超过 $k$。因此我们只需要枚举所有的 $x,y$ 对,对于每个 $x,y$ 对尝试删去它们之间的路径上的所有边,看是否能够让 $x$ 和 $y$ 成为两个新的连通块,进而更新答案即可。 时间复杂度 参考文献 python3 代码 算法2 (暴力枚举) $O(n^2)$ blablabla 时间复杂度 参考文献 C++ 代码 class Solution { public: const int N = 1e5+10; int n,k; int h[N],e[N<<1],ne[N<<1],idx; int w[N]; int sum[N]; bool st[N]; int res; void add(int a,int b) { e[idx] = b,ne[idx] = h[a],h[a] = idx++; } void dfs(int u,int father) { sum[u] = w[u]; for(int i=h[u];~i;i=ne[i]) { int j = e[i]; if(j == father) continue; dfs(j,u); sum[u] += sum[j]; } } void dfs2(int u,int father) { for(int i=h[u];~i;i=ne[i]) { int j = e[i]; if(j == father) continue; dfs2(j,u); if(sum[j] <= k) res++; else if(sum[u] - sum[j] <= k) res++; } } void solve() { cin >> n >> k; memset(h,-1,sizeof(h)); for(int i=1;i<=n;i++) cin >> w[i]; for(int i=1;i<n;i++) { int a,b; cin >> a >> b; add(a,b); add(b,a); } dfs(1,-1); dfs2(1,-1); cout << res << endl; } }; int main() { Solution().solve(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值