Submission #1985104


Source Code Expand

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
vector<int>g[110000];
int segtree[524288];
int query(int a,int b,int c,int d,int e){
    if(d<a||b<c)return 999999999;
    if(c<=a&&b<=d)return segtree[e];
    return min(query(a,(a+b)/2,c,d,e*2),query((a+b)/2+1,b,c,d,e*2+1));
}
void update(int a,int b){
    a+=262144;
    while(a){
        segtree[a]=min(segtree[a],b);
        a/=2;
    }
}
int rev[110000];
int num;
int sz;
int at[110000];
int e[300000];
int bfs[110000];
int left[110000];
void dfs(int a){
    at[a]=num;
    rev[num]=a;
    left[a]=sz;
    e[sz++]=num;
    num++;
    for(int i=0;i<g[a].size();i++){
        dfs(g[a][i]);
        e[sz++]=at[a];
    }
}
int main(){
    int a;scanf("%d",&a);
    for(int i=1;i<a;i++){
        int p;scanf("%d",&p);p--;
        g[p].push_back(i);
    }
    for(int i=0;i<524288;i++)segtree[i]=999999999;
    dfs(0);
    for(int i=0;i<sz;i++)update(i,e[i]);
    //for(int i=0;i<a;i++)printf("%d ",left[i]);printf("\n");
    //for(int i=0;i<a;i++)printf("%d ",at[i]);printf("\n");
     
    queue<int>Q;
    Q.push(0);
    long long ret=0;
    int last=0;
    while(Q.size()){
        int At=Q.front();Q.pop();
        ret+=bfs[At]+bfs[last]-2*bfs[rev[query(0,262143,min(left[At],left[last]),max(left[At],left[last]),1)]];
        last=At;
        for(int i=0;i<g[At].size();i++){
            bfs[g[At][i]]=bfs[At]+1;
            Q.push(g[At][i]);
        }
    }
    printf("%lld\n",ret);
}

Submission Info

Submission Time
Task A - Breadth-First Search by Foxpower
User tozangezan
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1547 Byte
Status AC
Exec Time 57 ms
Memory 15104 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:39:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int a;scanf("%d",&a);
                         ^
