-
Notifications
You must be signed in to change notification settings - Fork 0
/
P15978.rs
206 lines (191 loc) · 6.14 KB
/
P15978.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Author : quickn (quickn.ga)
Email : [email protected]
*/
use std::str;
use std::io::{self, BufWriter, Write};
/* https://github.com/EbTech/rust-algorithms */
/// Same API as Scanner but nearly twice as fast, using horribly unsafe dark arts
/// **REQUIRES** Rust 1.34 or higher
pub struct UnsafeScanner<R> {
reader: R,
buf_str: Vec<u8>,
buf_iter: str::SplitAsciiWhitespace<'static>,
}
impl<R: io::BufRead> UnsafeScanner<R> {
pub fn new(reader: R) -> Self {
Self {
reader,
buf_str: Vec::new(),
buf_iter: "".split_ascii_whitespace(),
}
}
/// This function should be marked unsafe, but noone has time for that in a
/// programming contest. Use at your own risk!
pub fn token<T: str::FromStr>(&mut self) -> T {
loop {
if let Some(token) = self.buf_iter.next() {
return token.parse().ok().expect("Failed parse");
}
self.buf_str.clear();
self.reader
.read_until(b'\n', &mut self.buf_str)
.expect("Failed read");
self.buf_iter = unsafe {
let slice = str::from_utf8_unchecked(&self.buf_str);
std::mem::transmute(slice.split_ascii_whitespace())
}
}
}
}
#[derive(Clone)]
struct Tree {
adj: Vec<Vec<u32>>,
res: Vec<u32>,
res2: Vec<u32>,
}
impl Tree {
fn new(adj: Vec<Vec<u32>>) -> Self {
let res: Vec<u32> = vec![0;adj.len()-1];
let res2: Vec<u32> = vec![0;adj.len()-1];
Self {
adj,
res,
res2,
}
}
fn dfs(&mut self, idx: u32) -> u32 {
if self.adj[idx as usize].len() == 0 {
self.res[(idx as usize)-1] = 1;
self.res2[(idx as usize)-1] = idx;
return idx;
}
let mut res: u32 = 0;
for u in self.adj[idx as usize].clone() {
self.res2[(idx as usize)-1] = self.dfs(u);
res += self.res[(u as usize)-1];
}
self.res[(idx as usize)-1] = res;
self.res2[(idx as usize)-1]
}
}
const MAX: usize = 3_000_000;
struct UnionFind {
pi: Box<[u32;MAX]>,
cnt: Box<[u32;MAX]>,
depth: Box<[u32;MAX]>,
}
use std::alloc::{alloc, Layout};
impl UnionFind {
fn new(k: usize) -> Self {
let layout1 = Layout::new::<[u32; MAX]>();
let layout2 = Layout::new::<[u32; MAX]>();
let layout3 = Layout::new::<[u32; MAX]>();
let mut pi = unsafe {
let ptr = alloc(layout1) as *mut [u32; MAX];
Box::from_raw(ptr)
};
let mut cnt = unsafe {
let ptr = alloc(layout2) as *mut [u32; MAX];
Box::from_raw(ptr)
};
let mut depth = unsafe {
let ptr = alloc(layout3) as *mut [u32; MAX];
Box::from_raw(ptr)
};
for i in 0..k {
pi[i] = i as u32;
cnt[i] = 1;
depth[i] = 0;
}
Self {
pi,
cnt,
depth,
}
}
fn root(&self, n: u32) -> u32 {
let mut res = n;
while self.pi[res as usize] != res {
res = self.pi[res as usize];
}
res
}
fn union(&mut self, n: u32, m: u32) {
let (n_t, m_t) = (self.root(n), self.root(m));
if n_t != m_t {
if self.depth[n_t as usize] < self.depth[m_t as usize] {
self.pi[n_t as usize] = m_t as u32;
self.cnt[m_t as usize] += self.cnt[n_t as usize];
} else if self.depth[n_t as usize] > self.depth[m_t as usize] {
self.pi[m_t as usize] = n_t as u32;
self.cnt[n_t as usize] += self.cnt[m_t as usize];
} else {
self.pi[m_t as usize] = n_t as u32;
self.depth[n_t as usize] += 1;
self.cnt[n_t as usize] += self.cnt[m_t as usize];
}
}
}
}
fn main() {
let (stdin, stdout) = (io::stdin(), io::stdout());
let (mut scan, mut sout) = (UnsafeScanner::new(stdin.lock()), BufWriter::new(stdout.lock()));
let (n1, n2, k): (usize, usize, usize) = (scan.token(), scan.token(), scan.token());
let mut adj1: Vec<Vec<u32>> = vec![vec![];n1+1];
let mut adj2: Vec<Vec<u32>> = vec![vec![];n2+1];
for i in 1..=n1 {
let u: usize = scan.token();
adj1[u].push(i as u32);
}
for i in 1..=n2 {
let u: usize = scan.token();
adj2[u].push(i as u32);
}
let mut uf: UnionFind = UnionFind::new(k);
let (mut tree1, mut tree2) = (Tree::new(adj1.clone()), Tree::new(adj2.clone()));
tree1.dfs(adj1[0][0]);
tree2.dfs(adj2[0][0]);
let mut t1: Vec<(u32, u32, u32)> = tree1.res.iter().enumerate().map(|(i, set)| (0, i as u32, *set)).collect();
let mut t2: Vec<(u32, u32, u32)> = tree2.res.iter().enumerate().map(|(i, set)| (1, i as u32, *set)).collect();
t1.append(&mut t2);
t1.sort_by(|(_i, _j, set), (_i2, _j2, set2)| set.cmp(&set2));
let mut res = true;
for (kind, idx, set) in t1 {
match kind {
0 => {
if adj1[(idx as usize)+1].len() == 0 {
continue;
}
let t = tree1.res2[(adj1[(idx as usize)+1][0] as usize)-1];
for &u in &adj1[(idx as usize)+1] {
let v = tree1.res2[(u as usize)-1];
uf.union(t-1, v-1);
}
if uf.cnt[uf.root(t-1) as usize] > set {
res = false;
break;
}
},
_ => {
if adj2[(idx as usize)+1].len() == 0 {
continue;
}
let t = tree2.res2[(adj2[(idx as usize)+1][0] as usize)-1];
for &u in &adj2[(idx as usize)+1] {
let v = tree2.res2[(u as usize)-1];
uf.union(t-1, v-1);
}
if uf.cnt[uf.root(t-1) as usize] > set {
res = false;
break;
}
}
}
}
if res {
writeln!(sout, "YES").ok();
} else {
writeln!(sout, "NO").ok();
}
}