QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#118537#2596. Even ForestScarlett_boyWA 6ms27836kbC++171.2kb2023-07-03 17:04:102023-07-03 17:04:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-03 17:04:14]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:27836kb
  • [2023-07-03 17:04:10]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
vector<int> G[N];
int n;
ll dp[N][2]; // 0 表示为偶数 ; 1 表示为奇数
void dfs(int x, int fa) {
    int dep = 0;
    for (int to: G[x]) {
        if (to != fa) {
            dep++;
            dfs(to, x);
        }
    }
    if (dep == 0) {
        dp[x][0] = 0;
        dp[x][1] = INF;
    } else {
        for (int to: G[x]) {
            if (to != fa) {
                dp[x][0] += min({dp[to][0] + 1, dp[to][1]});
                dp[x][1] += dp[to][0];
            }
        }
    }

}

void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int p;
    for (int i = 1; i <= n; i++) {
        if (G[i].size() == 1) {
            p = i;
            break;
        }
    }
    dfs(p, p);
    cout << dp[p][0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int _ = 1;
//    cin >> _;
    for (int o = 1; o <= _; o++) {
//        cout << "Case #<<" << o << ": ";
        solve();
    }
    return 0;
}

/*



 */


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 27836kb

input:

4
1 2
2 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 2ms
memory: 27596kb

input:

4
1 2
1 3
1 4

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 6ms
memory: 27036kb

input:

6
1 2
2 3
4 2
4 5
6 4

output:

2

result:

wrong answer 1st numbers differ - expected: '1', found: '2'