Submission #947568

# Submission time Handle Problem Language Result Execution time Memory
947568 2024-03-16T12:16:36 Z Pring Hard route (IZhO17_road) C++17
100 / 100
527 ms 92344 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif

#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;

const int MXN = 500005;
int n;
vector<int> edge[MXN];
pii d[MXN], u[MXN];
int pnt[MXN];

pii operator+(pii a, pii b) {
    if (a < b) swap(a, b);
    if (a.fs == b.fs) a.sc += b.sc;
    return a;
}

int SUM(vector<int> &v) {
    int ans = 0;
    for (auto &i : v) ans += i;
    return ans;
}

struct P {
    pii a, b;
    void init() {
        a = mp(0LL, 0LL);
        b = mp(0LL, 0LL);
    }
    void PUSH(pii x) {
        if (x.fs > a.fs) {
            b = a;
            a = x;
        } else if (x.fs == a.fs) {
            a.sc += x.sc;
        } else if (x.fs > b.fs) {
            b = x;
        } else if (x.fs == b.fs) {
            b.sc += x.sc;
        }
    }
    pii GET(pii x) {
        if (x.fs != a.fs) return a;
        if (x.sc == a.sc) return b;
        return mp(a.fs, a.sc - x.sc);
    }
} p[MXN];

bool chain() {
    int big = 0;
    FOR(i, 1, n + 1) big = max(big, (int) edge[i].size());
    return big <= 2;
}

void DFS1(int id, int par) {
    pnt[id] = par;
    d[id] = mp(0LL, 1LL);
    for (auto &i : edge[id]) {
        if (i == par) continue;
        DFS1(i, id);
        d[id] = d[id] + mp(d[i].fs + 1, d[i].sc);
    }
}

void DFS2(int id, int par) {
    u[id] = p[par].GET(mp(d[id].fs + 1, d[id].sc));
    u[id].fs++;
    p[id].init();
    p[id].PUSH(u[id]);
    for (auto &i : edge[id]) {
        if (i == par) continue;
        p[id].PUSH(mp(d[i].fs + 1, d[i].sc));
    }
    for (auto &i : edge[id]) {
        if (i == par) continue;
        DFS2(i, id);
    }
}

pii calc(int id) {
    if (edge[id].size() < 3) return mp(0LL, 0LL);
    vector<int> c(3, -1);
    vector<vector<int>> v(3);
    c[0] = u[id].fs;
    v[0].push_back(u[id].sc);
    for (auto &i : edge[id]) {
        if (i == pnt[id]) continue;
        debug(d[i].fs + 1, d[i].sc);
        bool f = false;
        FOR(j, 0, 3) if (d[i].fs + 1 == c[j]) {
            v[j].push_back(d[i].sc);
            f = true;
            break;
        }
        if (f) continue;
        FOR(j, 0, 3) if (d[i].fs + 1 > c[j]) {
            c.insert(c.begin() + j, d[i].fs + 1);
            v.insert(v.begin() + j, vector<int>(1, d[i].sc));
            break;
        }
        if (c.size() == 4) {
            c.pop_back();
            v.pop_back();
        }
    }
    if (v[0].size() >= 3) {
        int x = 2 * c[0] * c[0];
        int s = SUM(v[0]), y = 0;
        for (auto &i : v[0]) y += i * (s - i);
        return mp(x, y / 2);
    }
    if (v[0].size() == 2) {
        int x = c[0] * (c[0] + c[1]);
        return mp(x, (v[0][0] + v[0][1]) * SUM(v[1]));
    }
    if (v[1].size() >= 2) {
        int x = 2 * c[0] * c[1];
        int s = SUM(v[1]), y = 0;
        for (auto &i : v[1]) y += i * (s - i);
        return mp(x, y / 2);
    }
    return mp(c[0] * (c[1] + c[2]), v[1][0] * SUM(v[2]));
}

void miku() {
    int x, y;
    cin >> n;
    FOR(i, 1, n) {
        cin >> x >> y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    if (chain()) {
        cout << "0 1" << '\n';
        return;
    }
    int rt = [&]() -> int {
        FOR(i, 1, n + 1) if (edge[i].size() == 1) return i;
        return 0;
    }();
    DFS1(rt, 0);
    p[rt].init();
    p[rt].PUSH(mp(0LL, 1LL));
    for (auto &i : edge[rt]) p[rt].PUSH(mp(d[i].fs + 1, d[i].sc));
    for (auto &i : edge[rt]) DFS2(i, rt);
    debug(rt);
    FOR(i, 1, n + 1) debug(i, u[i].fs, u[i].sc);
    pii ans = mp(0LL, 0LL);
    FOR(i, 1, n + 1) {
        // ans = ans + calc(i);
        pii x = calc(i);
        debug(i, x.fs, x.sc);
        ans = ans + x;
    }
    cout << ans.fs << ' ' << ans.sc << '\n';
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    miku();
    return 0;
}

Compilation message

road.cpp: In function 'pii calc(long long int)':
road.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
road.cpp:103:9: note: in expansion of macro 'debug'
  103 |         debug(d[i].fs + 1, d[i].sc);
      |         ^~~~~
road.cpp: In function 'void miku()':
road.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
road.cpp:161:5: note: in expansion of macro 'debug'
  161 |     debug(rt);
      |     ^~~~~
road.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
road.cpp:162:22: note: in expansion of macro 'debug'
  162 |     FOR(i, 1, n + 1) debug(i, u[i].fs, u[i].sc);
      |                      ^~~~~
road.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
road.cpp:167:9: note: in expansion of macro 'debug'
  167 |         debug(i, x.fs, x.sc);
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18524 KB Output is correct
2 Correct 4 ms 18512 KB Output is correct
3 Correct 4 ms 12124 KB Output is correct
4 Correct 4 ms 12124 KB Output is correct
5 Correct 3 ms 12124 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Correct 4 ms 18532 KB Output is correct
8 Correct 4 ms 18524 KB Output is correct
9 Correct 4 ms 18524 KB Output is correct
10 Correct 4 ms 18524 KB Output is correct
11 Correct 4 ms 18524 KB Output is correct
12 Correct 5 ms 18520 KB Output is correct
13 Correct 4 ms 18524 KB Output is correct
14 Correct 4 ms 18524 KB Output is correct
15 Correct 5 ms 18524 KB Output is correct
16 Correct 4 ms 18524 KB Output is correct
17 Correct 4 ms 18512 KB Output is correct
18 Correct 4 ms 18520 KB Output is correct
19 Correct 3 ms 12124 KB Output is correct
20 Correct 3 ms 12340 KB Output is correct
21 Correct 4 ms 18524 KB Output is correct
22 Correct 4 ms 18524 KB Output is correct
23 Correct 4 ms 18372 KB Output is correct
24 Correct 4 ms 18508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18524 KB Output is correct
2 Correct 4 ms 18512 KB Output is correct
3 Correct 4 ms 12124 KB Output is correct
4 Correct 4 ms 12124 KB Output is correct
5 Correct 3 ms 12124 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Correct 4 ms 18532 KB Output is correct
8 Correct 4 ms 18524 KB Output is correct
9 Correct 4 ms 18524 KB Output is correct
10 Correct 4 ms 18524 KB Output is correct
11 Correct 4 ms 18524 KB Output is correct
12 Correct 5 ms 18520 KB Output is correct
13 Correct 4 ms 18524 KB Output is correct
14 Correct 4 ms 18524 KB Output is correct
15 Correct 5 ms 18524 KB Output is correct
16 Correct 4 ms 18524 KB Output is correct
17 Correct 4 ms 18512 KB Output is correct
18 Correct 4 ms 18520 KB Output is correct
19 Correct 3 ms 12124 KB Output is correct
20 Correct 3 ms 12340 KB Output is correct
21 Correct 4 ms 18524 KB Output is correct
22 Correct 4 ms 18524 KB Output is correct
23 Correct 4 ms 18372 KB Output is correct
24 Correct 4 ms 18508 KB Output is correct
25 Correct 6 ms 19036 KB Output is correct
26 Correct 5 ms 19036 KB Output is correct
27 Correct 6 ms 19040 KB Output is correct
28 Correct 7 ms 19040 KB Output is correct
29 Correct 8 ms 19036 KB Output is correct
30 Correct 6 ms 18880 KB Output is correct
31 Correct 6 ms 19036 KB Output is correct
32 Correct 7 ms 18880 KB Output is correct
33 Correct 5 ms 19032 KB Output is correct
34 Correct 5 ms 19120 KB Output is correct
35 Correct 6 ms 19036 KB Output is correct
36 Correct 5 ms 18864 KB Output is correct
37 Correct 4 ms 12380 KB Output is correct
38 Correct 4 ms 12376 KB Output is correct
39 Correct 6 ms 19036 KB Output is correct
40 Correct 5 ms 18780 KB Output is correct
41 Correct 5 ms 18708 KB Output is correct
42 Correct 7 ms 18780 KB Output is correct
43 Correct 5 ms 18780 KB Output is correct
44 Correct 5 ms 18780 KB Output is correct
45 Correct 5 ms 18780 KB Output is correct
46 Correct 6 ms 18776 KB Output is correct
47 Correct 5 ms 18780 KB Output is correct
48 Correct 5 ms 19000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18524 KB Output is correct
2 Correct 4 ms 18512 KB Output is correct
3 Correct 4 ms 12124 KB Output is correct
4 Correct 4 ms 12124 KB Output is correct
5 Correct 3 ms 12124 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Correct 4 ms 18532 KB Output is correct
8 Correct 4 ms 18524 KB Output is correct
9 Correct 4 ms 18524 KB Output is correct
10 Correct 4 ms 18524 KB Output is correct
11 Correct 4 ms 18524 KB Output is correct
12 Correct 5 ms 18520 KB Output is correct
13 Correct 4 ms 18524 KB Output is correct
14 Correct 4 ms 18524 KB Output is correct
15 Correct 5 ms 18524 KB Output is correct
16 Correct 4 ms 18524 KB Output is correct
17 Correct 4 ms 18512 KB Output is correct
18 Correct 4 ms 18520 KB Output is correct
19 Correct 3 ms 12124 KB Output is correct
20 Correct 3 ms 12340 KB Output is correct
21 Correct 4 ms 18524 KB Output is correct
22 Correct 4 ms 18524 KB Output is correct
23 Correct 4 ms 18372 KB Output is correct
24 Correct 4 ms 18508 KB Output is correct
25 Correct 6 ms 19036 KB Output is correct
26 Correct 5 ms 19036 KB Output is correct
27 Correct 6 ms 19040 KB Output is correct
28 Correct 7 ms 19040 KB Output is correct
29 Correct 8 ms 19036 KB Output is correct
30 Correct 6 ms 18880 KB Output is correct
31 Correct 6 ms 19036 KB Output is correct
32 Correct 7 ms 18880 KB Output is correct
33 Correct 5 ms 19032 KB Output is correct
34 Correct 5 ms 19120 KB Output is correct
35 Correct 6 ms 19036 KB Output is correct
36 Correct 5 ms 18864 KB Output is correct
37 Correct 4 ms 12380 KB Output is correct
38 Correct 4 ms 12376 KB Output is correct
39 Correct 6 ms 19036 KB Output is correct
40 Correct 5 ms 18780 KB Output is correct
41 Correct 5 ms 18708 KB Output is correct
42 Correct 7 ms 18780 KB Output is correct
43 Correct 5 ms 18780 KB Output is correct
44 Correct 5 ms 18780 KB Output is correct
45 Correct 5 ms 18780 KB Output is correct
46 Correct 6 ms 18776 KB Output is correct
47 Correct 5 ms 18780 KB Output is correct
48 Correct 5 ms 19000 KB Output is correct
49 Correct 341 ms 91220 KB Output is correct
50 Correct 344 ms 91216 KB Output is correct
51 Correct 356 ms 91216 KB Output is correct
52 Correct 355 ms 91252 KB Output is correct
53 Correct 362 ms 89892 KB Output is correct
54 Correct 365 ms 92344 KB Output is correct
55 Correct 351 ms 86100 KB Output is correct
56 Correct 380 ms 88908 KB Output is correct
57 Correct 290 ms 89604 KB Output is correct
58 Correct 310 ms 89696 KB Output is correct
59 Correct 310 ms 89524 KB Output is correct
60 Correct 321 ms 91988 KB Output is correct
61 Correct 220 ms 35156 KB Output is correct
62 Correct 229 ms 34896 KB Output is correct
63 Correct 507 ms 85904 KB Output is correct
64 Correct 516 ms 77932 KB Output is correct
65 Correct 494 ms 74064 KB Output is correct
66 Correct 489 ms 71724 KB Output is correct
67 Correct 496 ms 70888 KB Output is correct
68 Correct 455 ms 70772 KB Output is correct
69 Correct 527 ms 70236 KB Output is correct
70 Correct 475 ms 70200 KB Output is correct
71 Correct 490 ms 69940 KB Output is correct
72 Correct 487 ms 70228 KB Output is correct
73 Correct 523 ms 70260 KB Output is correct
74 Correct 510 ms 70552 KB Output is correct
75 Correct 521 ms 71276 KB Output is correct
76 Correct 476 ms 72428 KB Output is correct
77 Correct 341 ms 74800 KB Output is correct
78 Correct 208 ms 80260 KB Output is correct