QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#180954#6317. XOR Tree Pathucup-team228#AC ✓34ms18196kbC++173.3kb2023-09-16 14:40:182023-09-16 14:40:18

Judging History

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

  • [2023-09-16 14:40:18]
  • 评测
  • 测评结果:AC
  • 用时:34ms
  • 内存:18196kb
  • [2023-09-16 14:40:18]
  • 提交

answer

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <random>
#include <string>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <bitset>
#include <array>
#include <stack>
#include <sstream>
using namespace std;
typedef long long ll;
 
string to_string(string a) { return '"' + a + '"'; }
string to_string(char a) { return "'" + string(1, a) + "'"; }
string to_string(const char* a) { return to_string((string) a); }
string to_string(bool a) { return a ? "true" : "false"; }
template <class T1, class T2>
string to_string(pair<T1, T2> a) {
    return "(" + to_string(a.first) + ", " + to_string(a.second) + ")";
}
template <class T>
string to_string(T a) {
    bool first = true; string res = "{";
    for (const auto& i : a) {
        if (!first) res += ", ";
        first = false;
        res += to_string(i);
    }
    res += "}";
    return res;
}
void debug_out() { cerr << endl; }
template <class T1, class... T2>
void debug_out(T1 a, T2... b) {
    cerr << " " << to_string(a);
    debug_out(b...);
}
 
#ifdef LOCAL
#define out(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define out(...) 42
#endif

clock_t start_time; void start_timer() { start_time = clock(); }
double get_time() { return (double) (clock() - start_time) / CLOCKS_PER_SEC; }

void Solve();

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
    freopen("usr/share/man/man1/input.txt", "r", stdin);
#endif
    start_timer();
    Solve();
#ifdef LOCAL
    cerr << fixed << setprecision(3);
    cerr << endl << "Time spent: " << get_time() << endl;
#endif
    return 0;
}

// do something, stay focused
// look for stupid bugs
// guess, slow, stress
// don't overgeneralize
// don't rush

// don't waste time on standings

// SOLVE THE PROBLEM OR DIE TRYING
// THE SOLUTION IS ALWAYS SIMPLE
// THE CODE IS ALWAYS SHORT

const int N = 1e5 + 5;

int a[N];
int dp[2][N];
vector<int> g[N];

void dfs(int v = 1, int p = 0) {
    for (int u : g[v]) {
        if (u == p) continue;
        dfs(u, v);
    }
    dp[1][v] = -1;
    for (int u : g[v]) {
        if (u == p) continue;
        int nxt[] = {0, 0};
        if (dp[1][v] >= 0) {
            nxt[0] = max(dp[0][v] + dp[0][u], dp[1][v] + dp[1][u]);
            nxt[1] = max(dp[0][v] + dp[1][u], dp[1][v] + dp[0][u]);
        }
        else {
            nxt[0] = dp[0][v] + dp[0][u];
            nxt[1] = dp[0][v] + dp[1][u];
        }
        /*
        if (v == 2) {
            out(u, nxt[0], nxt[1]);
        }
        */
        dp[0][v] = nxt[0];
        dp[1][v] = nxt[1];
    }
    dp[0][v] += a[v];
    if (dp[1][v] >= 0) {
        dp[1][v] += 1 - a[v];
    }
    else {
        dp[1][v] = 1 - a[v];
    }
    //out(v, dp[0][v], dp[1][v]);
}

void Solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs();
    int ans = max(dp[0][1], dp[1][1]);
    cout << ans << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6540kb

input:

5
1 0 0 1 0
1 2
1 3
3 4
3 5

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 1ms
memory: 6224kb

input:

6
1 1 0 0 1 0
3 1
2 5
1 2
4 1
2 6

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 6036kb

input:

9
1 0 1 0 1 0 1 0 1
2 9
1 2
6 9
3 8
4 5
5 9
2 8
7 8

output:

6

result:

ok 1 number(s): "6"

Test #4:

score: 0
Accepted
time: 21ms
memory: 9512kb

input:

73537
0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 1 0 0 1 0 0 0 1 1 1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 1 0 1 1 1 1 0 1 0 1 1 0 0 1 1 0 0 1 0 0 1 1 0 0 1 1 1 1 1 1 1 0 1 0 0 1 0 1 1 1 0 0 0 1 0 1 1 0 0 ...

output:

56486

result:

ok 1 number(s): "56486"

Test #5:

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

input:

4440
1 1 1 1 1 0 1 1 1 1 0 0 1 0 0 1 0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 0 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 0 1 0 0 1 1 0 0 0 1 0 0...

output:

3419

result:

ok 1 number(s): "3419"

Test #6:

score: 0
Accepted
time: 8ms
memory: 8972kb

input:

55025
1 0 1 0 0 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 0 1 1 1 0 0 0 1 1 1 0 1 0 1 0 1 1 1 1 0 1 0 0 1 0 1 0 1 1 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 1 0 0 0 1 1 0 1 1 1 1 1 0 0 0 0 1 1 0 ...

output:

42221

result:

ok 1 number(s): "42221"

Test #7:

score: 0
Accepted
time: 14ms
memory: 8644kb

input:

59260
1 1 1 1 0 0 1 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 ...

output:

45452

result:

ok 1 number(s): "45452"

Test #8:

score: 0
Accepted
time: 7ms
memory: 8252kb

input:

48580
1 1 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 1 1 1 0 1 0 1 1 0 0 0 0 0 1 0 0 1 1 0 1 0 1 1 0 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 0 ...

