답안 #414854

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414854 2021-05-31T09:15:44 Z Ruxandra985 Joker (BOI20_joker) C++14
0 / 100
1066 ms 14776 KB
#include <bits/stdc++.h>
#define DIMN 400010
using namespace std;
int tt[DIMN];
int sz[DIMN] , opt[DIMN];
int n , m , where;
pair <int , int> mch[DIMN];
vector <pair <int , int> > s;
vector <int> v;

int root (int x){
    while (tt[x] != x)
        x = tt[x];
    return x;
}

inline void unite (int x, int y){
    int radx = root(x), rady = root(y);
    if (sz[radx] < sz[rady])
        swap (radx,rady);

    s.push_back(make_pair(radx,rady)); /// stiva pt undo
    sz[radx] += sz[rady];
    tt[rady] = radx;
}

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

void solve (int st , int dr , int l , int r){
    int mid = (st + dr)/2 , i , poz , x , y;

    //printf ("%d %d\n" , st , dr);

    //if (st == 199999 && dr == 200001){
      //  for (i = 0 ; i < v.size() ; i++){
        //    printf ("%d ", v[i]);
        //}

    //}

    for (i = min(dr , m) ; i >= mid ; i--){
        x = mch[i].first;
        y = mch[i].second;

        if (root(x) == root(y))
            break;

        unite (x , y + n);
        unite (y , x + n);
        v.push_back(i);
    }

    if (i >= mid){ /// nu se poate cu toate
        where = mid;

        i++;

        while (i <= min(dr , m)){

            undo();
            undo();
            v.pop_back();
            i++;

        }

        /// te duci doar in dreapta
        if (mid + 1 <= dr)
            solve(mid + 1 , dr , l , r);



        return;

    }

    l = max(l , 1);

    poz = 0;
    for (i = l ; i <= r && i < mid ; i++){

        x = mch[i].first;
        y = mch[i].second;

        if (root(x) == root(y) || root(y + n) == root(x + n)){
            break;
        }

        unite (x , y + n);

        unite (y , x + n);

        v.push_back(i);

        poz = i;

    }
    opt[mid] = poz;

    for (i = l ; i <= poz ; i++){
        undo();
        undo();
        v.pop_back();
    }

    if (st <= mid - 1)
        solve (st , mid - 1 , l , poz);

    for (i = mid ; i <= min(dr , m); i++){
        undo();
        undo();
        v.pop_back();
    }

    if (mid + 1 <= dr){

        for (i = l ; i < poz ; i++){
            x = mch[i].first;
            y = mch[i].second;
            unite (x , y + n);
            unite (y , x + n);
            v.push_back(i);

        }

        solve(mid + 1 , dr , poz , r);

        for (i = l ; i < poz ; i++){
            undo();
            undo();
            v.pop_back();
        }
    }

    /// convine sa te duci mai intai in dreapta, pastrezi tot ce ai adaugat
}

int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int q , i , x , y , poz , j , l , r;
    fscanf (fin,"%d%d%d",&n,&m,&q);
    for (i = 1 ; i <= m ; i++){
        fscanf (fin,"%d%d",&mch[i].first,&mch[i].second);
    }

    /// opt se calculeaza intre 1 si M + 1

    for (i = 1 ; i <= 2 * n ; i++){
        tt[i] = i;
        sz[i] = 1;
    }

    solve (1 , m + 1 , 1 , m); /// te asiguri ca dreapta nu cauzeaza ciclu impar

    for (;q;q--){
        fscanf (fin,"%d%d",&l,&r);
        if (opt[r + 1] >= l - 1 && r + 1 >= where)
            fprintf (fout,"NO\n");
        else fprintf (fout,"YES\n");
    }
    return 0;
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:147:17: warning: unused variable 'x' [-Wunused-variable]
  147 |     int q , i , x , y , poz , j , l , r;
      |                 ^
Joker.cpp:147:21: warning: unused variable 'y' [-Wunused-variable]
  147 |     int q , i , x , y , poz , j , l , r;
      |                     ^
Joker.cpp:147:25: warning: unused variable 'poz' [-Wunused-variable]
  147 |     int q , i , x , y , poz , j , l , r;
      |                         ^~~
Joker.cpp:147:31: warning: unused variable 'j' [-Wunused-variable]
  147 |     int q , i , x , y , poz , j , l , r;
      |                               ^
Joker.cpp:148:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |     fscanf (fin,"%d%d%d",&n,&m,&q);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:150:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         fscanf (fin,"%d%d",&mch[i].first,&mch[i].second);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:163:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |         fscanf (fin,"%d%d",&l,&r);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 Incorrect 1 ms 304 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 1 ms 304 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 231 ms 10904 KB Output is correct
4 Correct 1066 ms 14776 KB Output is correct
5 Incorrect 471 ms 13508 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 1 ms 304 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 1 ms 304 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 1 ms 304 KB Output isn't correct
5 Halted 0 ms 0 KB -