Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ∧_∧
- ( ・ω・。)つ━☆・*。
- ⊂ ノ ・゜
- しーJ Accepted
- */
- // #pragma GCC optimize("O3")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ll long long
- #define all(x) begin(x), end(x)
- #define x first
- #define y second
- #define int long long
- using namespace std;
- using namespace __gnu_pbds;
- typedef long double ld;
- template<typename T>
- using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- const ld PI = atan2(0, -1);
- void seriy() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout << fixed << setprecision(14);
- #ifdef _offline
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- freopen("river.in", "r", stdin);
- freopen("river.out", "w", stdout);
- #endif
- }
- const int MAXN = 1e6 + 10;
- const int MAXM = 600;
- const int INF = 1e18;
- const int BASE = 47;
- const int MOD = 1e9 + 7;
- const int MAXLOG = 21;
- mt19937 rr(time(0));
- struct treap {
- treap *left = nullptr;
- treap *right = nullptr;
- treap *parent = nullptr;
- int prior, sz, val;
- bool reversed = 0, cycle = 0, is_right = 0;
- treap(int val) {
- this->val = val;
- this->prior = rr();
- this->sz = 1;
- }
- };
- vector<vector<int>> g;
- vector<treap*> links;
- int get_size(treap *&root) {
- return ((root == nullptr) ? 0 : root->sz);
- }
- int get_val(treap *&root) {
- return ((root == nullptr) ? 0 : root->val);
- }
- void print(treap *a) {
- if(a->left != nullptr) print(a->left);
- cerr << get_val(a) << " ";
- if(a->right != nullptr) print(a->right);
- }
- void update(treap *&root) {
- if(root == nullptr) return;
- root->sz = get_size(root->left) + get_size(root->right) + 1;
- if(root->left != nullptr) {
- root->left->parent = root;
- root->left->is_right = 0;
- }
- if(root->right != nullptr) {
- root->right->parent = root;
- root->right->is_right = 1;
- }
- }
- void push(treap *&root) {
- if(root == nullptr || root->reversed == 0) {
- return;
- }
- if(root->left != nullptr) {
- root->left->reversed ^= true;
- }
- if(root->right != nullptr) {
- root->right->reversed ^= true;
- }
- swap(root->left, root->right);
- root->reversed = 0;
- }
- pair<treap*, treap*> split(treap *&root, int key) {
- push(root);
- if(root == nullptr) {
- return {nullptr, nullptr};
- }
- int root_key = get_size(root->left);
- // cerr << root->sz << " " << root_key << '\n';
- if(key > root_key) {
- pair<treap*, treap*> splitted = split(root->right, key - get_size(root->left) - 1);
- root->right = splitted.x;
- update(root);
- return {root, splitted.y};
- }
- else {
- pair<treap*, treap*> splitted = split(root->left, key);
- root->left = splitted.y;
- update(root);
- return {splitted.x, root};
- }
- }
- treap* merge(treap *&left, treap *&right) {
- push(left);
- push(right);
- if(left == nullptr) {
- return right;
- }
- if(right == nullptr) {
- return left;
- }
- if(left->prior > right->prior) {
- left->right = merge(left->right, right);
- update(left);
- return left;
- }
- else {
- right->left = merge(left, right->left);
- update(right);
- return right;
- }
- }
- int get(treap *&root, int pos) {
- if(pos < get_size(root->left)) {
- return get(root->left, pos);
- }
- else if(pos > get_size(root->left)) {
- return get(root->right, pos - get_size(root->left) - 1);
- }
- else {
- return get_val(root);
- }
- }
- void reverse(treap *&root, int l, int r) {
- pair<treap*, treap*> a = split(root, r);
- pair<treap*, treap*> b = split(a.x, l);
- b.y->reversed = 1;
- b.x = merge(b.x, b.y);
- root = merge(b.x, a.y);
- }
- void insert(treap *&root, int pos, int val) {
- pair<treap*, treap*> splitted = split(root, pos);
- treap *nw = new treap(val);
- splitted.x = merge(splitted.x, nw);
- update(splitted.x);
- splitted.x = merge(splitted.x, splitted.y);
- update(splitted.x);
- root = splitted.x;
- }
- vector<vector<int>> bamboos, cycles;
- vector<bool> used;
- vector<int> cur;
- bool is_cyc = 0;
- void dfs(int u) {
- used[u] = 1;
- cur.push_back(u);
- for(auto v : g[u]) {
- if(!used[v]) {
- dfs(v);
- }
- else {
- is_cyc = 1;
- }
- }
- }
- treap* get_root(treap *&a) {
- if(a->parent == nullptr) {
- return a;
- }
- return get_root(a->parent);
- }
- int get_pos(treap *&a, bool from_right) {
- if(a == nullptr) {
- return 0;
- }
- // cerr << a->val << " ";
- int ans = 0;
- if(from_right) {
- ans += get_size(a->left);
- }
- return ans + get_pos(a->parent, a->is_right);
- }
- signed main() {
- seriy();
- int n, m, q;
- cin >> n >> m >> q;
- links.resize(n);
- g.resize(n);
- used.resize(n);
- for(int i = 0; i < m; i++) {
- int u, v;
- cin >> u >> v;
- u--;
- v--;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- for(int i = 0; i < n; i++) {
- if(!used[i] && g[i].size() <= 1) {
- cur.clear();
- dfs(i);
- bamboos.push_back(cur);
- }
- }
- for(int i = 0; i < n; i++) {
- if(!used[i]) {
- cur.clear();
- dfs(i);
- cycles.push_back(cur);
- }
- }
- for(int i = 0; i < n; i++) {
- links[i] = new treap(i);
- }
- for(int i = 0; i < bamboos.size(); i++) {
- treap *start = links[bamboos[i][0]];
- for(int j = 1; j < bamboos[i].size(); j++) {
- int cur = bamboos[i][j];
- insert(start, j, cur);
- }
- }
- for(int i = 0; i < cycles.size(); i++) {
- for(int j = 1; j < cycles[i].size(); j++) {
- int start = cycles[i][0];
- int cur = cycles[i][j];
- treap *parent = get_root(links[start]);
- links[start] = merge(parent, links[cur]);
- cerr << cur << " " << links[cur]->val << '\n';
- }
- treap *root = get_root(links[cycles[i][0]]);
- root->cycle = 1;
- }
- cerr << '\n';
- while(q--) {
- char c;
- cin >> c;
- if(c == '?') {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- if(get_root(links[a]) == get_root(links[b])) {
- int p1 = get_pos(links[a], 1);
- cerr << a << " " << links[a]->val << '\n';
- // cerr << '\n';
- int p2 = get_pos(links[b], 1);
- // cerr << '\n';
- // cerr << p1 << " " << p2 << '\n';
- }
- else {
- cout << -1 << '\n';
- }
- }
- else if(c == '+') {
- }
- else if(c == '-') {
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement