Submission #773267

#TimeUsernameProblemLanguageResultExecution timeMemory
773267CyberCowPower Plant (JOI20_power)C++17
47 / 100
3 ms596 KiB
//#include <bits/stdc++.h> #include <random> #include <algorithm> #include <bitset> #include <chrono> #include <cmath> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <chrono> #define fr first #define sc second #define ad push_back using namespace std; using ll = long long; mt19937 rnd(348502); const int N = 2005; int n; vector<int> v[N]; int dp[N], a[N]; void Dfs(int g, int p) { for (auto to : v[g]) { if (to != p) { Dfs(to, g); } } if (p == -1) { int ma = 0, gum = 0; for(auto to: v[g]) { ma = max(ma, dp[to]); gum += dp[to]; } dp[g] = max(ma + 1, gum - 1); return; } int gum = 0; for (auto to : v[g]) { if (to != p) gum += dp[to]; } if (a[g] == 1) dp[g] = max(gum - 1, 1); else dp[g] = gum; } int ans = 0; void Dfs1(int g, int p, int arj) { int gum = arj, ma = arj, ma1 = 0; for (auto to : v[g]) { if (to != p) { gum += dp[to]; if (ma <= dp[to]) { ma1 = max(ma, ma1); ma = max(ma, dp[to]); } else { ma1 = max(ma1, dp[to]); } ma = max(ma, dp[to]); } } if (a[g] == 1) { ans = max(max(ans, ma + 1), max(1, gum - 1)); } for (auto to : v[g]) { if (to != p) { if (a[to] == 1) { if (dp[to] == ma) Dfs1(to, g, max(1, gum - dp[to] - 1)); else Dfs1(to, g, max(1, gum - dp[to] - 1)); } else { if (dp[to] == ma) Dfs1(to, g, gum - dp[to]); else Dfs1(to, g, gum - dp[to]); } } } } void solve() { int i, j, x, y; cin >> n; for (i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } char c; for (i = 1; i <= n; i++) { cin >> c; a[i] = c - '0'; } for ( i = 1; i <= n; i++) { if (a[i] == 1) { Dfs(i, -1); Dfs1(i, -1, 0); cout << ans; break; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt = 1; //cin >> tt; while (tt--) { solve(); } return 0; }

Compilation message (stderr)

power.cpp: In function 'void solve()':
power.cpp:114:12: warning: unused variable 'j' [-Wunused-variable]
  114 |     int i, j, x, y;
      |            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...