Submission #1091424

# Submission time Handle Problem Language Result Execution time Memory
1091424 2024-09-20T19:56:59 Z andrei_iorgulescu Joker (BOI20_joker) C++14
100 / 100
466 ms 20636 KB
#include <bits/stdc++.h>

using namespace std;

int n, m, q;
int u[200005], v[200005];
int rmin[200005];

pair<int, int> t[200005];
int sz[200005];
bool bip[200005];
int cnt_bad;
int curi;

struct ura
{
    int tip;///0 pentru p1.first != p2.first, 1 pentru celalalt
    int idx = 0, x = 0, y = 0;
    bool bipant = false;
};

vector<ura> op;

pair<int, int> par(int x)
{
    int ugabuga = 0;
    int s1 = 0;
    while (t[x].first != x)
    {
        s1 += t[x].second;
        x = t[x].first;
    }
    return make_pair(x, s1);
}

void join(int idx, int x, int y)
{
    //cout << idx << ' ' << x << ' ' << y << endl;
    pair<int,int> p1 = par(x);
    pair<int,int> p2 = par(y);
    //cout << x << ' ' << y << endl;
    if (p1.first != p2.first)
    {
        if (sz[p1.first] < sz[p2.first])
            swap(p1, p2);
        //cout << "c1" << endl;
        t[p2.first] = {p1.first, (1 + p1.second + p2.second) % 2};
        sz[p1.first] += sz[p2.first];
        ura aux;
        aux.tip = 0;
        aux.idx = idx, aux.x = p1.first, aux.y = p2.first;
        aux.bipant = bip[p1.first];
        if (bip[p1.first] == false and bip[p2.first] == false)
            cnt_bad--;
        if (bip[p2.first] == false)
            bip[p1.first] = false;
        op.push_back(aux);
    }
    else
    {
        ura aux;
        aux.tip = 1;
        aux.idx = idx;
        aux.x = p1.first;
        aux.y = p2.first;
        aux.bipant = bip[p1.first];
        if (p1.second % 2 == p2.second % 2 and bip[p1.first] == true)
        {
            //cout << "cmon" << p1.first << endl;
            bip[p1.first] = false;
            cnt_bad++;
        }
        op.push_back(aux);
    }
}

void und(ura uf)
{
    //cout << "und" << uf.idx << endl;
    if (uf.tip == 0)
    {
        if (bip[uf.y] == false and uf.bipant == false)
            cnt_bad++;
        bip[uf.x] = uf.bipant;
        sz[uf.x] -= sz[uf.y];
        t[uf.y] = {uf.y, 0};
    }
    else
    {
        if (bip[uf.x] != uf.bipant)
            cnt_bad--;
        bip[uf.x] = uf.bipant;
    }
}

bool cmp(ura A, ura B)
{
    if ((A.idx < curi and B.idx >= curi) or (A.idx >= curi and B.idx < curi))
        return A.idx < B.idx;
    else if (A.idx < curi)
        return A.idx < B.idx;
    else
        return A.idx > B.idx;
}

void undo(int idx)
{
    //cout << "u" << idx << endl;
    vector<ura> cur;
    int cate = 0;
    while (true)
    {
        cur.push_back(op.back());
        op.pop_back();
        cate++;
        if (cur.back().idx == idx)
            break;
    }
    while (cate != 0 and !op.empty())
    {
        cur.push_back(op.back());
        op.pop_back();
        cate--;
    }
    for (auto it : cur)
        und(it);
    sort(cur.begin(), cur.end(), cmp);
    for (auto it : cur)
        if (it.idx != idx)
            join(it.idx, u[it.idx], v[it.idx]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i++)
        cin >> u[i] >> v[i];
    for (int i = 1; i <= n; i++)
    {
        t[i] = {i, 0};
        sz[i] = 1;
        bip[i] = true;
    }
    for (int i = 1; i <= m; i++)
        join(i, u[i], v[i]);
    //cout << cnt_bad << endl;
    if (cnt_bad == 0)
    {
        for (int i = 1; i <= q; i++)
        {
            int x, y;
            cin >> x >> y;
            cout << "NO\n";
        }
        return 0;
    }
    int r = 0;
    for (int i = 1; i <= m; i++)
    {
        curi = i;
        while (r < m and cnt_bad > 0)
        {
            r++;
            undo(r);
        }
        if (cnt_bad != 0)
        {
            rmin[i] = m + 1;
            continue;
        }
        rmin[i] = r;
        join(i, u[i], v[i]);
    }
    //for (int i = 1; i <= m; i++)
      //  cout << rmin[i] << ' ';
    //cout << endl;
    for (int i = 1; i <= q; i++)
    {
        int x, y;
        cin >> x >> y;
        if (y < rmin[x])
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}

/**
deci teoretic daca bag elementele de la 1 la N, apoi fac two pointers-ul, o sa cam am queue-like undoing pe DSU, pe care tin si ceva de bipartit
**/

Compilation message

Joker.cpp: In function 'std::pair<int, int> par(int)':
Joker.cpp:26:9: warning: unused variable 'ugabuga' [-Wunused-variable]
   26 |     int ugabuga = 0;
      |         ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 464 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 464 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 472 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 2 ms 688 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 700 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 464 ms 16832 KB Output is correct
4 Correct 49 ms 11712 KB Output is correct
5 Correct 161 ms 18920 KB Output is correct
6 Correct 139 ms 16888 KB Output is correct
7 Correct 141 ms 16832 KB Output is correct
8 Correct 168 ms 16496 KB Output is correct
9 Correct 168 ms 16840 KB Output is correct
10 Correct 256 ms 18116 KB Output is correct
11 Correct 220 ms 16832 KB Output is correct
12 Correct 241 ms 18108 KB Output is correct
13 Correct 140 ms 15812 KB Output is correct
14 Correct 180 ms 16240 KB Output is correct
15 Correct 242 ms 17472 KB Output is correct
16 Correct 263 ms 18364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 464 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 464 ms 16832 KB Output is correct
30 Correct 49 ms 11712 KB Output is correct
31 Correct 161 ms 18920 KB Output is correct
32 Correct 139 ms 16888 KB Output is correct
33 Correct 141 ms 16832 KB Output is correct
34 Correct 168 ms 16496 KB Output is correct
35 Correct 168 ms 16840 KB Output is correct
36 Correct 256 ms 18116 KB Output is correct
37 Correct 220 ms 16832 KB Output is correct
38 Correct 241 ms 18108 KB Output is correct
39 Correct 140 ms 15812 KB Output is correct
40 Correct 180 ms 16240 KB Output is correct
41 Correct 242 ms 17472 KB Output is correct
42 Correct 263 ms 18364 KB Output is correct
43 Correct 449 ms 17596 KB Output is correct
44 Correct 53 ms 13244 KB Output is correct
45 Correct 240 ms 18620 KB Output is correct
46 Correct 121 ms 17340 KB Output is correct
47 Correct 152 ms 17340 KB Output is correct
48 Correct 216 ms 17340 KB Output is correct
49 Correct 244 ms 18652 KB Output is correct
50 Correct 238 ms 17084 KB Output is correct
51 Correct 234 ms 18120 KB Output is correct
52 Correct 236 ms 18624 KB Output is correct
53 Correct 138 ms 16064 KB Output is correct
54 Correct 191 ms 17084 KB Output is correct
55 Correct 228 ms 17856 KB Output is correct
56 Correct 259 ms 18648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 464 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 472 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 2 ms 688 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 700 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 437 ms 17088 KB Output is correct
56 Correct 31 ms 12224 KB Output is correct
57 Correct 112 ms 18368 KB Output is correct
58 Correct 98 ms 17084 KB Output is correct
59 Correct 194 ms 17088 KB Output is correct
60 Correct 241 ms 18016 KB Output is correct
61 Correct 204 ms 17088 KB Output is correct
62 Correct 214 ms 18368 KB Output is correct
63 Correct 137 ms 16316 KB Output is correct
64 Correct 211 ms 17344 KB Output is correct
65 Correct 245 ms 18368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 464 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 472 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 2 ms 688 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 700 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 464 ms 16832 KB Output is correct
56 Correct 49 ms 11712 KB Output is correct
57 Correct 161 ms 18920 KB Output is correct
58 Correct 139 ms 16888 KB Output is correct
59 Correct 141 ms 16832 KB Output is correct
60 Correct 168 ms 16496 KB Output is correct
61 Correct 168 ms 16840 KB Output is correct
62 Correct 256 ms 18116 KB Output is correct
63 Correct 220 ms 16832 KB Output is correct
64 Correct 241 ms 18108 KB Output is correct
65 Correct 140 ms 15812 KB Output is correct
66 Correct 180 ms 16240 KB Output is correct
67 Correct 242 ms 17472 KB Output is correct
68 Correct 263 ms 18364 KB Output is correct
69 Correct 449 ms 17596 KB Output is correct
70 Correct 53 ms 13244 KB Output is correct
71 Correct 240 ms 18620 KB Output is correct
72 Correct 121 ms 17340 KB Output is correct
73 Correct 152 ms 17340 KB Output is correct
74 Correct 216 ms 17340 KB Output is correct
75 Correct 244 ms 18652 KB Output is correct
76 Correct 238 ms 17084 KB Output is correct
77 Correct 234 ms 18120 KB Output is correct
78 Correct 236 ms 18624 KB Output is correct
79 Correct 138 ms 16064 KB Output is correct
80 Correct 191 ms 17084 KB Output is correct
81 Correct 228 ms 17856 KB Output is correct
82 Correct 259 ms 18648 KB Output is correct
83 Correct 437 ms 17088 KB Output is correct
84 Correct 31 ms 12224 KB Output is correct
85 Correct 112 ms 18368 KB Output is correct
86 Correct 98 ms 17084 KB Output is correct
87 Correct 194 ms 17088 KB Output is correct
88 Correct 241 ms 18016 KB Output is correct
89 Correct 204 ms 17088 KB Output is correct
90 Correct 214 ms 18368 KB Output is correct
91 Correct 137 ms 16316 KB Output is correct
92 Correct 211 ms 17344 KB Output is correct
93 Correct 245 ms 18368 KB Output is correct
94 Correct 454 ms 18032 KB Output is correct
95 Correct 463 ms 20636 KB Output is correct
96 Correct 149 ms 18652 KB Output is correct
97 Correct 126 ms 17892 KB Output is correct
98 Correct 150 ms 17344 KB Output is correct
99 Correct 167 ms 16828 KB Output is correct
100 Correct 184 ms 19668 KB Output is correct
101 Correct 222 ms 17380 KB Output is correct
102 Correct 284 ms 18388 KB Output is correct
103 Correct 290 ms 19668 KB Output is correct
104 Correct 166 ms 17328 KB Output is correct
105 Correct 217 ms 17580 KB Output is correct
106 Correct 272 ms 19028 KB Output is correct
107 Correct 97 ms 18884 KB Output is correct
108 Correct 450 ms 17844 KB Output is correct
109 Correct 445 ms 17860 KB Output is correct
110 Correct 466 ms 17884 KB Output is correct
111 Correct 456 ms 17960 KB Output is correct
112 Correct 463 ms 17884 KB Output is correct
113 Correct 458 ms 17928 KB Output is correct
114 Correct 455 ms 17884 KB Output is correct
115 Correct 452 ms 17836 KB Output is correct
116 Correct 455 ms 17856 KB Output is correct