Submission #370065

# Submission time Handle Problem Language Result Execution time Memory
370065 2021-02-23T07:19:53 Z maozkurt Mag (COCI16_mag) C++17
36 / 120
2979 ms 80204 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <numeric>
#include <cassert>

#define endl '\n'
#define sp ' '

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int maxn = 1e6+5;
const int inf = 2e9;

vector<int> adj[maxn];
int magic[maxn];

int p[maxn];

int dfs1(int v, int s){
    int ret = v;
    for(int i : adj[v]){
        if(magic[i] == 1 && p[i] != s){
            p[i] = s;
            ret = dfs1(i,s);
        }
    }
    return ret;
}

int dfs2(int v, int s){
    int ret = 1;
    for(int i : adj[v]){
        if(magic[i] == 1 && p[i] != s){
            p[i] = s;
            ret = max(ret, dfs2(i,s) + 1);
        }
    }
    return ret;
}

int dfs3(int s){
    int maxx = -1, iki = -1;
    int cnt = 0;
    for(int i : adj[s]){
        if(magic[i] == 1)
            cnt++;
    }
    if(cnt < 2)
        return -1;
    for(int i : adj[s]){
        if(magic[i] == 1){
            p[i] = s;
            int cur = dfs2(i,s);
            if(cur>maxx){
                iki = maxx;
                maxx = cur;
            }
            else if(cur>iki){
                iki = cur;
            }
        }
    }
    return (iki==-1 ? -1 : maxx + iki + 1);
}

int main(){

    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cerr.tie(nullptr);

    int n;cin>>n;
    for(int i=0;i<n-1;i++){
        int a,b;cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    int minn = inf;
    for(int i=1;i<=n;i++){
        cin>>magic[i];
        minn = min(minn,magic[i]);
    }

    if(minn > 1){
        cout << minn << '/' << 1 << endl;
        exit(0);
    }

    int maxbir = 0;
    for(int i=1;i<=n;i++){
        if(magic[i]==1 && p[i] == 0){
            p[i] = i;
            int cur = dfs1(i,i);
            if(cur==i){
                maxbir = max(maxbir,1);
            }
            else{
                p[cur] = cur;
                maxbir = dfs2(cur,cur);
            }
        }
    }

    int maxiki = 0;
    for(int i=1;i<=n;i++){
        if(magic[i] == 2){
            int cur = dfs3(i);
            maxiki = max(maxiki, cur);
        }
    }

    if(2*maxbir >= maxiki){
        cout << 1 << '/' << maxbir << endl;
    }
    else{
        int sol = 2;
        if(maxiki % 2 == 0){
            sol=1;
            maxiki/=2;
        }
        cout << sol << '/' << maxiki << endl;
    }
}











# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Incorrect 16 ms 23916 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 397 ms 80204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Incorrect 389 ms 62572 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 413 ms 63024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 409 ms 62828 KB Output is correct
2 Incorrect 306 ms 52460 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 390 ms 63836 KB Output is correct
2 Incorrect 235 ms 27884 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2979 ms 27924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 364 ms 60644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1093 ms 63212 KB Output is correct
2 Incorrect 398 ms 44012 KB Output isn't correct