답안 #292776

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
292776 2020-09-07T13:17:52 Z 최은수(#5797) 서류 전달 (ROI16_sending) C++17
12 / 100
170 ms 37880 KB
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e8+7;
const ll INF=1e18;
const int mx=200010;
int dep[mx];
int spa[mx][18];
inline int kpa(int x,int k)
{
    for(;k>0;k^=k&-k)
        x=spa[x][__builtin_ctz(k)];
    return x;
}
inline int lca(int x,int y)
{
    if(dep[x]>dep[y])
        x=kpa(x,dep[x]-dep[y]);
    else
        y=kpa(y,dep[y]-dep[x]);
    if(x==y)
        return x;
    for(int i=18;i-->0;)
        if(spa[x][i]!=spa[y][i])
            x=spa[x][i],y=spa[y][i];
    return spa[x][0];
}
vector<int>adj[mx];
void dfs(int x,int p)
{
    dep[x]=dep[spa[x][0]=p]+1;
    for(int i=0;i<17;i++)
        spa[x][i+1]=spa[spa[x][i]][i];
    for(int&t:adj[x])
        dfs(t,x);
    return;
}
map<int,vector<int> >mp[mx];
vector<int>vv[mx];
pi qry[mx];
int lc[mx];
int ansv=0,ansi=1,ansj=2;
pi mn1[mx],mn2[mx];
inline void upd(int x,pi v)
{
    if(v<mn1[x])
        mn2[x]=mn1[x],mn1[x]=v;
    else if(v<mn2[x])
        mn2[x]=v;
    return;
}
void dfs2(int x)
{
    mn1[x]=pi(inf,0);
    mn2[x]=pi(inf,0);
    for(int&t:vv[x])
        upd(x,pi(dep[lc[t]],t));
    for(int&t:adj[x])
        dfs2(t),upd(x,mn1[t]),upd(x,mn2[t]);
    if(dep[x]-mn2[x].fi>ansv)
        ansv=dep[x]-mn2[x].fi,ansi=mn1[x].se,ansj=mn2[x].se;
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,k;
    cin>>n>>k;
    for(int i=1;i++<n;)
    {
        int p;
        cin>>p;
        adj[p].eb(i);
    }
    dfs(1,0);
    for(int i=0;i++<k;)
    {
        cin>>qry[i].fi>>qry[i].se;
        const int&l=lc[i]=lca(qry[i].fi,qry[i].se);
        if(qry[i].fi!=l&&qry[i].se!=l)
        {
            int l1=kpa(qry[i].fi,dep[qry[i].fi]-dep[l]-1);
            int l2=kpa(qry[i].se,dep[qry[i].se]-dep[l]-1);
            if(l1>l2)
                swap(l1,l2),swap(qry[i].fi,qry[i].se);
            mp[l1][l2].eb(i);
        }
        if(qry[i].fi!=l)
            vv[qry[i].fi].eb(i);
        if(qry[i].se!=l)
            vv[qry[i].se].eb(i);
    }
    for(int i=0;i++<n;)
    {
        const int ldep=dep[i]-1;
        for(const auto&tv:mp[i])
        {
            if((int)tv.se.size()<2)
                continue;
            const vector<int>&v=tv.se;
            vector<pair<pi,int> >cv;
            for(const int&t:v)
                cv.eb(pi(dep[qry[t].fi]-ldep,dep[qry[t].se]-ldep),t);
            int cn=v.size();
            for(int i=0;i<cn;i++)
                for(int j=i+1;j<cn;j++)
                    if(min(cv[i].fi.fi,cv[j].fi.fi)+min(cv[i].fi.se,cv[j].fi.se)>ansv)
                        ansv=min(cv[i].fi.fi,cv[j].fi.fi)+min(cv[i].fi.se,cv[j].fi.se),
                        ansi=cv[i].se,ansj=cv[j].se;
            /*sort(all(cv));
            pi cmx=pi(cv.back().fi.se,cv.back().se);
            cv.pop_back();
            reverse(all(cv));
            for(auto&t:cv)
            {
                if(t.fi.fi+min(t.fi.se,cmx.fi)>ansv)
                    ansv=t.fi.fi+min(t.fi.se,cmx.fi),ansi=t.se,ansj=cmx.se;
                if(t.fi.se>cmx.fi)
                    cmx=pi(t.fi.se,t.se);
            }*/
        }
    }
    dfs2(1);
    cout<<ansv<<endl;
    cout<<ansi<<' '<<ansj<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 13 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 13 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 13 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 13 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 170 ms 32312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 31608 KB Output is correct
2 Correct 105 ms 31864 KB Output is correct
3 Correct 97 ms 37784 KB Output is correct
4 Correct 105 ms 35392 KB Output is correct
5 Correct 97 ms 33912 KB Output is correct
6 Correct 55 ms 30320 KB Output is correct
7 Correct 101 ms 37880 KB Output is correct
8 Correct 57 ms 30584 KB Output is correct
9 Correct 72 ms 31480 KB Output is correct
10 Correct 57 ms 36852 KB Output is correct
11 Correct 53 ms 37112 KB Output is correct
12 Correct 52 ms 36988 KB Output is correct
13 Correct 97 ms 37756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 13 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 13 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -