結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー popofypopofy
提出日時 2023-08-12 14:09:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 9,001 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 222,384 KB
実行使用メモリ 16,232 KB
最終ジャッジ日時 2024-04-30 05:10:01
合計ジャッジ時間 3,913 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 41 ms
14,404 KB
testcase_04 AC 21 ms
13,572 KB
testcase_05 AC 21 ms
7,040 KB
testcase_06 AC 34 ms
13,880 KB
testcase_07 AC 20 ms
6,944 KB
testcase_08 AC 40 ms
16,232 KB
testcase_09 AC 27 ms
8,064 KB
testcase_10 AC 40 ms
13,164 KB
testcase_11 AC 34 ms
12,388 KB
testcase_12 AC 26 ms
10,872 KB
testcase_13 AC 12 ms
6,944 KB
testcase_14 AC 22 ms
8,704 KB
testcase_15 AC 15 ms
6,944 KB
testcase_16 AC 35 ms
10,240 KB
testcase_17 AC 9 ms
6,940 KB
testcase_18 AC 27 ms
12,288 KB
testcase_19 AC 20 ms
12,672 KB
testcase_20 AC 16 ms
6,944 KB
testcase_21 AC 15 ms
7,040 KB
testcase_22 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if !__INCLUDE_LEVEL__
#include __FILE__

using PAIR = pair<int,int>;

struct dsu {
  int _n;
  V<int> parent_or_size, diffWeight, nextV;
  dsu() : _n(0) {}
  explicit dsu(int n) : _n(n), parent_or_size(n, -1), diffWeight(n, 0), nextV(n, 0) {
    iota(all(nextV), 0);
  }

  int merge(int a, int b) {
    assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
    int x = leader(a), y = leader(b);
    if (x == y) return x;
    if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
    swap(nextV[x], nextV[y]);
    parent_or_size[x] += parent_or_size[y];
    parent_or_size[y] = x;
    return x;
  }

  int merge(int a, int b, int w) {
    int x = leader(a), y = leader(b);
    if (x == y) return x;
    w += weight(a) - weight(b);
    if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y), w = -w;
    parent_or_size[x] += parent_or_size[y];
    parent_or_size[y] = x;
    diffWeight[y] = w;
    return x;
  }

  int leader(int a) {
    assert(0 <= a && a < _n);
    if (parent_or_size[a] < 0) return a;
    int r = leader(parent_or_size[a]);
    diffWeight[a] += diffWeight[parent_or_size[a]];
    return parent_or_size[a] = r;
  }

  bool same(int a, int b) {return leader(a) == leader(b);}
  int weight(int a) {leader(a);return diffWeight[a];}
  int diff(int x, int y) {return weight(y) - weight(x);}
  int size(int a) {return -parent_or_size[leader(a)];}

  VV<int> groups() {
    V<int> leader_buf(_n), group_size(_n);
    for (int i = 0; i < _n; i++) {
      leader_buf[i] = leader(i);
      group_size[leader_buf[i]]++;
    }
    VV<int> result(_n);
    for (int i = 0; i < _n; i++) result[i].reserve(group_size[i]);
    for (int i = 0; i < _n; i++) result[leader_buf[i]].push_back(i);
    result.erase(std::remove_if(result.begin(), result.end(), [&](const V<int>& v) { return v.empty(); }), result.end());
    return result;
  }

  V<int> enumerate(int u) {
    V<int> ret{u};
    int now = u;
    while (u != nextV[now]) ret.push_back(now = nextV[now]);
    sort(all(ret));
    return ret;
  }
};

struct Solver {
  void solve() {
    INT(n,m);
    int n2 = n * 2;
    dsu uf(n2);
    rep(i,m) {
      int a, b; cin >> a >> b; --a, --b;
      if (!uf.same(a,b)) uf.merge(a,b);
    }
    int sum = 0;
    for(auto e: uf.groups()) {
      sum += sz(e) % 2;
    }
    dump(uf.groups());
    print(sum/2);
  }
} solver;

signed main(void){
  NO_SYNC_STD;
  dump("");
#ifdef __OPTIMIZE
  dump("OPTIMIZE ON");
#endif
#ifdef __INTERACTIVE
  dump("INTERACTIVE MODE");
#endif
#ifdef __INCLUDE_ATCODER_LIB
  dump("INCLUDE ATCODER LIB");
#endif
  solver.solve();
  return 0;
}

