Submission #74535909


Source Code Expand

#include<bits/stdc++.h>

// #define XAERIC666
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#ifndef XAERIC666
  #define xaeric(...) {fprintf(stderr,__VA_ARGS__);fflush(stderr);}
#else
  #define xaeric(...) 114514
#endif
int read(){
  int x=0,f=1;
  char c=getchar();
  while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();}
  while('0'<=c&&c<='9'){x=x*10+c-48;c=getchar();}
  return x*f;
}
// void in(int*a,int n){for(int i=1;i<=n;i++)a[i]=read();}
// void out(int*a,int n){for(int i=1;i<=n;i++)printf("%lld%c",a[i]," \n"[i==n]);}
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define _rep(i,a,b) for(int i=(b);i>=(a);--i)
#define YES puts("YES");
#define NO puts("NO");
using namespace std;

const int P=998244353;
const int N=4e5+5;
int n,m;
int fa[N],c[N][2],ans;
int fd(int x){
  return fa[x]==x?x:fa[x]=fd(fa[x]);
}
void mrg(int x,int y){
  x=fd(x);
  y=fd(y);
  if(x!=y){
    ans-=min(c[x][0],c[x][1]);
    ans-=min(c[y][0],c[y][1]);
    c[y][0]+=c[x][0];
    c[y][1]+=c[x][1];
    ans+=min(c[y][0],c[y][1]);
    fa[x]=y;
  }
}

void solve(){
  n=read(),m=read();
  for(int i=1;i<=2*n;i++){
    fa[i]=i;
    if(i<=n){
      c[i][0]=1;
    }
    else{
      c[i][1]=1;
    }
  }
  bool f=0;
  for(int i=1;i<=m;i++){
    int u=read(),v=read();
    mrg(u,v+n);
    mrg(v,u+n);
    if(fd(u)==fd(u+n)){
      f=1;
    }
    if(fd(v)==fd(v+n)){
      f=1;
    }
    if(f){
      puts("-1");
    }
    else{
      printf("%lld\n",ans/2);
    }
  }  
}

signed main(){
  int TTTT=1;
  // int TTTT=read();
  while(TTTT--){
    solve();
  }
}

Submission Info

Submission Time
Task F - Make Bipartite 3
User Xaeric
Language C++23 (GCC 15.2.0)
Score 500
Code Size 1652 Byte
Status AC
Exec Time 52 ms
Memory 15460 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 25
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 02_corner_03.txt, 02_corner_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3692 KiB
00_sample_01.txt AC 1 ms 3868 KiB
01_random_00.txt AC 52 ms 13156 KiB
01_random_01.txt AC 46 ms 13084 KiB
01_random_02.txt AC 38 ms 13088 KiB
01_random_03.txt AC 29 ms 13216 KiB
01_random_04.txt AC 43 ms 12948 KiB
01_random_05.txt AC 45 ms 13028 KiB
01_random_06.txt AC 39 ms 12976 KiB
01_random_07.txt AC 38 ms 13216 KiB
01_random_08.txt AC 42 ms 13088 KiB
01_random_09.txt AC 40 ms 13152 KiB
01_random_10.txt AC 39 ms 13352 KiB
01_random_11.txt AC 41 ms 13224 KiB
01_random_12.txt AC 48 ms 13068 KiB
01_random_13.txt AC 49 ms 13088 KiB
01_random_14.txt AC 47 ms 12908 KiB
01_random_15.txt AC 47 ms 13156 KiB
01_random_16.txt AC 46 ms 13156 KiB
01_random_17.txt AC 47 ms 13100 KiB
02_corner_00.txt AC 38 ms 15460 KiB
02_corner_01.txt AC 38 ms 15004 KiB
02_corner_02.txt AC 38 ms 13800 KiB
02_corner_03.txt AC 39 ms 13472 KiB
02_corner_04.txt AC 1 ms 3812 KiB