답안 #1038643

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038643 2024-07-30T04:45:50 Z MMihalev Hard route (IZhO17_road) C++14
0 / 100
2000 ms 14824 KB
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<map>
#include<random>
#include<chrono>
using namespace std;
const long long MAX_N=5e5+5;
long long maxd[MAX_N];
long long cntd[MAX_N];
long long maxd2[MAX_N];
long long maxdup[MAX_N];
vector<long long>g[MAX_N];
long long n;
long long cnt;
long long ans;
long long depth[MAX_N];

void dfsdown(long long u,long long par)
{
    for(long long v:g[u])
    {
        if(v==par)continue;
        depth[v]=depth[u]+1;
        dfsdown(v,u);
        if(maxd[v]+1>maxd[u])
        {
            maxd2[u]=maxd[u];
            maxd[u]=maxd[v]+1;
            cntd[u]=cntd[v];
        }
        else if(maxd[v]+1==maxd[u])
        {
            maxd2[u]=maxd[u];
            cntd[u]+=cntd[v];
        }
    }
    if(g[u].size()==1)cntd[u]=1;
}
long long dp[MAX_N];
void dfsup(long long u,long long par)
{
    for(long long v:g[u])
    {
        if(v==par)continue;

        long long cur=maxd[u];
        if(maxd[v]+1==maxd[u])cur=maxd2[u];

        dp[v]=max(dp[u],cur);
        maxdup[v]=max(maxdup[u],cur)+1;

        dfsup(v,u);
    }
}
long long eachtwo(vector<long long>& v)
{
    long long sum=0;
    long long pairs=0;

    for(long long x:v)sum+=x;

    for(long long x:v)
    {
        sum-=x;
        pairs+=(x*sum);
    }

    return pairs;
}
void dfs(long long u,long long par)
{
    vector<pair<long long,long long>>V;//depth,cnt
    vector<pair<long long,long long>>all;
    vector<pair<long long,long long>>otherV;

    long long up=maxdup[u],curans=0,curcnt=0;

    for(long long v:g[u])
    {
        if(v==par)continue;
        dfs(v,u);
        all.push_back({maxd[v]+1,cntd[v]});
    }

    if(g[u].size()==1)return;

    sort(all.begin(),all.end());

    V.push_back(all[0]);
    otherV.push_back({all[0].first,1});

    for(long long i=1;i<all.size();i++)
    {
        if(all[i].first==all[i-1].first)
        {
            V.back().second+=all[i].second;
            otherV.back().second++;
        }
        else {V.push_back(all[i]);otherV.push_back({all[i].first,1});}
    }


    if(otherV.back().second>=3)
    {
        long long b=V.back().first;
        vector<long long>only;

        for(long long v:g[u])
        {
            if(v==par)continue;
            if(maxd[v]+1==b)only.push_back(cntd[v]);
        }

        long long eachtwob=eachtwo(only);

        curans=2*b*b;
        curcnt=eachtwob;
    }
    else
    {
        if(otherV.back().second==2)
        {
            if(V.size()>=2)
            {
                long long b=V.back().first,cb=V.back().second;
                long long a=V[V.size()-2].first,ca=V[V.size()-2].second;

                curans=b*(a+b);
                curcnt=ca*cb;
            }
        }
        else
        {
            if(V.size()==2)
            {
                if(otherV[0].second>=2)
                {
                    long long b=V[1].first;
                    long long a=V[0].first;
                    vector<long long>only;

                    for(long long v:g[u])
                    {
                        if(v==par)continue;
                        if(maxd[v]+1==a)only.push_back(cntd[v]);
                    }

                    long long eachtwoa=eachtwo(only);

                    curans=2*a*b;
                    curcnt=eachtwoa;
                }
            }
            else if(V.size()>2)
            {
                long long b=V.back().first,cb=V.back().second;
                long long a=V[V.size()-2].first,ca=V[V.size()-2].second;
                long long c=V[V.size()-3].first,cc=V[V.size()-3].second;

                curans=b*(a+c);
                curcnt=cc*ca;
            }
        }
    }
    ///without up


    if(otherV.back().second>=2)
    {
        long long b=V.back().first;
        vector<long long>only;

        for(long long v:g[u])
        {
            if(v==par)continue;
            if(maxd[v]+1==b)only.push_back(cntd[v]);
        }

        long long eachtwob=eachtwo(only);

        if(2*b*up>curans)
        {
            curans=2*b*up;
            curcnt=eachtwob;
        }
        else if(2*b*up==curans)
        {
            curcnt+=eachtwob;
        }
    }
    else
    {
        if(V.size()>=2)
        {
            long long b=V.back().first,cb=V.back().second;
            long long a=V[V.size()-2].first,ca=V[V.size()-2].second;

            if((a+b)*up>curans)
            {
                curans=(a+b)*up;
                curcnt=ca*cb;
            }
            else if((a+b)*up==curans)
            {
                curcnt+=ca*cb;
            }
        }
    }
    ///with up


    if(curans>ans)
    {
        ans=curans;
        cnt=curcnt;
    }
    else if(curans==ans)
    {
        cnt+=curcnt;
    }
}
pair<long long,long long>diam(long long u,long long par)
{
    pair<long long,long long>res={u,0};
    for(long long v:g[u])
    {
        if(v==par)continue;
        if(diam(v,u).second+1>res.second)
        {
            res=diam(v,u);
            res.second++;
        }
    }
    return res;
}
///
void solvefor(long long root)
{
    for(long long i=1;i<=n;i++)
    {
        if(g[i].size()!=1 or i==root)continue;
        long long curhardness=depth[i]*dp[i];
        if(curhardness>ans)
        {
            ans=curhardness;
            cnt=1;
        }
        else if(curhardness==ans)cnt++;
    }
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n;
    for(long long i=1;i<n;i++)
    {
        long long u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    long long distant=diam(1,0).first;
    long long root=diam(distant,0).first;

    dfsdown(root,0);
    dfsup(root,0);
    dfs(root,0);

    solvefor(root);

    if(ans==0)cnt=1;
    cout<<ans<<" "<<cnt<<"\n";

    return 0;
}

Compilation message

road.cpp: In function 'void dfs(long long int, long long int)':
road.cpp:100:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for(long long i=1;i<all.size();i++)
      |                       ~^~~~~~~~~~~
road.cpp:164:44: warning: unused variable 'cb' [-Wunused-variable]
  164 |                 long long b=V.back().first,cb=V.back().second;
      |                                            ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 13496 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 2 ms 13488 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Execution timed out 2059 ms 14824 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 13496 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 2 ms 13488 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Execution timed out 2059 ms 14824 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 13496 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 2 ms 13488 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Execution timed out 2059 ms 14824 KB Time limit exceeded
8 Halted 0 ms 0 KB -