QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#320182 | #8212. Football in Osijek | ucup-team1516# | WA | 1ms | 3884kb | C++17 | 2.7kb | 2024-02-03 14:25:44 | 2024-02-03 14:25:44 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
inline double time() {
return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> a(n);
vector<int> deg(n);
for (int i = 0; i < n; ++i) {
cin >> a[i]; a[i] -= 1;
deg[a[i]] += 1;
}
vector<bool> used(n);
vector<int> nam(n);
{
queue<int> q;
for (int i = 0; i < n; ++i) {
if (deg[i] == 0) {
q.push(i);
nam[i] = 1;
used[i] = true;
}
}
while (q.size()) {
int s = q.front(); q.pop();
nam[s] = true;
deg[a[s]] -= 1;
nam[a[s]] += nam[s];
if (deg[a[s]] == 0) {
used[a[s]] = true;
nam[a[s]] += 1;
q.push(a[s]);
}
}
}
vector<pair<int,int>> ren;
for (int i = 0; i < n; ++i) {
if (used[i]) continue;
int x = i;
used[x] = true;
vector<int> v = {x};
x = a[x];
int sz = 1;
int al = 1 + nam[x];
while (1) {
if (used[x]) {
reverse(v.begin(), v.end());
for (int j = 0; j < v.size(); ++j) {
if (v[j] == x) break;
sz += 1;
}
break;
}
else {
v.push_back(x);
used[x] = true;
al += 1 + nam[x];
x = a[x];
}
}
ren.push_back({sz, al});
}
vector<int> res(n+1, -1);
int mx = 0;
for (auto &p : ren) {
mx = max(mx, p.first);
for (int i = p.first; i <= p.second; ++i) {
res[i] = 0;
}
}
for (int i = 0; i < mx; ++i) {
if (res[i] == -1) res[i] = 1;
}
sort(ren.begin(), ren.end(), [&](auto i, auto j) {
return i.second > j.second;
});
int sz = ren[0].second;
for (int i = 1; i < ren.size(); ++i) {
int nxt = sz + ren[i].second;
for (int j = sz; j <= nxt; ++j) {
if (res[j] == -1) {
res[j] = i;
}
}
sz = nxt;
}
for (int i = 0; i < n; ++i) {
cout << res[i+1] << " ";
}
cout << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3640kb
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: 3884kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3876kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3848kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 -1 -1 -1 -1 -1
result:
wrong answer 6th numbers differ - expected: '0', found: '-1'