QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#638924 | #8212. Football in Osijek | Djangle162857# | WA | 0ms | 3824kb | C++23 | 3.0kb | 2024-10-13 17:20:36 | 2024-10-13 17:20:36 |
Judging History
answer
#define LOCAL
#include <bits/stdc++.h>
#define fir first
#define sec second
#define el '\n'
#ifdef LOCAL
#define FINISH cerr << "FINISH" << endl;
#else
#define FINISH ;
#endif
#ifdef LOCAL
#define debug(x) cerr << setw(4) << #x << " == " << x << endl
#else
#define debug(x)
#endif
#ifdef LOCAL
#define debugv(x) \
cerr << setw(4) << #x << ":: "; \
for (auto i : x) \
cerr << i << " "; \
cerr << endl
#else
#define debugv(x)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ostream& operator<<(ostream& out, PII& x)
{
out << x.fir << " " << x.sec << endl;
return out;
}
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 200020;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1, 0);
vector<int> inp(n + 1, 0);
vector<int> col(n + 1, 0);
vector<int> ans(n + 1, inf);
vector<int> dep(n + 1, 0);
vector<int> siz(1, 0);
vector<vector<int>> edge(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
edge[i].push_back(a[i]);
inp[a[i]]++;
}
vector<int> v;
for (int i = 1; i <= n; i++) {
if (inp[i] == 0)
v.push_back(i);
}
int cnt = 0;
auto dfs = [&](auto& self, int x) {
cnt++;
int tot = 0;
vector<int> res;
while (col[x] == 0) {
col[x] = cnt;
res.push_back(x);
x = a[x];
}
if (col[x] == cnt) { // 找到环
for (int i = res.size() - 1; i >= 0; i--) {
dep[res[i]] = 0;
if (res[i] == x) {
for (int j = i - 1; j >= 0; j--) {
dep[res[j]] = dep[res[j + 1]] + 1;
}
break;
}
}
}
else {
dep[res[res.size() - 1]] = dep[x] + 1;
for (int i = res.size() - 2; i >= 0; i--) {
dep[res[i]] = dep[res[i + 1]] + 1;
}
}
};
for (int i = 1; i <= n; i++) {
if (inp[i] == 0) {
dfs(dfs, i);
ans[dep[i]] = 0;
}
}
for (int i = 1; i <= n; i++) {
if (col[i] == 0) {
cnt++;
int x = i;
int tot = 0;
while (col[x] == 0) {
col[x] = cnt;
x = a[x];
tot++;
}
ans[tot] = 0;
siz.push_back(tot);
}
}
sort(v.begin(), v.end(), [&](int x, int y) { return dep[x] > dep[y]; });
int ct = 0;
vector<int> vis(n + 1, 0);
for (int i = 0; i < v.size(); i++) {
int x = v[i];
int tot = 0;
vector<int> r;
while (vis[x] == 0) {
vis[x] = cnt;
r.push_back(x);
x = a[x];
tot++;
}
siz.push_back(tot);
for (int i = 0; i < r.size(); i++) {
ans[tot] = 0;
tot--;
if (r[i] == x)
break;
}
}
sort(next(siz.begin()), siz.end());
vector<int> nans(n + 1, 0);
for (int i = 1; i < siz.size(); i++) {
siz[i] += siz[i - 1];
nans[siz[i]] = 1;
}
for (int i = 1; i <= n; i++) {
nans[i] += nans[i - 1];
}
for (int i = 1; i <= n; i++) {
cout << min(nans[i], ans[i]) << " ";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3824kb
input:
5 2 3 1 3 5
output:
0 1 0 0 2
result:
wrong answer 5th numbers differ - expected: '1', found: '2'