답안 #477117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477117 2021-09-30T15:29:53 Z blue Joker (BOI20_joker) C++17
100 / 100
563 ms 19568 KB
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
using namespace std;

struct disjoint_set
{
    int N;
    vector<int> parent;
    vector<int> parent_edge; //the color of this node is the xor of path up to parent
    vector<int> subtree;

    vector<int> op_node = vector<int>(1, 0);
    vector<int> op_oldparent = vector<int>(1, 0);
    vector<int> op_oldxor = vector<int>(1, 0);
    vector<int> op_time = vector<int>(1, 0);
    vector<bool> bipartite = vector<bool>(1, 1);
    vector<bool> subtree_change = vector<bool>(1, 0);

    int curr_time = 0;

    disjoint_set()
    {
        ;
    }

    disjoint_set(int N_)
    {
        N = N_;
        parent = vector<int>(1+N);
        parent_edge = vector<int>(1+N);
        subtree = vector<int>(1+N);
        for(int i = 1; i <= N; i++)
        {
            parent[i] = i;
            parent_edge[i] = 0;
            subtree[i] = 1;
        }
    }

    void add_entry(int u, int v, int x, bool b, bool s)
    {
        op_node.push_back(u);
        op_oldparent.push_back(v);
        op_oldxor.push_back(x);
        op_time.push_back(curr_time);
        bipartite.push_back(b);
        subtree_change.push_back(s);
    }

    void remove_entry()
    {
        op_node.pop_back();
        op_oldparent.pop_back();
        op_oldxor.pop_back();
        op_time.pop_back();
        bipartite.pop_back();
        subtree_change.pop_back();
    }

    pair<int, int> root(int u) //first = root, second = xor of root and u
    {
        int v = u;
        int edg = 0;
        while(parent[v] != v)
        {
            edg ^= parent_edge[v];
            v = parent[v];
        }

        if(v == u || v == parent[u]) return make_pair(parent[u], edg);


        add_entry(u, parent[u], parent_edge[u], bipartite.back(), 0);

        parent[u] = v;
        parent_edge[u] = edg;

        return make_pair(parent[u], parent_edge[u]);
    }

    void join(int u, int v)
    {
        curr_time++;

        pair<int, int> U = root(u);
        pair<int, int> V = root(v);

        if(U.first == V.first)
        {
            op_node.push_back(0);
            op_oldparent.push_back(0);
            op_oldxor.push_back(0);
            op_time.push_back(curr_time);

            if(U.second == V.second)
            {
                bipartite.push_back(0);
            }
            else
            {
                bipartite.push_back(bipartite.back());
            }

            subtree_change.push_back(0);
        }
        else
        {
            // cerr << "case 2\n";
            if(subtree[U.first] < subtree[V.first]) swap(U, V);
            // cerr << U.first << ' ' << V.first << '\n';

            add_entry(V.first, parent[V.first], parent_edge[V.first], bipartite.back(), 1);

            parent[V.first] = U.first;
            parent_edge[V.first] = 1 ^ V.second ^ U.second;
            subtree[U.first] += subtree[V.first];

            // cerr << U.first << ' ' << V.first << ' ' << parent[V.first] << '\n';
        }
    }

    void rollback()
    {
        while(op_time.back() == curr_time)
        {
            int u = op_node.back();
            int v = op_oldparent.back();
            int x = op_oldxor.back();
            bool s = subtree_change.back();

            remove_entry();

            if(s) subtree[ parent[u] ] -= subtree[u];
            parent[u] = v;
            parent_edge[u] = x;
        }

        curr_time--;
    }

    bool is_bipartite()
    {
        return bipartite.back();
    }
};

const int maxN = 200'000;
const int maxM = 200'000;

int N, M, Q;

vector<int> u(1+maxM), v(1+maxM);

vector<int> minR(1+maxN, 0);
//the minimum value of R so that if the roads i, ... R are blocked the graph is bipartite

disjoint_set DSU;

int L;
int R;

void expand_right()
{
    R++;
    DSU.rollback();
}

void contract_right()
{
    R--;
    DSU.join(u[R+1], v[R+1]);
}

void expand_left()
{
    L--;
    DSU.rollback();
}

