답안 #948215

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948215 2024-03-17T23:01:21 Z atom Hard route (IZhO17_road) C++17
19 / 100
6 ms 14172 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int N = 5e5 + 5;
const int INF = 1e9;
int n;
vector <int> adj[N];
ll ans, cnt;
using T = pair <ll, ll>;

T dp[N];
// <maximum distance from current node, number of ways>
void dfs(int u, int p) {
    dp[u] = make_pair(0, 1);
    for (int v : adj[u]) {
        if (v == p) continue;
        dfs(v, u);
        if (dp[u].first < dp[v].first + 1)
            dp[u] = make_pair(dp[v].first + 1, dp[v].second);
        else if (dp[u].first == dp[v].first + 1)
            dp[u].second += dp[v].second;
    }
}

void reroot(int u, int p, T pre) {
    vector <T> cand;
    if (u > 1 || (int) adj[u].size() == 1)
        cand.push_back(pre);

    for (int v : adj[u]) 
        if (v ^ p)
            cand.push_back(make_pair(dp[v].first + 1, dp[v].second));
    sort(cand.rbegin(), cand.rend());
    
    if ((int) cand.size() > 2) {
        ll hardness = cand[0].first * (cand[1].first + cand[2].first);
        ll cur_cnt = 0;
        ll ways = 0;

        debug(u, p, pre);
        debug(hardness);
        for (auto &[x, y] : cand) if (x == cand[2].first) ++ways;

        // case 1: all distinct values
        if (cand[0].first != cand[1].first && cand[1].first != cand[2].first)
            cur_cnt = cand[1].second * ways;
        // case 2: all same values
        else if (cand[0].first == cand[1].first && cand[1].first == cand[2].first) {
            cur_cnt = (ways - 1) * ways / 2;
        }
        // case 3: first two are the same
        else if (cand[0].first == cand[1].first && cand[1].first != cand[2].first) {
            cur_cnt = (cand[0].second + cand[1].second) * cand[2].second;
        }
        // case 4: last two are the same
        else {
            cur_cnt = (ways - 1) * ways / 2;
        }
        if (ans < hardness) {
            ans = hardness;
            cnt = cur_cnt;
        }
        else if (ans == hardness)
            cnt += cur_cnt;
    }

    // processing two nodes of current subtree;
    T c1, c2;
    for (auto &[x, y] : cand) {
        if (x + 1 > c1.first) {
            c2 = c1;
            c1 = make_pair(x + 1, y);
        }
        else if (x + 1 == c1.first) {
            c1.second += y;
        }
        else if (x + 1 > c2.first) {
            c2 = make_pair(x + 1, y);
        }
        else if (x + 1 == c2.first) {
            c2.second += y;
        }
    }

    for (int v : adj[u]) {
        if (v == p) continue;
        if (dp[v].first + 2 == c1.first) {
            if (dp[v].second == c1.second) // same branch;
                reroot(v, u, c2);
            else
                reroot(v, u, make_pair(c1.first, c1.second - dp[v].second));
        }
        else 
            reroot(v, u, c1);
    }
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);

    cin >> n;
    for (int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    //sub2: O(n^2)
    ans = 0, cnt = 1;
    
    dfs(1, 0);
    reroot(1, 0, {0, 1});

    cout << ans << " " << cnt << "\n";

    return 0;
}

/*
    leaf nodes always product maximum distance?
*/

Compilation message

road.cpp: In function 'void reroot(int, int, T)':
road.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
road.cpp:48:9: note: in expansion of macro 'debug'
   48 |         debug(u, p, pre);
      |         ^~~~~
road.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
road.cpp:49:9: note: in expansion of macro 'debug'
   49 |         debug(hardness);
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13656 KB Output is correct
5 Correct 4 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 5 ms 13656 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 4 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13504 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13508 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 3 ms 13492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13656 KB Output is correct
5 Correct 4 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 5 ms 13656 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 4 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13504 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13508 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 3 ms 13492 KB Output is correct
25 Correct 4 ms 14172 KB Output is correct
26 Incorrect 6 ms 14172 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13656 KB Output is correct
5 Correct 4 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 5 ms 13656 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 4 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13504 KB Output is correct
20 Correct 3 ms 13660 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 3 ms 13508 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 3 ms 13492 KB Output is correct
25 Correct 4 ms 14172 KB Output is correct
26 Incorrect 6 ms 14172 KB Output isn't correct
27 Halted 0 ms 0 KB -