Submission #519689

# Submission time Handle Problem Language Result Execution time Memory
519689 2022-01-27T04:25:01 Z Killer2501 Mag (COCI16_mag) C++14
120 / 120
403 ms 146748 KB
#include <bits/stdc++.h>
#define ll long long
#define ld double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 1e6+5;
const int M = 1e2+2;
const ll base = 1e4;
const ll mod = 998244353;
const int inf = 1e9;
const double ex = 1e-9;
int k, t,  n;
int a[N], b[N], tong, m, ans;
pii mx[N];
vector<int> vi, adj[N];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
void upd(int u, int x)
{
    if(x > mx[u].fi)mx[u].fi = x;
    else if(x > mx[u].se)mx[u].se = x;
}
void dfs(int u, int p = 0)
{
    for(int v: adj[u])
    {
        if(v == p)continue;
        dfs(v, u);
        if(a[v] == 1)upd(u, mx[v].fi+1);
    }
}
void dfs1(int u, int p = 0)
{
    for(int v: adj[u])
    {
        if(v == p)continue;
        if(a[u] == 1)
        {
            if(mx[u].fi != mx[v].fi+1)upd(v, mx[u].fi+1);
            else upd(v, mx[u].se+1);
        }

        dfs1(v, u);

    }
}
void sol(int icase)
{
    cin >> n;
    for(int i = 1; i < n; i ++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    for(int i = 1; i <= n; i ++)cin >> a[i];
    dfs(1);
    dfs1(1);
    pii res;
    res.fi = inf;
    res.se = 1;
    for(int u = 1; u <= n; u ++)
    {
        //cout << a[u] <<" "<<mx[u].fi<<" "<<mx[u].se<<'\n';
        if(1ll * res.fi * (mx[u].fi+mx[u].se+1) > 1ll * a[u] * res.se)
        {
            res.fi = a[u];
            res.se = mx[u].se+mx[u].fi+1;
        }
    }
    k = __gcd(res.fi, res.se);
    res.fi /= k;
    res.se /= k;
    cout << res.fi <<"/"<<res.se;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
    int test = 1;
    //cin >> test;
    for(int i = 1; i <= test; i ++)sol(i);
    return 0;
}

Compilation message

mag.cpp: In function 'int main()':
mag.cpp:89:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:90:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 13 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 15 ms 23756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 290 ms 97060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 388 ms 143632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 143720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 336 ms 81832 KB Output is correct
2 Correct 242 ms 66508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 308 ms 84820 KB Output is correct
2 Correct 58 ms 29696 KB Output is correct
3 Correct 363 ms 146748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 29516 KB Output is correct
2 Correct 309 ms 82304 KB Output is correct
3 Correct 296 ms 53480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 278 ms 79572 KB Output is correct
2 Correct 367 ms 83024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 82448 KB Output is correct
2 Correct 252 ms 53408 KB Output is correct