답안 #165676

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
165676 2019-11-28T08:20:18 Z Atill83 Mag (COCI16_mag) C++14
12 / 120
149 ms 33812 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) 3e5+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 erased[MAXN];
int dep[MAXN];

bool isg(pll a, pll b){
    if(a.ss == 0) return 1;
    if(b.ss == 0) return 0;
    long double a1 = (long double) a.first/ (long double)a.second;
    long double a2 = (long double) b.first/ (long double)b.second;
    if(a1 > a2){
        return true;
    }else{
        return false;
    }
}


int dfs(int v, int par = -1){
    ll siz = 1;
    for(int i: adj[v]){
        if(i != par && !erased[i]){
            siz += dfs(i, v);
        }
    }
    return dep[v] = siz;
}

int find_centroit(int v){
    int siz = dfs(v);
    dep[v] = 0;
    int par = -1;
    while(true){
        bool is = 0;
        //cout<<v<<endl;
        for(int i: adj[v]){
            if(!erased[i] && dep[i] > siz / 2 && i != par){
                par = v;
                v = i;
                is = 1;
                break;
            }
        }
        if(!is){
            break;
        }
    }
    return v;
}


pll ans = {INF,1};

pll solve(int v, int par){
    pll mx = {INF, 1};
    for(int i: adj[v]){
        if(erased[i]|| i == par) continue;
        pll dis = solve(i, v);
        if(isg({-dis.ff, dis.ss + 1LL},{-mx.ff, mx.ss + 1LL})){
            mx = dis;
        }
    }
    pll cur = {mg[v], 1};
    if(isg({mx.ss, mx.ff - 1} , {1, 1})){
        ll us = mg[v]*mx.ff;
        ll alt = 1 + mx.ss;
        return {us, alt};
    }else{
        return cur;
    }

}

void do_it(int v){
    v = find_centroit(v);
    // Islemler
    //cout<<v<<endl;
    pll mx1 = {INF,1}, mx2 = {INF,1};
    for(int i: adj[v]){
        if(erased[i]) continue;
        pll cur = solve(i, v);
        //cout<<i<<endl;
        //cout<<cur.ff<<" "<<cur.ss<<endl;
        if(isg({-cur.ff, cur.ss + 1LL},{-mx1.ff, mx1.ss + 1LL})){
            mx2 = mx1;
            mx1 = cur;
        }else if(isg({-cur.ff, cur.ss + 1LL},{-mx2.ff, mx2.ss + 1LL})){
            mx2 = cur;
        }
    }
    pll cur = {mg[v], 1};
    /*cout<<endl;
    cout<<mx1.ff<<" "<<mx1.ss<<endl;
    cout<<mx2.ff<<" "<<mx2.ss<<endl<<endl;*/
    if(isg({mx1.ss, mx1.ff - 1}, {1, 1})){
        ll us = mg[v]*mx1.ff;
        ll alt = 1 + mx1.ss;
        cur = {us, alt};
    }
    if(isg({mx2.ss, mx2.ff - 1}, {cur.ss, 1})){
        ll us = cur.ff*mx2.ff;
        ll alt = cur.ss + mx2.ss;
        cur = {us, alt};
    }
    //cout<<cur.ff<<" "<<cur.ss<<endl;
    if(isg(ans, cur)){
        ans = cur;
    }
    erased[v] = 1;
    for(int i: adj[v]){
        if(!erased[i])
            do_it(i);
    }

}




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

    #ifdef Local
        freopen("../IO/int.txt","r",stdin);
        freopen("../IO/out.txt","w",stdout);
    #endif

    cin>>n;

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

    do_it(1);
    ll gcd = __gcd(ans.ff, ans.ss);
    ans.ff /= gcd;
    ans.ss /= gcd;
    cout<<ans.ff<<"/"<<ans.ss<<endl;


    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 7416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 18 ms 14840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 7492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 130 ms 33808 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 19 ms 14712 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 18 ms 14840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 149 ms 11960 KB Output is correct
2 Runtime error 18 ms 14712 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 18 ms 14712 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 131 ms 33812 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -