Submission #294117

#TimeUsernameProblemLanguageResultExecution timeMemory
294117BTheroPower Plant (JOI20_power)C++17
100 / 100
311 ms43384 KiB
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = (int)2e5 + 5;

vector<int> adj[MAXN];
char can[MAXN];
int dp[MAXN], dpUp[MAXN];
int n, ans;

void dfs(int v, int pr = -1) {
  int S = 0;

  for (int to : adj[v]) {
    if (to != pr) {
      dfs(to, v);
      S += dp[to];
    }
  }
  
  if (can[v] == '1') {
    dp[v] = max(1, S - 1);
  }
  else {
    dp[v] = S;
  }
}

void dfs2(int v, int pr = -1) {
  int S = dpUp[v];
  vector<int> sons;

  for (int to : adj[v]) {
    if (to != pr) {
      sons.pb(to);
      S += dp[to];
    }
  }
  
  if (can[v] == '1') {
    ans = max(ans, max(1, S - 1));
  }
  else {
    ans = max(ans, S);
  }
  
  for (int to : sons) {
    S -= dp[to];
    
    if (can[v] == '1') {
      dpUp[to] = max(1, S - 1);
    }
    else {
      dpUp[to] = S;
    }
    
    dfs2(to, v);
    S += dp[to];
  }
}

void solve() {
  scanf("%d", &n);
  
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    adj[u].pb(v);
    adj[v].pb(u);
  }
  
  scanf("%s", can + 1);
  ans = 0;
  
  for (int i = 1; i <= n; ++i) {
    if (can[i] == '1') {
      ans++;
    }
  }
  
  ans = min(ans, 2);
  dfs(1);
  dfs2(1);  
  printf("%d\n", ans);
}

int main() {
  int tt = 1;
  
  while (tt--) {
    solve();
  }

  return 0;
}

Compilation message (stderr)

power.cpp: In function 'void solve()':
power.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
power.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |     scanf("%d %d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~~
power.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |   scanf("%s", can + 1);
      |   ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...