output:

37287

result:

ok 1 number(s): "37287"

Test #9:

score: 0
Accepted
time: 29ms
memory: 10336kb

input:

100000
1 1 0 1 1 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 1 1 1 0 0 1 1 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 0 1 1 0 0 1 0 0 0 1 0 0 1 1 1 1 1 1 0 1 1 0 1 1 0 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0 1 1 0 0 1 0 1 0 0 0 0 1 1 0 1 1...

output:

76831

result:

ok 1 number(s): "76831"

Test #10:

score: 0
Accepted
time: 29ms
memory: 10428kb

input:

100000
1 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 0 1 0 0 1 1 0...

output:

76692

result:

ok 1 number(s): "76692"

Test #11:

score: 0
Accepted
time: 30ms
memory: 10388kb

input:

100000
0 1 0 0 1 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 1 1 1 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0 1 1 1 0 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 1 1 1 1 0...

output:

76715

result:

ok 1 number(s): "76715"

Test #12:

score: 0
Accepted
time: 0ms
memory: 6360kb

input:

2
1 0
1 2

output:

1

result:

ok 1 number(s): "1"

Test #13:

score: 0
Accepted
time: 0ms
memory: 6260kb

input:

2
0 0
1 2

output:

2

result:

ok 1 number(s): "2"

Test #14:

score: 0
Accepted
time: 1ms
memory: 7300kb

input:

3
0 0 0
1 2
1 3

output:

2

result:

ok 1 number(s): "2"

Test #15:

score: 0
Accepted
time: 10ms
memory: 9692kb

input:

30258
1 1 0 0 0 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 1 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 ...

output:

15162

result:

ok 1 number(s): "15162"

Test #16:

score: 0
Accepted
time: 15ms
memory: 14020kb

input:

63626
1 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 0 0 1 0 0 1 1 1 1 1 1 1 0 1 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 1 0 0 0 0 0 1 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 1 0 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 ...

output:

31871

result:

ok 1 number(s): "31871"

Test #17:

score: 0
Accepted
time: 34ms
memory: 18196kb

input:

100000
0 0 1 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 1 1 0 0 0 0 1 1 0 1 1 0 1 0 0 1 1 1 1 0 1 1 0 1 0 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 0 0 0 1 0 1...

output:

50029

result:

ok 1 number(s): "50029"

Test #18:

score: 0
Accepted
time: 1ms
memory: 6844kb

input:

702
1 1 0 0 0 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 1 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 ...

output:

532

result:

ok 1 number(s): "532"

Test #19:

score: 0
Accepted
time: 1ms
memory: 6016kb

input:

555
1 0 1 1 1 1 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 1 0 0 0 0 1 1 1 0 0 0 0 1 1 0 1 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 0 1 1 0 1 0 1 1 0 0 0 0 0 1 0 1 1 1 0 0 1 0 ...

output:

424

result:

ok 1 number(s): "424"

Test #20:

score: 0
Accepted
time: 1ms
memory: 6636kb

input:

81
0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1
1 31
11 53
51 76
10 17
12 68
10 43
36 56
79 81
55 56
68 81
27 50
18 64
29 36
8 59
4 52
6 7
4 54
5 16
49 56
38 41
24 33
25 52
32 71
14 5...

output:

57

result:

ok 1 number(s): "57"

Test #21:

score: 0
Accepted
time: 1ms
memory: 6096kb

input:

654
0 1 0 1 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 1 1 0 1 1 1 1 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 1 0 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 0 1 0 1 0 1 1 1 1 0 1 0 1 1 1 1 0 ...

output:

503

result:

ok 1 number(s): "503"

Test #22:

score: 0
Accepted
time: 1ms
memory: 6632kb

input:

721
1 0 0 1 0 0 0 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 0 0 1 0 0 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 1 1 1 1 0 0 1 1 1 0 1 0 0 0 1 0 0 0 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 0 0 1 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 1 1 1 1 1 ...

output:

557

result:

ok 1 number(s): "557"

Test #23:

score: 0
Accepted
time: 3ms
memory: 8008kb

input:

30258
1 1 0 0 0 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 1 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 ...

output:

30258

result:

ok 1 number(s): "30258"

Test #24:

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

input:

43363
0 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 0 1 1 0 1 1 1 0 0 1 0 0 0 0 0 1 0 1 0 1 1 0 1 1 0 1 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0 1 0 0 0 0 1 0 1 1 0 1 0 0 1 ...

output:

43363

result:

ok 1 number(s): "43363"

Test #25:

score: 0
Accepted
time: 10ms
memory: 10556kb

input:

100000
0 0 0 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 1 0 0 1 0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 0 1 1 1 0 0 0 1 1 0 1 1 1 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 1 1 0 1 0 0 0...

output:

100000

result:

ok 1 number(s): "100000"

Test #26:

score: 0
Accepted
time: 0ms
memory: 6312kb

input:

20
0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 1 0
1 20
17 18
10 11
5 19
5 12
6 18
2 13
8 18
8 13
13 15
1 3
5 11
14 15
3 16
9 17
7 19
4 12
4 16
7 15

output:

13

result:

ok 1 number(s): "13"

Test #27:

score: 0
Accepted
time: 1ms
memory: 6048kb

input:

8
1 0 0 1 0 1 0 0
3 5
3 8
1 2
6 8
3 4
1 6
7 8

output:

6

result:

ok 1 number(s): "6"