QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99489#6317. XOR Tree PathSorting#WA 28ms9052kbC++141.4kb2023-04-22 18:19:352023-04-22 18:19:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-22 18:19:39]
  • 评测
  • 测评结果:WA
  • 用时:28ms
  • 内存:9052kb
  • [2023-04-22 18:19:35]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
template<class T> void check_min(T &a, const T &b){ a = (a < b) ? a : b; }
template<class T> void check_max(T &a, const T &b){ a = (a > b) ? a : b; }
#define all(x) (x).begin(), (x).end()

const int N = 1e5 + 3;
const int INF = 1e9;

int n, a[N];
vector<int> adj[N];

int dp[N][2];

void solve(int u, int p = -1){
    vector<int> v;
    for(int to: adj[u]){
        if(to == p) continue;
        v.push_back(to);
        solve(to, u);
    }

    static int dp2[N][2];
    for(int i = 0; i <= v.size(); ++i){
        if(i == 0){
            dp2[i][0] = 0;
            dp2[i][1] = -INF;
            continue;
        }
        int x = v[i - 1];
        dp2[i][0] = max(dp[x][0] + dp2[i - 1][0], dp[x][1] + dp2[i - 1][1]);
        dp2[i][1] = max(dp[x][1] + dp2[i - 1][1], dp[x][1] + dp2[i - 1][0]);
    }

    if(v.size() == 0){
        dp[u][0] = a[u];
        dp[u][1] = !a[u];
    }
    else{
        dp[u][0] = dp2[v.size()][0] + a[u];
        dp[u][1] = dp2[v.size()][1] + !a[u];
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    solve(1);

    cout << max(dp[1][0], dp[1][1]) << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 7532kb

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: 3ms
memory: 7496kb

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: 28ms
memory: 9052kb

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:

53002

result:

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