Submission #720587

# Submission time Handle Problem Language Result Execution time Memory
720587 2023-04-08T14:54:39 Z Ahmed57 Mag (COCI16_mag) C++14
24 / 120
842 ms 147616 KB
//LCA
#include <bits/stdc++.h>

using namespace std;
#define int long long
vector<int> adj[1000001];
long long ma[1000001];
long long lon[1000001];
int n ;
void calc(int i,int pr){
    long long ch = (ma[i]==1?1:0);
    for(auto j:adj[i]){
        if(j!=pr){
            calc(j,i);
            if(ma[i]==1)ch = max(ch,lon[j]+1);
        }
    }
    lon[i] = ch;
}
long long maa = 0;
void dfs(int i,int pr){
    maa = max(maa,lon[i]);
    for(auto j:adj[i]){
        if(j==pr)continue;
        dfs(j,i);
    }
    if(ma[i]==1){
        long long firstma = 0 , secondma = 0;
        for(auto j:adj[i]){
            if(j==pr)continue;
            if(firstma<=lon[j]){
                secondma = firstma;
                firstma = lon[j];
            }else if(secondma<=lon[j]){
                secondma = lon[j];
            }
        }
        maa = max(maa,firstma+secondma+1);
    }
}
signed main(){
    cin>>n;
    for(int i = 0;i<n-1;i++){
        int a,b;cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    long long mi = 1e18;
    for(int i = 0;i<n;i++){
        cin>>ma[i+1];
        mi = min(mi,ma[i+1]);
    }
    calc(1,0);
    dfs(1,0);
    if(mi!=1)cout<<mi<<"/"<<1<<endl;
    else cout<<2<<"/"<<2*maa+1<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Incorrect 15 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Incorrect 13 ms 23800 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 668 ms 87080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Incorrect 822 ms 147616 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 842 ms 131856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 771 ms 71080 KB Output is correct
2 Incorrect 554 ms 69308 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 779 ms 72908 KB Output is correct
2 Incorrect 115 ms 30540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 113 ms 29600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 724 ms 68528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 782 ms 72324 KB Output isn't correct
2 Halted 0 ms 0 KB -