QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#770954 | #8212. Football in Osijek | tassei903 | WA | 1ms | 3524kb | C++23 | 3.2kb | 2024-11-22 04:39:06 | 2024-11-22 04:39:06 |
Judging History
answer
#include <bits/stdc++.h>
#define rep1(n) for(int i = 0; i < (int)n; i++)
#define rep2(i, n) for(int i = 0; i < (int)n; i++)
#define rep3(i, l, r) for(int i = (int)l; i < (int)r; i++)
#define overloadrep(a, b, c, x, ...) x
#define rep(...) overloadrep(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
using namespace std;
using ll = long long;
using vi = vector<int>;
void solve() {
int n;cin >> n;
vector<int> p(n);for (auto &x: p) cin >> x, x--;
vector<int> deg(n, 0);
vector<vi> rev(n);
rep(i, n)deg[p[i]]++;
queue<int> q;
rep(i, n)if (deg[i] == 0) q.push(i);
vector<int> dp(n, 0);
vector<int> order;
while (!q.empty()) {
int x = q.front();q.pop();
order.emplace_back(x);
rev[p[x]].emplace_back(x);
dp[x] = -1;
deg[p[x]]--;
if (deg[p[x]] == 0)q.push(p[x]);
}
rep(i, n) {
if (dp[i] != 0) continue;
int cnt = 0, x = i;
do {
cnt++;
x = p[x];
} while (x != i);
do {
dp[x] = cnt;
}while (x != i);
}
vector<bool> flag(n+1);
for (int i : order | views :: reverse) {
dp[i] = dp[p[i]] + 1;
flag[dp[i]] = true;
}
for (int i : order | views :: reverse) {
dp[i] = -1;
}
vector<int> res;
auto dfs = [&](auto self, int v) -> int {
// cout << v + 1 << endl;
vector<int> f;
for (int u: rev[v]) {
f.emplace_back(self(self, u) + 1);
if (f.back() > f.front()) swap(f.back(), f.front());
}
// cout << v + 1 << " :";
for (int x : f | views::drop(1)) res.emplace_back(x);
// for (int x : f) cout << x << " ";
// cout << endl;
if (sz(f)) return f.front();
return 0;
};
rep(i, n) if (dp[i] != -1) {
// cout << i + 1 << " " << dp[i] << endl;
vector<int> f;
int v = i;
int y = dp[i];
do {
f.emplace_back(dfs(dfs, v));
if (f.back() > f.front()) swap(f.back(), f.front());
flag[dp[v]] = true;
dp[v] = -1;
v = p[v];
} while (v != i);
for (int x : f | views::drop(1)) if (x) res.emplace_back(x);
// for (int x : f) cout << x << " ";
// cout << endl;
res.emplace_back(f.front() + y);
}
// rep(i, n) {
// cout << i + 1 << " : ";
// for (int x : rev[i])cout << x+1 << " ";
// cout << endl;
// }
// for (int x : res) cout << x << " ";
// cout << endl;
sort(all(res));
reverse(all(res));
int x = 0;
rep(i, sz(res)) {
rep(j, res[i]) {
dp[x + j] = i;
}
x += res[i];
}
rep(i, n) {
if (i) cout << " ";
if (flag[i+1]) cout << 0;
else cout << dp[i] + 1;
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// int T;cin >> T;
while(T--) {
solve();
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3524kb
input:
5 2 3 1 3 5
output:
0 1 0 0 2
result:
wrong answer 5th numbers differ - expected: '1', found: '2'