Submission #623822

#TimeUsernameProblemLanguageResultExecution timeMemory
623822ArnchPower Plant (JOI20_power)C++17
100 / 100
163 ms62400 KiB
// oooo /* har chi delet mikhad bebar ~ gitar o ba khodet nabar! ~ ;Amoo_Hasan; */ #include<bits/stdc++.h> //#pragma GCC optimize("O3,no-stack-protector,unroll-loops") //#pragma GCC target("avx2,fma") using namespace std; typedef long long ll; typedef long double ld; #define Sz(x) int((x).size()) #define All(x) (x).begin(), (x).end() #define wtf(x) cout<<#x <<" : " <<x <<endl constexpr ll inf = 1e18, N = 1e6 + 10, mod = 1e9 + 7, pr = 1000696969; int dp[N][2]; bool mark[N]; string s; vector<int> adj[N]; void dfs(int x, int p = 0) { vector<int> cnt_0, cnt_1; for(auto j : adj[x]) { if(j == p) continue; dfs(j, x); cnt_0.push_back(dp[j][0]); cnt_1.push_back(dp[j][1]); } sort(All(cnt_0), greater<int>()); sort(All(cnt_1), greater<int>()); if(mark[x]) { dp[x][1] = 1; int val = 0; for(auto j : cnt_1) val += j; dp[x][1] = max(dp[x][1], val - 1); dp[x][0] = 1; if(Sz(cnt_1) > 0) dp[x][0] = max(dp[x][0], 1 + cnt_1[0]); if(Sz(cnt_0) > 0) dp[x][0] = max(dp[x][0], cnt_0[0]); val = 0; for(auto j : cnt_1) val += j; dp[x][0] = max(dp[x][0], val - 1); return; } int val = 0; for(auto j : cnt_1) val += j; dp[x][1] = val; if(Sz(cnt_0) > 0) dp[x][0] = cnt_0[0]; val = 0; for(auto j : cnt_1) val += j; dp[x][0] = max(dp[x][0], val); } int main() { ios :: sync_with_stdio(0), cin.tie(0); int n; cin >>n; for(int i = 0; i < n - 1; i++) { int u, v; cin >>u >>v; adj[u].push_back(v), adj[v].push_back(u); } cin >>s; for(int i = 0; i < Sz(s); i++) mark[i + 1] = (s[i] == '1'); dfs(1); cout<<dp[1][0]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...