Submission #1116891

# Submission time Handle Problem Language Result Execution time Memory
1116891 2024-11-22T14:15:32 Z kfhjad Hard route (IZhO17_road) C++17
52 / 100
581 ms 151860 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

vector<int> adj[500001];
int up[500001], down[500001];
int upFreq[500001], downFreq[500001];
ll ans = 0;
int 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 4 ms 15696 KB Output is correct
2 Correct 4 ms 15696 KB Output is correct
3 Correct 3 ms 15696 KB Output is correct
4 Correct 4 ms 15696 KB Output is correct
5 Correct 3 ms 15696 KB Output is correct
6 Correct 3 ms 15696 KB Output is correct
7 Correct 3 ms 15696 KB Output is correct
8 Correct 3 ms 15696 KB Output is correct
9 Correct 3 ms 15696 KB Output is correct
10 Correct 4 ms 15920 KB Output is correct
11 Correct 3 ms 15748 KB Output is correct
12 Correct 4 ms 15696 KB Output is correct
13 Correct 3 ms 15696 KB Output is correct
14 Correct 3 ms 15696 KB Output is correct
15 Correct 4 ms 15864 KB Output is correct
16 Correct 4 ms 15696 KB Output is correct
17 Correct 3 ms 15696 KB Output is correct
18 Correct 4 ms 15696 KB Output is correct
19 Correct 3 ms 15696 KB Output is correct
20 Correct 4 ms 15696 KB Output is correct
21 Correct 4 ms 15696 KB Output is correct
22 Correct 3 ms 15696 KB Output is correct
23 Correct 4 ms 15696 KB Output is correct
24 Correct 3 ms 15696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15696 KB Output is correct
2 Correct 4 ms 15696 KB Output is correct
3 Correct 3 ms 15696 KB Output is correct
4 Correct 4 ms 15696 KB Output is correct
5 Correct 3 ms 15696 KB Output is correct
6 Correct 3 ms 15696 KB Output is correct
7 Correct 3 ms 15696 KB Output is correct
8 Correct 3 ms 15696 KB Output is correct
9 Correct 3 ms 15696 KB Output is correct
10 Correct 4 ms 15920 KB Output is correct
11 Correct 3 ms 15748 KB Output is correct
12 Correct 4 ms 15696 KB Output is correct
13 Correct 3 ms 15696 KB Output is correct
14 Correct 3 ms 15696 KB Output is correct
15 Correct 4 ms 15864 KB Output is correct
16 Correct 4 ms 15696 KB Output is correct
17 Correct 3 ms 15696 KB Output is correct
18 Correct 4 ms 15696 KB Output is correct
19 Correct 3 ms 15696 KB Output is correct
20 Correct 4 ms 15696 KB Output is correct
21 Correct 4 ms 15696 KB Output is correct
22 Correct 3 ms 15696 KB Output is correct
23 Correct 4 ms 15696 KB Output is correct
24 Correct 3 ms 15696 KB Output is correct
25 Correct 6 ms 16208 KB Output is correct
26 Correct 5 ms 16476 KB Output is correct
27 Correct 6 ms 16588 KB Output is correct
28 Correct 6 ms 16464 KB Output is correct
29 Correct 6 ms 16312 KB Output is correct
30 Correct 6 ms 16464 KB Output is correct
31 Correct 6 ms 16464 KB Output is correct
32 Correct 5 ms 16208 KB Output is correct
33 Correct 5 ms 16464 KB Output is correct
34 Correct 6 ms 16464 KB Output is correct
35 Correct 6 ms 16464 KB Output is correct
36 Correct 5 ms 16464 KB Output is correct
37 Correct 6 ms 16464 KB Output is correct
38 Correct 7 ms 16976 KB Output is correct
39 Correct 6 ms 16208 KB Output is correct
40 Correct 5 ms 16208 KB Output is correct
41 Correct 6 ms 15952 KB Output is correct
42 Correct 6 ms 15952 KB Output is correct
43 Correct 5 ms 15952 KB Output is correct
44 Correct 6 ms 15952 KB Output is correct
45 Correct 5 ms 15960 KB Output is correct
46 Correct 5 ms 15952 KB Output is correct
47 Correct 5 ms 16192 KB Output is correct
48 Correct 5 ms 16376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15696 KB Output is correct
2 Correct 4 ms 15696 KB Output is correct
3 Correct 3 ms 15696 KB Output is correct
4 Correct 4 ms 15696 KB Output is correct
5 Correct 3 ms 15696 KB Output is correct
6 Correct 3 ms 15696 KB Output is correct
7 Correct 3 ms 15696 KB Output is correct
8 Correct 3 ms 15696 KB Output is correct
9 Correct 3 ms 15696 KB Output is correct
10 Correct 4 ms 15920 KB Output is correct
11 Correct 3 ms 15748 KB Output is correct
12 Correct 4 ms 15696 KB Output is correct
13 Correct 3 ms 15696 KB Output is correct
14 Correct 3 ms 15696 KB Output is correct
15 Correct 4 ms 15864 KB Output is correct
16 Correct 4 ms 15696 KB Output is correct
17 Correct 3 ms 15696 KB Output is correct
18 Correct 4 ms 15696 KB Output is correct
19 Correct 3 ms 15696 KB Output is correct
20 Correct 4 ms 15696 KB Output is correct
21 Correct 4 ms 15696 KB Output is correct
22 Correct 3 ms 15696 KB Output is correct
23 Correct 4 ms 15696 KB Output is correct
24 Correct 3 ms 15696 KB Output is correct
25 Correct 6 ms 16208 KB Output is correct
26 Correct 5 ms 16476 KB Output is correct
27 Correct 6 ms 16588 KB Output is correct
28 Correct 6 ms 16464 KB Output is correct
29 Correct 6 ms 16312 KB Output is correct
30 Correct 6 ms 16464 KB Output is correct
31 Correct 6 ms 16464 KB Output is correct
32 Correct 5 ms 16208 KB Output is correct
33 Correct 5 ms 16464 KB Output is correct
34 Correct 6 ms 16464 KB Output is correct
35 Correct 6 ms 16464 KB Output is correct
36 Correct 5 ms 16464 KB Output is correct
37 Correct 6 ms 16464 KB Output is correct
38 Correct 7 ms 16976 KB Output is correct
39 Correct 6 ms 16208 KB Output is correct
40 Correct 5 ms 16208 KB Output is correct
41 Correct 6 ms 15952 KB Output is correct
42 Correct 6 ms 15952 KB Output is correct
43 Correct 5 ms 15952 KB Output is correct
44 Correct 6 ms 15952 KB Output is correct
45 Correct 5 ms 15960 KB Output is correct
46 Correct 5 ms 15952 KB Output is correct
47 Correct 5 ms 16192 KB Output is correct
48 Correct 5 ms 16376 KB Output is correct
49 Correct 365 ms 83688 KB Output is correct
50 Correct 351 ms 91208 KB Output is correct
51 Correct 408 ms 97728 KB Output is correct
52 Correct 347 ms 74512 KB Output is correct
53 Correct 288 ms 93576 KB Output is correct
54 Correct 309 ms 101832 KB Output is correct
55 Correct 301 ms 81480 KB Output is correct
56 Correct 294 ms 90184 KB Output is correct
57 Correct 361 ms 102472 KB Output is correct
58 Correct 313 ms 93768 KB Output is correct
59 Correct 312 ms 93860 KB Output is correct
60 Correct 349 ms 89672 KB Output is correct
61 Correct 581 ms 151860 KB Output is correct
62 Correct 573 ms 131924 KB Output is correct
63 Correct 503 ms 74100 KB Output is correct
64 Correct 458 ms 60440 KB Output is correct
65 Correct 413 ms 51372 KB Output is correct
66 Correct 474 ms 46920 KB Output is correct
67 Correct 460 ms 44240 KB Output is correct
68 Correct 466 ms 43324 KB Output is correct
69 Correct 484 ms 43300 KB Output is correct
70 Correct 353 ms 42836 KB Output is correct
71 Correct 351 ms 42568 KB Output is correct
72 Correct 431 ms 43116 KB Output is correct
73 Correct 449 ms 43160 KB Output is correct
74 Correct 493 ms 43312 KB Output is correct
75 Correct 511 ms 43980 KB Output is correct
76 Incorrect 427 ms 45704 KB Output isn't correct
77 Halted 0 ms 0 KB -