답안 #1101819

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101819 2024-10-16T21:59:42 Z epicci23 Joker (BOI20_joker) C++17
0 / 100
1549 ms 22488 KB
#include "bits/stdc++.h"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;

const int N = 2e5 + 5;

struct DSU{
 int n;
 stack<array<int,4>> st;
 vector<int> par,siz,col;
 bool ok=1;
 DSU(int _n){
  n=_n+5;
  par.assign(n,0);
  col.assign(n,0);
  iota(all(par),0);
  siz.assign(n,1);
 }
 int find(int a){
  while(par[a]!=a) a=par[a];
  return a;
 }
 int color(int a){
  if(par[a]==a) return col[a];
  return col[a]^color(par[a]);
 }
 bool unite(int a,int b){
  if(find(a)!=find(b)){
   a=find(a),b=find(b);
   if(siz[a]>siz[b]) swap(a,b);
   st.push({a,b,siz[a],col[a]});
   par[a]=b;
   col[a]=col[a]^col[b]^1;
   siz[b]+=siz[a];
   return true;
  }
  else{
    st.push({-1,-1,-1,ok});
    if(color(a)==color(b)) ok=0;
    return ok;
  }
 }
 void rollback(){
   if(st.empty()) return;
   array<int,4> c = st.top();
   st.pop();
   if(c[0]==-1) ok=c[3];
   else{
    par[c[0]]=c[0];
    col[c[0]]=c[3];
    siz[c[1]]-=c[2];
   }
 }
};

vector<array<int,2>> v;
vector<int> last;
int n,m,q;
DSU dsu(N);

void f(int l,int r,int optl,int optr){
  int mid=(l+r)/2,p=0;

  for(int i=l;i<mid;i++) p++,dsu.unite(v[i][0],v[i][1]);
  
  for(int i=optr;i>=mid;i--){
    p++;
    if(!dsu.unite(v[i][0],v[i][1])){
      last[mid]=i;
      break;
    } 
  }
  while(p>0){
    p--;
    dsu.rollback();
  }
  
  if(mid<r){
    for(int i=l;i<=mid;i++) p++,dsu.unite(v[i][0],v[i][1]);
    f(mid+1,r,max(mid+1,last[mid]),optr);
  }
  while(p>0){
    p--;
    dsu.rollback();
  }
 
  if(l<mid){
    for(int i=optr;i>last[mid];i--) p++,dsu.unite(v[i][0],v[i][1]);
    f(l,mid-1,optl,last[mid]);
  }
  while(p>0){
    p--;
    dsu.rollback();
  }
}

void _(){
  cin >> n >> m >> q;
  last.resize(m+5);
  v.resize(m+5);
  for(int i=1;i<=m;i++) cin >> v[i][0] >> v[i][1];
  
  int p=0,hm=1;
  for(int i=1;i<=m;i++){
    p++;
    if(!dsu.unite(v[i][0],v[i][1])) break;
    hm++;
  }
  while(p>0){
    p--;
    dsu.rollback();
  }  

  for(int i=hm+1;i<=m;i++) last[i]=m+1;
 
  if(hm<=m) f(1,hm,1,m);

  while(q--){
    int l,r;
    cin >> l >> r;
    if(last[l]<=r) cout << "NO\n";
    else cout << "YES\n";
  }
}

int32_t main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tc=1;//cin >> tc;
  while(tc--) _();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Incorrect 3 ms 5136 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Incorrect 3 ms 5136 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 140 ms 13128 KB Output is correct
4 Correct 1549 ms 22488 KB Output is correct
5 Incorrect 155 ms 15728 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Incorrect 3 ms 5136 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Incorrect 3 ms 5136 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Incorrect 3 ms 5136 KB Output isn't correct
5 Halted 0 ms 0 KB -