AtCoder Regular Contest 036

Submission #1101514

Source codeソースコード

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
template<class T> using vv=vector<vector< T > >;

struct UF {
  vector<int> par; // parent
  vector<int> sizes;
  vi odd_edge;
  deque<bool> include_odd;
  UF(int n) : par(n), sizes(n, 1), odd_edge(n, -1), include_odd(n, false) {
    for (int i = 0; i < n; ++i) {
      par[i] = i;
    }
  }
  int root(int x) {
    if (x == par[x]) {
      return x;
    }
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (sizes[x] < sizes[y]) {
      swap(x, y);
    }
    par[y] = x;
    sizes[x] += sizes[y];
    sizes[y] = 0;
  }
  void unite_even(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (odd_edge[x] == y && odd_edge[y] == x) {
      odd_edge[x] = -1;
      odd_edge[y] = -1;
      unite(x, y);
      x = root(x);
      include_odd[x] = true;
      return;
    }
    if (include_odd[x] || include_odd[y]) {
      if (include_odd[x]) {
        swap(x, y);
      }
      if (odd_edge[x] != -1) {
        int v = odd_edge[x];
        odd_edge[v] = -1;
        odd_edge[x] = -1;
        unite(x, y);
        unite(x, v);
        x = root(x);
        include_odd[x] = true;
        return;
      }
      unite(x, y);
      return;
    }
    if (odd_edge[x] != -1) {
      swap(x, y);
    }
    if (odd_edge[y] != -1) {
      int u = odd_edge[y];
      if (odd_edge[x] != -1) {
        int v = odd_edge[x];
        odd_edge[u] = -1;
        odd_edge[v] = -1;
        unite(u, v);
        odd_edge[x] = -1;
        odd_edge[y] = -1;
        unite(x, y);
        u = root(u);
        y = root(y);
        odd_edge[u] = y;
        odd_edge[y] = u;
        return;
      }
      odd_edge[y] = -1;
      unite(x, y);
      x = root(x);
      odd_edge[u] = x;
      odd_edge[x] = u;
      return;
    }
    unite(x, y);
    return;
  }
  void unite_odd(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      if (odd_edge[x] != -1) {
        int v = odd_edge[x];
        odd_edge[v] = -1;
        odd_edge[x] = -1;
        unite(x, v);
        x = root(x);
      }
      include_odd[x] = true;
      return;
    }
    if (odd_edge[x] == y && odd_edge[y] == x) {
      return;
    }
    if (include_odd[x] || include_odd[y]) {
      int u = -1;
      if (odd_edge[x] != -1) {
        u = x;
      } else if (odd_edge[y] != -1) {
        u = y;
      }
      if (u != -1) {
        int v = odd_edge[u];
        odd_edge[u] = -1;
        odd_edge[v] = -1;
        unite(u, v);
      }
      unite(x, y);
      x = root(x);
      include_odd[x] = true;
      return;
    }
    int u, v;
    u = v = -1;
    if (odd_edge[x] != -1) {
      swap(x, y);
    }
    if (odd_edge[y] != -1) {
      u = odd_edge[y];
      odd_edge[u] = -1;
      odd_edge[y] = -1;
      if (odd_edge[x] != -1) {
        v = odd_edge[x];
        odd_edge[v] = -1;
        odd_edge[x] = -1;
        unite(y, v);
        unite(x, u);
        u = root(u);
        v = root(v);
        odd_edge[u] = v;
        odd_edge[v] = u;
        return;
      }
      unite(x, u);
      x = root(x);
      odd_edge[x] = y;
      odd_edge[y] = x;
      return;
    }
    odd_edge[x] = y;
    odd_edge[y] = x;
    return;
  }
  void unite_main(int x, int y, int z) {
    if ((z&1) == 0) {
      unite_even(x, y);
    }
    else {
      unite_odd(x, y);
    }
    return;
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int size(int x) {
    return sizes[root(x)];
  }
};

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  UF uf(n);
  int w, x, y, z;
  rep (query, q) {
    scanf("%d %d %d %d", &w, &x, &y, &z);
    x -= 1;
    y -= 1;
    if (w == 2) {
      if (uf.same(x, y)) {
        printf("YES\n");
      } else {
        printf("NO\n");
      }
      continue;
    } else {
      uf.unite_main(x, y, z);
    }
  }

  return 0;
}

Submission

