Submission #292786

# Submission time Handle Problem Language Result Execution time Memory
292786 2020-09-07T13:26:32 Z 최은수(#5797) ROI16_sending (ROI16_sending) C++17
68 / 100
5000 ms 37776 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;
            int cn=v.size();
            for(int i=0;i<cn;i++)
            {
                for(int j=i+1;j<cn;j++)
                {
                    int cur=dep[lca(qry[v[i]].fi,qry[v[j]].fi)]-ldep
                    +dep[lca(qry[v[i]].se,qry[v[j]].se)]-ldep;
                    if(cur>ansv)
                        ansv=cur,ansi=v[i],ansj=v[j];
                }
            }
                        
        }
    }
    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 15 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 13 ms 19200 KB Output is correct
10 Correct 13 ms 19200 KB Output is correct
11 Correct 13 ms 19232 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 13 ms 19200 KB Output is correct
14 Correct 13 ms 19200 KB Output is correct
15 Correct 13 ms 19200 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 13 ms 19200 KB Output is correct
19 Correct 14 ms 19200 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 15 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 13 ms 19200 KB Output is correct
10 Correct 13 ms 19200 KB Output is correct
11 Correct 13 ms 19232 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 13 ms 19200 KB Output is correct
14 Correct 13 ms 19200 KB Output is correct
15 Correct 13 ms 19200 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 13 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 21 ms 19712 KB Output is correct
21 Correct 28 ms 19704 KB Output is correct
22 Correct 15 ms 19840 KB Output is correct
23 Correct 21 ms 19840 KB Output is correct
24 Correct 15 ms 19712 KB Output is correct
25 Correct 16 ms 19712 KB Output is correct
26 Correct 15 ms 19840 KB Output is correct
27 Correct 19 ms 19584 KB Output is correct
28 Correct 34 ms 19704 KB Output is correct
29 Correct 15 ms 19840 KB Output is correct
30 Correct 15 ms 19840 KB Output is correct
31 Correct 14 ms 19840 KB Output is correct
32 Correct 16 ms 19840 KB Output is correct
33 Correct 75 ms 19704 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 15 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 13 ms 19200 KB Output is correct
10 Correct 13 ms 19200 KB Output is correct
11 Correct 13 ms 19232 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 13 ms 19200 KB Output is correct
14 Correct 13 ms 19200 KB Output is correct
15 Correct 13 ms 19200 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 13 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 21 ms 19712 KB Output is correct
21 Correct 28 ms 19704 KB Output is correct
22 Correct 15 ms 19840 KB Output is correct
23 Correct 21 ms 19840 KB Output is correct
24 Correct 15 ms 19712 KB Output is correct
25 Correct 16 ms 19712 KB Output is correct
26 Correct 15 ms 19840 KB Output is correct
27 Correct 19 ms 19584 KB Output is correct
28 Correct 34 ms 19704 KB Output is correct
29 Correct 15 ms 19840 KB Output is correct
30 Correct 15 ms 19840 KB Output is correct
31 Correct 14 ms 19840 KB Output is correct
32 Correct 16 ms 19840 KB Output is correct
33 Correct 75 ms 19704 KB Output is correct
34 Correct 88 ms 29920 KB Output is correct
35 Correct 93 ms 30072 KB Output is correct
36 Correct 49 ms 35960 KB Output is correct
37 Correct 86 ms 33656 KB Output is correct
38 Correct 48 ms 32120 KB Output is correct
39 Correct 32 ms 28788 KB Output is correct
40 Correct 46 ms 36008 KB Output is correct
41 Correct 37 ms 28792 KB Output is correct
42 Correct 60 ms 29816 KB Output is correct
43 Correct 54 ms 35192 KB Output is correct
44 Correct 57 ms 35192 KB Output is correct
45 Correct 53 ms 35192 KB Output is correct
46 Correct 55 ms 35320 KB Output is correct
47 Correct 152 ms 30840 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 15 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 13 ms 19200 KB Output is correct
10 Correct 13 ms 19200 KB Output is correct
11 Correct 13 ms 19232 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 13 ms 19200 KB Output is correct
14 Correct 13 ms 19200 KB Output is correct
15 Correct 13 ms 19200 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 13 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 21 ms 19712 KB Output is correct
21 Correct 28 ms 19704 KB Output is correct
22 Correct 15 ms 19840 KB Output is correct
23 Correct 21 ms 19840 KB Output is correct
24 Correct 15 ms 19712 KB Output is correct
25 Correct 16 ms 19712 KB Output is correct
26 Correct 15 ms 19840 KB Output is correct
27 Correct 19 ms 19584 KB Output is correct
28 Correct 34 ms 19704 KB Output is correct
29 Correct 15 ms 19840 KB Output is correct
30 Correct 15 ms 19840 KB Output is correct
31 Correct 14 ms 19840 KB Output is correct
32 Correct 16 ms 19840 KB Output is correct
33 Correct 75 ms 19704 KB Output is correct
34 Correct 88 ms 29920 KB Output is correct
35 Correct 93 ms 30072 KB Output is correct
36 Correct 49 ms 35960 KB Output is correct
37 Correct 86 ms 33656 KB Output is correct
38 Correct 48 ms 32120 KB Output is correct
39 Correct 32 ms 28788 KB Output is correct
40 Correct 46 ms 36008 KB Output is correct
41 Correct 37 ms 28792 KB Output is correct
42 Correct 60 ms 29816 KB Output is correct
43 Correct 54 ms 35192 KB Output is correct
44 Correct 57 ms 35192 KB Output is correct
45 Correct 53 ms 35192 KB Output is correct
46 Correct 55 ms 35320 KB Output is correct
47 Correct 152 ms 30840 KB Output is correct
48 Correct 344 ms 30208 KB Output is correct
49 Correct 501 ms 30456 KB Output is correct
50 Correct 51 ms 36216 KB Output is correct
51 Correct 898 ms 33932 KB Output is correct
52 Correct 56 ms 32632 KB Output is correct
53 Correct 36 ms 29428 KB Output is correct
54 Correct 50 ms 36216 KB Output is correct
55 Correct 140 ms 29176 KB Output is correct
56 Correct 594 ms 30200 KB Output is correct
57 Correct 56 ms 35320 KB Output is correct
58 Correct 57 ms 35320 KB Output is correct
59 Correct 54 ms 35320 KB Output is correct
60 Correct 61 ms 35448 KB Output is correct
61 Correct 1977 ms 35120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5025 ms 30584 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 103 ms 31608 KB Output is correct
2 Correct 108 ms 31864 KB Output is correct
3 Correct 99 ms 37752 KB Output is correct
4 Correct 101 ms 35448 KB Output is correct
5 Correct 95 ms 33912 KB Output is correct
6 Correct 54 ms 30324 KB Output is correct
7 Correct 101 ms 37728 KB Output is correct
8 Correct 65 ms 30584 KB Output is correct
9 Correct 79 ms 31480 KB Output is correct
10 Correct 57 ms 36956 KB Output is correct
11 Correct 52 ms 37112 KB Output is correct
12 Correct 56 ms 37112 KB Output is correct
13 Correct 98 ms 37776 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 15 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 13 ms 19200 KB Output is correct
10 Correct 13 ms 19200 KB Output is correct
11 Correct 13 ms 19232 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 13 ms 19200 KB Output is correct
14 Correct 13 ms 19200 KB Output is correct
15 Correct 13 ms 19200 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 13 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 21 ms 19712 KB Output is correct
21 Correct 28 ms 19704 KB Output is correct
22 Correct 15 ms 19840 KB Output is correct
23 Correct 21 ms 19840 KB Output is correct
24 Correct 15 ms 19712 KB Output is correct
25 Correct 16 ms 19712 KB Output is correct
26 Correct 15 ms 19840 KB Output is correct
27 Correct 19 ms 19584 KB Output is correct
28 Correct 34 ms 19704 KB Output is correct
29 Correct 15 ms 19840 KB Output is correct
30 Correct 15 ms 19840 KB Output is correct
31 Correct 14 ms 19840 KB Output is correct
32 Correct 16 ms 19840 KB Output is correct
33 Correct 75 ms 19704 KB Output is correct
34 Correct 88 ms 29920 KB Output is correct
35 Correct 93 ms 30072 KB Output is correct
36 Correct 49 ms 35960 KB Output is correct
37 Correct 86 ms 33656 KB Output is correct
38 Correct 48 ms 32120 KB Output is correct
39 Correct 32 ms 28788 KB Output is correct
40 Correct 46 ms 36008 KB Output is correct
41 Correct 37 ms 28792 KB Output is correct
42 Correct 60 ms 29816 KB Output is correct
43 Correct 54 ms 35192 KB Output is correct
44 Correct 57 ms 35192 KB Output is correct
45 Correct 53 ms 35192 KB Output is correct
46 Correct 55 ms 35320 KB Output is correct
47 Correct 152 ms 30840 KB Output is correct
48 Correct 344 ms 30208 KB Output is correct
49 Correct 501 ms 30456 KB Output is correct
50 Correct 51 ms 36216 KB Output is correct
51 Correct 898 ms 33932 KB Output is correct
52 Correct 56 ms 32632 KB Output is correct
53 Correct 36 ms 29428 KB Output is correct
54 Correct 50 ms 36216 KB Output is correct
55 Correct 140 ms 29176 KB Output is correct
56 Correct 594 ms 30200 KB Output is correct
57 Correct 56 ms 35320 KB Output is correct
58 Correct 57 ms 35320 KB Output is correct
59 Correct 54 ms 35320 KB Output is correct
60 Correct 61 ms 35448 KB Output is correct
61 Correct 1977 ms 35120 KB Output is correct
62 Execution timed out 5025 ms 30584 KB Time limit exceeded
63 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 15 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 14 ms 19200 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 13 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 13 ms 19200 KB Output is correct
10 Correct 13 ms 19200 KB Output is correct
11 Correct 13 ms 19232 KB Output is correct
12 Correct 14 ms 19200 KB Output is correct
13 Correct 13 ms 19200 KB Output is correct
14 Correct 13 ms 19200 KB Output is correct
15 Correct 13 ms 19200 KB Output is correct
16 Correct 13 ms 19200 KB Output is correct
17 Correct 13 ms 19200 KB Output is correct
18 Correct 13 ms 19200 KB Output is correct
19 Correct 14 ms 19200 KB Output is correct
20 Correct 21 ms 19712 KB Output is correct
21 Correct 28 ms 19704 KB Output is correct
22 Correct 15 ms 19840 KB Output is correct
23 Correct 21 ms 19840 KB Output is correct
24 Correct 15 ms 19712 KB Output is correct
25 Correct 16 ms 19712 KB Output is correct
26 Correct 15 ms 19840 KB Output is correct
27 Correct 19 ms 19584 KB Output is correct
28 Correct 34 ms 19704 KB Output is correct
29 Correct 15 ms 19840 KB Output is correct
30 Correct 15 ms 19840 KB Output is correct
31 Correct 14 ms 19840 KB Output is correct
32 Correct 16 ms 19840 KB Output is correct
33 Correct 75 ms 19704 KB Output is correct
34 Correct 88 ms 29920 KB Output is correct
35 Correct 93 ms 30072 KB Output is correct
36 Correct 49 ms 35960 KB Output is correct
37 Correct 86 ms 33656 KB Output is correct
38 Correct 48 ms 32120 KB Output is correct
39 Correct 32 ms 28788 KB Output is correct
40 Correct 46 ms 36008 KB Output is correct
41 Correct 37 ms 28792 KB Output is correct
42 Correct 60 ms 29816 KB Output is correct
43 Correct 54 ms 35192 KB Output is correct
44 Correct 57 ms 35192 KB Output is correct
45 Correct 53 ms 35192 KB Output is correct
46 Correct 55 ms 35320 KB Output is correct
47 Correct 152 ms 30840 KB Output is correct
48 Correct 344 ms 30208 KB Output is correct
49 Correct 501 ms 30456 KB Output is correct
50 Correct 51 ms 36216 KB Output is correct
51 Correct 898 ms 33932 KB Output is correct
52 Correct 56 ms 32632 KB Output is correct
53 Correct 36 ms 29428 KB Output is correct
54 Correct 50 ms 36216 KB Output is correct
55 Correct 140 ms 29176 KB Output is correct
56 Correct 594 ms 30200 KB Output is correct
57 Correct 56 ms 35320 KB Output is correct
58 Correct 57 ms 35320 KB Output is correct
59 Correct 54 ms 35320 KB Output is correct
60 Correct 61 ms 35448 KB Output is correct
61 Correct 1977 ms 35120 KB Output is correct
62 Execution timed out 5025 ms 30584 KB Time limit exceeded
63 Halted 0 ms 0 KB -