Submission #393948

# Submission time Handle Problem Language Result Execution time Memory
393948 2021-04-25T02:47:25 Z erray Power Plant (JOI20_power) C++14
100 / 100
299 ms 55452 KB
// author: erray
#include <bits/stdc++.h>
 
using namespace std;
template<typename A, typename B> string to_string(const pair<A, B>& p);
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t);
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char& c) {
  return "'"s + c + "'";
}

string to_string(const char *c) {
  return to_string(string(c));
}

string to_string(const bool& b) {
  return (b ? "true" : "false");
}

string to_string(const vector<bool>& v) {
  string res = "{";
  for (int i = 0; i < (int) v.size(); ++i) {
    if (i > 0) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template<size_t T> string to_string(const bitset<T>& bs) {
  return bs.to_string();
}

template<typename T> string to_string(queue<T> q) {
  string res = "{";
  size_t sz = q.size();
  while (sz--) {
    T cur = q.front();
    q.pop();
    q.push(cur);
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}

template<typename T, class C> string to_string(priority_queue<T, vector<T>, C> pq) {
  vector<T> tmp;
  string res = "{";
  while (!pq.empty()) {
    T cur = pq.top();
    pq.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
    tmp.push_back(cur);
  }
  for (auto el : tmp) {
    pq.push(el);
  }
  res += "}";
  return res;
}

template<typename T> string to_string(const T& v) {
  string res = "{";
  for (auto &el : v) {
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(el);
  }
  res += "}";
  return res;
}

template<typename A, typename B> string to_string(const pair<A, B>& p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ')';
}

void debug_out(int size, bool first, string name) {
  vector<string> tmp = {name};
  vector<int> tmp2 = {size, first};
  cerr << endl;
}

constexpr int buffer_size = 255;

template<typename Head, typename... Tail> void debug_out(int size, bool first, string name, Head H, Tail... T) {
  string tmp;
  int off = 0;
  while ((!name.empty() && name[0] != ',') || off != 0) {
    tmp += name[0];
    name.erase(name.begin());
    char c = tmp.back();
    if (c == '{' || c == '(') {
      ++off;
    } else if (c == '}' || c == ')'){
      --off;
    }
  }
  if (!name.empty()) {
    name.erase(name.begin());
  }
  if (tmp[0] == ' ') {
    tmp.erase(tmp.begin());
  }

  string buff = to_string(H);
  if ((int) buff.size() + size + (int) tmp.size() > buffer_size - 5 && !first) {
    cerr << '\n';
    size = 0;
  }
  cerr << '[' << tmp << ": " << buff << "] ";
  debug_out(((int) buff.size() + size + (int) tmp.size() + 5) % buffer_size, false, name, T...);
}

#ifdef DEBUG
#define debug(...) cerr << "-> ", debug_out(3, true, string(#__VA_ARGS__), __VA_ARGS__)
#define here cerr << "-> " << __LINE__ << endl
#else
#define debug(...) void(37)
#define here void(37)
#endif
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  string s;
  cin >> s;

  vector<vector<int>> dp(n, vector<int>(2));
  function<void(int, int)> Dfs = [&](int v, int pr) {
    vector<int> on(2, s[v] - '0'), off(2, -(s[v] - '0'));
    for (auto u : g[v]) {
      if (u == pr) {
        continue;
      }
      Dfs(u, v);
      off[0] += dp[u][1];
      off[1] += dp[u][1];
      if (s[v] == '1') {
        on[0] = max(on[0], dp[u][1] + 1);
        on[1] = 1;
      } 
    }
    dp[v][0] = max(on[0], off[0]);
    dp[v][1] = max(on[1], off[1]);
    debug(v, dp[v]);
  };
  Dfs(0, -1);
  debug(dp);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, dp[i][0]);
  }
  cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 708 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 456 KB Output is correct
33 Correct 2 ms 452 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 2 ms 444 KB Output is correct
36 Correct 2 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 2 ms 588 KB Output is correct
40 Correct 2 ms 588 KB Output is correct
41 Correct 2 ms 588 KB Output is correct
42 Correct 2 ms 588 KB Output is correct
43 Correct 2 ms 588 KB Output is correct
44 Correct 2 ms 588 KB Output is correct
45 Correct 2 ms 588 KB Output is correct
46 Correct 2 ms 588 KB Output is correct
47 Correct 2 ms 588 KB Output is correct
48 Correct 2 ms 716 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 2 ms 588 KB Output is correct
51 Correct 2 ms 588 KB Output is correct
52 Correct 2 ms 460 KB Output is correct
53 Correct 2 ms 580 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
55 Correct 2 ms 584 KB Output is correct
56 Correct 2 ms 452 KB Output is correct
57 Correct 2 ms 844 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 1 ms 460 KB Output is correct
60 Correct 2 ms 700 KB Output is correct
61 Correct 1 ms 452 KB Output is correct
62 Correct 2 ms 716 KB Output is correct
63 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 708 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 456 KB Output is correct
33 Correct 2 ms 452 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 2 ms 444 KB Output is correct
36 Correct 2 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 2 ms 588 KB Output is correct
40 Correct 2 ms 588 KB Output is correct
41 Correct 2 ms 588 KB Output is correct
42 Correct 2 ms 588 KB Output is correct
43 Correct 2 ms 588 KB Output is correct
44 Correct 2 ms 588 KB Output is correct
45 Correct 2 ms 588 KB Output is correct
46 Correct 2 ms 588 KB Output is correct
47 Correct 2 ms 588 KB Output is correct
48 Correct 2 ms 716 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 2 ms 588 KB Output is correct
51 Correct 2 ms 588 KB Output is correct
52 Correct 2 ms 460 KB Output is correct
53 Correct 2 ms 580 KB Output is correct
54 Correct 2 ms 588 KB Output is correct
55 Correct 2 ms 584 KB Output is correct
56 Correct 2 ms 452 KB Output is correct
57 Correct 2 ms 844 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 1 ms 460 KB Output is correct
60 Correct 2 ms 700 KB Output is correct
61 Correct 1 ms 452 KB Output is correct
62 Correct 2 ms 716 KB Output is correct
63 Correct 2 ms 460 KB Output is correct
64 Correct 211 ms 25376 KB Output is correct
65 Correct 211 ms 25224 KB Output is correct
66 Correct 208 ms 25200 KB Output is correct
67 Correct 218 ms 25184 KB Output is correct
68 Correct 218 ms 25252 KB Output is correct
69 Correct 205 ms 25276 KB Output is correct
70 Correct 202 ms 25236 KB Output is correct
71 Correct 206 ms 25248 KB Output is correct
72 Correct 209 ms 25328 KB Output is correct
73 Correct 231 ms 53144 KB Output is correct
74 Correct 232 ms 52904 KB Output is correct
75 Correct 202 ms 25184 KB Output is correct
76 Correct 200 ms 25100 KB Output is correct
77 Correct 222 ms 25148 KB Output is correct
78 Correct 199 ms 25216 KB Output is correct
79 Correct 236 ms 25044 KB Output is correct
80 Correct 196 ms 25136 KB Output is correct
81 Correct 199 ms 25520 KB Output is correct
82 Correct 228 ms 25284 KB Output is correct
83 Correct 214 ms 36484 KB Output is correct
84 Correct 225 ms 41176 KB Output is correct
85 Correct 213 ms 40520 KB Output is correct
86 Correct 208 ms 28360 KB Output is correct
87 Correct 238 ms 26992 KB Output is correct
88 Correct 187 ms 27252 KB Output is correct
89 Correct 200 ms 27268 KB Output is correct
90 Correct 216 ms 26860 KB Output is correct
91 Correct 204 ms 27240 KB Output is correct
92 Correct 195 ms 28020 KB Output is correct
93 Correct 206 ms 33804 KB Output is correct
94 Correct 198 ms 37308 KB Output is correct
95 Correct 191 ms 34636 KB Output is correct
96 Correct 229 ms 30448 KB Output is correct
97 Correct 181 ms 26988 KB Output is correct
98 Correct 179 ms 25904 KB Output is correct
99 Correct 206 ms 25604 KB Output is correct
100 Correct 196 ms 32496 KB Output is correct
101 Correct 194 ms 28204 KB Output is correct
102 Correct 183 ms 26328 KB Output is correct
103 Correct 199 ms 26072 KB Output is correct
104 Correct 158 ms 25932 KB Output is correct
105 Correct 243 ms 46124 KB Output is correct
106 Correct 144 ms 25844 KB Output is correct
107 Correct 167 ms 25732 KB Output is correct
108 Correct 299 ms 54852 KB Output is correct
109 Correct 150 ms 25968 KB Output is correct
110 Correct 253 ms 55452 KB Output is correct
111 Correct 197 ms 25112 KB Output is correct