Submission #502471

# Submission time Handle Problem Language Result Execution time Memory
502471 2022-01-06T05:16:21 Z tengiz05 Power Plant (JOI20_power) C++17
100 / 100
195 ms 33100 KB
#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 200000;
int n;
std::vector<int> e[N];
int a[N], dp[N], sum[N];
void dfs(int u, int p) {
    for (int v : e[u]) {
        if (v != p) {
            dfs(v, u);
            sum[u] += dp[v];
        }
    }
    dp[u] = std::max(a[u], sum[u] - a[u]); 
}
int ans = 0;
void dfs2(int u, int p) {
    ans = std::max(ans, dp[u]);
    for (int v : e[u]) {
        if (v == p) {
            continue;
        }
        int dpu = dp[u];
        int dpv = dp[v];
        int sumu = sum[u];
        int sumv = sum[v];
        
        sum[u] -= dp[v];
        dp[u] = std::max(a[u], sum[u] - a[u]);
        sum[v] += dp[u];
        dp[v] = std::max(a[v], sum[v] - a[v]);
        
        dfs2(v, u);
        
        dp[u] = dpu;
        dp[v] = dpv;
        sum[u] = sumu;
        sum[v] = sumv;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    std::cin >> n;
    
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    
    std::string S;
    std::cin >> S;
    for (int i = 0; i < n; i++) {
        a[i] = S[i] - '0';
    }
    
    for (int i = 0; i < n; i++) {
        for (int j : e[i]) {
            ans = std::max(ans, a[i] + a[j]);
        }
    }
    
    dfs(0, -1);
    dfs2(0, -1);
    
    std::cout << ans << "\n";
        
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4992 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 2 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4992 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 2 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
22 Correct 3 ms 5028 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 3 ms 4996 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 3 ms 5068 KB Output is correct
29 Correct 3 ms 5180 KB Output is correct
30 Correct 3 ms 5196 KB Output is correct
31 Correct 3 ms 5068 KB Output is correct
32 Correct 3 ms 5068 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 5 ms 5112 KB Output is correct
35 Correct 3 ms 5068 KB Output is correct
36 Correct 3 ms 5068 KB Output is correct
37 Correct 5 ms 5068 KB Output is correct
38 Correct 4 ms 5068 KB Output is correct
39 Correct 3 ms 5068 KB Output is correct
40 Correct 3 ms 5068 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 5068 KB Output is correct
43 Correct 3 ms 5068 KB Output is correct
44 Correct 4 ms 5068 KB Output is correct
45 Correct 3 ms 5068 KB Output is correct
46 Correct 3 ms 5068 KB Output is correct
47 Correct 3 ms 5068 KB Output is correct
48 Correct 3 ms 5196 KB Output is correct
49 Correct 3 ms 5196 KB Output is correct
50 Correct 4 ms 5068 KB Output is correct
51 Correct 5 ms 5068 KB Output is correct
52 Correct 3 ms 5068 KB Output is correct
53 Correct 5 ms 5068 KB Output is correct
54 Correct 4 ms 5068 KB Output is correct
55 Correct 6 ms 5068 KB Output is correct
56 Correct 4 ms 5068 KB Output is correct
57 Correct 5 ms 5196 KB Output is correct
58 Correct 3 ms 5068 KB Output is correct
59 Correct 3 ms 5024 KB Output is correct
60 Correct 3 ms 5196 KB Output is correct
61 Correct 3 ms 5068 KB Output is correct
62 Correct 3 ms 5196 KB Output is correct
63 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4992 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 2 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 2 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
22 Correct 3 ms 5028 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 3 ms 4996 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 3 ms 5068 KB Output is correct
29 Correct 3 ms 5180 KB Output is correct
30 Correct 3 ms 5196 KB Output is correct
31 Correct 3 ms 5068 KB Output is correct
32 Correct 3 ms 5068 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 5 ms 5112 KB Output is correct
35 Correct 3 ms 5068 KB Output is correct
36 Correct 3 ms 5068 KB Output is correct
37 Correct 5 ms 5068 KB Output is correct
38 Correct 4 ms 5068 KB Output is correct
39 Correct 3 ms 5068 KB Output is correct
40 Correct 3 ms 5068 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 5068 KB Output is correct
43 Correct 3 ms 5068 KB Output is correct
44 Correct 4 ms 5068 KB Output is correct
45 Correct 3 ms 5068 KB Output is correct
46 Correct 3 ms 5068 KB Output is correct
47 Correct 3 ms 5068 KB Output is correct
48 Correct 3 ms 5196 KB Output is correct
49 Correct 3 ms 5196 KB Output is correct
50 Correct 4 ms 5068 KB Output is correct
51 Correct 5 ms 5068 KB Output is correct
52 Correct 3 ms 5068 KB Output is correct
53 Correct 5 ms 5068 KB Output is correct
54 Correct 4 ms 5068 KB Output is correct
55 Correct 6 ms 5068 KB Output is correct
56 Correct 4 ms 5068 KB Output is correct
57 Correct 5 ms 5196 KB Output is correct
58 Correct 3 ms 5068 KB Output is correct
59 Correct 3 ms 5024 KB Output is correct
60 Correct 3 ms 5196 KB Output is correct
61 Correct 3 ms 5068 KB Output is correct
62 Correct 3 ms 5196 KB Output is correct
63 Correct 4 ms 5068 KB Output is correct
64 Correct 156 ms 14100 KB Output is correct
65 Correct 158 ms 16768 KB Output is correct
66 Correct 148 ms 16760 KB Output is correct
67 Correct 181 ms 16700 KB Output is correct
68 Correct 158 ms 16760 KB Output is correct
69 Correct 163 ms 16764 KB Output is correct
70 Correct 144 ms 16704 KB Output is correct
71 Correct 165 ms 16640 KB Output is correct
72 Correct 150 ms 16768 KB Output is correct
73 Correct 174 ms 31944 KB Output is correct
74 Correct 178 ms 31800 KB Output is correct
75 Correct 154 ms 16588 KB Output is correct
76 Correct 146 ms 16700 KB Output is correct
77 Correct 168 ms 16632 KB Output is correct
78 Correct 152 ms 16644 KB Output is correct
79 Correct 177 ms 16628 KB Output is correct
80 Correct 166 ms 16644 KB Output is correct
81 Correct 120 ms 17068 KB Output is correct
82 Correct 178 ms 16860 KB Output is correct
83 Correct 172 ms 22868 KB Output is correct
84 Correct 163 ms 25380 KB Output is correct
85 Correct 165 ms 25072 KB Output is correct
86 Correct 155 ms 18292 KB Output is correct
87 Correct 152 ms 17680 KB Output is correct
88 Correct 161 ms 18064 KB Output is correct
89 Correct 142 ms 18104 KB Output is correct
90 Correct 142 ms 17792 KB Output is correct
91 Correct 149 ms 18080 KB Output is correct
92 Correct 132 ms 18504 KB Output is correct
93 Correct 186 ms 21416 KB Output is correct
94 Correct 140 ms 23564 KB Output is correct
95 Correct 131 ms 21844 KB Output is correct
96 Correct 149 ms 19688 KB Output is correct
97 Correct 178 ms 17928 KB Output is correct
98 Correct 132 ms 17292 KB Output is correct
99 Correct 147 ms 17012 KB Output is correct
100 Correct 160 ms 20920 KB Output is correct
101 Correct 143 ms 18420 KB Output is correct
102 Correct 140 ms 17708 KB Output is correct
103 Correct 140 ms 17344 KB Output is correct
104 Correct 92 ms 17512 KB Output is correct
105 Correct 178 ms 28088 KB Output is correct
106 Correct 96 ms 17508 KB Output is correct
107 Correct 80 ms 17484 KB Output is correct
108 Correct 195 ms 32808 KB Output is correct
109 Correct 77 ms 17524 KB Output is correct
110 Correct 181 ms 33100 KB Output is correct
111 Correct 161 ms 16624 KB Output is correct