Submission #720573

# Submission time Handle Problem Language Result Execution time Memory
720573 2023-04-08T13:38:02 Z Ahmed57 Mag (COCI16_mag) C++14
24 / 120
951 ms 130572 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<<1<<"/"<<maa<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 729 ms 86416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 23792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 923 ms 130572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 838 ms 70408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 924 ms 72864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 132 ms 29032 KB Output is correct
2 Incorrect 919 ms 71004 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 824 ms 67856 KB Output is correct
2 Incorrect 951 ms 86928 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 828 ms 71168 KB Output is correct
2 Correct 545 ms 57952 KB Output is correct