답안 #476878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476878 2021-09-28T19:20:08 Z ogibogi2004 관광지 (IZhO14_shymbulak) C++14
100 / 100
277 ms 25864 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)
{
    //cout<<u<<endl;
    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);
            }
            ansu.second/=2;
        }
        else
        {
            ansu.second=mpmp[farthest[0].first]*mpmp[farthest[1].first];
        }
    }
    else if(farthest.size()>=1)
    {
        ansu.first=farthest[0].first;
        ansu.second=mpmp[farthest[0].first];
    }
    else
    {
        ansu.first=0;
        ansu.second=1;
    }
    //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;
    memset(vis,0,sizeof(vis));
    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(int i=1;i<=n;i++)
    {
        cout<<maxd[i].first<<","<<maxd[i].second<<" ";
    }
    cout<<endl;*/
    for(ll i=0;i<cycle_size*2;i++)
    {
        val1[i]=-i+maxd[cycle[i]].first;
        val2[i]=i+maxd[cycle[i]].first;
       // cout<<val1[i]<<" ";
    }
    //cout<<endl;
    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=maxdist1;
    if(maxdist2.first>maxdist.first)maxdist={maxdist2.first,0};
    if(maxdist2.first==maxdist.first)maxdist.second+=maxdist2.second;
    //cout<<maxdist1.first<<" "<<maxdist1.second<<endl;
    //cout<<maxdist3.first<<" "<<maxdist3.second<<endl;
    //if(maxdist2.first==maxdist.first&&maxdist2!=maxdist4)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:65: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]
   65 |             for(ll i=0;i<farthest.size();i++)
      |                        ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5196 KB Output is correct
2 Correct 2 ms 5196 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 3 ms 5196 KB Output is correct
7 Correct 3 ms 5196 KB Output is correct
8 Correct 3 ms 5196 KB Output is correct
9 Correct 3 ms 5196 KB Output is correct
10 Correct 3 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 3 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 3 ms 5268 KB Output is correct
15 Correct 3 ms 5196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5396 KB Output is correct
2 Correct 5 ms 5196 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 7 ms 5580 KB Output is correct
6 Correct 7 ms 5836 KB Output is correct
7 Correct 7 ms 5580 KB Output is correct
8 Correct 9 ms 5548 KB Output is correct
9 Correct 7 ms 5560 KB Output is correct
10 Correct 9 ms 5552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 12232 KB Output is correct
2 Correct 122 ms 12356 KB Output is correct
3 Correct 120 ms 21132 KB Output is correct
4 Correct 92 ms 12056 KB Output is correct
5 Correct 98 ms 12116 KB Output is correct
6 Correct 277 ms 18556 KB Output is correct
7 Correct 193 ms 15216 KB Output is correct
8 Correct 207 ms 18768 KB Output is correct
9 Correct 213 ms 18976 KB Output is correct
10 Correct 193 ms 20152 KB Output is correct
11 Correct 174 ms 22928 KB Output is correct
12 Correct 203 ms 25864 KB Output is correct