QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#240509#5455. TreeScriptucup-team1769#ML 0ms8072kbC++20763b2023-11-05 16:01:212023-11-05 16:01:21

Judging History

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

  • [2023-11-05 16:01:21]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:8072kb
  • [2023-11-05 16:01:21]
  • 提交

answer

#include<bits/stdc++.h>
#define endl "\n"
typedef long long ll;
const int maxn=2e5+5;
std::vector<int> g[maxn];
int n;
int ans[maxn];

int dfs(int fa,int u)
{
	ans[u]=1;
	for(auto v:g[u])
	{
		if(v==fa) continue;
		dfs(u,v);
		ans[u]=std::max(ans[u],ans[v]);
	}
	int tot=0;
	for(auto v:g[u])
	{
		if(v==fa) continue;
		if(ans[u]==ans[v]) ++tot;
	}
	if(tot>1) ++ans[u];
	return ans[u];
}

void solve()
{
	std::cin>>n;
	for(int u=1;u<=n;++u)
	{
		int v;
		std::cin>>v;
		g[u].push_back(v);
		g[v].push_back(u);
		// ans[u]=maxn;
	}
	dfs(-1,1);
	std::cout<<ans[1]-1<<endl;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int T=1;
	std::cin>>T;
	while(T--)
	{
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8072kb

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
Memory Limit Exceeded

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:


result: