답안 #720590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
720590 2023-04-08T15:02:58 Z Ahmed57 Mag (COCI16_mag) C++14
84 / 120
1049 ms 262144 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);
    }
}
bool ss = 0;
void reroor(int i,int pr,int cnt){
    if(ma[i]==2){
        long long firstma = cnt , 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];
            }
        }
        if(firstma==secondma&&firstma==maa){
            ss = 1;
        }
    }
    vector<int> pref,suf,ch;
    int mm= 0;
    for(auto j:adj[i]){
        if(j==pr)continue;
        mm = max(mm,lon[j]);
        pref.push_back(mm);
        ch.push_back(j);
    }
    mm = cnt;suf.push_back(mm);
    reverse(ch.begin(),ch.end());
    for(auto j:ch){
        mm = max(mm,lon[j]);
        suf.push_back(mm);
    }
    reverse(suf.begin(),suf.end());
    reverse(ch.begin(),ch.end());
    for(int j = 0;j<ch.size();j++){
        reroor(ch[j],i,(ma[i]==1?max(suf[j+1],(j==0?0:pref[j-1]))+1:0));
    }
}
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);
    reroor(1,0,0);
    if(mi!=1)cout<<mi<<"/"<<1<<endl;
    else if(ss) cout<<2<<"/"<<2*maa+1<<endl;
    else cout<<1<<"/"<<maa<<endl;
}

Compilation message

mag.cpp: In function 'void reroor(long long int, long long int, long long int)':
mag.cpp:74:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for(int j = 0;j<ch.size();j++){
      |                   ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 13 ms 23828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 899 ms 174076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Runtime error 989 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1016 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 910 ms 70608 KB Output is correct
2 Correct 660 ms 58832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 956 ms 84240 KB Output is correct
2 Correct 143 ms 29072 KB Output is correct
3 Runtime error 1049 ms 262144 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 29004 KB Output is correct
2 Correct 940 ms 73768 KB Output is correct
3 Correct 606 ms 57868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 854 ms 73840 KB Output is correct
2 Correct 964 ms 70328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 927 ms 72632 KB Output is correct
2 Correct 624 ms 51844 KB Output is correct