Submission #417953

# Submission time Handle Problem Language Result Execution time Memory
417953 2021-06-04T16:51:03 Z nicolaalexandra Joker (BOI20_joker) C++14
100 / 100
976 ms 14988 KB
#include <bits/stdc++.h>
#define DIM 400010
using namespace std;

int fth[DIM],Size[DIM],dp[DIM];
pair <int,int> mch[DIM];
deque <pair<int,int> > s;
int n,m,q,x,y,i,first;

int get_rad (int x){
    while (fth[x] != x)
        x = fth[x];
    return x;
}

int uneste (int x, int y){
    int radx = get_rad (x), rady = get_rad (y);
    if (radx == rady)
        return 0;

    if (Size[radx] < Size[rady])
        swap (radx,rady);

    s.push_back(make_pair(radx,rady));
    Size[radx] += Size[rady];
    fth[rady] = radx;

    return 1;
}

void undo(){
    int x = s.back().first, y = s.back().second;
    s.pop_back();
    Size[x] -= Size[y];
    fth[y] = y;
}

int verif (int x, int y){
    int radx = get_rad(x), rady = get_rad(y);
    return radx == rady;
}

void solve (int st, int dr, int opt_st, int opt_dr){
    if (st > dr)
        return;
    int mid = (st+dr)>>1, opt_mid = m;

    /// adaug tot pana la mid

    int cnt = 0, ok = 0;
    for (int i=st;i<mid;i++){
        int x = mch[i].first, y = mch[i].second;
        cnt += uneste(x,y+n);
        cnt += uneste(x+n,y);

        if (verif(x,x+n)){ /// am ciclu impar deja
            ok = 1;
            break;
        }
    }

    if (ok){
        dp[mid] = opt_mid = m+1;
    } else {

        /// adaug muchii din dreapta si vad daca se pastreaza propr de bipartit
        if (mid == 188)
            mid = 188;
        for (opt_mid = opt_dr; opt_mid >= opt_st && opt_mid >= mid; opt_mid--){

            int x = mch[opt_mid].first, y = mch[opt_mid].second;
            cnt += uneste(x,y+n);
            cnt += uneste(x+n,y);

            if (verif(x,x+n))
                break;
        }
        dp[mid] = opt_mid;

        if (dp[mid] == 201)
            dp[mid] = 201;
    }

    while (cnt > 0){
        undo();
        cnt--;
    }

    /// acum trb sa refac padurile a.i. sa se potriveasca pt intervalele urmatoare

    ok = 0;
    for (int i=opt_mid+1;i<=opt_dr;i++){
        int x = mch[i].first, y = mch[i].second;
        cnt += uneste(x,y+n);
        cnt += uneste(x+n,y);

        if (verif(x,x+n)){
            ok = 1;
            while (cnt > 0){
                undo();
                cnt--;
            }
            for (int j=st;j<mid;j++)
                dp[j] = m+1;

            break;
        }
    }

    if (!ok){
        solve (st,mid-1,opt_st,opt_mid);


        /// iar undo
        while (cnt > 0){
            undo();
            cnt--;
        }
    }
    /// reconstructie

    ok = 0;
    for (int i=st;i<=mid;i++){
        int x = mch[i].first, y = mch[i].second;
        cnt += uneste(x,y+n);
        cnt += uneste(x+n,y);

        if (verif(x,x+n)){
            ok = 1;
            while (cnt > 0){
                undo();
                cnt--;
            }

            for (int j=mid+1;j<=dr;j++)
                dp[j] = m+1;

            break;
        }
    }

    if (!ok){
        solve (mid+1,dr,opt_mid,opt_dr);
        while (cnt > 0){
            undo();
            cnt--;
        }
    }

}



int main (){

    //ifstream cin ("date.in");
    //ofstream cout ("date.out");

    cin>>n>>m>>q;
    for (i=1;i<=m;i++)
        cin>>mch[i].first>>mch[i].second;

    mch[m+1] = make_pair(n+1,n+2); /// nu ma intereseaza asta
    n += 2;

    for (i=1;i<=2*n;i++)
        fth[i] = i, Size[i] = 1;

    solve (1,m,1,m+1);

    //for (i=1;i<=m;i++)
      //  cout<<dp[i]<<"\n";

    for (;q--;){
        cin>>x>>y;

        if (y >= dp[x])
            cout<<"NO\n";
        else cout<<"YES\n";

    }


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 220 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 220 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 6 ms 404 KB Output is correct
30 Correct 7 ms 332 KB Output is correct
31 Correct 7 ms 332 KB Output is correct
32 Correct 6 ms 332 KB Output is correct
33 Correct 6 ms 332 KB Output is correct
34 Correct 7 ms 332 KB Output is correct
35 Correct 8 ms 332 KB Output is correct
36 Correct 7 ms 332 KB Output is correct
37 Correct 8 ms 332 KB Output is correct
38 Correct 8 ms 448 KB Output is correct
39 Correct 7 ms 440 KB Output is correct
40 Correct 7 ms 332 KB Output is correct
41 Correct 7 ms 332 KB Output is correct
42 Correct 7 ms 460 KB Output is correct
43 Correct 7 ms 400 KB Output is correct
44 Correct 7 ms 412 KB Output is correct
45 Correct 9 ms 332 KB Output is correct
46 Correct 8 ms 332 KB Output is correct
47 Correct 7 ms 332 KB Output is correct
48 Correct 7 ms 332 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 7 ms 332 KB Output is correct
51 Correct 7 ms 308 KB Output is correct
52 Correct 7 ms 332 KB Output is correct
53 Correct 7 ms 308 KB Output is correct
54 Correct 7 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 767 ms 6448 KB Output is correct
4 Correct 948 ms 9548 KB Output is correct
5 Correct 773 ms 9092 KB Output is correct
6 Correct 691 ms 6524 KB Output is correct
7 Correct 780 ms 6592 KB Output is correct
8 Correct 730 ms 5428 KB Output is correct
9 Correct 774 ms 6316 KB Output is correct
10 Correct 857 ms 8456 KB Output is correct
11 Correct 759 ms 6372 KB Output is correct
12 Correct 801 ms 7844 KB Output is correct
13 Correct 703 ms 4164 KB Output is correct
14 Correct 756 ms 5312 KB Output is correct
15 Correct 883 ms 6592 KB Output is correct
16 Correct 884 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 220 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 767 ms 6448 KB Output is correct
30 Correct 948 ms 9548 KB Output is correct
31 Correct 773 ms 9092 KB Output is correct
32 Correct 691 ms 6524 KB Output is correct
33 Correct 780 ms 6592 KB Output is correct
34 Correct 730 ms 5428 KB Output is correct
35 Correct 774 ms 6316 KB Output is correct
36 Correct 857 ms 8456 KB Output is correct
37 Correct 759 ms 6372 KB Output is correct
38 Correct 801 ms 7844 KB Output is correct
39 Correct 703 ms 4164 KB Output is correct
40 Correct 756 ms 5312 KB Output is correct
41 Correct 883 ms 6592 KB Output is correct
42 Correct 884 ms 8192 KB Output is correct
43 Correct 752 ms 10780 KB Output is correct
44 Correct 976 ms 13816 KB Output is correct
45 Correct 902 ms 14148 KB Output is correct
46 Correct 681 ms 10808 KB Output is correct
47 Correct 721 ms 10908 KB Output is correct
48 Correct 800 ms 9924 KB Output is correct
49 Correct 876 ms 12360 KB Output is correct
50 Correct 739 ms 9992 KB Output is correct
51 Correct 775 ms 11460 KB Output is correct
52 Correct 809 ms 12476 KB Output is correct
53 Correct 724 ms 8152 KB Output is correct
54 Correct 820 ms 10056 KB Output is correct
55 Correct 868 ms 10884 KB Output is correct
56 Correct 883 ms 12496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 220 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 6 ms 404 KB Output is correct
30 Correct 7 ms 332 KB Output is correct
31 Correct 7 ms 332 KB Output is correct
32 Correct 6 ms 332 KB Output is correct
33 Correct 6 ms 332 KB Output is correct
34 Correct 7 ms 332 KB Output is correct
35 Correct 8 ms 332 KB Output is correct
36 Correct 7 ms 332 KB Output is correct
37 Correct 8 ms 332 KB Output is correct
38 Correct 8 ms 448 KB Output is correct
39 Correct 7 ms 440 KB Output is correct
40 Correct 7 ms 332 KB Output is correct
41 Correct 7 ms 332 KB Output is correct
42 Correct 7 ms 460 KB Output is correct
43 Correct 7 ms 400 KB Output is correct
44 Correct 7 ms 412 KB Output is correct
45 Correct 9 ms 332 KB Output is correct
46 Correct 8 ms 332 KB Output is correct
47 Correct 7 ms 332 KB Output is correct
48 Correct 7 ms 332 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 7 ms 332 KB Output is correct
51 Correct 7 ms 308 KB Output is correct
52 Correct 7 ms 332 KB Output is correct
53 Correct 7 ms 308 KB Output is correct
54 Correct 7 ms 332 KB Output is correct
55 Correct 326 ms 8116 KB Output is correct
56 Correct 535 ms 11624 KB Output is correct
57 Correct 318 ms 10828 KB Output is correct
58 Correct 273 ms 8184 KB Output is correct
59 Correct 379 ms 8004 KB Output is correct
60 Correct 467 ms 9264 KB Output is correct
61 Correct 357 ms 7856 KB Output is correct
62 Correct 369 ms 9688 KB Output is correct
63 Correct 306 ms 6144 KB Output is correct
64 Correct 424 ms 8328 KB Output is correct
65 Correct 459 ms 9640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 220 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 204 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 6 ms 404 KB Output is correct
30 Correct 7 ms 332 KB Output is correct
31 Correct 7 ms 332 KB Output is correct
32 Correct 6 ms 332 KB Output is correct
33 Correct 6 ms 332 KB Output is correct
34 Correct 7 ms 332 KB Output is correct
35 Correct 8 ms 332 KB Output is correct
36 Correct 7 ms 332 KB Output is correct
37 Correct 8 ms 332 KB Output is correct
38 Correct 8 ms 448 KB Output is correct
39 Correct 7 ms 440 KB Output is correct
40 Correct 7 ms 332 KB Output is correct
41 Correct 7 ms 332 KB Output is correct
42 Correct 7 ms 460 KB Output is correct
43 Correct 7 ms 400 KB Output is correct
44 Correct 7 ms 412 KB Output is correct
45 Correct 9 ms 332 KB Output is correct
46 Correct 8 ms 332 KB Output is correct
47 Correct 7 ms 332 KB Output is correct
48 Correct 7 ms 332 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 7 ms 332 KB Output is correct
51 Correct 7 ms 308 KB Output is correct
52 Correct 7 ms 332 KB Output is correct
53 Correct 7 ms 308 KB Output is correct
54 Correct 7 ms 332 KB Output is correct
55 Correct 767 ms 6448 KB Output is correct
56 Correct 948 ms 9548 KB Output is correct
57 Correct 773 ms 9092 KB Output is correct
58 Correct 691 ms 6524 KB Output is correct
59 Correct 780 ms 6592 KB Output is correct
60 Correct 730 ms 5428 KB Output is correct
61 Correct 774 ms 6316 KB Output is correct
62 Correct 857 ms 8456 KB Output is correct
63 Correct 759 ms 6372 KB Output is correct
64 Correct 801 ms 7844 KB Output is correct
65 Correct 703 ms 4164 KB Output is correct
66 Correct 756 ms 5312 KB Output is correct
67 Correct 883 ms 6592 KB Output is correct
68 Correct 884 ms 8192 KB Output is correct
69 Correct 752 ms 10780 KB Output is correct
70 Correct 976 ms 13816 KB Output is correct
71 Correct 902 ms 14148 KB Output is correct
72 Correct 681 ms 10808 KB Output is correct
73 Correct 721 ms 10908 KB Output is correct
74 Correct 800 ms 9924 KB Output is correct
75 Correct 876 ms 12360 KB Output is correct
76 Correct 739 ms 9992 KB Output is correct
77 Correct 775 ms 11460 KB Output is correct
78 Correct 809 ms 12476 KB Output is correct
79 Correct 724 ms 8152 KB Output is correct
80 Correct 820 ms 10056 KB Output is correct
81 Correct 868 ms 10884 KB Output is correct
82 Correct 883 ms 12496 KB Output is correct
83 Correct 326 ms 8116 KB Output is correct
84 Correct 535 ms 11624 KB Output is correct
85 Correct 318 ms 10828 KB Output is correct
86 Correct 273 ms 8184 KB Output is correct
87 Correct 379 ms 8004 KB Output is correct
88 Correct 467 ms 9264 KB Output is correct
89 Correct 357 ms 7856 KB Output is correct
90 Correct 369 ms 9688 KB Output is correct
91 Correct 306 ms 6144 KB Output is correct
92 Correct 424 ms 8328 KB Output is correct
93 Correct 459 ms 9640 KB Output is correct
94 Correct 772 ms 11520 KB Output is correct
95 Correct 905 ms 14432 KB Output is correct
96 Correct 782 ms 14332 KB Output is correct
97 Correct 717 ms 11568 KB Output is correct
98 Correct 750 ms 11616 KB Output is correct
99 Correct 787 ms 10308 KB Output is correct
100 Correct 862 ms 13432 KB Output is correct
101 Correct 786 ms 10692 KB Output is correct
102 Correct 818 ms 12136 KB Output is correct
103 Correct 872 ms 13476 KB Output is correct
104 Correct 764 ms 9372 KB Output is correct
105 Correct 877 ms 11196 KB Output is correct
106 Correct 925 ms 12492 KB Output is correct
107 Correct 732 ms 14988 KB Output is correct
108 Correct 793 ms 11292 KB Output is correct
109 Correct 860 ms 11304 KB Output is correct
110 Correct 843 ms 11188 KB Output is correct
111 Correct 793 ms 11292 KB Output is correct
112 Correct 787 ms 11328 KB Output is correct
113 Correct 772 ms 11368 KB Output is correct
114 Correct 789 ms 11220 KB Output is correct
115 Correct 775 ms 11348 KB Output is correct
116 Correct 773 ms 11400 KB Output is correct