Submission #1116895

# Submission time Handle Problem Language Result Execution time Memory
1116895 2024-11-22T14:20:47 Z kfhjad Hard route (IZhO17_road) C++17
100 / 100
588 ms 148552 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

vector<int> adj[500001];
int up[500001], down[500001];
ll upFreq[500001], downFreq[500001];
ll ans = 0;
ll freq = 1;

void update(ll x, ll f)
{
    if (x > ans)
        ans = x, freq = f;
    else if (x == ans)
        freq += f;
}

void dfs(int node, int p)
{
    downFreq[node] = upFreq[node] = 1;
    
    for (int i : adj[node])
    {
        if (i == p)
            continue;
        
        dfs(i, node);
        
        if (down[node] < down[i] + 1)
            down[node] = down[i] + 1, downFreq[node] = downFreq[i];
        else if (down[node] == down[i] + 1)
            downFreq[node] += downFreq[i];
    }
}

void dfs1(int node, int p)
{
    vector<array<ll, 2>> paths;
    paths.push_back({up[node], upFreq[node]});
    
    for (int i : adj[node])
        if (i != p)
            paths.push_back({down[i] + 1, downFreq[i]});

    sort(paths.rbegin(), paths.rend());

    if (adj[node].size() > 2)
    {
        ll a = paths[0][0];
        ll b = paths[1][0];
        ll c = paths[2][0];
        ll x = a * (b + c);

        // all distinct
        if (a != b && b != c)
        {
            ll cnt = 0;
            for (auto [len, f] : paths)
                if (len == c)
                    cnt += f;
            
            update(x, paths[1][1] * cnt);
        }
        else if (a == b && b == c) // all same
        {
            ll cnt = 0;
            ll res = 0;
            for (auto [len, f] : paths)
                if (len == c)
                    cnt += f;
            
            // if (node == 2)
            //     cout << "HI " << cnt << endl;
                    
            for (auto [len, f] : paths)
                if (len == c)
                    res += f * (cnt - f);
            
            update(x, res / 2);
        }
        else if (a == b) // only first two are the same
        {
            ll cnt = 0;
            for (auto [len, f] : paths)
                if (len == c)
                    cnt += f;
            
            update(x, (paths[0][1] + paths[1][1]) * cnt);
        }
        else if (b == c) // last two are the same
        {
            ll cnt = 0;
            ll res = 0;
            for (auto [len, f] : paths)
                if (len == c)
                    cnt += f;
                    
            for (auto [len, f] : paths)
                if (len == c)
                    res += f * (cnt - f);         
            
            update(x, res / 2);
        }
    }
    
    ll best1 = 0, best2 = 0;
    
    for (auto [len, f] : paths)
        if (len == paths[0][0])
            best1 += f;
    
    for (auto [len, f] : paths)
        if (len == paths[1][0])
            best2 += f;

    for (int i : adj[node])
    {
        if (i == p)
            continue;
        
        ll path = down[i] + 1;
        
        if (path == paths[0][0])
        {
            best1 -= downFreq[i];
            
            if (best1 == 0) // only 1 longest path
                up[i] = paths[1][0] + 1, upFreq[i] = best2;
            else
                up[i] = paths[0][0] + 1, upFreq[i] = best1;
        }
        else
            up[i] = paths[0][0] + 1, upFreq[i] = best1;
            
        dfs1(i, node);
    }
}

