Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/aoj/2885.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/2885
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
// 2色 + 連結成分
#include <iostream>
#include <vector>
#include <bitset>
#include "src/DataStructure/UnionFind_Potentialized.hpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
 for (int N, M; cin >> N >> M && N;) {
  UnionFind_Potentialized<bool> uf(N);
  bool isok= true;
  for (int i= 0; i < M; ++i) {
   int u, v;
   cin >> u >> v;
   isok&= uf.unite(--u, --v, 1);
  }
  if (!isok) {
   cout << 0 << '\n';
   continue;
  }
  vector<int> cnt(N);
  for (int i= 0; i < N; ++i) cnt[uf.leader(i)]+= uf.potential(i);
  bitset<1010> dp;
  dp[0]= 1;
  for (int i= 0; i < N; ++i) {
   if (i != uf.leader(i)) continue;
   dp= (dp << cnt[i]) | (dp << (uf.size(i) - cnt[i]));
  }
  vector<int> ans;
  for (int i= 0; i <= N; i+= 2)
   if (dp[i]) ans.push_back(i / 2);
  cout << ans.size() << '\n';
  for (int a: ans) cout << a << '\n';
 }
 return 0;
}
#line 1 "test/aoj/2885.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/2885
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
// 2色 + 連結成分
#include <iostream>
#include <vector>
#include <bitset>
#line 3 "src/DataStructure/UnionFind_Potentialized.hpp"
#include <algorithm>
#include <cassert>
template <class weight_t> class UnionFind_Potentialized {
 std::vector<int> par;
 std::vector<weight_t> val;
public:
 UnionFind_Potentialized(int n): par(n, -1), val(n) {}
 int leader(int u) {
  if (par[u] < 0) return u;
  int r= leader(par[u]);
  if constexpr (std::is_same_v<weight_t, bool>) val[u]= val[u] ^ val[par[u]];
  else val[u]= val[par[u]] + val[u];
  return par[u]= r;
 }
 //  -p(v) + p(u) = w
 bool unite(int u, int v, weight_t w) {
  int a= leader(u), b= leader(v);
  if constexpr (std::is_same_v<weight_t, bool>) w^= val[u] ^ val[v];
  else w= val[v] + w - val[u];
  if (a == b) return w == weight_t();
  if (par[b] > par[a]) std::swap(a, b), w= -w;
  return par[b]+= par[a], par[a]= b, val[a]= w, true;
 }
 bool connected(int u, int v) { return leader(u) == leader(v); }
 int size(int u) { return -par[leader(u)]; }
 weight_t potential(int u) { return leader(u), val[u]; }
 //  -p(v) + p(u)
 weight_t diff(int u, int v) {
  if constexpr (std::is_same_v<weight_t, bool>) return potential(u) ^ potential(v);
  else return -potential(v) + potential(u);
 }
};
#line 9 "test/aoj/2885.test.cpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
 for (int N, M; cin >> N >> M && N;) {
  UnionFind_Potentialized<bool> uf(N);
  bool isok= true;
  for (int i= 0; i < M; ++i) {
   int u, v;
   cin >> u >> v;
   isok&= uf.unite(--u, --v, 1);
  }
  if (!isok) {
   cout << 0 << '\n';
   continue;
  }
  vector<int> cnt(N);
  for (int i= 0; i < N; ++i) cnt[uf.leader(i)]+= uf.potential(i);
  bitset<1010> dp;
  dp[0]= 1;
  for (int i= 0; i < N; ++i) {
   if (i != uf.leader(i)) continue;
   dp= (dp << cnt[i]) | (dp << (uf.size(i) - cnt[i]));
  }
  vector<int> ans;
  for (int i= 0; i <= N; i+= 2)
   if (dp[i]) ans.push_back(i / 2);
  cout << ans.size() << '\n';
  for (int a: ans) cout << a << '\n';
 }
 return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-13 testcase_00 :heavy_check_mark: AC 10 ms 4 MB
g++-13 testcase_01 :heavy_check_mark: AC 9 ms 4 MB
clang++-18 testcase_00 :heavy_check_mark: AC 10 ms 4 MB
clang++-18 testcase_01 :heavy_check_mark: AC 9 ms 4 MB
Back to top page