Submission #98271

# Submission time Handle Problem Language Result Execution time Memory
98271 2019-02-21T18:26:07 Z dalgerok Mag (COCI16_mag) C++17
24 / 120
604 ms 138544 KB
#include<bits/stdc++.h>
using namespace std;



const int N = 1e6 + 5;



int n, a[N], dp[N], ans1 = -1, ans2 = -1;
vector < int > g[N];


void dfs1(int v, int pr = -1){
    int mx1 = 0, mx2 = 0;
    for(int to : g[v]){
        if(to != pr){
            dfs1(to, v);
            if(dp[to] > mx1){
                mx2 = mx1;
                mx1 = dp[to];
            }
            else if(dp[to] > mx2){
                mx2 = dp[to];
            }
        }
    }
    if(a[v] == 1){
        dp[v] = mx1 + 1;
        ans1 = max(ans1, mx1 + mx2 + 1);
    }
}
void dfs2(int v, int pr = -1){
    int mx1 = 0, mx2 = 0;
    for(int to : g[v]){
        if(to != pr){
            dfs2(to, v);
            if(dp[to] > mx1){
                mx2 = mx1;
                mx1 = dp[to];
            }
            else if(dp[to] > mx2){
                mx2 = dp[to];
            }
        }
    }
    if(a[v] == 2){
        dp[v] = mx1 + 1;
        ans2 = max(ans2, mx1 + mx2 + 1);
    }
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 1; i < n; i++){
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    ans1 = ans2 = -1;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        if(a[i] == 1){
            ans1 = 1;
        }
        if(a[i] == 2){
            ans2 = 1;
        }
    }
    memset(dp, 0, sizeof(dp));
    dfs1(1);
    memset(dp, 0, sizeof(dp));
    dfs2(1);
    if(ans1 != -1){
        if(ans2 == -1){
            cout << "1/" << ans1;
        }
        else if(ans1 * 2 > ans2){
            cout << "1/" << ans1;
        }
        else{
            if(ans2 & 1){
                cout << "2/" << ans2;
            }
            else{
                cout << "1/" << ans2 / 2;
            }
        }
        return 0;
    }
    int ans = 2e9;
    for(int i = 1; i <= n; i++){
        ans = min(ans, a[i]);
    }
    cout << ans << "/1";
}
# Verdict Execution time Memory Grader output
1 Correct 33 ms 27748 KB Output is correct
2 Correct 33 ms 27776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 27904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 475 ms 87452 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 27816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 560 ms 138544 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 604 ms 62836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 498 ms 63944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 150 ms 31580 KB Output is correct
2 Incorrect 503 ms 63504 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 416 ms 61140 KB Output is correct
2 Incorrect 487 ms 62284 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 462 ms 63528 KB Output is correct
2 Correct 553 ms 45956 KB Output is correct