Submission #766786

# Submission time Handle Problem Language Result Execution time Memory
766786 2023-06-26T07:20:46 Z ParsaS Power Plant (JOI20_power) C++17
100 / 100
143 ms 27656 KB
// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 2e5 + 5;
int n;
vector<int> adj[N];
string s;
int dp[N], up[N], par[N];

void dfs(int v, int p) {
    par[v] = p;
    int sum = 0;
    for (auto u : adj[v]) {
        if (u != p) {
            dfs(u, v);
            sum += dp[u];
        }
    }
    dp[v] = max(0, sum - 1);
    if (s[v] == '0')
        dp[v] = sum;
    else
        dp[v] = max(dp[v], 1);
}
void dfs2(int v, int p) {
    int sum = 0;
    for (auto u : adj[v]) {
        if (u != p)
            sum += dp[u];
    }
    for (auto u : adj[v]) {
        if (u != p) {
            up[u] = max(0, up[v] + sum - dp[u] - 1);
            if (s[v] == '0')
                up[u] = up[v] + sum - dp[u];
            else
                up[u] = max(up[u], 1);
            dfs2(u, v);
        }
    } 
}

void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int v, u; cin >> v >> u;
        v--, u--;
        adj[v].pb(u), adj[u].pb(v);
    }
    cin >> s;
    dfs(0, 0);
    dfs2(0, 0);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
            ans = max(ans, up[i] + 1);
            for (auto u : adj[i])
                if (u != par[i])
                    ans = max(ans, dp[u] + 1);
        }
    }
    cout << ans << '\n';
}
int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4980 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4980 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5184 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
31 Correct 4 ms 5020 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 3 ms 5076 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 3 ms 5076 KB Output is correct
49 Correct 3 ms 5076 KB Output is correct
50 Correct 3 ms 5076 KB Output is correct
51 Correct 3 ms 5076 KB Output is correct
52 Correct 3 ms 5076 KB Output is correct
53 Correct 3 ms 5076 KB Output is correct
54 Correct 3 ms 5076 KB Output is correct
55 Correct 3 ms 5076 KB Output is correct
56 Correct 3 ms 5076 KB Output is correct
57 Correct 3 ms 5204 KB Output is correct
58 Correct 3 ms 5076 KB Output is correct
59 Correct 3 ms 5076 KB Output is correct
60 Correct 3 ms 5076 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 3 ms 5204 KB Output is correct
63 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4980 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5184 KB Output is correct
30 Correct 3 ms 5204 KB Output is correct
31 Correct 4 ms 5020 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 3 ms 5076 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 3 ms 5076 KB Output is correct
49 Correct 3 ms 5076 KB Output is correct
50 Correct 3 ms 5076 KB Output is correct
51 Correct 3 ms 5076 KB Output is correct
52 Correct 3 ms 5076 KB Output is correct
53 Correct 3 ms 5076 KB Output is correct
54 Correct 3 ms 5076 KB Output is correct
55 Correct 3 ms 5076 KB Output is correct
56 Correct 3 ms 5076 KB Output is correct
57 Correct 3 ms 5204 KB Output is correct
58 Correct 3 ms 5076 KB Output is correct
59 Correct 3 ms 5076 KB Output is correct
60 Correct 3 ms 5076 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 3 ms 5204 KB Output is correct
63 Correct 3 ms 5076 KB Output is correct
64 Correct 79 ms 13984 KB Output is correct
65 Correct 80 ms 14032 KB Output is correct
66 Correct 82 ms 13988 KB Output is correct
67 Correct 80 ms 14032 KB Output is correct
68 Correct 79 ms 14044 KB Output is correct
69 Correct 116 ms 14036 KB Output is correct
70 Correct 109 ms 14000 KB Output is correct
71 Correct 81 ms 13928 KB Output is correct
72 Correct 87 ms 14036 KB Output is correct
73 Correct 126 ms 26664 KB Output is correct
74 Correct 143 ms 26504 KB Output is correct
75 Correct 112 ms 13872 KB Output is correct
76 Correct 79 ms 13820 KB Output is correct
77 Correct 78 ms 13832 KB Output is correct
78 Correct 84 ms 13832 KB Output is correct
79 Correct 108 ms 13976 KB Output is correct
80 Correct 91 ms 13816 KB Output is correct
81 Correct 69 ms 14416 KB Output is correct
82 Correct 84 ms 14172 KB Output is correct
83 Correct 97 ms 19124 KB Output is correct
84 Correct 104 ms 21228 KB Output is correct
85 Correct 99 ms 20904 KB Output is correct
86 Correct 82 ms 15312 KB Output is correct
87 Correct 83 ms 14740 KB Output is correct
88 Correct 80 ms 15176 KB Output is correct
89 Correct 87 ms 15300 KB Output is correct
90 Correct 78 ms 15012 KB Output is correct
91 Correct 92 ms 15272 KB Output is correct
92 Correct 80 ms 15528 KB Output is correct
93 Correct 86 ms 18016 KB Output is correct
94 Correct 84 ms 19784 KB Output is correct
95 Correct 79 ms 18420 KB Output is correct
96 Correct 125 ms 16516 KB Output is correct
97 Correct 94 ms 15040 KB Output is correct
98 Correct 83 ms 14592 KB Output is correct
99 Correct 107 ms 14376 KB Output is correct
100 Correct 78 ms 17548 KB Output is correct
101 Correct 94 ms 15484 KB Output is correct
102 Correct 79 ms 14732 KB Output is correct
103 Correct 78 ms 14508 KB Output is correct
104 Correct 47 ms 14756 KB Output is correct
105 Correct 118 ms 23400 KB Output is correct
106 Correct 47 ms 14776 KB Output is correct
107 Correct 48 ms 14880 KB Output is correct
108 Correct 112 ms 27412 KB Output is correct
109 Correct 53 ms 14760 KB Output is correct
110 Correct 111 ms 27656 KB Output is correct
111 Correct 68 ms 13848 KB Output is correct