Submission #331686

# Submission time Handle Problem Language Result Execution time Memory
331686 2020-11-29T14:22:55 Z BeanZ Mag (COCI16_mag) C++14
120 / 120
495 ms 155628 KB
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int lim = 100;
ll dp1[N], dp2[N], a[N];
vector<ll> node[N];
void upd(ll u, ll c){
    dp2[u] = max(dp2[u], c);
    if (dp2[u] > dp1[u]) swap(dp2[u], dp1[u]);
}
void init(ll u, ll p){
    for (auto j : node[u]){
        if (j == p) continue;
        init(j, u);
        if (a[j] == 1) upd(u, dp1[j] + 1);
    }
}
void dfs(ll u, ll p){
    for (auto j : node[u]){
        if (j == p) continue;
        if (a[u] == 1){
            if ((dp1[j] + 1) == dp1[u]) upd(j, dp2[u] + 1);
            else upd(j, dp1[u] + 1);
        }
        dfs(j, u);
    }
}
void red(pair<ll, ll> &u, pair<ll, ll> v){
    if (u.first * v.second > u.second * v.first){
        u = v;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if (fopen("A.inp", "r")){
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    ll n;
    cin >> n;
    for (int i = 1; i < n; i++){
        ll u, v;
        cin >> u >> v;
        node[u].push_back(v);
        node[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) cin >> a[i];
    init(1, 1);
    dfs(1, 1);
    pair<ll, ll> ans = {1e9, 1};
    for (int i = 1; i <= n; i++){
        red(ans, {a[i], dp1[i] + dp2[i] + 1});
    }
    ll gcd = __gcd(ans.first, ans.second);
    ans.first /= gcd;
    ans.second /= gcd;
    cout << ans.first << "/" << ans.second;
}
/*
*/

Compilation message

mag.cpp: In function 'int main()':
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("test.inp", "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("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 366 ms 92704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Correct 480 ms 155628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 138424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 400 ms 78444 KB Output is correct
2 Correct 314 ms 63980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 80852 KB Output is correct
2 Correct 101 ms 30064 KB Output is correct
3 Correct 489 ms 141292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 29932 KB Output is correct
2 Correct 397 ms 78828 KB Output is correct
3 Correct 411 ms 54252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 75192 KB Output is correct
2 Correct 389 ms 77752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 410 ms 79084 KB Output is correct
2 Correct 404 ms 54304 KB Output is correct