답안 #369775

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
369775 2021-02-22T11:50:16 Z maozkurt Mag (COCI16_mag) C++17
12 / 120
419 ms 80232 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(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;
    }
    


}











# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 15 ms 23916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 348 ms 80232 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 384 ms 63340 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 419 ms 62668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 385 ms 65368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 79 ms 28012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 356 ms 61032 KB Output is correct
2 Incorrect 377 ms 62316 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 383 ms 63468 KB Output is correct
2 Incorrect 356 ms 43952 KB Output isn't correct