답안 #365357

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
365357 2021-02-11T14:05:07 Z Atill83 Mag (COCI16_mag) C++14
36 / 120
564 ms 237924 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e6+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
vector<int> adj[MAXN];
ll mg[MAXN];
bool vis[MAXN];
int dep[MAXN];
int dp[MAXN][2];
int vans1 = 1, vans0 = 1;

void dfs(int v){
    if(mg[v] == 2){
        dp[v][0] = -mod;
        dp[v][1] = 1;
    }else{
        dp[v][0] = 1;
    }

    vector<int> ones, twos;
    vis[v] = 1;
    for(int i: adj[v]){
        if(!vis[i]){
            dfs(i);
            ones.push_back(dp[i][0]);
            twos.push_back(dp[i][1]);
            if(mg[v] == 2){
                dp[v][1] = max(dp[v][1], dp[i][0] + 1);
            }else{
                dp[v][1] = max(dp[v][1], dp[i][1] + 1);
                dp[v][0] = max(dp[v][0], dp[i][0] + 1);
            }
        }
    }
    sort(ones.begin(), ones.end(), greater<int>());
    sort(twos.begin(), twos.end(), greater<int>());
    if(mg[v] == 2){
        if(ones.size() >= 2)
            vans1 = max(vans1, ones[0] + ones[1] + 1);
    }else{
        if(ones.size() >= 1 && twos.size() >= 1){
            vans1 = max(vans1, ones[0] + twos[0] + 1);
        }
        if(ones.size() >= 2){
            vans0 = max(vans0, ones[0] + ones[1] + 1);
        }
    }


    vans1 = max(vans1, dp[v][1]);
    vans0 = max(vans0, dp[v][0]);
}

pii ed[MAXN];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n;

    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin>>a>>b;
        ed[i] = {a, b};
    }
    

    bool var = 0;



    for(int i = 1; i <= n; i++){
        cin>>mg[i];
        var = (var || mg[i] == 1);
    }

    if(!var){
        sort(mg + 1, mg + n + 1);
        cout<<mg[1]<<"/1"<<endl;
        return 0;
    }

    for(int i = 0; i < n - 1; i++){
        if(max(mg[ed[i].ff], mg[ed[i].ss]) > 2)
            continue;
        adj[ed[i].ff].push_back(ed[i].ss);
        adj[ed[i].ss].push_back(ed[i].ff);
    }


    for(int i = 1; i <= n; i++)
        if(!vis[i])
            dfs(i);

    int a, b;

    if(vans1 > 2*vans0){
        a = 2;
        b = vans1;
    }else{
        a = 1;
        b = vans0;
    }


    int d = __gcd(a, b);
    a /= d;
    b /= d;


    cout<<a<<"/"<<b<<endl;


    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Incorrect 18 ms 23916 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 23916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 438 ms 124780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 23788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 564 ms 215772 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 461 ms 79340 KB Output is correct
2 Incorrect 356 ms 76140 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 442 ms 85084 KB Output is correct
2 Correct 96 ms 29548 KB Output is correct
3 Incorrect 559 ms 237924 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 29676 KB Output is correct
2 Correct 457 ms 95852 KB Output is correct
3 Correct 428 ms 60012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 417 ms 77988 KB Output is correct
2 Correct 445 ms 78828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 455 ms 80876 KB Output isn't correct
2 Halted 0 ms 0 KB -