void contract_left()
{
    L++;
    DSU.join(u[L-1], v[L-1]);
}

void solve(int l1, int l2, int r1, int r2) // [L, R] == [l1, r2] at the beginning
{
    int lmid = (l1+l2)/2;
    int rmid;

    if(!DSU.is_bipartite())
    {
        for(int i = lmid; i <= l2; i++)
        {
            minR[i] = M+1;
        }
        if(l1 <= lmid-1)
        {
            solve(l1, lmid-1, r1, r2); //ADJUST L AND R!!!!! -> not necessary
        }
        return;
    }

    while(L < lmid)
        contract_left();
    if(!DSU.is_bipartite())
    {
        while(l1 < L)
            expand_left();
        for(int i = lmid; i <= l2; i++)
        {
            minR[i] = M+1;
        }
        if(l1 <= lmid-1)
        {
            solve(l1, lmid-1, r1, r2); //ADJUST L AND R!!!!! -> not necessary
        }
        return;
    }
    // if(F) cerr << "check\n";

    rmid = r2;
    while(DSU.is_bipartite())
    {
        rmid = min(rmid, R);
        contract_right();
    }

    minR[lmid] = rmid;

    while(R < r2)
        expand_right();
    while(l1 < L)
        expand_left();
    if(l1 <= lmid-1)
    {
        while(rmid < R)
            contract_right();
            
        solve(l1, lmid-1, r1, rmid);

        while(R < r2)
            expand_right();
    }

    if(lmid+1 <= l2)
    {
        while(L < lmid+1)
            contract_left();
        solve(lmid+1, l2, rmid, r2);

        while(l1 < L)
            expand_left();
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N >> M >> Q;

    for(int j = 1; j <= M; j++) cin >> u[j] >> v[j];

    DSU = disjoint_set(N);

    disjoint_set temp(N);
    for(int j = 1; j <= M; j++) temp.join(u[j], v[j]);
    if(temp.is_bipartite())
    {
        int x;
        for(int u = 1; u <= Q; u++)
        {
            cin >> x >> x;
            cout << "NO\n";
        }
        return 0;
    }

    L = 1;
    R = M;
    solve(1, M, 1, M);

    for(int q = 1; q <= Q; q++)
    {
        int l, r;
        cin >> l >> r;

        if(minR[l] <= r) cout << "NO\n"; //bipartite
        else cout << "YES\n"; //not bipartite
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2684 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2704 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2684 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2704 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
31 Correct 5 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 3 ms 2764 KB Output is correct
34 Correct 4 ms 2764 KB Output is correct
35 Correct 5 ms 2824 KB Output is correct
36 Correct 4 ms 2820 KB Output is correct
37 Correct 7 ms 2764 KB Output is correct
38 Correct 3 ms 2764 KB Output is correct
39 Correct 4 ms 2764 KB Output is correct
40 Correct 3 ms 2764 KB Output is correct
41 Correct 4 ms 2764 KB Output is correct
42 Correct 4 ms 2764 KB Output is correct
43 Correct 5 ms 2764 KB Output is correct
44 Correct 4 ms 2796 KB Output is correct
45 Correct 4 ms 2764 KB Output is correct
46 Correct 5 ms 2820 KB Output is correct
47 Correct 4 ms 2764 KB Output is correct
48 Correct 4 ms 2764 KB Output is correct
49 Correct 4 ms 2764 KB Output is correct
50 Correct 5 ms 2764 KB Output is correct
51 Correct 4 ms 2764 KB Output is correct
52 Correct 4 ms 2764 KB Output is correct
53 Correct 4 ms 2764 KB Output is correct
54 Correct 5 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 346 ms 14760 KB Output is correct
4 Correct 82 ms 13076 KB Output is correct
5 Correct 335 ms 16908 KB Output is correct
6 Correct 292 ms 17984 KB Output is correct
7 Correct 287 ms 17816 KB Output is correct
8 Correct 326 ms 14912 KB Output is correct
9 Correct 374 ms 17428 KB Output is correct
10 Correct 475 ms 19552 KB Output is correct
11 Correct 369 ms 17480 KB Output is correct
12 Correct 423 ms 17576 KB Output is correct
13 Correct 251 ms 13096 KB Output is correct
14 Correct 324 ms 14780 KB Output is correct
15 Correct 468 ms 18348 KB Output is correct
16 Correct 508 ms 19464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2684 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2704 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 346 ms 14760 KB Output is correct
30 Correct 82 ms 13076 KB Output is correct
31 Correct 335 ms 16908 KB Output is correct
32 Correct 292 ms 17984 KB Output is correct
33 Correct 287 ms 17816 KB Output is correct
34 Correct 326 ms 14912 KB Output is correct
35 Correct 374 ms 17428 KB Output is correct
36 Correct 475 ms 19552 KB Output is correct
37 Correct 369 ms 17480 KB Output is correct
38 Correct 423 ms 17576 KB Output is correct
39 Correct 251 ms 13096 KB Output is correct
40 Correct 324 ms 14780 KB Output is correct
41 Correct 468 ms 18348 KB Output is correct
42 Correct 508 ms 19464 KB Output is correct
43 Correct 349 ms 14636 KB Output is correct
44 Correct 93 ms 13196 KB Output is correct
45 Correct 563 ms 17500 KB Output is correct
46 Correct 282 ms 17984 KB Output is correct
47 Correct 301 ms 17816 KB Output is correct
48 Correct 416 ms 17392 KB Output is correct
49 Correct 513 ms 19568 KB Output is correct
50 Correct 367 ms 16840 KB Output is correct
51 Correct 440 ms 18188 KB Output is correct
52 Correct 426 ms 18208 KB Output is correct
53 Correct 259 ms 13212 KB Output is correct
54 Correct 397 ms 15520 KB Output is correct
55 Correct 467 ms 18332 KB Output is correct
56 Correct 541 ms 19452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2684 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2704 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
31 Correct 5 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 3 ms 2764 KB Output is correct
34 Correct 4 ms 2764 KB Output is correct
35 Correct 5 ms 2824 KB Output is correct
36 Correct 4 ms 2820 KB Output is correct
37 Correct 7 ms 2764 KB Output is correct
38 Correct 3 ms 2764 KB Output is correct
39 Correct 4 ms 2764 KB Output is correct
40 Correct 3 ms 2764 KB Output is correct
41 Correct 4 ms 2764 KB Output is correct
42 Correct 4 ms 2764 KB Output is correct
43 Correct 5 ms 2764 KB Output is correct
44 Correct 4 ms 2796 KB Output is correct
45 Correct 4 ms 2764 KB Output is correct
46 Correct 5 ms 2820 KB Output is correct
47 Correct 4 ms 2764 KB Output is correct
48 Correct 4 ms 2764 KB Output is correct
49 Correct 4 ms 2764 KB Output is correct
50 Correct 5 ms 2764 KB Output is correct
51 Correct 4 ms 2764 KB Output is correct
52 Correct 4 ms 2764 KB Output is correct
53 Correct 4 ms 2764 KB Output is correct
54 Correct 5 ms 2764 KB Output is correct
55 Correct 326 ms 14000 KB Output is correct
56 Correct 56 ms 12584 KB Output is correct
57 Correct 309 ms 16280 KB Output is correct
58 Correct 246 ms 17304 KB Output is correct
59 Correct 393 ms 16728 KB Output is correct
60 Correct 476 ms 18116 KB Output is correct
61 Correct 383 ms 16836 KB Output is correct
62 Correct 387 ms 17132 KB Output is correct
63 Correct 265 ms 13240 KB Output is correct
64 Correct 379 ms 17292 KB Output is correct
65 Correct 453 ms 18480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2684 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2704 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
31 Correct 5 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 3 ms 2764 KB Output is correct
34 Correct 4 ms 2764 KB Output is correct
35 Correct 5 ms 2824 KB Output is correct
36 Correct 4 ms 2820 KB Output is correct
37 Correct 7 ms 2764 KB Output is correct
38 Correct 3 ms 2764 KB Output is correct
39 Correct 4 ms 2764 KB Output is correct
40 Correct 3 ms 2764 KB Output is correct
41 Correct 4 ms 2764 KB Output is correct
42 Correct 4 ms 2764 KB Output is correct
43 Correct 5 ms 2764 KB Output is correct
44 Correct 4 ms 2796 KB Output is correct
45 Correct 4 ms 2764 KB Output is correct
46 Correct 5 ms 2820 KB Output is correct
47 Correct 4 ms 2764 KB Output is correct
48 Correct 4 ms 2764 KB Output is correct
49 Correct 4 ms 2764 KB Output is correct
50 Correct 5 ms 2764 KB Output is correct
51 Correct 4 ms 2764 KB Output is correct
52 Correct 4 ms 2764 KB Output is correct
53 Correct 4 ms 2764 KB Output is correct
54 Correct 5 ms 2764 KB Output is correct
55 Correct 346 ms 14760 KB Output is correct
56 Correct 82 ms 13076 KB Output is correct
57 Correct 335 ms 16908 KB Output is correct
58 Correct 292 ms 17984 KB Output is correct
59 Correct 287 ms 17816 KB Output is correct
60 Correct 326 ms 14912 KB Output is correct
61 Correct 374 ms 17428 KB Output is correct
62 Correct 475 ms 19552 KB Output is correct
63 Correct 369 ms 17480 KB Output is correct
64 Correct 423 ms 17576 KB Output is correct
65 Correct 251 ms 13096 KB Output is correct
66 Correct 324 ms 14780 KB Output is correct
67 Correct 468 ms 18348 KB Output is correct
68 Correct 508 ms 19464 KB Output is correct
69 Correct 349 ms 14636 KB Output is correct
70 Correct 93 ms 13196 KB Output is correct
71 Correct 563 ms 17500 KB Output is correct
72 Correct 282 ms 17984 KB Output is correct
73 Correct 301 ms 17816 KB Output is correct
74 Correct 416 ms 17392 KB Output is correct
75 Correct 513 ms 19568 KB Output is correct
76 Correct 367 ms 16840 KB Output is correct
77 Correct 440 ms 18188 KB Output is correct
78 Correct 426 ms 18208 KB Output is correct
79 Correct 259 ms 13212 KB Output is correct
80 Correct 397 ms 15520 KB Output is correct
81 Correct 467 ms 18332 KB Output is correct
82 Correct 541 ms 19452 KB Output is correct
83 Correct 326 ms 14000 KB Output is correct
84 Correct 56 ms 12584 KB Output is correct
85 Correct 309 ms 16280 KB Output is correct
86 Correct 246 ms 17304 KB Output is correct
87 Correct 393 ms 16728 KB Output is correct
88 Correct 476 ms 18116 KB Output is correct
89 Correct 383 ms 16836 KB Output is correct
90 Correct 387 ms 17132 KB Output is correct
91 Correct 265 ms 13240 KB Output is correct
92 Correct 379 ms 17292 KB Output is correct
93 Correct 453 ms 18480 KB Output is correct
94 Correct 335 ms 14772 KB Output is correct
95 Correct 521 ms 17648 KB Output is correct
96 Correct 420 ms 16964 KB Output is correct
97 Correct 291 ms 17940 KB Output is correct
98 Correct 335 ms 17892 KB Output is correct
99 Correct 356 ms 14756 KB Output is correct
100 Correct 454 ms 19568 KB Output is correct
101 Correct 367 ms 16656 KB Output is correct
102 Correct 415 ms 18012 KB Output is correct
103 Correct 453 ms 17856 KB Output is correct
104 Correct 307 ms 13728 KB Output is correct
105 Correct 445 ms 17684 KB Output is correct
106 Correct 518 ms 18996 KB Output is correct
107 Correct 288 ms 17192 KB Output is correct
108 Correct 344 ms 14404 KB Output is correct
109 Correct 343 ms 14488 KB Output is correct
110 Correct 345 ms 14380 KB Output is correct
111 Correct 362 ms 14316 KB Output is correct
112 Correct 339 ms 14296 KB Output is correct
113 Correct 341 ms 14384 KB Output is correct
114 Correct 355 ms 14408 KB Output is correct
115 Correct 362 ms 14252 KB Output is correct
116 Correct 367 ms 14176 KB Output is correct