zoukankan      html  css  js  c++  java
  • CF982C Cut 'em all!

    思路:

    在深搜过程中,贪心地把树划分成若干个连通分支就可以了。划分的条件是某个子树有偶数个节点。注意到在一次划分之后并不需要重新计数,因为一个数加上一个偶数并不影响这个数的奇偶性。

    实现:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAXN = 100005;
     4 vector<int> G[MAXN];
     5 bool vis[MAXN];
     6 int ans = 0;
     7 int dfs(int u)
     8 {
     9     vis[u] = true;
    10     int cnt = 0;
    11     for (int i = 0; i < G[u].size(); i++)
    12     {
    13         int tmp = G[u][i];
    14         if (!vis[tmp]) cnt += dfs(tmp);
    15     }
    16     if (cnt & 1) ans++;
    17     return cnt + 1;
    18 }
    19 int main()
    20 {
    21     int n, x, y;
    22     while (cin >> n)
    23     {
    24         for (int i = 1; i <= n; i++) G[i].clear();
    25         memset(vis, 0, sizeof vis);
    26         ans = 0;
    27         for (int i = 0; i < n - 1; i++)
    28         {
    29             cin >> x >> y;
    30             G[x].push_back(y);
    31             G[y].push_back(x);
    32         }
    33         if (n & 1) { cout << -1 << endl; continue; }
    34         dfs(1);
    35         cout << ans - 1 << endl;
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    css3 object-fit详解
    Timing path
    IUS
    FIFO深度
    UVM中的class--2
    UVM中的class
    Binding
    Concurrent Assertion
    Immediate assertion
    sdf
  • 原文地址:https://www.cnblogs.com/wangyiming/p/9060464.html
Copyright © 2011-2022 走看看