QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#612546#9449. New School Termucup-team008#RE 1ms3844kbC++176.2kb2024-10-05 11:51:582024-10-05 11:52:04

Judging History

你现在查看的是最新测评结果

  • [2024-10-05 11:52:04]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3844kb
  • [2024-10-05 11:51:58]
  • 提交

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef int64_t ll;

struct disjoint_set {
  vector<int> p, sz;
  disjoint_set(int n) {
    p.resize(n); fill(all(p), -1);
    sz.resize(n); fill(all(sz), 1);
  }
  int find(int x) {
    return p[x] < 0 ? x : (p[x] = find(p[x]));
  }
  int getsz(int x) {
    return sz[find(x)];
  }
  bool merge(int x, int y) {
    // x goes to y
    x = find(x);
    y = find(y);
    if(x == y) return false;
    p[x] = y;
    sz[y] += sz[x];
    return true;
  }
};

void solve() {
  int n, m;
  cin >> n >> m;
  vector<array<int, 2>> v(m);
  for(auto& x: v) {
    cin >> x[0] >> x[1];
    x[0]--; x[1]--;
  }
  reverse(all(v));
  disjoint_set dsu(4*n);
  for(int i = 2*n; i < 4*n; i++) dsu.sz[i] = -1;
  vector<int> compcount(4*n+1);
  compcount[1] = 2*n;
  int wantsz = 2*n;
  vector<int> used(2*n+1);
  auto can = [&]() -> bool {
    // can you make (wantsz/2) size
    bitset<20005> dp;
    dp.set(0);
    for(int sz = 1; !dp[wantsz] && 2*sz <= wantsz; sz++) {
      vector<int> touched;
      if(compcount[sz] == 0) continue;
      for(int a = sz; a <= wantsz; a++) {
        if(!dp[a-sz]) continue;
        if(dp[a]) continue;
        if(used[a-sz] < compcount[sz]) {
          used[a] = used[a-sz] + 1;
          touched.pb(a);
          dp.set(a);
        }
      }
      for(int out: touched) used[out] = 0;
    }
    return dp[wantsz];
  };
  for(auto [a, b]: v) {
    int agood = dsu.find(a);
    int bgood = dsu.find(b);
    if(agood == bgood) continue;
    int bbad = dsu.find(b+2*n);
    if(agood == bbad) continue;
    int agoodsz = abs(dsu.getsz(agood));
    int bbadsz = abs(dsu.getsz(bbad));
    int combine = abs(dsu.getsz(agood) + dsu.getsz(bbad));
    wantsz += combine - agoodsz - bbadsz;
    compcount[agoodsz]--; compcount[bbadsz]--; compcount[combine]++;
    if(can()) {
      dsu.merge(a, b+2*n);
      dsu.merge(b, a+2*n);
      continue;
    }
    // could not make it, undo
    wantsz -= combine - agoodsz - bbadsz;
    compcount[agoodsz]++; compcount[bbadsz]++; compcount[combine]--;
    // merge agood and bgood then
    int bgoodsz = abs(dsu.getsz(bgood));
    combine = abs(dsu.getsz(agood) + dsu.getsz(bgood));
    compcount[agoodsz]--; compcount[bgoodsz]--; compcount[combine]++;
    dsu.merge(a, b);
    wantsz += combine - agoodsz - bgoodsz;
    dsu.merge(a+2*n, b+2*n);
  }
  vector<int> ret(4*n+1, -1);
  bitset<20005> dp;
  dp.set(0);
  vector<int> par(4*n+1, -1);
  // start to reconstruct
  for(int i = 0; i < 2*n; i++) {
    int root = dsu.find(i);
    if(ret[root] != -1) continue;
    ret[root] = -39; ret[dsu.find(i+2*n)] = -39;
    int sz = abs(dsu.getsz(root));
    for(int a = wantsz/2; a >= sz; a--) {
      if(dp[a-sz] && !dp[a]) {
        dp.set(a);
        par[a] = i;
      }
    }
  }
  wantsz /= 2;
  while(wantsz) {
    int root = par[wantsz];
    assert(root >= 0);
    ret[root] = (dsu.getsz(root) > 0 ? 1 : 0);
    ret[dsu.find(root+2*n)] = 1 - ret[root];
    wantsz -= abs(dsu.getsz(root));
  }
  int ans = 0;
  for(int i = 0; i < 2*n; i++) {
    int root = dsu.find(i);
    if(ret[root] < 0) {
      assert(ret[root] == -39);
      ret[root] = (dsu.getsz(root) < 0 ? 1 : 0);
      ret[dsu.find(i+2*n)] = 1 - ret[root];
    }
    cout << ret[root];
    ans += ret[root];
  }
  assert(ans == n);
  cout << "\n";
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3568kb

input:

2 4
1 3
2 4
1 4
1 2

output:

0101

result:

ok Output is valid. OK

Test #2:

score: 0
Accepted
time: 0ms
memory: 3808kb

input:

3 7
2 5
1 3
4 6
2 6
4 5
2 4
5 6

output:

001101

result:

ok Output is valid. OK

Test #3:

score: 0
Accepted
time: 1ms
memory: 3844kb

input:

1 0

output:

10

result:

ok Output is valid. OK

Test #4:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

2 3
2 4
3 4
1 2

output:

0110

result:

ok Output is valid. OK

Test #6:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

3 8
4 6
3 5
1 4
2 4
1 6
1 2
3 4
4 5

output:

010101

result:

ok Output is valid. OK

Test #7:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

4 9
4 7
3 8
1 5
2 7
2 8
6 8
7 8
1 4
1 6

output:

01010110

result:

ok Output is valid. OK

Test #8:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

5 16
3 6
9 10
2 7
1 10
1 5
2 10
3 5
5 6
3 4
2 5
4 5
3 8
4 7
6 8
1 6
7 10

output:

0010111010

result:

ok Output is valid. OK

Test #9:

score: -100
Runtime Error

input:

6 13
4 5
2 9
3 8
4 8
4 11
10 12
3 4
3 9
5 11
2 8
5 10
5 8
1 11

output:


result: