QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#368938#6317. XOR Tree PathVengeful_Spirit#WA 19ms8788kbC++201.2kb2024-03-27 18:19:172024-03-27 18:19:17

Judging History

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

  • [2024-03-27 18:19:17]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:8788kb
  • [2024-03-27 18:19:17]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
const int N = 1e5 + 10;
vector<int> G[N];
int c[N], f[N][2];

void dfs(int x, int fa) {
    f[x][0] = f[x][1] = -1;
    int pos = 0, son = 0;
    vector<int> p;
    for(int y : G[x]) if(y != fa) {
        dfs(y, x);
        son++;
        pos += f[y][0];
        p.push_back(f[y][1] - f[y][0]);
    }
    sort(all(p)), reverse(all(p));
    int tmp = 0;
    for(int i = 0; i + 1 < p.size(); i += 2) {
        int j = i + 1;
        tmp = max(tmp, pos + p[i]);
        if(p[i] + p[j] > 0) pos += p[i] + p[j];
    }
    if(p.size() & 1) tmp += max(p.back(), 0);
    if(!son) {
        f[x][0] = c[x];
        f[x][1] = !c[x];
    } else {
        f[x][0] = pos + c[x];
        f[x][1] = tmp + (!c[x]);
    }
    // cerr << x << " " << f[x][0] << " " << f[x][1] << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> c[i];
    for(int i = 1, x, y; i < n; ++i) {
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 0);
    cout << max(f[1][0], f[1][1]) << "\n";
    return 0;
}

详细

Test #1:

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

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: 0ms
memory: 5680kb

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: 1ms
memory: 3532kb

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: -100
Wrong Answer
time: 19ms
memory: 8788kb

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:

49880

result:

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