./Main.cpp:41:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         int p;scanf("%d",&p);p--;
                             ^

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 61
Set Name Test Cases
All 00_sample_00, 00_sample_01, 00_sample_02, 10_Random_000005_00, 10_Random_000006_04, 10_Random_000008_08, 10_Random_000033_05, 10_Random_000042_01, 10_Random_000080_09, 10_Random_000358_10, 10_Random_000463_06, 10_Random_000822_02, 10_Random_007468_03, 10_Random_013086_07, 10_Random_096889_11, 10_Random_100000_12, 10_Random_100000_13, 20_Binary_000032_00, 20_Binary_001000_01, 20_Binary_010000_02, 20_Binary_100000_03, 30_Star_100000_00, 30_Star_100000_01, 40_Skew_000023_09, 40_Skew_000031_01, 40_Skew_000043_05, 40_Skew_000044_08, 40_Skew_000046_04, 40_Skew_000047_00, 40_Skew_000354_02, 40_Skew_000539_07, 40_Skew_000814_10, 40_Skew_000853_11, 40_Skew_000935_03, 40_Skew_000951_06, 40_Skew_100000_12, 40_Skew_100000_13, 40_Skew_100000_14, 40_Skew_100000_15, 40_Skew_100000_16, 40_Skew_100000_17, 40_Skew_100000_18, 40_Skew_100000_19, 40_Skew_100000_20, 40_Skew_100000_21, 50_PowerRandom_000047_00, 50_PowerRandom_000068_04, 50_PowerRandom_000094_08, 50_PowerRandom_000126_09, 50_PowerRandom_000497_05, 50_PowerRandom_000852_01, 50_PowerRandom_008600_02, 50_PowerRandom_015225_10, 50_PowerRandom_040917_06, 50_PowerRandom_100000_03, 50_PowerRandom_100000_07, 50_PowerRandom_100000_11, 80_line_00, 81_max_ans_00, 90_teuch_00, 90_teuch_01
Case Name Status Exec Time Memory
00_sample_00 AC 3 ms 5760 KB
00_sample_01 AC 3 ms 5760 KB
00_sample_02 AC 3 ms 5760 KB
10_Random_000005_00 AC 3 ms 5760 KB
10_Random_000006_04 AC 3 ms 5760 KB
10_Random_000008_08 AC 3 ms 5760 KB
10_Random_000033_05 AC 3 ms 5760 KB
10_Random_000042_01 AC 3 ms 5760 KB
10_Random_000080_09 AC 3 ms 5760 KB
10_Random_000358_10 AC 3 ms 5760 KB
10_Random_000463_06 AC 3 ms 5760 KB
10_Random_000822_02 AC 3 ms 5760 KB
10_Random_007468_03 AC 6 ms 6016 KB
10_Random_013086_07 AC 9 ms 6144 KB
10_Random_096889_11 AC 52 ms 9088 KB
10_Random_100000_12 AC 53 ms 9216 KB
10_Random_100000_13 AC 52 ms 9216 KB
20_Binary_000032_00 AC 3 ms 5760 KB
20_Binary_001000_01 AC 3 ms 5760 KB
20_Binary_010000_02 AC 7 ms 6016 KB
20_Binary_100000_03 AC 47 ms 8960 KB
30_Star_100000_00 AC 34 ms 8056 KB
30_Star_100000_01 AC 34 ms 8056 KB
40_Skew_000023_09 AC 3 ms 5760 KB
40_Skew_000031_01 AC 3 ms 5760 KB
40_Skew_000043_05 AC 2 ms 5760 KB
40_Skew_000044_08 AC 2 ms 5760 KB
40_Skew_000046_04 AC 2 ms 5760 KB
40_Skew_000047_00 AC 2 ms 5760 KB
40_Skew_000354_02 AC 2 ms 5760 KB
40_Skew_000539_07 AC 2 ms 5760 KB
40_Skew_000814_10 AC 3 ms 5760 KB
40_Skew_000853_11 AC 3 ms 5760 KB
40_Skew_000935_03 AC 3 ms 5760 KB
40_Skew_000951_06 AC 3 ms 5760 KB
40_Skew_100000_12 AC 45 ms 14336 KB
40_Skew_100000_13 AC 55 ms 11520 KB
40_Skew_100000_14 AC 51 ms 9088 KB
40_Skew_100000_15 AC 51 ms 9088 KB
40_Skew_100000_16 AC 49 ms 9216 KB
40_Skew_100000_17 AC 48 ms 9088 KB
40_Skew_100000_18 AC 45 ms 8576 KB
40_Skew_100000_19 AC 40 ms 8192 KB
40_Skew_100000_20 AC 51 ms 8960 KB
40_Skew_100000_21 AC 57 ms 10240 KB
50_PowerRandom_000047_00 AC 3 ms 5760 KB
50_PowerRandom_000068_04 AC 3 ms 5760 KB
50_PowerRandom_000094_08 AC 3 ms 5760 KB
50_PowerRandom_000126_09 AC 3 ms 5760 KB
50_PowerRandom_000497_05 AC 3 ms 5760 KB
50_PowerRandom_000852_01 AC 3 ms 5760 KB
50_PowerRandom_008600_02 AC 7 ms 6016 KB
50_PowerRandom_015225_10 AC 9 ms 6144 KB
50_PowerRandom_040917_06 AC 21 ms 6784 KB
50_PowerRandom_100000_03 AC 50 ms 8704 KB
50_PowerRandom_100000_07 AC 48 ms 8320 KB
50_PowerRandom_100000_11 AC 47 ms 8192 KB
80_line_00 AC 44 ms 15104 KB
81_max_ans_00 AC 53 ms 12800 KB
90_teuch_00 AC 3 ms 5760 KB
90_teuch_01 AC 3 ms 5760 KB