Submission #370089

# Submission time Handle Problem Language Result Execution time Memory
370089 2021-02-23T08:24:24 Z maozkurt Mag (COCI16_mag) C++17
120 / 120
3966 ms 86764 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],depth[maxn];

int dfs1(int v, int par){
    int ret = v;
    for(int i : adj[v]){
        if(magic[i] == 1 && par != i){
            depth[i] = depth[v] + 1;
            int a = dfs1(i,v);
            if(depth[a] >= depth[ret]){
                ret = a;
            }
        }
    }
    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){
            depth[i] = 1;
            int cur = depth[dfs1(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 && depth[i] == 0){
            depth[i] = 1;
            int cur = dfs1(i,i);
            if(cur==i){
                maxbir = max(maxbir,1);
            }
            else{
                depth[cur] = 1;
                maxbir = max(maxbir,depth[dfs1(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;
    }
    //cerr << maxbir << sp << maxiki << endl;
}











# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 465 ms 86764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 394 ms 62700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 63596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 417 ms 62896 KB Output is correct
2 Correct 310 ms 52460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 386 ms 63836 KB Output is correct
2 Correct 318 ms 27884 KB Output is correct
3 Correct 432 ms 80612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3966 ms 27924 KB Output is correct
2 Correct 425 ms 78828 KB Output is correct
3 Correct 705 ms 51692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 372 ms 60772 KB Output is correct
2 Correct 420 ms 62572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1121 ms 63268 KB Output is correct
2 Correct 457 ms 44048 KB Output is correct