답안 #1110035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1110035 2024-11-08T14:06:08 Z Whisper Hard route (IZhO17_road) C++17
100 / 100
524 ms 115620 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX                     500005
vector<pair<int, int>> G[MAX];

int numNode;

struct DP{
    int max_value, cnt;
    DP():max_value(0), cnt(1){}
    DP(int _max_value, int _cnt): max_value(_max_value), cnt(_cnt){}
};

DP combine(DP a, DP b){
    if(a.max_value > b.max_value) return a;
    if(a.max_value < b.max_value) return b;
    return DP(a.max_value, a.cnt + b.cnt);
}
DP res;
DP dp[MAX];

void dfs(int u, int p = -1){
    for (pair<int, int>&x : G[u]){
        int v = x.first;
        if(v ^ p){
            dfs(v, u);
            dp[u] = combine(dp[u], DP(dp[v].max_value + 1, dp[v].cnt));
        }
    }
}

void reroot(int u, int p = -1){
    sort(G[u].begin(), G[u].end(), [&](const pair<int, int>&a, const pair<int, int>&b){
        return dp[a.first].max_value > dp[b.first].max_value;
    });
    if((int)G[u].size() >= 3){
        int node[3];
        node[0] = dp[G[u][0].first].max_value + 1, node[1] = dp[G[u][1].first].max_value + 1, node[2] = dp[G[u][2].first].max_value + 1;
        DP cand = DP(node[0] * (node[1] + node[2]), 0);
        int sum = 0;
        for (pair<int, int>&x : G[u]) {
            int v = x.first;
            if(dp[v].max_value + 1 == node[2]) cand.cnt += sum * dp[v].cnt;
            if(dp[v].max_value + 1 == node[1]) sum += dp[v].cnt;
        }
        res = combine(res, cand);
    }
    DP mx[2];
    mx[1] = DP(0, 0);
    for (pair<int, int>&x : G[u]) {
        int v = x.first;
        if(dp[v].max_value + 1 > mx[0].max_value){
            mx[1] = mx[0];
            mx[0] = DP(dp[v].max_value + 1, dp[v].cnt);
        }
        else if(dp[v].max_value + 1 == mx[0].max_value) mx[0].cnt += dp[v].cnt;
        else if(dp[v].max_value + 1 > mx[1].max_value) mx[1] = DP(dp[v].max_value + 1, dp[v].cnt);
        else if(dp[v].max_value + 1 == mx[1].max_value) mx[1].cnt += dp[v].cnt;
    }

    DP rem = dp[u];
    for (pair<int, int>&x : G[u]) {
        int v = x.first;
        if(v ^ p){
            dp[u] = mx[0];
            if(dp[v].max_value + 1 == dp[u].max_value){
                if(dp[v].cnt == dp[u].cnt){
                    dp[u] = mx[1];
                }
                else{
                    dp[u].cnt -= dp[v].cnt;
                }
            }
            reroot(v, u);
        }
    }
    dp[u] = rem;
}

void process(void){
    cin >> numNode;
    for (int i = 1; i < numNode; ++i){
        int u, v; cin >> u >> v;
        G[u].emplace_back(v, 1);
        G[v].emplace_back(u, 1);
    }
    dfs(1);
    reroot(1);
    cout << res.max_value << " " << res.cnt;
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}




# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19792 KB Output is correct
2 Correct 5 ms 19792 KB Output is correct
3 Correct 5 ms 19792 KB Output is correct
4 Correct 4 ms 19792 KB Output is correct
5 Correct 5 ms 19792 KB Output is correct
6 Correct 5 ms 19792 KB Output is correct
7 Correct 6 ms 19888 KB Output is correct
8 Correct 4 ms 19792 KB Output is correct
9 Correct 4 ms 19792 KB Output is correct
10 Correct 4 ms 19792 KB Output is correct
11 Correct 4 ms 19792 KB Output is correct
12 Correct 5 ms 19792 KB Output is correct
13 Correct 4 ms 19792 KB Output is correct
14 Correct 5 ms 20036 KB Output is correct
15 Correct 6 ms 20048 KB Output is correct
16 Correct 4 ms 20044 KB Output is correct
17 Correct 5 ms 19792 KB Output is correct
18 Correct 4 ms 19972 KB Output is correct
19 Correct 5 ms 20048 KB Output is correct
20 Correct 4 ms 20048 KB Output is correct
21 Correct 5 ms 20220 KB Output is correct
22 Correct 5 ms 19792 KB Output is correct
23 Correct 5 ms 19792 KB Output is correct
24 Correct 4 ms 19792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19792 KB Output is correct
2 Correct 5 ms 19792 KB Output is correct
3 Correct 5 ms 19792 KB Output is correct
4 Correct 4 ms 19792 KB Output is correct
5 Correct 5 ms 19792 KB Output is correct
6 Correct 5 ms 19792 KB Output is correct
7 Correct 6 ms 19888 KB Output is correct
8 Correct 4 ms 19792 KB Output is correct
9 Correct 4 ms 19792 KB Output is correct
10 Correct 4 ms 19792 KB Output is correct
11 Correct 4 ms 19792 KB Output is correct
12 Correct 5 ms 19792 KB Output is correct
13 Correct 4 ms 19792 KB Output is correct
14 Correct 5 ms 20036 KB Output is correct
15 Correct 6 ms 20048 KB Output is correct
16 Correct 4 ms 20044 KB Output is correct
17 Correct 5 ms 19792 KB Output is correct
18 Correct 4 ms 19972 KB Output is correct
19 Correct 5 ms 20048 KB Output is correct
20 Correct 4 ms 20048 KB Output is correct
21 Correct 5 ms 20220 KB Output is correct
22 Correct 5 ms 19792 KB Output is correct
23 Correct 5 ms 19792 KB Output is correct
24 Correct 4 ms 19792 KB Output is correct
25 Correct 6 ms 20304 KB Output is correct
26 Correct 6 ms 20560 KB Output is correct
27 Correct 6 ms 20596 KB Output is correct
28 Correct 6 ms 20560 KB Output is correct
29 Correct 6 ms 20560 KB Output is correct
30 Correct 6 ms 20560 KB Output is correct
31 Correct 6 ms 20560 KB Output is correct
32 Correct 6 ms 20316 KB Output is correct
33 Correct 9 ms 20560 KB Output is correct
34 Correct 5 ms 20560 KB Output is correct
35 Correct 6 ms 20560 KB Output is correct
36 Correct 7 ms 20560 KB Output is correct
37 Correct 6 ms 20560 KB Output is correct
38 Correct 7 ms 20816 KB Output is correct
39 Correct 6 ms 20560 KB Output is correct
40 Correct 7 ms 20308 KB Output is correct
41 Correct 6 ms 20304 KB Output is correct
42 Correct 5 ms 20304 KB Output is correct
43 Correct 6 ms 20304 KB Output is correct
44 Correct 5 ms 20304 KB Output is correct
45 Correct 5 ms 20304 KB Output is correct
46 Correct 5 ms 20304 KB Output is correct
47 Correct 5 ms 20304 KB Output is correct
48 Correct 5 ms 20304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19792 KB Output is correct
2 Correct 5 ms 19792 KB Output is correct
3 Correct 5 ms 19792 KB Output is correct
4 Correct 4 ms 19792 KB Output is correct
5 Correct 5 ms 19792 KB Output is correct
6 Correct 5 ms 19792 KB Output is correct
7 Correct 6 ms 19888 KB Output is correct
8 Correct 4 ms 19792 KB Output is correct
9 Correct 4 ms 19792 KB Output is correct
10 Correct 4 ms 19792 KB Output is correct
11 Correct 4 ms 19792 KB Output is correct
12 Correct 5 ms 19792 KB Output is correct
13 Correct 4 ms 19792 KB Output is correct
14 Correct 5 ms 20036 KB Output is correct
15 Correct 6 ms 20048 KB Output is correct
16 Correct 4 ms 20044 KB Output is correct
17 Correct 5 ms 19792 KB Output is correct
18 Correct 4 ms 19972 KB Output is correct
19 Correct 5 ms 20048 KB Output is correct
20 Correct 4 ms 20048 KB Output is correct
21 Correct 5 ms 20220 KB Output is correct
22 Correct 5 ms 19792 KB Output is correct
23 Correct 5 ms 19792 KB Output is correct
24 Correct 4 ms 19792 KB Output is correct
25 Correct 6 ms 20304 KB Output is correct
26 Correct 6 ms 20560 KB Output is correct
27 Correct 6 ms 20596 KB Output is correct
28 Correct 6 ms 20560 KB Output is correct
29 Correct 6 ms 20560 KB Output is correct
30 Correct 6 ms 20560 KB Output is correct
31 Correct 6 ms 20560 KB Output is correct
32 Correct 6 ms 20316 KB Output is correct
33 Correct 9 ms 20560 KB Output is correct
34 Correct 5 ms 20560 KB Output is correct
35 Correct 6 ms 20560 KB Output is correct
36 Correct 7 ms 20560 KB Output is correct
37 Correct 6 ms 20560 KB Output is correct
38 Correct 7 ms 20816 KB Output is correct
39 Correct 6 ms 20560 KB Output is correct
40 Correct 7 ms 20308 KB Output is correct
41 Correct 6 ms 20304 KB Output is correct
42 Correct 5 ms 20304 KB Output is correct
43 Correct 6 ms 20304 KB Output is correct
44 Correct 5 ms 20304 KB Output is correct
45 Correct 5 ms 20304 KB Output is correct
46 Correct 5 ms 20304 KB Output is correct
47 Correct 5 ms 20304 KB Output is correct
48 Correct 5 ms 20304 KB Output is correct
49 Correct 316 ms 74740 KB Output is correct
50 Correct 309 ms 79176 KB Output is correct
51 Correct 313 ms 82856 KB Output is correct
52 Correct 307 ms 69704 KB Output is correct
53 Correct 262 ms 79904 KB Output is correct
54 Correct 253 ms 84040 KB Output is correct
55 Correct 256 ms 73808 KB Output is correct
56 Correct 242 ms 78160 KB Output is correct
57 Correct 263 ms 84800 KB Output is correct
58 Correct 259 ms 79700 KB Output is correct
59 Correct 283 ms 79688 KB Output is correct
60 Correct 294 ms 77444 KB Output is correct
61 Correct 524 ms 115620 KB Output is correct
62 Correct 488 ms 104008 KB Output is correct
63 Correct 488 ms 70984 KB Output is correct
64 Correct 489 ms 63048 KB Output is correct
65 Correct 463 ms 58244 KB Output is correct
66 Correct 432 ms 55460 KB Output is correct
67 Correct 377 ms 54088 KB Output is correct
68 Correct 380 ms 53512 KB Output is correct
69 Correct 407 ms 53324 KB Output is correct
70 Correct 331 ms 53068 KB Output is correct
71 Correct 380 ms 53064 KB Output is correct
72 Correct 317 ms 53016 KB Output is correct
73 Correct 317 ms 53188 KB Output is correct
74 Correct 337 ms 52928 KB Output is correct
75 Correct 366 ms 52924 KB Output is correct
76 Correct 275 ms 53176 KB Output is correct
77 Correct 252 ms 51376 KB Output is correct
78 Correct 172 ms 51112 KB Output is correct