답안 #292754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
292754 2020-09-07T12:53:58 Z 최은수(#5797) 서류 전달 (ROI16_sending) C++17
12 / 100
114 ms 37752 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);
            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;)
    {
        int ldep=dep[i]-1;
        for(const auto&t:mp[i])
        {
            if((int)t.se.size()<2)
                continue;
            const vector<int>&v=t.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);
            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 14 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 13 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 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 14 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 13 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 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 14 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 13 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 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 14 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 13 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 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 105 ms 32324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 31608 KB Output is correct
2 Correct 103 ms 31864 KB Output is correct
3 Correct 102 ms 37752 KB Output is correct
4 Correct 111 ms 35448 KB Output is correct
5 Correct 104 ms 33912 KB Output is correct
6 Correct 62 ms 30324 KB Output is correct
7 Correct 101 ms 37752 KB Output is correct
8 Correct 57 ms 30584 KB Output is correct
9 Correct 75 ms 31576 KB Output is correct
10 Correct 71 ms 36852 KB Output is correct
11 Correct 61 ms 37112 KB Output is correct
12 Correct 54 ms 37112 KB Output is correct
13 Correct 114 ms 37752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 13 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 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 14 ms 19200 KB Output is correct
2 Correct 13 ms 19200 KB Output is correct
3 Correct 13 ms 19200 KB Output is correct
4 Correct 14 ms 19200 KB Output is correct
5 Correct 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -