QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#523333 | #8212. Football in Osijek | UNos_maricones# | RE | 7ms | 29580kb | C++14 | 2.9kb | 2024-08-18 07:32:22 | 2024-08-18 07:32:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
const int N = 1e6;
const int mod = 1e9 + 7;
const int oo = N + 5;
typedef pair<int,int> pii;
typedef long long ll;
vector <int> g[N];
int fat[N];
pii lson[N];
void dfs (int u) {
lson[u] = {1, u};
for (auto &v : g[u]) {
dfs(v);
lson[u] = max(lson[u], {lson[v].ff + 1, lson[v].ss});
}
}
int main() {
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
vector <int> a(n);
for (auto &e : a) cin >> e, e--;
vector <int> visi(n, 0);
vector <int> cyc(n, -1);
vector <vector <int>> sz(n);
vector <int> ans(n + 1, 0);
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (visi[i]) continue;
int u = i;
vector <int> seen;
while (!visi[u]) {
visi[u] = 1;
seen.pb(u);
u = a[u];
}
if (cyc[u] != -1) continue;
while (1) {
assert(seen.size());
cyc[seen.back()] = cnt;
if (seen.back() == u) break;
seen.pop_back();
}
cnt++;
}
for (int i = 0; i < n; ++i) {
if (cyc[i] == -1) continue;
sz[cyc[i]].pb(i);
}
for (int i = 0; i < n; ++i) {
if (cyc[i] == -1) {
fat[i] = a[i];
g[a[i]].pb(i);
}
}
for (int i = 0; i < n; ++i) {
if (cyc[i] != -1) {
dfs(i);
}
}
for (int i = 0; i < cnt; ++i) {
pii lrg = {-1,-1};
for (auto &j : sz[i]) {
lrg = max(lrg, {lson[j].ff, j});
}
int u = lrg.ss;
while (a[u] != lrg.ss) {
fat[u] = a[u];
g[a[u]].pb(u);
cyc[u] = -1;
u = a[u];
}
}
priority_queue <pii> pq;
vector <int> nzer(n + 1, 0);
for (int i = 0; i < n; ++i) {
if (cyc[i] != -1) {
dfs(i);
pq.push({lson[i].ff, i});
nzer[(int)sz[cyc[i]].size()]++;
nzer[lson[i].ff + 1]--;
}
}
for (int i = 1; i <= n; ++i) nzer[i] += nzer[i - 1];
int curr_size = 0;
int curr_ans = 0;
for (int i = 1; i <= n; ++i) {
while (curr_size < i) {
//cout << "=> " << curr_size << endl;
pii curr = pq.top(); pq.pop();
curr_size += curr.ff;
int son = lson[curr.ss].ss;
int u = fat[son];
if (son != curr.ss) {
int it = 0;
while (1) {
it++;
assert(it <= curr.ff);
//cout << u << '\n';
for (auto &v : g[u]) {
if (v == son) continue;
pq.push({lson[v].ff, v});
}
if (u == curr.ss) break;
son = u;
u = fat[u];
}
}
curr_ans++;
}
ans[i] = curr_ans - 1;
}
for (int i = 1; i <= n; ++i) {
if (ans[i] == 0 && nzer[i] == 0) cout << 1 << " ";
else cout << ans[i] << " ";
}
cout << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 29580kb
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: 6ms
memory: 27184kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 7ms
memory: 27028kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Runtime Error
input:
10 4 7 2 8 4 3 4 9 7 3