QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#810010 | #7216. Boxes on tree | eggegg185 | WA | 1ms | 4028kb | C++14 | 1023b | 2024-12-11 19:06:02 | 2024-12-11 19:06:13 |
Judging History
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;
}
详细
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'