Task問題 D - 偶数メートル
User nameユーザ名 mu
Created time投稿日時
Language言語 C++14 (Clang++ 3.4)
Status状態 AC
Score得点 100
Source lengthソースコード長 4903 Byte
File nameファイル名
Exec time実行時間 70 ms
Memory usageメモリ使用量 2204 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - sample_01.txt,sample_02.txt,sample_03.txt
Subtask1 30 / 30 sample_01.txt,sample_02.txt,sample_03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask1_14.txt,subtask1_15.txt,subtask1_16.txt,subtask1_17.txt,subtask1_18.txt,subtask1_19.txt,subtask1_20.txt,subtask1_21.txt,subtask1_22.txt,subtask1_23.txt,subtask1_24.txt,subtask1_25.txt,subtask1_26.txt,subtask1_27.txt,subtask1_28.txt,subtask1_29.txt
Subtask2 70 / 70 sample_01.txt,sample_02.txt,sample_03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask1_14.txt,subtask1_15.txt,subtask1_16.txt,subtask1_17.txt,subtask1_18.txt,subtask1_19.txt,subtask1_20.txt,subtask1_21.txt,subtask1_22.txt,subtask1_23.txt,subtask1_24.txt,subtask1_25.txt,subtask1_26.txt,subtask1_27.txt,subtask1_28.txt,subtask1_29.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt,subtask2_13.txt,subtask2_14.txt,subtask2_15.txt,subtask2_16.txt,subtask2_17.txt,subtask2_18.txt,subtask2_19.txt,subtask2_20.txt,subtask2_21.txt,subtask2_22.txt,subtask2_23.txt,subtask2_24.txt,subtask2_25.txt,subtask2_26.txt,subtask2_27.txt,subtask2_28.txt,subtask2_29.txt,subtask2_30.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
sample_01.txt AC 31 ms 816 KB
sample_02.txt AC 17 ms 924 KB
sample_03.txt AC 17 ms 792 KB
subtask1_01.txt AC 17 ms 792 KB
subtask1_02.txt AC 17 ms 920 KB
subtask1_03.txt AC 18 ms 924 KB
subtask1_04.txt AC 17 ms 924 KB
subtask1_05.txt AC 17 ms 792 KB
subtask1_06.txt AC 17 ms 792 KB
subtask1_07.txt AC 17 ms 792 KB
subtask1_08.txt AC 17 ms 924 KB
subtask1_09.txt AC 18 ms 924 KB
subtask1_10.txt AC 18 ms 924 KB
subtask1_11.txt AC 20 ms 920 KB
subtask1_12.txt AC 18 ms 920 KB
subtask1_13.txt AC 18 ms 920 KB
subtask1_14.txt AC 19 ms 784 KB
subtask1_15.txt AC 19 ms 920 KB
subtask1_16.txt AC 19 ms 924 KB
subtask1_17.txt AC 19 ms 856 KB
subtask1_18.txt AC 18 ms 792 KB
subtask1_19.txt AC 19 ms 924 KB
subtask1_20.txt AC 18 ms 920 KB
subtask1_21.txt AC 18 ms 796 KB
subtask1_22.txt AC 18 ms 796 KB
subtask1_23.txt AC 19 ms 796 KB
subtask1_24.txt AC 18 ms 788 KB
subtask1_25.txt AC 18 ms 792 KB
subtask1_26.txt AC 19 ms 920 KB
subtask1_27.txt AC 19 ms 920 KB
subtask1_28.txt AC 18 ms 732 KB
subtask1_29.txt AC 18 ms 920 KB
subtask2_01.txt AC 36 ms 1108 KB
subtask2_02.txt AC 61 ms 1940 KB
subtask2_03.txt AC 20 ms 1168 KB
subtask2_04.txt AC 60 ms 1692 KB
subtask2_05.txt AC 38 ms 2068 KB
subtask2_06.txt AC 39 ms 1688 KB
subtask2_07.txt AC 58 ms 2072 KB
subtask2_08.txt AC 63 ms 1680 KB
subtask2_09.txt AC 47 ms 1300 KB
subtask2_10.txt AC 68 ms 2076 KB
subtask2_11.txt AC 69 ms 2064 KB
subtask2_12.txt AC 68 ms 2076 KB
subtask2_13.txt AC 68 ms 2072 KB
subtask2_14.txt AC 70 ms 2076 KB
subtask2_15.txt AC 70 ms 2072 KB
subtask2_16.txt AC 68 ms 2072 KB
subtask2_17.txt AC 67 ms 2008 KB
subtask2_18.txt AC 70 ms 2200 KB
subtask2_19.txt AC 69 ms 2072 KB
subtask2_20.txt AC 68 ms 2200 KB
subtask2_21.txt AC 60 ms 2080 KB
subtask2_22.txt AC 58 ms 2068 KB
subtask2_23.txt AC 57 ms 2204 KB
subtask2_24.txt AC 57 ms 2076 KB
subtask2_25.txt AC 58 ms 2200 KB
subtask2_26.txt AC 59 ms 2200 KB
subtask2_27.txt AC 57 ms 2072 KB
subtask2_28.txt AC 57 ms 2200 KB
subtask2_29.txt AC 58 ms 2192 KB
subtask2_30.txt AC 67 ms 1436 KB