QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#320839 | #8212. Football in Osijek | ucup-team635# | WA | 0ms | 2112kb | Rust | 6.4kb | 2024-02-03 22:18:09 | 2024-02-03 22:18:09 |
Judging History
answer
use std::io::Write;
use std::collections::*;
type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;
fn main() {
input! {
n: usize,
a: [usize1; n],
}
let mut dsu = DSU::new(n);
for i in 0..n {
dsu.unite(i, a[i]);
}
let mut cycle = vec![0; n];
let mut hige = vec![0; n];
#[derive(Clone, Copy, Eq, PartialEq)]
enum State {
No,
Cycle,
Hige(usize),
On,
}
let mut state = vec![State::No; n];
for i in 0..n {
if state[i] != State::No {
continue;
}
let mut pos = i;
let mut stack = vec![];
while state[pos] == State::No {
stack.push(pos);
state[pos] = State::On;
pos = a[pos];
}
if state[pos] == State::On {
let s = pos;
let mut len = 1;
pos = a[pos];
while pos != s {
pos = a[pos];
len += 1;
}
cycle[dsu.root(i)] = len;
for _ in 0..len {
state[pos] = State::Cycle;
pos = a[pos];
stack.pop();
}
}
for &s in stack.iter().rev() {
let d = if let State::Hige(x) = state[a[s]] {
x + 1
} else {
1
};
state[s] = State::Hige(d);
hige[s] = hige[s].max(d);
}
}
let inf = n + 2;
let mut ans = vec![inf; n + 1];
let mut max_c = 0;
for i in 0..n {
if i == dsu.root(i) {
let c = cycle[i];
let h = hige[i];
ans[c..=(c + h)].fill(0);
max_c = max_c.max(c);
}
}
for i in 1..=max_c {
ans[i] = ans[i].min(1);
}
let mut ord = (0..n).collect::<Vec<_>>();
ord.sort_by_key(|x| {
match state[*x] {
State::Hige(d) => d,
_ => 0,
}
});
let mut used = vec![false; n];
let mut data = vec![];
for &v in ord.iter().rev() {
let mut val = 0;
let mut pos = v;
while !used[pos] {
val += 1;
used[pos] = true;
pos = a[pos];
}
if val > 0 {
data.push(val);
}
}
data.sort();
let mut sum = 0;
for (i, v) in data.iter().rev().enumerate() {
sum += *v;
for x in (1..=sum).rev() {
if ans[x] != inf {
break;
}
ans[x] = i;
}
}
use util::*;
println!("{}", ans[1..].iter().join(" "));
}
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}
#[macro_export]
macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}
#[macro_export]
macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
// ---------- end input macro ----------
//---------- begin union_find ----------
pub struct DSU {
p: Vec<i32>,
}
impl DSU {
pub fn new(n: usize) -> DSU {
assert!(n < std::i32::MAX as usize);
DSU { p: vec![-1; n] }
}
pub fn init(&mut self) {
self.p.iter_mut().for_each(|p| *p = -1);
}
pub fn root(&self, mut x: usize) -> usize {
assert!(x < self.p.len());
while self.p[x] >= 0 {
x = self.p[x] as usize;
}
x
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.p.len() && y < self.p.len());
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.p.len() && y < self.p.len());
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
std::mem::swap(&mut x, &mut y);
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
pub fn parent(&self, x: usize) -> Option<usize> {
assert!(x < self.p.len());
let p = self.p[x];
if p >= 0 {
Some(p as usize)
} else {
None
}
}
pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
where
F: FnMut(usize),
{
while let Some(p) = self.parent(x) {
f(x);
x = p;
}
x
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.p.len());
let r = self.root(x);
(-self.p[r]) as usize
}
}
//---------- end union_find ----------
mod util {
pub trait Join {
fn join(self, sep: &str) -> String;
}
impl<T, I> Join for I
where
I: Iterator<Item = T>,
T: std::fmt::Display,
{
fn join(self, sep: &str) -> String {
let mut s = String::new();
use std::fmt::*;
for (i, v) in self.enumerate() {
if i > 0 {
write!(&mut s, "{}", sep).ok();
}
write!(&mut s, "{}", v).ok();
}
s
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 2112kb
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: 2056kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 2060kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 2096kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 0 0 1 2 3
result:
ok 10 numbers
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 2108kb
input:
10 8 7 4 8 3 4 2 5 3 7
output:
1 0 1 0 0 1 1 1 2 3
result:
wrong answer 3rd numbers differ - expected: '0', found: '1'