답안 #293711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293711 2020-09-08T10:09:12 Z arman_ferdous Power Plant (JOI20_power) C++17
100 / 100
243 ms 26872 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define dbg(x) cerr << #x << " is " << x << "\n";

using ll = long long;
using ii = pair<ll,ll>;

const int N = 2e5+10;

int n;
vector<int> g[N];
char s[N];

int dp[2][N];

void solve(int u, int f) {
  int childcnt = 0;
  for(int v : g[u]) if(v != f) {
    childcnt++;
    solve(v, u);
  }
  if(childcnt == 0) {
    dp[0][u] = dp[1][u] = (s[u] == '1');
    return;
  }
  if(s[u] == '0') {
    dp[0][u] = 0;
    for(int v : g[u]) if(v != f)
      dp[0][u] = max(dp[0][u], dp[0][v]);
    dp[1][u] = 0;
    for(int v : g[u]) if(v != f)
      dp[1][u] += dp[1][v];
  }
  else {
    int on = 1, off = 0;

    off = 0;
    for(int v : g[u]) if(v != f)
      off = max(off, dp[0][v]);

    for(int v : g[u]) if(v != f)
      on = max(on, dp[1][v] + 1);
    dp[0][u] = max(on, off);

    dp[1][u] = -1;
    for(int v : g[u]) if(v != f)
      dp[1][u] += dp[1][v];
    dp[1][u] = max(1, dp[1][u]);
  }
}

int main() {
  scanf("%d", &n);
  for(int i = 1; i < n; i++) {
    int u, v; scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  scanf(" %s", s + 1);
  solve(1, 0);
  printf("%d\n", max(dp[0][1], dp[1][1]));
  return 0;
}

Compilation message

power.cpp: In function 'int main()':
power.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   59 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
power.cpp:61:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   61 |     int u, v; scanf("%d %d", &u, &v);
      |               ~~~~~^~~~~~~~~~~~~~~~~
power.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |   scanf(" %s", s + 1);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 5 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4968 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4968 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 5 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4968 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4968 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
22 Correct 5 ms 5120 KB Output is correct
23 Correct 5 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 5120 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5248 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 4 ms 5120 KB Output is correct
33 Correct 4 ms 5120 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 4 ms 5120 KB Output is correct
38 Correct 4 ms 5120 KB Output is correct
39 Correct 5 ms 5120 KB Output is correct
40 Correct 5 ms 5096 KB Output is correct
41 Correct 5 ms 5120 KB Output is correct
42 Correct 5 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 5 ms 5120 KB Output is correct
45 Correct 5 ms 5120 KB Output is correct
46 Correct 6 ms 5120 KB Output is correct
47 Correct 5 ms 5120 KB Output is correct
48 Correct 5 ms 5120 KB Output is correct
49 Correct 4 ms 5120 KB Output is correct
50 Correct 5 ms 5120 KB Output is correct
51 Correct 5 ms 5120 KB Output is correct
52 Correct 5 ms 5120 KB Output is correct
53 Correct 4 ms 5120 KB Output is correct
54 Correct 5 ms 5120 KB Output is correct
55 Correct 5 ms 5120 KB Output is correct
56 Correct 5 ms 5120 KB Output is correct
57 Correct 5 ms 5248 KB Output is correct
58 Correct 4 ms 5120 KB Output is correct
59 Correct 4 ms 5120 KB Output is correct
60 Correct 5 ms 5120 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 5 ms 5248 KB Output is correct
63 Correct 4 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 5 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4968 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4968 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
22 Correct 5 ms 5120 KB Output is correct
23 Correct 5 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 5120 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5248 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 4 ms 5120 KB Output is correct
33 Correct 4 ms 5120 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 4 ms 5120 KB Output is correct
38 Correct 4 ms 5120 KB Output is correct
39 Correct 5 ms 5120 KB Output is correct
40 Correct 5 ms 5096 KB Output is correct
41 Correct 5 ms 5120 KB Output is correct
42 Correct 5 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 5 ms 5120 KB Output is correct
45 Correct 5 ms 5120 KB Output is correct
46 Correct 6 ms 5120 KB Output is correct
47 Correct 5 ms 5120 KB Output is correct
48 Correct 5 ms 5120 KB Output is correct
49 Correct 4 ms 5120 KB Output is correct
50 Correct 5 ms 5120 KB Output is correct
51 Correct 5 ms 5120 KB Output is correct
52 Correct 5 ms 5120 KB Output is correct
53 Correct 4 ms 5120 KB Output is correct
54 Correct 5 ms 5120 KB Output is correct
55 Correct 5 ms 5120 KB Output is correct
56 Correct 5 ms 5120 KB Output is correct
57 Correct 5 ms 5248 KB Output is correct
58 Correct 4 ms 5120 KB Output is correct
59 Correct 4 ms 5120 KB Output is correct
60 Correct 5 ms 5120 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 5 ms 5248 KB Output is correct
63 Correct 4 ms 5120 KB Output is correct
64 Correct 216 ms 13300 KB Output is correct
65 Correct 200 ms 13160 KB Output is correct
66 Correct 203 ms 13132 KB Output is correct
67 Correct 201 ms 13256 KB Output is correct
68 Correct 221 ms 13176 KB Output is correct
69 Correct 207 ms 13176 KB Output is correct
70 Correct 195 ms 13176 KB Output is correct
71 Correct 205 ms 13176 KB Output is correct
72 Correct 203 ms 13076 KB Output is correct
73 Correct 243 ms 25720 KB Output is correct
74 Correct 237 ms 25720 KB Output is correct
75 Correct 192 ms 13048 KB Output is correct
76 Correct 208 ms 13048 KB Output is correct
77 Correct 220 ms 13048 KB Output is correct
78 Correct 203 ms 13084 KB Output is correct
79 Correct 201 ms 13048 KB Output is correct
80 Correct 205 ms 13052 KB Output is correct
81 Correct 184 ms 13444 KB Output is correct
82 Correct 182 ms 13208 KB Output is correct
83 Correct 213 ms 18296 KB Output is correct
84 Correct 228 ms 20368 KB Output is correct
85 Correct 210 ms 20116 KB Output is correct
86 Correct 213 ms 14584 KB Output is correct
87 Correct 199 ms 13940 KB Output is correct
88 Correct 192 ms 14404 KB Output is correct
89 Correct 195 ms 14432 KB Output is correct
90 Correct 189 ms 14140 KB Output is correct
91 Correct 192 ms 14324 KB Output is correct
92 Correct 190 ms 14716 KB Output is correct
93 Correct 194 ms 17140 KB Output is correct
94 Correct 181 ms 18800 KB Output is correct
95 Correct 174 ms 17520 KB Output is correct
96 Correct 186 ms 15736 KB Output is correct
97 Correct 193 ms 14196 KB Output is correct
98 Correct 184 ms 13776 KB Output is correct
99 Correct 195 ms 13620 KB Output is correct
100 Correct 192 ms 16632 KB Output is correct
101 Correct 198 ms 14688 KB Output is correct
102 Correct 179 ms 13880 KB Output is correct
103 Correct 199 ms 13764 KB Output is correct
104 Correct 121 ms 13800 KB Output is correct
105 Correct 219 ms 22520 KB Output is correct
106 Correct 116 ms 13836 KB Output is correct
107 Correct 123 ms 13880 KB Output is correct
108 Correct 225 ms 26620 KB Output is correct
109 Correct 123 ms 13812 KB Output is correct
110 Correct 230 ms 26872 KB Output is correct
111 Correct 182 ms 13132 KB Output is correct