답안 #254787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254787 2020-07-30T15:21:15 Z model_code Joker (BOI20_joker) C++11
71 / 100
2000 ms 12640 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;


#define MAXN 200005
#define MAXM 200005
#define MAXQ 200005


int comp[MAXN], h[MAXN];
bool col_switch[MAXN];
int n_mem, mem_cv[MAXM], mem_cu[MAXM], mem_hcu[MAXM];
bool mem_swcv[MAXM];

// init DSU state
void init_dsu(int n)
{
    for(int i = 0; i < n; i++) {
        comp[i] = i;
        h[i] = 0;
        col_switch[i] = 0;
    }
    n_mem = 0;
}

// find root and color for given node
pair<int, bool> find(int v)
{
    bool col = col_switch[v];
    while (comp[v] != v) {
        v = comp[v];
        col ^= 1;
        col ^= col_switch[v];
    }
    return make_pair(v, col);
}

// merge given nodes
// silent merge if already in the same component
void merge(int v, int u)
{
    pair<int, int> ansv = find(v), ansu = find(u);
    int cv = ansv.first, cu = ansu.first;
    bool colv = ansv.second, colu = ansu.second;
    if (cv != cu) {
        // make sure to point the smaller (lower rank) set to the bigger one
        if (h[cv] > h[cu]) {
            swap(cv, cu);
            swap(v, u);
        }
        // cache operation for rollback
        mem_cv[n_mem] = cv;
        mem_cu[n_mem] = cu;
        mem_hcu[n_mem] = h[cu];
        mem_swcv[n_mem] = col_switch[cv];
        n_mem++;
        // merge and update values
        comp[cv] = cu;
        if (h[cv] == h[cu]) h[cu]++;
        col_switch[cv] = (colv != colu);
    }
    else {
        mem_cv[n_mem] = -1;
        n_mem++;
    }
}

// check how two noodes relate to each other
int check(int v, int u)
{
    pair<int, int> ansv = find(v), ansu = find(u);
    int cv = ansv.first, cu = ansu.first;
    bool colv = ansv.second, colu = ansu.second;
    if (cv != cu) return 2; // different sets
    if (colv == colu) return -1; // same set and same color (odd cycle)
    return 1; // same set, different color
}

// reset last k DSU merges
void rollback(int k)
{
    while (k--) {
        n_mem--;
        int cv = mem_cv[n_mem], cu = mem_cu[n_mem];
        bool swcv = mem_swcv[n_mem];
        if (cv == -1) continue;
        comp[cv] = cv;
        h[cu] = cu;
        col_switch[cv] = swcv;
    }
}


int N, M, Q;
int E[2][MAXM], queries[2][MAXQ], res[MAXQ];
vector<int> inds;


// solve for queries in `inds`
// must have properly set DSU state (containing all edges before current block's left edge)
void solve_for_inds(int e_left_next)
{
    int k = inds.size();

    // order queries by right index, descending
    vector<pair<int, int> > v(k);
    for (int i = 0; i < k; i++) {
        int ind = inds[i];
        v[i] = make_pair(-queries[1][ind], ind);
    }
    sort(v.begin(), v.end());

    int e_right_next = M-1, steps = 0;
    bool got_cycle = false;

    // process queries
    // * first add all edges to the right of current query window
    // * then add all edges to the left
    // * after done, rollback left-added edges
    for (int i = 0; i < k; i++) {
        int ind = v[i].second;
        int L = queries[0][ind], R = queries[1][ind];
        // add all edges to the right of current query window
        if (not got_cycle) {
            while (e_right_next > R) {
                int status = check(E[0][e_right_next], E[1][e_right_next]);
                if (status == -1) {
                    got_cycle = true;
                    break;
                }
                merge(E[0][e_right_next], E[1][e_right_next]);
                e_right_next--;
                steps++;
            }
        }
        bool got_cycle_inner = got_cycle;
        int steps_inner = 0;
        // then add all edges to the left
        if (not got_cycle_inner) {
            for (int e = e_left_next; e < L; e++) {
                int status = check(E[0][e], E[1][e]);
                if (status == -1) {
                    got_cycle_inner = true;
                    break;
                }
                merge(E[0][e], E[1][e]);
                steps_inner++;
            }
        }
        res[ind] = int(got_cycle or got_cycle_inner);
        // rollback left-added edges
        rollback(steps_inner);
    }

    // rollback all right-added edges
    rollback(steps);
}


