답안 #476865

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476865 2021-09-28T18:57:20 Z ogibogi2004 관광지 (IZhO14_shymbulak) C++14
0 / 100
1500 ms 20428 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=2e5+6;
const ll INF=2e9;
ll par[MAXN],n,depth[MAXN];
vector<ll>g[MAXN];
vector<ll>cycle;
bool vis[MAXN];
bool found;
ll x1,x2;
ll cycle_size;
pair<ll,ll> maxd[MAXN];
pair<ll,ll> maxdist1,maxdist2;
void find_cycle(ll u,ll p)
{
    par[u]=p;
    for(auto xd:g[u])
    {
        if(par[xd]==0)
        {
            depth[xd]=depth[u]+1;
            find_cycle(xd,u);
        }
        else if(xd!=par[u]&&!found)
        {
            x1=u;
            x2=xd;
            found=1;
        }
    }
}
void make_cycle()
{
    if(depth[x1]<depth[x2])swap(x1,x2);
    cycle.push_back(x1);
    for(;;)
    {
        x1=par[x1];
        cycle.push_back(x1);
        if(x1==x2)break;
    }
}
void dfs1(ll u)
{
    pair<ll,ll> ansu;
    ansu={0,0};
    vis[u]=1;
    vector<pair<ll,ll> >farthest;
    for(auto xd:g[u])
    {
        if(vis[xd])continue;
        dfs1(xd);
        farthest.push_back({maxd[xd].first+1,maxd[xd].second});
    }
    sort(farthest.rbegin(),farthest.rend());
    map<ll,ll> mpmp;
    if(farthest.size()>=2)
    {
        for(auto xd:farthest)mpmp[xd.first]+=xd.second;
        ansu.first=farthest[0].first+farthest[1].first;
        if(farthest[0].first==farthest[1].first)
        {
            for(ll i=0;i<farthest.size();i++)
            {
                if(farthest[i].first!=farthest[0].first)break;
                ansu.second+=farthest[i].second*(mpmp[farthest[i].first]-farthest[i].second);
            }
        }
        else
        {
            ansu.second=mpmp[farthest[0].first]*mpmp[farthest[1].first];
        }
    }
    //cout<<u<<" "<<ansu.first<<" "<<ansu.second<<endl;
    if(ansu.first>maxdist2.first)
    {
        maxdist2.first=ansu.first;maxdist2.second=0;
    }
    if(ansu.first==maxdist2.first)
    {
        maxdist2.second+=ansu.second;
    }
}
pair<ll,ll> calc(ll u)
{
    vis[u]=1;
    pair<ll,ll> ret={0,1};
    for(auto xd:g[u])
    {
        if(vis[xd])continue;
        pair<ll,ll> f=calc(xd);
        f.first++;
        if(f.first==ret.first)ret.second+=f.second;
        else if(f.first>ret.first)ret=f;
    }
    return maxd[u]=ret;
}
ll ans;
ll val1[2*MAXN],val2[2*MAXN];
map<ll,ll> mp1;
map<ll,ll> mp2;
ll w[512][512],ww[512][512];
int which1[MAXN];
void dfs1000(int u,int c)
{
    which1[u]=c;vis[u]=1;
    for(auto xd:g[u])
    {
        if(vis[xd])continue;
        dfs1000(xd,c);
    }
}
int main()
{
    maxdist1={0,0};
    maxdist2={0,0};
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        ll x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    find_cycle(1,-1);
    make_cycle();
    cycle_size=cycle.size();
    for(ll i=0;i<cycle_size;i++)
    {
        //cout<<cycle[i]<<" ";
        cycle.push_back(cycle[i]);
    }
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        w[i][i]=0;
        ww[i][i]=1;
        vis[i]=1;
        queue<int>q;
        q.push(i);
        while(!q.empty())
        {
            int u=q.front();q.pop();
            for(auto xd:g[u])
            {
                if(vis[xd])
                {
                    if(w[i][xd]==w[i][u]+1)
                    {
                        ww[i][xd]+=ww[i][u];
                    }
                    continue;
                }
                vis[xd]=1;
                w[i][xd]=w[i][u]+1;
                ww[i][xd]=ww[i][u];
                q.push(xd);
            }
        }
    }
    //cout<<endl;
    ll t=cycle_size/2;
    for(ll i=1;i<=cycle_size;i++)
    {
        vis[cycle[i]]=1;
    }
    for(ll i=1;i<=cycle_size;i++)
    {
        calc(cycle[i]);
    }
    multiset<ll>s1;
    for(ll i=0;i<cycle_size*2;i++)
    {
        val1[i]=-i+maxd[cycle[i]].first;
        val2[i]=i+maxd[cycle[i]].first;
    }
    for(ll i=0;i<t;i++)
    {
        s1.insert(val1[i]);
        mp1[val1[i]]+=maxd[cycle[i]].second;
        //s2.insert(val2[t+1+i]);
        //mp2[val2[t+1+i]]+=maxd[cycle[t+1+i]].second;
    }
    /*for(ll i=0;i<cycle.size();i++)
    {
        cout<<cycle[i]<<" ";
    }
    cout<<endl;*/
    //cout<<t<<" "<<cycle[t]<<":\n";
    ll max1=(*s1.rbegin());
    //ll max2=(*s2.rbegin());
    ll cnt,dist;
    cnt=mp1[max1]*maxd[cycle[t]].second;
    dist=max1+t+maxd[cycle[t]].first;
    //cout<<dist<<" "<<cnt<<endl;
    if(dist>maxdist1.first)
    {
        maxdist1.first=dist;
        maxdist1.second=0;
    }
    if(dist==maxdist1.first)
    {
        maxdist1.second+=cnt;
    }
    /*cnt=mp2[max2]*maxd[cycle[t]].second;
    dist=max2-t+maxd[cycle[t]].first;
    cout<<dist<<" "<<cnt<<endl;
    if(dist>maxdist.first)
    {
        maxdist.first=dist;
        maxdist.second=0;
    }
    if(dist==maxdist.first)
    {
        maxdist.second+=cnt;
    }*/
    for(ll mid=t+1;mid<cycle_size+t;mid++)
    {
        //cout<<mid<<" "<<cycle[mid]<<":\n";
        s1.erase(s1.find(val1[mid-t-1]));
        mp1[val1[mid-t-1]]-=maxd[cycle[mid-t-1]].second;
        //s2.erase(val2[mid]);
        //mp2[val2[mid]]-=maxd[cycle[mid]].second;
        s1.insert(val1[mid-1]);
        mp1[val1[mid-1]]+=maxd[cycle[mid-1]].second;
        //s2.insert(val2[mid+t]);
        //mp2[val2[mid+t]]+=maxd[cycle[mid+t]].second;
        max1=(*s1.rbegin());
        //max2=(*s2.rbegin());
        cnt=mp1[max1]*maxd[cycle[mid]].second;
        dist=max1+mid+maxd[cycle[mid]].first;
        //cout<<dist<<" "<<cnt<<endl;
        if(dist>maxdist1.first)
        {
            maxdist1.first=dist;
            maxdist1.second=0;
        }
        if(dist==maxdist1.first)
        {
            maxdist1.second+=cnt;
        }
        /*cnt=mp2[max2]*maxd[cycle[mid]].second;
        dist=max2-mid+maxd[cycle[mid]].first;
        cout<<dist<<" "<<cnt<<endl;
        if(dist>maxdist.first)
        {
            maxdist.first=dist;
            maxdist.second=0;
        }
        if(dist==maxdist.first)
        {
            maxdist.second+=cnt;
        }
        */
        /*cout<<"s1:\n";
        for(auto xd:s1)cout<<xd<<" ";
        cout<<endl;*/
    }
    //cout<<maxdist.first<<" "<<maxdist.second<<endl;
    //ans=maxdist.second;
    /*if(cycle_size%2==0)
    {
        for(ll i=0;i<t;i++)
        {
            ll j=i+t;
            ll dist=maxd[cycle[i]].first+t+maxd[cycle[j]].first;
            if(dist==maxdist.first)
            {
                ans+=maxd[cycle[i]].second*maxd[cycle[j]].second;
            }
        }
    }*/
    //cout<<ans<<endl;
    memset(vis,0,sizeof(vis));
    for(ll i=0;i<cycle_size;i++)vis[cycle[i]]=1;
    for(ll i=0;i<cycle_size;i++)
    {
        dfs1(cycle[i]);
    }
    //cout<<maxdist.first<<" ";
    memset(vis,0,sizeof(vis));
    for(ll i=0;i<cycle_size;i++)vis[cycle[i]]=1;
    for(ll i=0;i<cycle_size;i++)
    {
        dfs1000(cycle[i],cycle[i]);
    }
    pair<ll,ll>maxdist3,maxdist4;
    maxdist3={0,0};
    maxdist4={0,0};
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            //cout<<i<<" "<<j<<" "<<w[i][j]<<" "<<ww[i][j]<<endl;
            if(which1[i]!=which1[j])
            {
                if(w[i][j]>maxdist3.first)
                {
                    maxdist3={w[i][j],0};
                }
                if(w[i][j]==maxdist3.first)
                {
                    maxdist3.second+=ww[i][j];
                }
            }
            else
            {
                if(w[i][j]>maxdist4.first)
                {
                    maxdist4={w[i][j],0};
                }
                if(w[i][j]==maxdist4.first)
                {
                    maxdist4.second+=ww[i][j];
                }
            }
        }
    }
    pair<ll,ll> maxdist=maxdist3;
    if(maxdist4.first>maxdist.first)maxdist={maxdist4.first,0};
    if(maxdist4.first==maxdist.first)maxdist.second+=maxdist4.second;
    if(maxdist4.first!=maxdist2.first)assert(false);
    cout<<maxdist.second<<endl;
return 0;
}
/*
8
1 2
2 3
2 4
2 5
2 8
3 6
6 4
4 7
*/

Compilation message

shymbulak.cpp: In function 'void dfs1(long long int)':
shymbulak.cpp:64:25: 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]
   64 |             for(ll i=0;i<farthest.size();i++)
      |                        ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5196 KB Output is correct
2 Correct 3 ms 5196 KB Output is correct
3 Runtime error 9 ms 10504 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 30 ms 20428 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1585 ms 14108 KB Time limit exceeded
2 Halted 0 ms 0 KB -