답안 #490899

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490899 2021-11-29T16:31:43 Z Dirii Mag (COCI16_mag) C++14
96 / 120
501 ms 262148 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define cll const ll
#define lp(a, b, c) for(ll a = b; a <= c; ++a)
#define lpd(a, b, c) for(ll a = b; a >= c; --a)
#define vec(a) vector<a>
#define pp(a, b) pair<a, b>
#define EACHCASE lpd(cs, read(), 1)
#define Fname "f"
using namespace std;

template <typename T> inline void Read(T &x){
    x = 0; char c;
    ll dau = 1;
    while(!isdigit(c = getchar())) if(c == '-') dau = -1;
    do{
        x = x * 10 + c - '0';
    } while (isdigit(c = getchar()));
    x *= dau;
}

ll read(){
    ll tmp;
    cin >> tmp;
    return tmp;
}

void giuncute(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

void OF(){
    if(fopen(Fname".inp", "r")){
        freopen(Fname".inp", "r", stdin);
        freopen(Fname".out", "w", stdout);
    } else if(fopen(Fname".in", "r")){
        freopen(Fname".in", "r", stdin);
        freopen(Fname".out", "w", stdout);
    }
}

cll mxn = 1e6 + 7;
ll n, a[mxn], up[mxn] = {0}, down[mxn] = {0}, anstu = 1e9, ansmau = 1;
vec(ll) g[mxn];

void init(ll u, ll p){
    for(ll v : g[u]){
        if(v == p) continue;
        init(v, u);
        down[u] = max(down[u], down[v]);
    }
    if(a[u] == 1) ++down[u];
    else down[u] = 0;
}

void dfs(ll u, ll p){
    vec(pp(ll, ll)) dp;
    for(ll v : g[u]){
        if(v == p) dp.push_back({up[u], v});
        else dp.push_back({down[v], v});
    }
    sort(dp.begin(), dp.end());
    for(ll v : g[u]){
        if(v == p) continue;
        if(dp.back().second != v) up[v] = dp.back().first;
        else if(dp.size() > 1) up[v] = dp[dp.size() - 2].first;
        if(a[u] != 1) up[v] = 0;
        else ++up[v];
        dfs(v, u);
    }
    if(dp.empty()) anstu = min(anstu, a[u]);
    else if(dp.size() == 1){
        ll tu = a[u], mau = dp[0].first + 1;
        if(tu * ansmau < mau * anstu) anstu = tu, ansmau = mau;
    } else{
        ll tu = a[u], mau = dp.back().first + dp[dp.size() - 2].first + 1;
        if(tu * ansmau < mau * anstu) anstu = tu, ansmau = mau;
    }
}

int main(){
    giuncute();
    #ifndef ONLINE_JUDGE
    OF();
    #endif
    cin >> n;
    lp(i, 1, n - 1){
        ll u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    lp(i, 1, n) cin >> a[i];
    init(1, -1);
    dfs(1, -1);
    ll tmp = __gcd(anstu, ansmau);
    cout << anstu / tmp << '/' << ansmau / tmp;
}

Compilation message

mag.cpp: In function 'void OF()':
mag.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen(Fname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(Fname".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23884 KB Output is correct
2 Correct 13 ms 23876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 13 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 478 ms 144000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Runtime error 499 ms 262148 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Correct 501 ms 260716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 369 ms 78808 KB Output is correct
2 Correct 305 ms 64952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 352 ms 86124 KB Output is correct
2 Correct 91 ms 30516 KB Output is correct
3 Runtime error 488 ms 262144 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 30136 KB Output is correct
2 Correct 398 ms 80556 KB Output is correct
3 Correct 439 ms 54884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 348 ms 79036 KB Output is correct
2 Correct 409 ms 78016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 459 ms 80688 KB Output is correct
2 Correct 448 ms 54680 KB Output is correct