Submission #331685

# Submission time Handle Problem Language Result Execution time Memory
331685 2020-11-29T14:21:10 Z BeanZ Mag (COCI16_mag) C++14
60 / 120
488 ms 141824 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]);
            else upd(j, dp1[u]);
        }
        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 16 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 363 ms 102636 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 488 ms 139628 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 408 ms 85144 KB Output is correct
2 Correct 309 ms 75244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 382 ms 81364 KB Output is correct
2 Correct 103 ms 31468 KB Output is correct
3 Incorrect 471 ms 141824 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 31340 KB Output is correct
2 Correct 403 ms 85540 KB Output is correct
3 Correct 418 ms 62060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 370 ms 81632 KB Output is correct
2 Correct 420 ms 94700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 417 ms 88428 KB Output is correct
2 Correct 422 ms 61988 KB Output is correct