Submission #297520

#TimeUsernameProblemLanguageResultExecution timeMemory
297520fedoseevtimofeyPower Plant (JOI20_power)C++14
100 / 100
280 ms53380 KiB
#include <iostream> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <random> #include <iomanip> #include <functional> #include <cassert> #include <bitset> #include <chrono> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n; cin >> n; vector <vector <int>> g(n); for (int i = 0; i + 1 < n; ++i) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); } vector <int> c(n); for (int i = 0; i < n; ++i) { char r; cin >> r; c[i] = r - '0'; } vector <vector <int>> dp(n, vector <int> (2)); function <void(int, int)> dfs = [&] (int u, int p) { vector <int> gs; for (auto v : g[u]) { if (v != p) { gs.push_back(v); dfs(v, u); } } if (c[u] == 0) { for (auto v : gs) { dp[u][1] += dp[v][1]; dp[u][0] = max(dp[u][0], dp[v][0]); } } else { dp[u][0] = -1; dp[u][1] = -1; int mx = 0; for (auto v : gs) { dp[u][0] += dp[v][1]; dp[u][1] += dp[v][1]; mx = max(mx, dp[v][1]); } dp[u][0] = max(dp[u][0], 1 + mx); dp[u][1] = max(dp[u][1], 1); } }; dfs(0, -1); int ans = 0; for (int i = 0; i < n; ++i) ans = max(ans, max(dp[i][0], dp[i][1])); /*for (int i = 0; i < n; ++i) { cout << dp[i][0] << ' ' << dp[i][1] << endl; }*/ cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...