답안 #321237

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
321237 2020-11-11T16:03:55 Z tushar_2658 Power Plant (JOI20_power) C++14
100 / 100
233 ms 29856 KB
#include "bits/stdc++.h"
using namespace std;

const int maxn = 200005;

vector<int> edges[maxn];
int a[maxn], dp[maxn], ans = 0;

void dfs(int x, int p){
  int cur = a[x], res = 0;
  dp[x] = cur;
  for(auto i : edges[x]){
    if(i != p){
      dfs(i, x);
      res += dp[i];
    }
  }
  res -= cur;
  dp[x] = max(dp[x], res);
  ans = max(ans, dp[x]);
}

void dfs1(int x, int p, int carry){
  int res = carry - a[x];
  for(auto i : edges[x]){
    if(i != p){
      res += dp[i];
    }
  }
  ans = max(ans, res);
  for(auto i : edges[x]){
    if(i != p){
      dfs1(i, x, max(a[x], res - dp[i]));
    }
  }
}

int main(int argc, char const *argv[])
{
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  for(int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    edges[x].push_back(y);
    edges[y].push_back(x);
  }
  string s;
  cin >> s;
  int cnt = 0;
  for(int i = 1; i <= n; ++i){
    a[i] = (s[i - 1] - '0');
    cnt += a[i];
  }
  ans = min(2, cnt);
  dfs(1, 1);
  dfs1(1, 1, 0);
  cout << ans << endl;

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 3 ms 4972 KB Output is correct
6 Correct 3 ms 5100 KB Output is correct
7 Correct 3 ms 5100 KB Output is correct
8 Correct 3 ms 5100 KB Output is correct
9 Correct 3 ms 5100 KB Output is correct
10 Correct 3 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 3 ms 5100 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 3 ms 5100 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 3 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 3 ms 4972 KB Output is correct
6 Correct 3 ms 5100 KB Output is correct
7 Correct 3 ms 5100 KB Output is correct
8 Correct 3 ms 5100 KB Output is correct
9 Correct 3 ms 5100 KB Output is correct
10 Correct 3 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 3 ms 5100 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 3 ms 5100 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 3 ms 5100 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 4 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5100 KB Output is correct
28 Correct 5 ms 5100 KB Output is correct
29 Correct 5 ms 5228 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
31 Correct 4 ms 5100 KB Output is correct
32 Correct 4 ms 5100 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 4 ms 5100 KB Output is correct
35 Correct 4 ms 5100 KB Output is correct
36 Correct 4 ms 5100 KB Output is correct
37 Correct 4 ms 5100 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 4 ms 5228 KB Output is correct
40 Correct 4 ms 5228 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5228 KB Output is correct
43 Correct 4 ms 5100 KB Output is correct
44 Correct 4 ms 5228 KB Output is correct
45 Correct 4 ms 5228 KB Output is correct
46 Correct 4 ms 5228 KB Output is correct
47 Correct 4 ms 5228 KB Output is correct
48 Correct 4 ms 5228 KB Output is correct
49 Correct 5 ms 5228 KB Output is correct
50 Correct 4 ms 5228 KB Output is correct
51 Correct 4 ms 5228 KB Output is correct
52 Correct 4 ms 5100 KB Output is correct
53 Correct 6 ms 5228 KB Output is correct
54 Correct 4 ms 5228 KB Output is correct
55 Correct 4 ms 5100 KB Output is correct
56 Correct 4 ms 5100 KB Output is correct
57 Correct 4 ms 5228 KB Output is correct
58 Correct 4 ms 5100 KB Output is correct
59 Correct 4 ms 5100 KB Output is correct
60 Correct 4 ms 5228 KB Output is correct
61 Correct 4 ms 5100 KB Output is correct
62 Correct 4 ms 5228 KB Output is correct
63 Correct 4 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 3 ms 4972 KB Output is correct
6 Correct 3 ms 5100 KB Output is correct
7 Correct 3 ms 5100 KB Output is correct
8 Correct 3 ms 5100 KB Output is correct
9 Correct 3 ms 5100 KB Output is correct
10 Correct 3 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 3 ms 5100 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 3 ms 5100 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 3 ms 5100 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 4 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5100 KB Output is correct
28 Correct 5 ms 5100 KB Output is correct
29 Correct 5 ms 5228 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
31 Correct 4 ms 5100 KB Output is correct
32 Correct 4 ms 5100 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 4 ms 5100 KB Output is correct
35 Correct 4 ms 5100 KB Output is correct
36 Correct 4 ms 5100 KB Output is correct
37 Correct 4 ms 5100 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 4 ms 5228 KB Output is correct
40 Correct 4 ms 5228 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5228 KB Output is correct
43 Correct 4 ms 5100 KB Output is correct
44 Correct 4 ms 5228 KB Output is correct
45 Correct 4 ms 5228 KB Output is correct
46 Correct 4 ms 5228 KB Output is correct
47 Correct 4 ms 5228 KB Output is correct
48 Correct 4 ms 5228 KB Output is correct
49 Correct 5 ms 5228 KB Output is correct
50 Correct 4 ms 5228 KB Output is correct
51 Correct 4 ms 5228 KB Output is correct
52 Correct 4 ms 5100 KB Output is correct
53 Correct 6 ms 5228 KB Output is correct
54 Correct 4 ms 5228 KB Output is correct
55 Correct 4 ms 5100 KB Output is correct
56 Correct 4 ms 5100 KB Output is correct
57 Correct 4 ms 5228 KB Output is correct
58 Correct 4 ms 5100 KB Output is correct
59 Correct 4 ms 5100 KB Output is correct
60 Correct 4 ms 5228 KB Output is correct
61 Correct 4 ms 5100 KB Output is correct
62 Correct 4 ms 5228 KB Output is correct
63 Correct 4 ms 5100 KB Output is correct
64 Correct 199 ms 16040 KB Output is correct
65 Correct 197 ms 16036 KB Output is correct
66 Correct 200 ms 16144 KB Output is correct
67 Correct 194 ms 16036 KB Output is correct
68 Correct 199 ms 16144 KB Output is correct
69 Correct 192 ms 16028 KB Output is correct
70 Correct 196 ms 16052 KB Output is correct
71 Correct 193 ms 15984 KB Output is correct
72 Correct 189 ms 16032 KB Output is correct
73 Correct 226 ms 28716 KB Output is correct
74 Correct 227 ms 28580 KB Output is correct
75 Correct 198 ms 15904 KB Output is correct
76 Correct 187 ms 15912 KB Output is correct
77 Correct 203 ms 15900 KB Output is correct
78 Correct 202 ms 15904 KB Output is correct
79 Correct 200 ms 15912 KB Output is correct
80 Correct 211 ms 15904 KB Output is correct
81 Correct 159 ms 16352 KB Output is correct
82 Correct 175 ms 16100 KB Output is correct
83 Correct 210 ms 21160 KB Output is correct
84 Correct 207 ms 23216 KB Output is correct
85 Correct 214 ms 23140 KB Output is correct
86 Correct 199 ms 17568 KB Output is correct
87 Correct 197 ms 16928 KB Output is correct
88 Correct 182 ms 17248 KB Output is correct
89 Correct 187 ms 17328 KB Output is correct
90 Correct 180 ms 17156 KB Output is correct
91 Correct 181 ms 17416 KB Output is correct
92 Correct 188 ms 17696 KB Output is correct
93 Correct 182 ms 19956 KB Output is correct
94 Correct 169 ms 21852 KB Output is correct
95 Correct 177 ms 20444 KB Output is correct
96 Correct 183 ms 18704 KB Output is correct
97 Correct 183 ms 17148 KB Output is correct
98 Correct 182 ms 16724 KB Output is correct
99 Correct 186 ms 16416 KB Output is correct
100 Correct 189 ms 19728 KB Output is correct
101 Correct 181 ms 17568 KB Output is correct
102 Correct 177 ms 16756 KB Output is correct
103 Correct 187 ms 16580 KB Output is correct
104 Correct 106 ms 16856 KB Output is correct
105 Correct 221 ms 25508 KB Output is correct
106 Correct 108 ms 16724 KB Output is correct
107 Correct 102 ms 16720 KB Output is correct
108 Correct 227 ms 29476 KB Output is correct
109 Correct 106 ms 16988 KB Output is correct
110 Correct 233 ms 29856 KB Output is correct
111 Correct 178 ms 16048 KB Output is correct