Submission #345959

#TimeUsernameProblemLanguageResultExecution timeMemory
345959jacynkaaPower Plant (JOI20_power)C++14
100 / 100
210 ms52524 KiB
#include <bits/stdc++.h>
#include <chrono>
#include <math.h>
#pragma GCC optimize("-O3")
using namespace std;
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
#ifdef LOCAL
ostream &operator<<(ostream &out, string str) {
   for (char c : str)
      out << c;
   return out;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; }
template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
   out << '{';
   for (auto it = a.begin(); it != a.end(); it = next(it))
      out << (it != a.begin() ? ", " : "") << *it;
   return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     \
   {}
#endif

const int MAXN = 1e6;

vector<int> G[MAXN];
string S;
int res = 1;
int n;

int dfs(int x, int p = -1) {
   vector<int> Y;
   for (int y : G[x])
      if (y != p) {
         int a = dfs(y, x);
         if (a > 0)
            Y.push_back(a);
      }
   if (Y.empty())
      return S[x] == '1';

   sort(all(Y));

   int T = max(accumulate(all(Y), 0) - (S[x] == '1'), 1);
   debug(x + 1, Y, T);
   res = max(res, max(T, Y.back() + (S[x] == '1')));
   return T;
}

int32_t main() {
   _upgrade;
   cin >> n;
   rep(i, n - 1) {
      int a, b;
      cin >> a >> b;
      G[--a].push_back(--b);
      G[b].push_back(a);
   }
   cin >> S;
   dfs(0);
   cout << res << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...