QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#770940 | #8212. Football in Osijek | tassei903 | TL | 1ms | 3584kb | C++23 | 1.8kb | 2024-11-22 03:49:23 | 2024-11-22 03:49:24 |
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);
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);
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);
}
// rep(i, sz(order)) cout << order[i] << " ";
// cout << endl;
for (int i : order | views :: reverse) {
dp[i] = dp[p[i]] + 1;
}
// rep(i, n) cout << dp[i] << " " ;
// cout << endl;
vector<bool> flag(n+1, 0);
int m = 0;
rep(i, n)flag[dp[i]] = true, m = max(m, dp[i]);
rep(i, 1, n+1) {
if (i > 1) cout << " ";
if (flag[i]) cout << 0;
else if (i <= m) cout << 1;
else cout << i - m;
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// int T;cin >> T;
while(T--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
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: 0ms
memory: 3560kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3528kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Time Limit Exceeded
input:
10 4 7 2 8 4 3 4 9 7 3