Submission #1116782

# Submission time Handle Problem Language Result Execution time Memory
1116782 2024-11-22T11:20:15 Z kfhjad Hard route (IZhO17_road) C++17
0 / 100
4 ms 13816 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

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

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

void dfs1(int node, int p)
{
    ll m1 = 0, m2 = 0, m3 = 0;
    
    for (int i : adj[node])
    {
        if (i == p)
            continue;
        
        if (down[i] + 1 > m3) m3 = down[i] + 1;
        if (m3 > m2) swap(m3, m2);
        if (m2 > m1) swap(m2, m1);
    }
    
    if (up[node] > m3) m3 = up[node];
    if (m3 > m2) swap(m3, m2);
    if (m2 > m1) swap(m2, m1);
    
    for (int i : adj[node])
    {
        if (i == p)
            continue;
        
        if (down[i] + 1 == m1)
            up[i] = max((ll)up[node], m2) + 1;
        else
            up[i] = max((ll)up[node], m1) + 1;
        
        if (adj[node].size() > 2)
        {
            ll path = down[i] + 1;
            
            if (path == m1)
                update((m2 + m3) * path);
            else if (path == m2)
                update((m1 + m3) * path);
            else
                update((m1 + m2) * path);
        }
            
        dfs1(i, node);
    }

    if (adj[node].size() > 2)
    {
        ll path = up[node];
        
        if (path == m1)
            update((m2 + m3) * path);
        else if (path == m2)
            update((m1 + m3) * path);
        else
            update((m1 + m2) * path);
    }
}

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 13648 KB Output is correct
2 Correct 4 ms 13648 KB Output is correct
3 Correct 4 ms 13648 KB Output is correct
4 Correct 4 ms 13648 KB Output is correct
5 Correct 3 ms 13816 KB Output is correct
6 Correct 3 ms 13648 KB Output is correct
7 Incorrect 4 ms 13648 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13648 KB Output is correct
2 Correct 4 ms 13648 KB Output is correct
3 Correct 4 ms 13648 KB Output is correct
4 Correct 4 ms 13648 KB Output is correct
5 Correct 3 ms 13816 KB Output is correct
6 Correct 3 ms 13648 KB Output is correct
7 Incorrect 4 ms 13648 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13648 KB Output is correct
2 Correct 4 ms 13648 KB Output is correct
3 Correct 4 ms 13648 KB Output is correct
4 Correct 4 ms 13648 KB Output is correct
5 Correct 3 ms 13816 KB Output is correct
6 Correct 3 ms 13648 KB Output is correct
7 Incorrect 4 ms 13648 KB Output isn't correct
8 Halted 0 ms 0 KB -