void solve()
{
    // block size
    int B = max(int(M / sqrt(Q)), 1);

    // order queries by block index
    vector<pair<int, int> > v(Q);
    for (int i = 0; i < Q; i++) {
         v[i] = make_pair(queries[0][i] / B, i);
    }
    sort(v.begin(), v.end());

    init_dsu(N);
    int e_left_next = 0; // left pointer
    bool got_cycle = false; // once we get a (odd) cycle we just answer all following queries with YES without any work

    inds = vector<int>();
    for (int i = 0; i < Q; i++) {
        // collect all queries for current blocck
        inds.push_back(v[i].second);
        // process current block
        if (i + 1 == Q or v[i].first != v[i+1].first) {
            // add left (smaller indexed) edges until the first edge of current block
            // break whenever we detect a cycle
            while (not got_cycle and e_left_next < v[i].first * B) {
                int status = check(E[0][e_left_next], E[1][e_left_next]);
                if (status == -1) {
                    got_cycle = true;
                    break;
                }
                merge(E[0][e_left_next], E[1][e_left_next]);
                e_left_next++;
            }
            // if cycle, just answer all block's queries with YES
            if (got_cycle) {
                for (int j = 0; j < (int)inds.size(); j++) {
                    int ind = inds[j];
                    res[ind] = 1;
                }
            }
            // otherwise solve for the queries within current block
            else {
                solve_for_inds(e_left_next);
            }
            // clear "current block"
            inds = vector<int>();
        }
    }
}


