제출 #1325310

#제출 시각아이디문제언어결과실행 시간메모리
1325310tralalero_tralalaPower Plant (JOI20_power)C++20
47 / 100
1594 ms26176 KiB
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fore(i,a,b) for(lli i = (a), abcdxd = (b); i < abcdxd; i++)
#define f first
#define s second
#define pb push_back
#define ENDL '\n'
#define sz(s) lli((s).size())
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using namespace std;
typedef int lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
typedef vector<ii> vii;
typedef long double ld;
#define deb(x) cout << #x << ": " << x << endl;

const lli N = 2e5 + 12;

lli n;

vii adj[N];
bool pw[N];

lli dp[N+N];
bool vis[N+N];

// vii rev;

lli calc(lli u, lli p, lli id){
  lli& ans = dp[id];
  bool& mem = vis[id];
  if (!mem){
    mem = true;
    ans = 0;
    for (auto i : adj[u]) if (i.f != p) ans += calc(i.f, u, i.s);
    ans = max(lli(pw[u]), ans - pw[u]);
  }
  return ans;
}

lli f(lli u){
  lli ans = 1, sum = -1;
  for (auto i : adj[u]){
    lli xx = calc(i.f, u, i.s);
    sum += xx;
    ans = max(ans, xx + 1);
  }
  return max(ans, sum - 1);
}

void solve(){
  cin >> n;
  fore(i,0,n-1){
    lli a, b; cin >> a >> b; a--, b--;
    adj[a].pb({b, i+i});
    adj[b].pb({a, i+i+1});
  }
  fore(i,0,n){
    char c; cin >> c;
    pw[i] = (c == '1');
  }
  lli ans = 0;
  fore(i,0,n) if (pw[i]) ans = max(ans, f(i));
  cout << ans << endl;
  // sort(all(rev)); for (auto i : rev) cout << i.f << ": " << i.s << endl;
}

int main(){ // _
  // freopen("tracing.in", "r", stdin);
  // freopen("tracing.out", "w", stdout);
  // lli t; cin >> t;
  // while (t--)
    solve();
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...