QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#187457#5455. TreeScriptCokeWA 9ms9632kbC++141.3kb2023-09-24 17:31:212023-09-24 17:31:22

Judging History

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

  • [2023-09-24 17:31:22]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:9632kb
  • [2023-09-24 17:31:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define debug(x) cerr<<#x<<' '<<x<<' ';
typedef pair<int, int>PII;
typedef long long LL;
constexpr int N = 200010, mod = 1e9 + 7;
int a[N];
vector<int>v[N];
int cnt[N];
void dfs(int u,int fa)
{
    int f=0,ma=0;
    if(v[u].size()==0)
    {
        cnt[u]=1;
        return ;
    }
    int d1=0,d2=0;
    for(auto c:v[u])
    {
        if(c==fa)continue;
        f++;
        dfs(c,u);
        if(cnt[c]>d1)
        {
            d2=d1;
            d1=cnt[c];
        }
        else if(cnt[c]>d2)d2=cnt[c];

    }
    if(u==1)
    {
        if(f>=2)
        cnt[u]=d1+1;
        else cnt[u]=d1+1;
        return ;
    }
    if(f>=2)
    {
        if(d2+1>=d1)cnt[u]=d2+1;
        else
        cnt[u]=d1;
    }
    else cnt[u]=d1;
}
signed main()
{
    //freopen(".in","r",stdin);freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for(int i=0;i<=n;i++)v[i].clear(),cnt[i]=0;
        for(int i = 1; i <= n; i++)
        {
            int x ;
            cin>>x;
            v[x].push_back(i);
        }
        dfs(1,0);
        cout<<cnt[1]-1<<endl;
    }

}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 9532kb

input:

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

output:

1
2

result:

ok 2 number(s): "1 2"

Test #2:

score: -100
Wrong Answer
time: 9ms
memory: 9632kb

input:

1000
197
0 1 1 2 1 4 1 5 8 3 5 1 4 7 12 14 4 7 10 9 12 11 16 10 21 19 22 17 25 13 28 9 5 15 26 26 33 25 15 1 35 6 32 17 37 8 19 43 19 27 29 9 30 6 31 27 35 35 37 13 28 38 57 31 38 8 22 14 33 9 18 62 52 37 10 19 22 60 54 12 38 59 64 65 80 82 28 60 85 78 27 25 71 14 52 6 59 14 87 32 33 41 59 41 88 38 ...

output:

3
4
2
4
3
3
4
5
3
4
4
4
5
4
3
3
4
5
3
5
5
3
5
4
5
4
5
3
4
4
1
5
3
3
5
4
3
3
3
4
4
3
3
2
3
3
5
4
4
4
3
2
4
4
5
4
3
4
4
4
5
4
3
4
4
3
3
4
4
4
4
4
4
3
4
5
2
3
4
1
1
5
5
3
3
4
4
5
4
5
4
4
4
4
3
3
4
4
4
3
4
4
4
5
4
5
2
2
4
3
4
3
3
3
4
3
2
2
4
4
3
5
2
5
3
4
3
3
5
4
5
3
5
3
4
2
5
4
3
3
2
3
3
1
5
4
3
2
4
5
...

result:

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