QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#49087#4599. Orgrimmarckiseki#AC ✓1824ms89120kbC++897b2022-09-19 14:18:042022-09-19 14:18:06

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-19 14:18:06]
  • Judged
  • Verdict: AC
  • Time: 1824ms
  • Memory: 89120kb
  • [2022-09-19 14:18:04]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
static constexpr int maxn = 500000 + 5;

vector<int> g[maxn];
int dp[maxn][3];

void dfs(int u, int f) {
    dp[u][0] = dp[u][1] = dp[u][2] = 0;
    int m = 0;
    for (int v : g[u]) {
        if (v == f) continue;
        dfs(v, u);
        dp[u][0] += max({dp[v][0], dp[v][1], dp[v][2]});
        dp[u][1] += dp[v][0];
        m = max(m, dp[v][1] - dp[v][0]);
    }
    dp[u][1]++;
    dp[u][2] = dp[u][1] + m;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t; cin >> t;
    while (t--) {
        int n; 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);
        }
        dfs(1, 1);
        cout << max({dp[1][0], dp[1][1], dp[1][2]}) << '\n';
        for (int i = 1; i <= n; ++i)
            g[i].clear();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1824ms
memory: 89120kb

input:

10
500000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
5...

output:

333334
499999
374859
374664
374655
374764
374530
374548
374848
374511

result:

ok 10 lines