Submission #133444

#TimeUsernameProblemLanguageResultExecution timeMemory
133444tdwnMag (COCI16_mag)C++17
120 / 120
1925 ms203996 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; const int maxn = 1000100; vector<int>g[maxn]; ll arr[maxn], n; ll result1; ll result2; ll dp[maxn][2]; void dfs(int node, int p) { if(arr[node] == 1) dp[node][0] = 1; if(arr[node] == 2) dp[node][1] = 1; vector<ll>v; ll max_0 = dp[node][0]; ll max_1 = dp[node][1]; for(int i:g[node]) { if(i != p) { dfs(i, node); if(arr[node] == 1) { if(arr[i] == 1) { dp[node][0] = max(dp[node][0], 1 + dp[i][0]); v.pb(dp[i][0]); } if(dp[i][1] != 0) dp[node][1] = max(dp[node][1], 1 + dp[i][1]); } else if(arr[node] == 2) { dp[node][1] = max(dp[node][1], 1 + dp[i][0]); } //cout<<"Updating "<<node<<" from "<<i<<" - "<<dp[i][0]<<" "<<dp[i][1]<<"\n"; result2 = max(result2, max_0 + dp[i][1]); result2 = max(result2, max_1 + dp[i][0]); max_0 = max(max_0, dp[node][0]); max_1 = max(max_1, dp[node][1]); } } result1 = max(result1, dp[node][0]); result2 = max(result2, dp[node][1]); v.pb(0); v.pb(0); sort(v.rbegin(), v.rend()); if(arr[node] == 2) { result2 = max(result2, v[0] + v[1] + 1); } else if(arr[node] == 1) { result1 = max(result1, v[0] + v[1] + 1); } } int main() { cin>>n; ll a, b; for(int i=0;i<n-1;i++) { cin>>a>>b; g[a].pb(b); g[b].pb(a); } ll minval = LLONG_MAX; for(int i=1;i<=n;i++) { cin>>arr[i]; minval = min(minval, arr[i]); } if(minval != 1) { cout<<minval<<"/1\n"; } else { dfs(1, -1); if(result2 > 2*result1) { cout<<"2/"<<result2<<"\n"; } else { cout<<"1/"<<result1<<"\n"; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...