# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
167563 | manh9203 | Mag (COCI16_mag) | C++17 | 552 ms | 178272 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n,u,v,a[N],dp[N][2],ans1,ans2;
vector<int> adj[N];
void dfs(int u,int p){
int mx1 = 0, mx2 = 0, mx3 = 0, mx4 = 0;
int luu1 = -1, luu2 = -1, luu3 = -1, luu4 = -1;
for(int v: adj[u]){
if(v != p){
dfs(v, u);
if(dp[v][0] > mx1){
mx2 = mx1;
luu2 = luu1;
mx1 = dp[v][0];
luu1 = v;
}else{
if(dp[v][0] > mx2){
mx2 = dp[v][0];
luu2 = v;
}
}
if(dp[v][1] > mx3){
mx4 = mx3;
luu4 = luu3;
mx3 = dp[v][1];
luu3 = v;
}else{
if(dp[v][1] > mx4){
mx4 = dp[v][1];
luu4 = v;
}
}
}
}
if(a[u] == 1){
ans1 = max(ans1, mx1 + mx2 + 1);
if(luu1 != luu3){
ans2 = max(ans2, mx1 + mx3 + 1);
}else{
ans2 = max(ans2, max(mx1 + mx4, mx2 + mx3) + 1);
}
dp[u][0] = mx1 + 1;
dp[u][1] = mx3 + 1;
}else{
if(a[u] == 2){
ans2 = max(ans2, mx1 + mx2 + 1);
dp[u][0] = 0;
dp[u][1] = mx1 + 1;
}else{
dp[u][0] = dp[u][1] = 0;
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i=1;i<n;i++){
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
long long mn = 1e18;
for(int i=1;i<=n;i++){
cin >> a[i];
mn = min(mn, (long long)a[i]);
}
dfs(1, 1);
long long tu = mn, mau = 1;
if(mau < tu*ans1){
tu = 1;
mau = ans1;
}
if(2*mau < tu*ans2){
tu = 2;
mau = ans2;
}
long long gd = __gcd(tu, mau);
tu /= gd; mau /= gd;
cout << tu << '/' << mau;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |