Submission #885503

# Submission time Handle Problem Language Result Execution time Memory
885503 2023-12-09T21:43:16 Z alexdd Hard route (IZhO17_road) C++17
0 / 100
16 ms 44192 KB
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define int long long
int n;
vector<int> con[500005];
map<int,pair<int,int>> dp[500005];
int root;
int depth[500005];
int siz[500005];
int maxd[500005];
int sus[500005];
int tin[500005];
int tout[500005],timer;
int pose[500005],cnte;
int parent[500005];
pair<int,int> mxm = {0,0};
pair<int,int> combine(pair<int,int> x, pair<int,int> y)
{
    if(x.second==0)
        return y;
    if(y.second==0)
        return x;
    if(x.first > y.first)
        return x;
    if(x.first < y.first)
        return y;
    return {x.first, x.second + y.second};
}
bool isanc(int x, int y)
{
    if(tin[x]<=tin[y] && tout[x]>=tout[y])
        return 1;
    return 0;
}
void dfs_init(int nod, int par)
{
    parent[nod]=par;
    pose[nod]=++cnte;
    tin[nod]=++timer;
    siz[nod]=1;
    maxd[nod]=depth[nod];
    for(auto adj:con[nod])
    {
        if(adj!=par)
        {
            depth[adj]=depth[nod]+1;
            dfs_init(adj,nod);
            maxd[nod] = max(maxd[nod], maxd[adj]);
            siz[nod]+=siz[adj];
        }
    }
    tout[nod]=++timer;
}
void dfs(int nod)
{
    //cout<<nod<<" start\n";
    int cntc=0;
    set<pair<int,int>> s;
    s.insert({-1,-1});
    for(auto adj:con[nod])
    {
        if(adj!=parent[nod])
        {
            dfs(adj);
            s.insert({maxd[adj], adj});
            cntc++;
        }
    }
    //cout<<nod<<" dfs done\n";
    //cout<<nod<<" aib qry done\n";
    for(auto adj:con[nod])
    {
        if(adj==parent[nod])
            continue;
        for(auto adj2:con[nod])
        {
            if(adj2==parent[nod] || adj==adj2)
                continue;
            s.erase({maxd[adj],adj});
            s.erase({maxd[adj2],adj2});
            for(auto it:dp[adj])
            {
                for(auto it2:dp[adj2])
                {
                    pair<int,int> idk = *s.rbegin();
                    pair<int,int> aux = {(it.first + it2.first - 2*depth[nod]) * max({it.second.first,it2.second.first,sus[nod],idk.first-depth[nod]}), it.second.second * it2.second.second};
                    //cout<<nod<<"   "<<adj<<" "<<adj2<<"   "<<it.first<<" "<<it2.first<<"   moduri = "<<aux.second<<"\n";
                    mxm = combine(mxm, aux);
                }
            }
            s.insert({maxd[adj],adj});
            s.insert({maxd[adj2],adj2});
        }
    }
    //cout<<nod<<" mxm calc done\n";
    if(cntc==0)
    {
        dp[nod][depth[nod]] = {0,1};
    }
    else
    {
        for(auto adj:con[nod])
        {
            if(adj==parent[nod])
                continue;
            s.erase({maxd[adj],adj});
            pair<int,int> idk = *s.rbegin();
            //cout<<nod<<"   "<<adj<<"   "<<idk.first<<" "<<idk.second<<" idk\n";
            for(auto it:dp[adj])
            {
                pair<int,int> x = it.second;
                x.first = max(x.first, idk.first - depth[nod]);
                dp[nod][it.first] = combine(dp[nod][it.first], x);
            }
            s.insert({maxd[adj],adj});
            //dp[adj].clear();
        }
    }
    //for(auto it:dp[nod])
    //    cout<<nod<<"   "<<it.first<<"  "<<it.second.first<<" "<<it.second.second<<"\n";
    //cout<<nod<<" end\n";
}
void calc_sus()
{
    for(int i=1;i<=n;i++)
    {
        sus[i] = depth[i];
        int prec=i;
        for(int j=parent[i];j!=0;j=parent[j])
        {
            for(auto adj:con[j])
            {
                if(adj!=parent[j] && adj!=prec)
                {
                    sus[i] = max(sus[i], (maxd[adj] - depth[j]) + (depth[i] - depth[j]));
                }
            }
            prec=j;
        }
        //cout<<i<<" "<<sus[i]<<" sus\n";
    }
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n;
    if(n==2)
    {
        cout<<0;
        return 0;
    }
    int a,b;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        con[a].push_back(b);
        con[b].push_back(a);
    }
    for(int i=n;i>0;i--)
        if((int)con[i].size()>1)
            root=i;
    //cout<<root<<" is the root\n";
    dfs_init(root,0);
    calc_sus();
    dfs(root);
    cout<<mxm.first<<" "<<mxm.second/2<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 44128 KB Output is correct
2 Correct 8 ms 44120 KB Output is correct
3 Correct 11 ms 44132 KB Output is correct
4 Correct 9 ms 44192 KB Output is correct
5 Correct 10 ms 44124 KB Output is correct
6 Incorrect 8 ms 35936 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 44128 KB Output is correct
2 Correct 8 ms 44120 KB Output is correct
3 Correct 11 ms 44132 KB Output is correct
4 Correct 9 ms 44192 KB Output is correct
5 Correct 10 ms 44124 KB Output is correct
6 Incorrect 8 ms 35936 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 44128 KB Output is correct
2 Correct 8 ms 44120 KB Output is correct
3 Correct 11 ms 44132 KB Output is correct
4 Correct 9 ms 44192 KB Output is correct
5 Correct 10 ms 44124 KB Output is correct
6 Incorrect 8 ms 35936 KB Output isn't correct
7 Halted 0 ms 0 KB -