Submission #720351

# Submission time Handle Problem Language Result Execution time Memory
720351 2023-04-08T04:36:13 Z n0sk1ll Joker (BOI20_joker) C++14
14 / 100
2000 ms 8180 KB
#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;







//Note to self: Check for overflow

int up[200005];
bool wh[200005];
bool isbip=true;
int kolkosamlos=0;

stack<pair<int,pii>> stek;

void dsu(int a, int b)
{
    int wa=wh[a],wb=wh[b];
    while (up[a]>=0)
    {
        a=up[a];
        wa^=wh[a];
    }
    while (up[b]>=0)
    {
        b=up[b];
        wb^=wh[b];
    }

    if (a==b || !isbip)
    {
        if (!isbip || wa==wb) isbip=false,stek.push({-1,{-1,-1}}),stek.push({-1,{-1,-1}}),kolkosamlos+=2;
        else stek.push({0,{0,0}}),stek.push({0,{0,0}});
    }
    else
    {
        if (up[a]<up[b]) swap(a,b);
        stek.push({b,{up[b],wh[b]}});
        stek.push({a,{up[a],wh[a]}});
        if (wa==wb) wh[a]^=1;
        up[b]+=up[a],up[a]=b;
    }
}

void rollback()
{
    int rep=2;
    while (rep--)
    {
        auto it=stek.top(); stek.pop();
        if (it.xx!=-1){
            up[it.xx]=it.yy.xx,wh[it.xx]=it.yy.yy;
        }
        else
        {
            kolkosamlos--;
            if (kolkosamlos==0) isbip=true;
        }
    }
}

void printstack()
{
    vector<pair<int,pii>> sta;
    while (!stek.empty())
    {
        sta.pb(stek.top());
        stek.pop();
    }

    for (auto it : sta) cout<<it.xx<<": "<<it.yy.xx<<","<<it.yy.yy<<endl;
    cout<<endl;

    while (!sta.empty())
    {
        stek.push(sta.back());
        sta.popb();
    }
}

pii eds[200005];
int rmost[200005]; //prvi za koji zabrana [i,rmost[i]] daje bipartitivan graf

void binarna(int l, int r, int okle, int dokle) //popunjeni su [1,okle-1] i [r+1,n]
{
    //cout<<"["<<okle<<","<<dokle<<"] se slika u ["<<l<<","<<r<<"]"<<endl;
    //printstack();
    if (okle>dokle) return;

    if (l==r)
    {
        fff(i,okle,dokle) rmost[i]=l;
    }
    else
    {
        int mid=(l+r)/2;
        fff(i,mid+1,r) dsu(eds[i].xx,eds[i].yy);
        if (!isbip)
        {
            fff(i,mid+1,r) rollback();
            binarna(mid+1,r,okle,dokle);
        }
        else
        {
            int mokle=okle,kokle=0; //mid [okle,dokle]
            while (mokle<dokle)
            {
                dsu(eds[mokle].xx,eds[mokle].yy),kokle++;
                if (!isbip) break; mokle++;
            }
            //dakle, [okle,mokle] predstavlja one koji SU bipartitivni, [mokle,dokle] one koji NISU

            while (kokle--) rollback();
            binarna(l,mid,okle,mokle);

            fff(i,mid+1,r) rollback();
            fff(i,okle,mokle) dsu(eds[i].xx,eds[i].yy);
            binarna(mid+1,r,mokle+1,dokle);
            fff(i,okle,mokle) rollback();
        }
    }
}

int main()
{
    FAST;

    int n,m,q; cin>>n>>m>>q;
    fff(i,1,n) up[i]=-1,wh[i]=0; isbip=true;
    fff(i,1,m) cin>>eds[i].xx>>eds[i].yy;
    binarna(0,m,1,m);

    while (!stek.empty()) rollback();
    fff(i,1,m)
    {
        if (!isbip) rmost[i]=m+1;
        dsu(eds[i].xx,eds[i].yy);
    }
    //fff(i,1,m) cout<<i<<": "<<rmost[i]<<endl;

    fff(i,1,n) up[i]=-1,wh[i]=0; isbip=true;
    while (q--)
    {
        while (!stek.empty()) rollback();
        int ll,rr; cin>>ll>>rr;
        /*if (rr>=rmost[ll]) cout<<"NO\n";
        else cout<<"YES\n";*/
        ff(i,1,ll) dsu(eds[i].xx,eds[i].yy);
        fff(i,rr+1,m) dsu(eds[i].xx,eds[i].yy);
        if (isbip) cout<<"NO\n";
        else cout<<"YES\n";
    }




    /*fff(i,1,1000) up[i]=-1,wh[i]=0; isbip=true;

    while (true)
    {
        string t; cin>>t;
        if (t=="add")
        {
            int u,v; cin>>u>>v;
            dsu(u,v);
        }
        else if (t=="rollback")
        {
            rollback();
        }
        else cout<<"unsupported command!"<<endl;
        cout<<(isbip?"bipartite":"has odd cycle")<<endl;
    }*/
}

//Note to self: Check for overflow

/*

3 3 0
1 2
2 3
3 1

4 4 0
1 2
2 3
3 4
4 1

*/

Compilation message

Joker.cpp: In function 'void binarna(int, int, int, int)':
Joker.cpp:137:17: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  137 |                 if (!isbip) break; mokle++;
      |                 ^~
