답안 #490890

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490890 2021-11-29T16:06:28 Z Dirii Mag (COCI16_mag) C++14
60 / 120
517 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(a[v] == 1){
            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 16 ms 23884 KB Output is correct
2 Correct 12 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 397 ms 153384 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 23816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 517 ms 260796 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 386 ms 89304 KB Output is correct
2 Correct 300 ms 75508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 393 ms 86236 KB Output is correct
2 Correct 74 ms 31448 KB Output is correct
3 Runtime error 486 ms 262148 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 31172 KB Output is correct
2 Correct 392 ms 95100 KB Output is correct
3 Correct 333 ms 61888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 89528 KB Output is correct
2 Correct 403 ms 78384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 386 ms 90924 KB Output is correct
2 Correct 344 ms 61832 KB Output is correct