Submission #1116775

# Submission time Handle Problem Language Result Execution time Memory
1116775 2024-11-22T11:06:23 Z kfhjad Hard route (IZhO17_road) C++17
0 / 100
5 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 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 (down[i] + 1 == m1 && adj[node].size() > 2)
        {
            ll x = (m2 + m3) * m1;
            
            if (x > ans)
                ans = x, freq = 1;
            else if (x == ans)
                ++freq;
        }
            
        dfs1(i, node);
    }
    
    if (up[node] == m1 && adj[node].size() > 2 && node != 1)
    {
        ll x = (m2 + m3) * m1;
        
        if (x > ans)
            ans = x, freq = 1;
        else if (x == ans)
            ++freq;
    }
    
    // cout << m1 << ' ' << m2 << ' ' << m3 << endl;
}

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 3 ms 13816 KB Output is correct
4 Correct 3 ms 13772 KB Output is correct
5 Correct 3 ms 13816 KB Output is correct
6 Correct 5 ms 13648 KB Output is correct
7 Incorrect 3 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 3 ms 13816 KB Output is correct
4 Correct 3 ms 13772 KB Output is correct
5 Correct 3 ms 13816 KB Output is correct
6 Correct 5 ms 13648 KB Output is correct
7 Incorrect 3 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 3 ms 13816 KB Output is correct
4 Correct 3 ms 13772 KB Output is correct
5 Correct 3 ms 13816 KB Output is correct
6 Correct 5 ms 13648 KB Output is correct
7 Incorrect 3 ms 13648 KB Output isn't correct
8 Halted 0 ms 0 KB -