QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#578023 | #8212. Football in Osijek | ucup-team3519# | WA | 1ms | 5868kb | C++20 | 2.6kb | 2024-09-20 16:03:22 | 2024-09-20 16:03:23 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define V vector
#define pb push_back
const int MN = 5e5 + 10;
int f[MN], siz[MN];
int par(int x) {
if(x == f[x]) return x;
return f[x] = par(f[x]);
}
void mer(int a, int b) {
a = par(a), b = par(b);
if(a == b) return;
f[a] = b;
siz[b] += siz[a];
}
void ini(int n) {
for(int i = 1; i <= n; i++) f[i] = i, siz[i] = 1;
}
void solve() {
int n; cin >> n;
V<int> a(n + 1);
V<V<int>> e(n + 1);
ini(n);
for(int i = 1; i <= n; i++) cin >> a[i], mer(i, a[i]), e[a[i]].pb(i);
V<int> vis(n + 1);
V<int> lim(n + 1);
V<int> on_cyc(n + 1);
V<int> longest(n + 1);
int aim;
auto cal_cyc = [&](int x, int now, auto dfs) -> int {
if(vis[x]) {
aim = vis[x];
return now - vis[x];
}
vis[x] = now;
int t = dfs(a[x], now + 1, dfs);
if(now >= aim) on_cyc[x] = 1;
return t;
};
V<int> hav(n + 1);
auto cal_len = [&](int x, auto dfs) -> int {
int ans = !on_cyc[x];
for(auto y : e[x]) {
if(!on_cyc[y]) ans += dfs(y, dfs);
}
return ans;
};
for(int i = 1; i <= n; i++) if(par(i) == i) lim[i] = cal_cyc(i, 1, cal_cyc);
for(int i = 1; i <= n; i++) {
if(on_cyc[i]) {
for(auto y : e[i]) {
if(on_cyc[y]) continue;
int len = cal_len(y, cal_len);
// cout << " i : " << y << " " << len << endl;
if(longest[par(i)] < len) {
hav[longest[par(i)]]++;
longest[par(i)] = len;
}
else hav[len]++;
}
}
}
V<int> szs, ins(n + 1);
for(int i = 1; i <= n; i++) if(par(i) == i) {
for(int j = lim[i]; j <= lim[i] + longest[i]; j++) ins[j] = 1;
szs.pb(lim[i] + longest[i]);
}
for(int i = 1; i <= n; i++) {
while(hav[i]) szs.pb(i), hav[i]--;
}
sort(szs.begin(), szs.end(), greater<int>());
// for(auto x : szs) cout << x << " ";
// cout << endl;
for(int i = 1; i < szs.size(); i++) szs[i] += szs[i - 1];
assert(szs.back() == n);
for(int i = 1; i <= n; i++) {
if(ins[i]) cout << 0 << " ";
else {
cout << max(1, int(lower_bound(szs.begin(), szs.end(), i) - szs.begin())) << " ";
}
}
cout << endl;
//cyc size size...
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
// int t; cin >> t;
int t = 1;
while(t--) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5804kb
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: 1ms
memory: 5868kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5800kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 5540kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 0 0 0 1 2
result:
wrong answer 8th numbers differ - expected: '1', found: '0'