QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#713108#5455. TreeScriptWzyWA 1ms9736kbC++14966b2024-11-05 18:06:332024-11-05 18:06:34

Judging History

你现在查看的是最新测评结果

  • [2024-11-05 18:06:34]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:9736kb
  • [2024-11-05 18:06:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef  long long LL;
typedef pair<int,int> PII;
const int N=1e5+10,M=1e6+10;
const int mod=1e9+7;
int INF = 1e9;
int p[N];
int h[N],ne[M],e[M],idx;
int d[N];
LL res;

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int u){
    int t=0;
    int mx1=1,mx2=0;
    for(int i=h[u];~i;i=ne[i]){
        int j=e[i];
        t++;
        dfs(j);
        if(d[j]>mx1) mx2=mx1,mx1=d[j];
        else if(d[j]>mx2) mx2=d[j]; 
    }


    d[u]=max(mx1,mx2+1);
}
 
void solve(){
    res=0;
    int n;
    cin>>n;
    for(int i=0;i<=n;i++) h[i]=-1;
    idx=0;

    for(int i=1;i<=n;i++) {
        int p;
        cin>>p;
        add(p,i);
    }

    dfs(1);

    //cout<<d[4]<<endl;
    cout<<d[1]<<endl;
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T=1;
    cin>>T;;
    while(T--) solve();
 
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 9736kb

input:

2
3
0 1 2
7
0 1 2 2 1 4 1

output:

2
2

result:

wrong answer 1st numbers differ - expected: '1', found: '2'