Joker.cpp:137:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  137 |                 if (!isbip) break; mokle++;
      |                                    ^~~~~
Joker.cpp: In function 'int main()':
Joker.cpp:13:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   13 | #define fff(i,a,b) for (int i = a; i <= b; i++)
      |                    ^~~
Joker.cpp:157:5: note: in expansion of macro 'fff'
  157 |     fff(i,1,n) up[i]=-1,wh[i]=0; isbip=true;
      |     ^~~
Joker.cpp:157:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  157 |     fff(i,1,n) up[i]=-1,wh[i]=0; isbip=true;
      |                                  ^~~~~
Joker.cpp:13:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   13 | #define fff(i,a,b) for (int i = a; i <= b; i++)
      |                    ^~~
Joker.cpp:169:5: note: in expansion of macro 'fff'
  169 |     fff(i,1,n) up[i]=-1,wh[i]=0; isbip=true;
      |     ^~~
Joker.cpp:169:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  169 |     fff(i,1,n) up[i]=-1,wh[i]=0; isbip=true;
      |                                  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 356 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 356 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 74 ms 412 KB Output is correct
30 Correct 87 ms 408 KB Output is correct
31 Correct 95 ms 424 KB Output is correct
32 Correct 83 ms 340 KB Output is correct
33 Correct 103 ms 404 KB Output is correct
34 Correct 100 ms 428 KB Output is correct
35 Correct 107 ms 412 KB Output is correct
36 Correct 89 ms 408 KB Output is correct
37 Correct 133 ms 424 KB Output is correct
38 Correct 157 ms 468 KB Output is correct
39 Correct 95 ms 340 KB Output is correct
40 Correct 92 ms 424 KB Output is correct
41 Correct 92 ms 424 KB Output is correct
42 Correct 92 ms 340 KB Output is correct
43 Correct 151 ms 400 KB Output is correct
44 Correct 149 ms 408 KB Output is correct
45 Correct 141 ms 340 KB Output is correct
46 Correct 129 ms 428 KB Output is correct
47 Correct 88 ms 468 KB Output is correct
48 Correct 93 ms 416 KB Output is correct
49 Correct 95 ms 404 KB Output is correct
50 Correct 107 ms 408 KB Output is correct
51 Correct 95 ms 420 KB Output is correct
52 Correct 93 ms 424 KB Output is correct
53 Correct 96 ms 340 KB Output is correct
54 Correct 113 ms 412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Execution timed out 2058 ms 8076 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 356 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Execution timed out 2058 ms 8076 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 356 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 74 ms 412 KB Output is correct
30 Correct 87 ms 408 KB Output is correct
31 Correct 95 ms 424 KB Output is correct
32 Correct 83 ms 340 KB Output is correct
33 Correct 103 ms 404 KB Output is correct
34 Correct 100 ms 428 KB Output is correct
35 Correct 107 ms 412 KB Output is correct
36 Correct 89 ms 408 KB Output is correct
37 Correct 133 ms 424 KB Output is correct
38 Correct 157 ms 468 KB Output is correct
39 Correct 95 ms 340 KB Output is correct
40 Correct 92 ms 424 KB Output is correct
41 Correct 92 ms 424 KB Output is correct
42 Correct 92 ms 340 KB Output is correct
43 Correct 151 ms 400 KB Output is correct
44 Correct 149 ms 408 KB Output is correct
45 Correct 141 ms 340 KB Output is correct
46 Correct 129 ms 428 KB Output is correct
47 Correct 88 ms 468 KB Output is correct
48 Correct 93 ms 416 KB Output is correct
49 Correct 95 ms 404 KB Output is correct
50 Correct 107 ms 408 KB Output is correct
51 Correct 95 ms 420 KB Output is correct
52 Correct 93 ms 424 KB Output is correct
53 Correct 96 ms 340 KB Output is correct
54 Correct 113 ms 412 KB Output is correct
55 Execution timed out 2051 ms 8180 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 356 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 74 ms 412 KB Output is correct
30 Correct 87 ms 408 KB Output is correct
31 Correct 95 ms 424 KB Output is correct
32 Correct 83 ms 340 KB Output is correct
33 Correct 103 ms 404 KB Output is correct
34 Correct 100 ms 428 KB Output is correct
35 Correct 107 ms 412 KB Output is correct
36 Correct 89 ms 408 KB Output is correct
37 Correct 133 ms 424 KB Output is correct
38 Correct 157 ms 468 KB Output is correct
39 Correct 95 ms 340 KB Output is correct
40 Correct 92 ms 424 KB Output is correct
41 Correct 92 ms 424 KB Output is correct
42 Correct 92 ms 340 KB Output is correct
43 Correct 151 ms 400 KB Output is correct
44 Correct 149 ms 408 KB Output is correct
45 Correct 141 ms 340 KB Output is correct
46 Correct 129 ms 428 KB Output is correct
47 Correct 88 ms 468 KB Output is correct
48 Correct 93 ms 416 KB Output is correct
49 Correct 95 ms 404 KB Output is correct
50 Correct 107 ms 408 KB Output is correct
51 Correct 95 ms 420 KB Output is correct
52 Correct 93 ms 424 KB Output is correct
53 Correct 96 ms 340 KB Output is correct
54 Correct 113 ms 412 KB Output is correct
55 Execution timed out 2058 ms 8076 KB Time limit exceeded
56 Halted 0 ms 0 KB -