int main()
{
    cin.tie(NULL) -> sync_with_stdio(false);
    int N;
    cin >> N;
    
    while (--N)    
    {
        int a, b;
        cin >> a >> b;
        
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    
    dfs(1, 0);
    dfs1(1, 0);
    
    cout << ans << ' ' << freq << endl;
	
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15440 KB Output is correct
2 Correct 3 ms 15608 KB Output is correct
3 Correct 3 ms 15556 KB Output is correct
4 Correct 3 ms 15440 KB Output is correct
5 Correct 3 ms 15440 KB Output is correct
6 Correct 3 ms 15440 KB Output is correct
7 Correct 3 ms 15440 KB Output is correct
8 Correct 3 ms 15440 KB Output is correct
9 Correct 3 ms 15440 KB Output is correct
10 Correct 3 ms 15568 KB Output is correct
11 Correct 4 ms 15440 KB Output is correct
12 Correct 3 ms 15440 KB Output is correct
13 Correct 4 ms 15440 KB Output is correct
14 Correct 4 ms 15440 KB Output is correct
15 Correct 4 ms 15440 KB Output is correct
16 Correct 3 ms 15440 KB Output is correct
17 Correct 4 ms 15440 KB Output is correct
18 Correct 3 ms 15440 KB Output is correct
19 Correct 3 ms 15608 KB Output is correct
20 Correct 4 ms 15440 KB Output is correct
21 Correct 4 ms 15440 KB Output is correct
22 Correct 3 ms 15440 KB Output is correct
23 Correct 3 ms 15440 KB Output is correct
24 Correct 4 ms 15440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15440 KB Output is correct
2 Correct 3 ms 15608 KB Output is correct
3 Correct 3 ms 15556 KB Output is correct
4 Correct 3 ms 15440 KB Output is correct
5 Correct 3 ms 15440 KB Output is correct
6 Correct 3 ms 15440 KB Output is correct
7 Correct 3 ms 15440 KB Output is correct
8 Correct 3 ms 15440 KB Output is correct
9 Correct 3 ms 15440 KB Output is correct
10 Correct 3 ms 15568 KB Output is correct
11 Correct 4 ms 15440 KB Output is correct
12 Correct 3 ms 15440 KB Output is correct
13 Correct 4 ms 15440 KB Output is correct
14 Correct 4 ms 15440 KB Output is correct
15 Correct 4 ms 15440 KB Output is correct
16 Correct 3 ms 15440 KB Output is correct
17 Correct 4 ms 15440 KB Output is correct
18 Correct 3 ms 15440 KB Output is correct
19 Correct 3 ms 15608 KB Output is correct
20 Correct 4 ms 15440 KB Output is correct
21 Correct 4 ms 15440 KB Output is correct
22 Correct 3 ms 15440 KB Output is correct
23 Correct 3 ms 15440 KB Output is correct
24 Correct 4 ms 15440 KB Output is correct
25 Correct 5 ms 15952 KB Output is correct
26 Correct 5 ms 16376 KB Output is correct
27 Correct 5 ms 16208 KB Output is correct
28 Correct 5 ms 16208 KB Output is correct
29 Correct 6 ms 16208 KB Output is correct
30 Correct 5 ms 16208 KB Output is correct
31 Correct 5 ms 16208 KB Output is correct
32 Correct 5 ms 16208 KB Output is correct
33 Correct 5 ms 16208 KB Output is correct
34 Correct 5 ms 16208 KB Output is correct
35 Correct 5 ms 16208 KB Output is correct
36 Correct 5 ms 16208 KB Output is correct
37 Correct 6 ms 16208 KB Output is correct
38 Correct 6 ms 16720 KB Output is correct
39 Correct 6 ms 15952 KB Output is correct
40 Correct 6 ms 16120 KB Output is correct
41 Correct 5 ms 15696 KB Output is correct
42 Correct 4 ms 15696 KB Output is correct
43 Correct 5 ms 15696 KB Output is correct
44 Correct 4 ms 15696 KB Output is correct
45 Correct 5 ms 15696 KB Output is correct
46 Correct 4 ms 15696 KB Output is correct
47 Correct 4 ms 15704 KB Output is correct
48 Correct 5 ms 15952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15440 KB Output is correct
2 Correct 3 ms 15608 KB Output is correct
3 Correct 3 ms 15556 KB Output is correct
4 Correct 3 ms 15440 KB Output is correct
5 Correct 3 ms 15440 KB Output is correct
6 Correct 3 ms 15440 KB Output is correct
7 Correct 3 ms 15440 KB Output is correct
8 Correct 3 ms 15440 KB Output is correct
9 Correct 3 ms 15440 KB Output is correct
10 Correct 3 ms 15568 KB Output is correct
11 Correct 4 ms 15440 KB Output is correct
12 Correct 3 ms 15440 KB Output is correct
13 Correct 4 ms 15440 KB Output is correct
14 Correct 4 ms 15440 KB Output is correct
15 Correct 4 ms 15440 KB Output is correct
16 Correct 3 ms 15440 KB Output is correct
17 Correct 4 ms 15440 KB Output is correct
18 Correct 3 ms 15440 KB Output is correct
19 Correct 3 ms 15608 KB Output is correct
20 Correct 4 ms 15440 KB Output is correct
21 Correct 4 ms 15440 KB Output is correct
22 Correct 3 ms 15440 KB Output is correct
23 Correct 3 ms 15440 KB Output is correct
24 Correct 4 ms 15440 KB Output is correct
25 Correct 5 ms 15952 KB Output is correct
26 Correct 5 ms 16376 KB Output is correct
27 Correct 5 ms 16208 KB Output is correct
28 Correct 5 ms 16208 KB Output is correct
29 Correct 6 ms 16208 KB Output is correct
30 Correct 5 ms 16208 KB Output is correct
31 Correct 5 ms 16208 KB Output is correct
32 Correct 5 ms 16208 KB Output is correct
33 Correct 5 ms 16208 KB Output is correct
34 Correct 5 ms 16208 KB Output is correct
35 Correct 5 ms 16208 KB Output is correct
36 Correct 5 ms 16208 KB Output is correct
37 Correct 6 ms 16208 KB Output is correct
38 Correct 6 ms 16720 KB Output is correct
39 Correct 6 ms 15952 KB Output is correct
40 Correct 6 ms 16120 KB Output is correct
41 Correct 5 ms 15696 KB Output is correct
42 Correct 4 ms 15696 KB Output is correct
43 Correct 5 ms 15696 KB Output is correct
44 Correct 4 ms 15696 KB Output is correct
45 Correct 5 ms 15696 KB Output is correct
46 Correct 4 ms 15696 KB Output is correct
47 Correct 4 ms 15704 KB Output is correct
48 Correct 5 ms 15952 KB Output is correct
49 Correct 345 ms 80400 KB Output is correct
50 Correct 367 ms 87904 KB Output is correct
51 Correct 373 ms 94536 KB Output is correct
52 Correct 355 ms 71392 KB Output is correct
53 Correct 351 ms 90332 KB Output is correct
54 Correct 308 ms 98632 KB Output is correct
55 Correct 304 ms 78412 KB Output is correct
56 Correct 306 ms 87156 KB Output is correct
57 Correct 345 ms 99404 KB Output is correct
58 Correct 367 ms 90660 KB Output is correct
59 Correct 325 ms 90444 KB Output is correct
60 Correct 313 ms 86600 KB Output is correct
61 Correct 506 ms 148552 KB Output is correct
62 Correct 491 ms 128792 KB Output is correct
63 Correct 546 ms 70980 KB Output is correct
64 Correct 520 ms 57016 KB Output is correct
65 Correct 588 ms 48164 KB Output is correct
66 Correct 497 ms 43848 KB Output is correct
67 Correct 446 ms 41272 KB Output is correct
68 Correct 455 ms 40276 KB Output is correct
69 Correct 383 ms 39952 KB Output is correct
70 Correct 419 ms 39452 KB Output is correct
71 Correct 475 ms 39496 KB Output is correct
72 Correct 514 ms 39544 KB Output is correct
73 Correct 463 ms 39884 KB Output is correct
74 Correct 445 ms 39888 KB Output is correct
75 Correct 425 ms 40636 KB Output is correct
76 Correct 387 ms 42304 KB Output is correct
77 Correct 311 ms 53052 KB Output is correct
78 Correct 237 ms 55748 KB Output is correct