QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#350244 | #8212. Football in Osijek | willow# | WA | 4ms | 24332kb | C++17 | 2.0kb | 2024-03-10 15:50:14 | 2024-03-10 15:50:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, tot, stk[maxn], tp, vis[maxn], bel[maxn], sz[maxn], a[maxn];
void Dfs(int u) {
stk[++ tp] = u;
vis[u] = 1;
if(!vis[a[u]])
Dfs(a[u]);
else if(!bel[a[u]]) {
++ tot;
do {
int v = stk[tp --];
bel[v] = tot;
++ sz[tot];
if(v == a[u])
break;
}while(1);
}
if(stk[tp] == u) {
bel[u] = ++ tot;
sz[tot] = 1;
-- tp;
}
}
vector<int> G[maxn], len;
int dep[maxn], fa[maxn], son[maxn], dif[maxn], ans[maxn], mx;
void Solve(int u) {
dep[u] = dep[fa[u]] + 1;
son[u] = u;
for(auto v : G[u]) {
Solve(v);
if(dep[son[u]] < dep[v])
son[u] = v;
}
}
void Add(int u) {
len.push_back(dep[son[u]] - dep[u] + sz[u]);
for(auto v : G[u])
if(v != son[u])
Add(v);
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++ i) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; ++ i) {
if(!vis[i]) {
Dfs(i);
}
}
for(int i = 1; i <= n; ++ i) {
if(bel[i] != bel[a[i]]) {
G[bel[a[i]]].push_back(bel[i]);
fa[bel[i]] = bel[a[i]];
}
}
for(int i = 1; i <= tot; ++ i) {
if(!fa[i]) {
Solve(i);
mx = max(mx, dep[son[i]] - dep[i] + sz[i]);
++ dif[sz[i]];
-- dif[sz[i] + dep[son[i]] - dep[i] + 1];
Add(i);
}
}
sort(len.begin(), len.end(), greater<int>());
int now = len[0];
for(int i = 1; i < (int)len.size(); ++ i) {
for(int j = now + 1; j <= now + len[i]; ++ j)
ans[j] = i;
now += len[i];
}
for(int i = 1; i <= n; ++ i) {
dif[i] += dif[i - 1];
if(i <= mx && !dif[i])
ans[i] = 1;
printf("%d ", ans[i]);
}
puts("");
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 24332kb
input:
5 2 3 1 3 5
output:
0 1 0 0 1
result:
ok 5 number(s): "0 1 0 0 1"
Test #2:
score: 0
Accepted
time: 4ms
memory: 22396kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 22384kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 22272kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 1 1 2 0 0
result:
wrong answer 6th numbers differ - expected: '0', found: '1'