mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 19:48:51 +00:00
2049. [SDOI2008]Cave 洞穴勘测
https://hydro.ac/d/bzoj/record/63b2479c246ae133e6cb8da4
This commit is contained in:
parent
9a3be113d0
commit
bbb3cb135a
165
BZOJ/2049/2049.cpp
Normal file
165
BZOJ/2049/2049.cpp
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <stack>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using std::cin;
|
||||||
|
using std::cout;
|
||||||
|
const char endl = '\n';
|
||||||
|
|
||||||
|
const int N = 1e4 + 5;
|
||||||
|
|
||||||
|
class LinkCutTree {
|
||||||
|
private:
|
||||||
|
struct node {
|
||||||
|
size_t l, r, f;
|
||||||
|
bool rev;
|
||||||
|
|
||||||
|
node(const size_t &_f = 0)
|
||||||
|
: l(0), r(0), f(_f), rev(false) {}
|
||||||
|
|
||||||
|
size_t &child(unsigned x) {
|
||||||
|
return !x ? l : r;
|
||||||
|
}
|
||||||
|
} tr[N];
|
||||||
|
|
||||||
|
inline void pushdown(const size_t &u) {
|
||||||
|
if (!tr[u].rev) return;
|
||||||
|
|
||||||
|
std::swap(tr[u].l, tr[u].r);
|
||||||
|
tr[tr[u].l].rev = !tr[tr[u].l].rev;
|
||||||
|
tr[tr[u].r].rev = !tr[tr[u].r].rev;
|
||||||
|
tr[u].rev = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned relation(const size_t &u) {
|
||||||
|
return u == tr[tr[u].f].l ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isRoot(const size_t &u) {
|
||||||
|
return tr[tr[u].f].l != u && tr[tr[u].f].r != u;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate(size_t u) {
|
||||||
|
size_t p = tr[u].f;
|
||||||
|
unsigned x = relation(u);
|
||||||
|
|
||||||
|
if (!isRoot(p)) {
|
||||||
|
tr[tr[p].f].child(relation(p)) = u;
|
||||||
|
}
|
||||||
|
tr[u].f = tr[p].f;
|
||||||
|
|
||||||
|
if (tr[u].child(x ^ 1)) {
|
||||||
|
tr[tr[u].child(x ^ 1)].f = p;
|
||||||
|
}
|
||||||
|
tr[p].child(x) = tr[u].child(x ^ 1);
|
||||||
|
|
||||||
|
tr[u].child(x ^ 1) = p;
|
||||||
|
tr[p].f = u;
|
||||||
|
}
|
||||||
|
|
||||||
|
void splay(size_t u) {
|
||||||
|
std::stack<size_t> st;
|
||||||
|
|
||||||
|
size_t cur = u;
|
||||||
|
st.push(cur);
|
||||||
|
while (!isRoot(cur)) {
|
||||||
|
st.push(tr[cur].f);
|
||||||
|
cur = tr[cur].f;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!st.empty()) {
|
||||||
|
pushdown(st.top());
|
||||||
|
st.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!isRoot(u)) {
|
||||||
|
if (isRoot(tr[u].f)) {
|
||||||
|
rotate(u);
|
||||||
|
} else if (relation(u) == relation(tr[u].f)) {
|
||||||
|
rotate(tr[u].f);
|
||||||
|
rotate(u);
|
||||||
|
} else {
|
||||||
|
rotate(u);
|
||||||
|
rotate(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void access(size_t u) {
|
||||||
|
for (size_t f = 0; u; u = tr[f = u].f) {
|
||||||
|
splay(u);
|
||||||
|
tr[u].r = f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void makeRoot(const size_t &u) {
|
||||||
|
access(u);
|
||||||
|
splay(u);
|
||||||
|
tr[u].rev = !tr[u].rev;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t findRoot(size_t u) {
|
||||||
|
access(u);
|
||||||
|
splay(u);
|
||||||
|
|
||||||
|
while (tr[u].l) {
|
||||||
|
u = tr[u].l;
|
||||||
|
}
|
||||||
|
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
|
||||||
|
void split(const size_t &x, const size_t &y) {
|
||||||
|
makeRoot(x);
|
||||||
|
access(y);
|
||||||
|
splay(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool check(int x, int y) {
|
||||||
|
return findRoot(x) == findRoot(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void link(const int &x, const int &y) {
|
||||||
|
makeRoot(x);
|
||||||
|
|
||||||
|
if (findRoot(y) != x) {
|
||||||
|
tr[x].f = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cut(int x, int y) {
|
||||||
|
split(x, y);
|
||||||
|
|
||||||
|
if (tr[y].l == x) {
|
||||||
|
tr[y].l = 0;
|
||||||
|
tr[x].f = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} lct;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
int n, m;
|
||||||
|
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
while (m--) {
|
||||||
|
std::string op;
|
||||||
|
int u, v;
|
||||||
|
|
||||||
|
cin >> op >> u >> v;
|
||||||
|
|
||||||
|
if (op == "Connect") {
|
||||||
|
lct.link(u, v);
|
||||||
|
} else if (op == "Destroy") {
|
||||||
|
lct.cut(u, v);
|
||||||
|
} else { // op == "Query"
|
||||||
|
cout << (lct.check(u, v) ? "Yes" : "No") << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
BZOJ/2049/data/1.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/1.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/10.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/10.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/2.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/2.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/3.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/3.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/4.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/4.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/5.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/5.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/6.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/6.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/7.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/7.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/8.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/8.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/9.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2049/data/9.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2049/data/9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user