Submission #292762

# Submission time Handle Problem Language Result Execution time Memory
292762 2020-09-07T13:02:13 Z 최은수(#5797) ROI16_sending (ROI16_sending) C++17
12 / 100
109 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);
            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;)
    {
        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);
            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;
}
# Verdict Execution time Memory 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 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 32292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 102 ms 31608 KB Output is correct
2 Correct 109 ms 31864 KB Output is correct
3 Correct 101 ms 37752 KB Output is correct
4 Correct 97 ms 35448 KB Output is correct
5 Correct 94 ms 33912 KB Output is correct
6 Correct 53 ms 30452 KB Output is correct
7 Correct 97 ms 37752 KB Output is correct
8 Correct 65 ms 30584 KB Output is correct
9 Correct 71 ms 31512 KB Output is correct
10 Correct 59 ms 36852 KB Output is correct
11 Correct 53 ms 36984 KB Output is correct
12 Correct 53 ms 37112 KB Output is correct
13 Correct 93 ms 37752 KB Output is correct
# Verdict Execution time Memory 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 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 13 ms 19200 KB Output is correct
6 Incorrect 13 ms 19200 KB Output isn't correct
7 Halted 0 ms 0 KB -