답안 #241477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
241477 2020-06-24T08:44:02 Z Vimmer Mag (COCI16_mag) C++14
120 / 120
1723 ms 121136 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 1000501
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9


using namespace std;
//using namespace __gnu_pbds;
const long long CUR = 50000000;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 3> a3;

//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


int a[N];

int f[N], kol[N], mt;

ld ans = -1;

int l = 1, r = 1;

vector <int> g[N];

void dfs(int v, int p)
{
    if (ans == -1 || ans - ld(a[v]) > 0) {ans = a[v]; l = a[v]; r = 1;}

    if (a[v] == 1) f[v] = 1;

    for (auto it : g[v])
    {
        if (it == p) continue;

        dfs(it, v);

        if (a[v] != 1) continue;

        f[v] = max(f[v], 1 + f[it]);
    }
}

void rec(int v, int p, int koler)
{
    vector <int> pr; pr.clear();

    for (auto it : g[v])
    {
        if (it == p) continue;

        pr.pb(f[it]);
    }

    sort(pr.begin(), pr.end());

    reverse(pr.begin(), pr.end());

    if (sz(pr) > 1)
    {
        int sum = a[v], kol = pr[0] + pr[1] + 1;

        ld val = ld(sum) / ld(kol);

        if (ans == -1 || ans - val > 0) {ans = val; l = sum; r = kol;}
    }

    if (sz(pr) > 0)
    {
        int sum = a[v], kol = koler + pr[0] + 1;

        ld val = ld(sum) / ld(kol);

        if (ans == -1 || ans - val > 0) {ans = val; l = sum; r = kol;}
    }

    for (auto it : g[v])
    {
        if (it == p) continue;

        rec(it, v, (a[v] == 1 ? koler + 1 : 0));
    }
}

bool cmp(int x, int y)
{
    if (a[x] != a[y]) return a[x] < a[y];

    return kol[x] < kol[y];
}

inline void dfser(int v, int p, int kol, ll sum)
{
    mt++;

    ll sumr = sum;

    sum *= ll(a[v]);

    if (sum < sumr) return;

    if (sum > 1e9) return;

    ld res = ld(ld(sum) / ld(kol));

    if (ans < 0 || ans - res > 0)
    {
        ans = res;

        l = sum; r = kol;
    }

    for (auto it : g[v])
    {
        if (mt > CUR) return;

        if (it == p) continue;

        dfser(it, v, kol + 1, sum);
    }
}

int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;

    cin >> n;

    for (int i = 1; i < n; i++)
    {
        int x, y;

        cin >> x >> y;

        g[x].pb(y);

        g[y].pb(x);
    }

    bool f = 1;

    vector <int> gr; gr.clear();

    for (int i = 1; i <= n; i++)  {if (sz(g[i]) > 2) f = 0; cin >> a[i];}

    if (f)
    {
         for (int i = 1; i <= n; i++) {gr.pb(i); cin >> a[i];}

        for (int i = 1; i <= n; i++)
        {
            int koler = 0;

            for (auto it : g[i]) if (a[it] == 1) koler++;

            kol[i] = koler;
        }

        sort(gr.begin(), gr.end(), cmp);

        for (int i = 0; i < n && mt < CUR; i++) dfser(gr[i], -1, 1, 1);

        cout << l << "/" << r << endl;

        exit(0);
    }
    dfs(1, -1);

    rec(1, -1, 0);

    int gcd = __gcd(l, r);

    l /= gcd;

    r /= gcd;

    cout << l << "/" << r << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23976 KB Output is correct
2 Correct 19 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 22 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1556 ms 121136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23808 KB Output is correct
2 Correct 1159 ms 66956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1723 ms 106812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 510 ms 62840 KB Output is correct
2 Correct 360 ms 52868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 459 ms 66916 KB Output is correct
2 Correct 88 ms 27896 KB Output is correct
3 Correct 1118 ms 68828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 27896 KB Output is correct
2 Correct 498 ms 64224 KB Output is correct
3 Correct 384 ms 44024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 429 ms 62632 KB Output is correct
2 Correct 491 ms 62332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 497 ms 64024 KB Output is correct
2 Correct 370 ms 43896 KB Output is correct