int main()
{
    while (cin >> N >> M >> Q) {
        for (int i = 0; i < M; i++) {
            scanf("%d %d", &E[0][i], &E[1][i]);
            E[0][i]--;
            E[1][i]--;
        }
        for (int i = 0; i < Q; i++) {
            scanf("%d %d", &queries[0][i], &queries[1][i]);
            queries[0][i]--;
            queries[1][i]--;
        }
        solve();
        for (int i = 0; i < Q; i++) {
            if (res[i]) printf("YES\n");
            else printf("NO\n");
        }
    }
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:218:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &E[0][i], &E[1][i]);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:223:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &queries[0][i], &queries[1][i]);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 5 ms 512 KB Output is correct
40 Correct 3 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 3 ms 512 KB Output is correct
44 Correct 3 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 4 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 3 ms 512 KB Output is correct
53 Correct 2 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 153 ms 10476 KB Output is correct
4 Correct 155 ms 12524 KB Output is correct
5 Correct 160 ms 10132 KB Output is correct
6 Correct 144 ms 10388 KB Output is correct
7 Correct 143 ms 10476 KB Output is correct
8 Correct 143 ms 10056 KB Output is correct
9 Correct 137 ms 10476 KB Output is correct
10 Correct 146 ms 11324 KB Output is correct
11 Correct 149 ms 10440 KB Output is correct
12 Correct 191 ms 11116 KB Output is correct
13 Correct 199 ms 9600 KB Output is correct
14 Correct 151 ms 10140 KB Output is correct
15 Correct 149 ms 10860 KB Output is correct
16 Correct 155 ms 11372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 153 ms 10476 KB Output is correct
30 Correct 155 ms 12524 KB Output is correct
31 Correct 160 ms 10132 KB Output is correct
32 Correct 144 ms 10388 KB Output is correct
33 Correct 143 ms 10476 KB Output is correct
34 Correct 143 ms 10056 KB Output is correct
35 Correct 137 ms 10476 KB Output is correct
36 Correct 146 ms 11324 KB Output is correct
37 Correct 149 ms 10440 KB Output is correct
38 Correct 191 ms 11116 KB Output is correct
39 Correct 199 ms 9600 KB Output is correct
40 Correct 151 ms 10140 KB Output is correct
41 Correct 149 ms 10860 KB Output is correct
42 Correct 155 ms 11372 KB Output is correct
43 Correct 486 ms 10452 KB Output is correct
44 Correct 652 ms 12640 KB Output is correct
45 Correct 533 ms 12396 KB Output is correct
46 Correct 425 ms 10444 KB Output is correct
47 Correct 481 ms 10428 KB Output is correct
48 Correct 184 ms 10476 KB Output is correct
49 Correct 222 ms 11380 KB Output is correct
50 Correct 488 ms 10288 KB Output is correct
51 Correct 502 ms 10884 KB Output is correct
52 Correct 547 ms 11388 KB Output is correct
53 Correct 531 ms 9580 KB Output is correct
54 Correct 609 ms 10332 KB Output is correct
55 Correct 580 ms 10868 KB Output is correct
56 Correct 616 ms 11476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 5 ms 512 KB Output is correct
40 Correct 3 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 3 ms 512 KB Output is correct
44 Correct 3 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 4 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 3 ms 512 KB Output is correct
53 Correct 2 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
55 Correct 158 ms 4216 KB Output is correct
56 Correct 380 ms 6288 KB Output is correct
57 Correct 150 ms 5804 KB Output is correct
58 Correct 118 ms 4060 KB Output is correct
59 Correct 82 ms 4088 KB Output is correct
60 Correct 103 ms 4600 KB Output is correct
61 Correct 139 ms 4088 KB Output is correct
62 Correct 171 ms 5068 KB Output is correct
63 Correct 152 ms 3636 KB Output is correct
64 Correct 216 ms 4344 KB Output is correct
65 Correct 282 ms 4856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 5 ms 512 KB Output is correct
40 Correct 3 ms 512 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 3 ms 512 KB Output is correct
43 Correct 3 ms 512 KB Output is correct
44 Correct 3 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 4 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 3 ms 512 KB Output is correct
53 Correct 2 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
55 Correct 153 ms 10476 KB Output is correct
56 Correct 155 ms 12524 KB Output is correct
57 Correct 160 ms 10132 KB Output is correct
58 Correct 144 ms 10388 KB Output is correct
59 Correct 143 ms 10476 KB Output is correct
60 Correct 143 ms 10056 KB Output is correct
61 Correct 137 ms 10476 KB Output is correct
62 Correct 146 ms 11324 KB Output is correct
63 Correct 149 ms 10440 KB Output is correct
64 Correct 191 ms 11116 KB Output is correct
65 Correct 199 ms 9600 KB Output is correct
66 Correct 151 ms 10140 KB Output is correct
67 Correct 149 ms 10860 KB Output is correct
68 Correct 155 ms 11372 KB Output is correct
69 Correct 486 ms 10452 KB Output is correct
70 Correct 652 ms 12640 KB Output is correct
71 Correct 533 ms 12396 KB Output is correct
72 Correct 425 ms 10444 KB Output is correct
73 Correct 481 ms 10428 KB Output is correct
74 Correct 184 ms 10476 KB Output is correct
75 Correct 222 ms 11380 KB Output is correct
76 Correct 488 ms 10288 KB Output is correct
77 Correct 502 ms 10884 KB Output is correct
78 Correct 547 ms 11388 KB Output is correct
79 Correct 531 ms 9580 KB Output is correct
80 Correct 609 ms 10332 KB Output is correct
81 Correct 580 ms 10868 KB Output is correct
82 Correct 616 ms 11476 KB Output is correct
83 Correct 158 ms 4216 KB Output is correct
84 Correct 380 ms 6288 KB Output is correct
85 Correct 150 ms 5804 KB Output is correct
86 Correct 118 ms 4060 KB Output is correct
87 Correct 82 ms 4088 KB Output is correct
88 Correct 103 ms 4600 KB Output is correct
89 Correct 139 ms 4088 KB Output is correct
90 Correct 171 ms 5068 KB Output is correct
91 Correct 152 ms 3636 KB Output is correct
92 Correct 216 ms 4344 KB Output is correct
93 Correct 282 ms 4856 KB Output is correct
94 Correct 856 ms 7928 KB Output is correct
95 Execution timed out 2079 ms 10048 KB Time limit exceeded
96 Halted 0 ms 0 KB -