Submission #369777

# Submission time Handle Problem Language Result Execution time Memory
369777 2021-02-22T11:51:36 Z maozkurt Mag (COCI16_mag) C++17
84 / 120
2796 ms 80156 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 = 1e9;

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

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

    return ret;
}

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;
    
    vector<int> iki;
    for(int i=1;i<=n;i++){
        cin>>magic[i];
        minn = min(minn, magic[i]);
        if(magic[i] == 2)
            iki.pb(i);
    }   
    if(minn != 1){
        cout << minn << '/' << 1 << endl;
        exit(0);
    }
    
    int ikimaxx = 0;
    for(int i : iki){
        int birmaxx = -1;
        int biriki = -1;
        int birr = 0;
        for(int v : adj[i]){
            if(magic[v]==1)
                birr++;
        }       
        if(birr<2)
            continue;
        for(int v : adj[i]){
            if(magic[v] == 1){
                visited[v] = i;
                int cur = dfs1(v,i);
                if(cur>birmaxx){
                    biriki = birmaxx;
                    birmaxx = cur;
                }
                else if(cur>biriki){
                    biriki = cur;
                }
            }
        }
        //cerr << i << sp << biriki << sp << birmaxx << endl;
        if(birmaxx != -1 && biriki != -1){
            ikimaxx = max(ikimaxx, birmaxx + biriki);
        }
    }

    int maxx = 0;
    for(int i=1;i<=n;i++){
        if(visited[i]!=-1 && magic[i] == 1){
            visited[i] = -1;
            maxx = max(maxx, dfs1(i,-1));
        }
    }
    if(2*maxx >= ikimaxx+1){
        cout << 1 << '/' << maxx << endl;
    }
    else{
        cout << ((ikimaxx+1)%2==0 ? 1 : 2) << '/' << ((ikimaxx+1)%2==0?(ikimaxx+1)/2:ikimaxx+1) << endl;
    }
    


}











# Verdict Execution time Memory Grader output
1 Correct 15 ms 23820 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23928 KB Output is correct
2 Correct 15 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 401 ms 80156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23788 KB Output is correct
2 Correct 401 ms 64880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 401 ms 63128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 401 ms 62700 KB Output is correct
2 Correct 303 ms 52588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 385 ms 65416 KB Output is correct
2 Incorrect 246 ms 27884 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2796 ms 28140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 362 ms 61032 KB Output is correct
2 Correct 407 ms 62316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1032 ms 63460 KB Output is correct
2 Incorrect 373 ms 44012 KB Output isn't correct