#else
// #define __OPTIMIZE
// #define __INCLUDE_ATCODER_LIB
// #define __INTERACTIVE
#define _GLIBCXX_DEQUE_BUF_SIZE 64
#ifdef __OPTIMIZE
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#ifdef __INCLUDE_ATCODER_LIB
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
#define overload4(a, b, c, d, e, ...) e
#define rep1(a)          for(decltype(a) i = 0, i##_len = (a); i < i##_len; ++i)
#define rep2(i, a)       for(decltype(a) i = 0, i##_len = (a); i < i##_len; ++i)
#define rep3(i, a, b)    for(decltype(b) i = (a), i##_len = (b); i < i##_len; ++i)
#define rep4(i, a, b, c) for(decltype(b) i = (a), i##_len = (b); i < i##_len; i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(a)          for(decltype(a) i = (a); i--;)
#define rrep2(i, a)       for(decltype(a) i = (a); i--;)
#define rrep3(i, a, b)    for(decltype(a) i = (b), i##_len = (a); i-- > i##_len;)
#define rrep4(i, a, b, c) for(decltype(a) i = (a)+((b)-(a)-1)/(c)*(c), i##_len = (a); i >= i##_len; i -= c)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define yes(s) cout << ((s)?"Yes":"No") << "\n";
#define bit(n) (1LL << ((int)(n)))
#define get1bit(x,n) (((x) >> (int)(n)) & 1)
#define INF ((1 << 30) - 1)
#define INFL (1LL << 60)
#define PRECISION std::setprecision(16)
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define VEC(type, name, size) vector<type> name(size); input(name)
#ifdef __INTERACTIVE
#define NO_SYNC_STD
#define ENDL std::endl
#else
#define NO_SYNC_STD std::cin.tie(nullptr);ios::sync_with_stdio(false)
#define ENDL "\n"
#endif
#ifdef __LOCAL
#include <dump.hpp>
#define dump(...) DUMPOUT << "  " << string(#__VA_ARGS__) << ": " << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl << "    ", dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
using ll=long long;using ull=unsigned long long;using ld=long double;template<class T> using V=vector<T>;template<class T> using VV=vector<vector<T>>;template<class T> using PQ=priority_queue<T,V<T>,greater<T>>;
template<class T> istream &operator>>(istream &is,complex<T> &v){T x,y; is >> x >> y;v.real(x);v.imag(y);return is;}
template<class T> istream &operator>>(istream &is,V<T> &v){for(auto&& e:v)is >> e;return is;}
template<class T,class U> istream &operator>>(istream &is,pair<T,U> &v){is >> v.first >> v.second;return is;}
template<class T,size_t n> istream &operator>>(istream &is,array<T,n> &v){for(auto&& e:v)is >> e;return is;}
template<class... A> void input(A&&... args){(cin >> ... >> args);}
template<class... A> void print_rest(){cout << ENDL;}
template<class T,class... A> void print_rest(const T& first,const A&... rest){cout << " " << first;print_rest(rest...);}
template<class T,class... A> void print(const T& first,const A&... rest){cout << fixed << PRECISION << first;print_rest(rest...);}
template<class T,class... A> void die(const T& first,const A&... rest){cout << fixed << PRECISION << first;print_rest(rest...);exit(0);}
template<class T> inline string join(const T& v,string sep=" "){if(v.size() == 0)return "";stringstream ss;for(auto&& e:v)ss << sep << e;return ss.str().substr(sz(sep));}
V<string> split(const string &s,char sep=' ') {V<string> ret;stringstream ss(s);string buf;while(getline(ss,buf,sep))ret.push_back(buf);return ret;}
template<class T> inline string padding(const T& v,int length,char pad=' ',bool left=false){stringstream ss;ss << (left?std::left:std::right) << setw(length) << setfill(pad) << v;return ss.str();}
template<class T> V<T> make_vec(size_t n,T a){return V<T>(n,a);}
template<class... Ts> auto make_vec(size_t n,Ts... ts){return V<decltype(make_vec(ts...))>(n,make_vec(ts...));}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;} return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;} return 0;}
template<class T,class F> pair<T,T> binarysearch(T ng,T ok,T eps,F f,bool sign=false){while(abs(ng-ok)>eps){auto mid=ng+(ok-ng)/2;if(sign^f(mid)){ok=mid;}else{ng=mid;}}return{ng,ok};}
template<class T> constexpr T cdiv(T x,T y){return (x+y-1)/y;}
template<class T> constexpr bool between(T a,T x,T b){return(a<=x&&x<b);}
template<class T> inline int lower_index(V<T>&a,T x){return (int)distance(a.begin(),lower_bound(all(a),x));}
template<class T> inline int upper_index(V<T>&a,T x){return (int)distance(a.begin(),upper_bound(all(a),x));}
template<class T> inline V<T> transposed(V<T>& A){int h=sz(A),w=sz(A[0]);V<T> tA(w);rep(i,h)rep(j,w)tA[j].push_back(A[i][j]);return tA;}
template<class T> constexpr T pos1d(T y,T x,T h,T w){assert(between(T(0),y,h));assert(between(T(0),x,w));return y*w+x;}
template<class T> constexpr pair<T,T> pos2d(T p,T h,T w){T y=p/w,x=p-y*w;assert(between(T(0),y,h));assert(between(T(0),x,w));return{y,x};}
template<class T> constexpr T sign(T n) {return (n > 0) - (n < 0);}
inline string upper(string s) {for(auto&& e: s) e = between('a',e,(char)('z'+1)) ? e - ('a'-'A') : e;return s;}
inline string lower(string s) {for(auto&& e: s) e = between('A',e,(char)('Z'+1)) ? e + ('a'-'A') : e;return s;}
inline string replace(string s, map<char, int> &from, V<int> &to) {for (auto&& e: s) e = '0' + (char)(to[from[e]]);return s;}
constexpr ll modpow(ll x,ll n,ll m=1152921504606846976LL){ll ret=1;for(;n>0;x=x*x%m,n>>=1)if(n&1)ret=ret*x%m;return ret;}
constexpr ll safe_mod(ll x, ll m) {x%=m;if(x<0)x+=m;return x;}
constexpr ll keta(ll n, ll base = 10LL) {ll ret = 0; while(n > 0) {n /= base, ret++;} return ret;}
constexpr int pcnt(ll x) {return __builtin_popcountll(x);}
constexpr int flog(ll x) {return 63 - __builtin_clzll(x);}
constexpr int clog(ll x) {return (x==1LL)?0:(64-__builtin_clzll(x-1LL));}
constexpr ll nC2(ll n) {return n*(n-1)/2;}
constexpr ld deg2rad(ll degree){return (ld)degree * M_PI/180;}
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx6[6] = {1, 0, -1, 0, 1, -1};
const int dy6[6] = {0, 1, 0, -1, 1, -1};
const int dx8[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy8[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#endif
0