QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#810010#7216. Boxes on treeeggegg185WA 1ms4028kbC++141023b2024-12-11 19:06:022024-12-11 19:06:13

Judging History

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

  • [2024-12-11 19:06:13]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4028kb
  • [2024-12-11 19:06:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int n,p[5005],fa[5005],deg[5005]; vector<int> vec[5005];
void dfs(int now,int pa) {
	fa[now] = pa; for(auto v:vec[now]) {
		if(v == pa) continue; dfs(v,now);
	}
} int pa[5005]; int find(int x) {return (x==pa[x])?(x):(pa[x]=find(pa[x]));}
int solve() {
	cin >> n; for(int i = 1; i <= n; i++) cin >> p[i],pa[i] = i,deg[i] = 0;
	for(int i = 1; i <= n; i++) pa[find(p[i])] = find(i);
	for(int i = 1; i < n; i++) {
		int u,v; cin >> u >> v;
		vec[u].push_back(v); vec[v].push_back(u);
	} int ans = 0;
	for(int i = 1; i <= n; i++) {
		dfs(i,0); int x = p[i]; ans--;
		while(x) {ans++; if(find(x) != find(i)) deg[find(x)]++; x = fa[x];}
	} for(int i = 1; i <= n; i++) vec[i].clear(),ans += 2*(pa[i]==i)*(!deg[i])*(p[i]!=i);
	printf("%d\n",ans-2*(!deg[find(1)])*(p[1]!=1));
	return 0;
}
int main() {
	//freopen("classic.in","r",stdin); freopen("classic.out","w",stdout);
	cin.tie(0)->sync_with_stdio(false);
	int T; 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: 4028kb

input:

3
1 3 2
1 2
2 3

output:

0
0
0

result:

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