prithivMLmods/Coder-Stat huggingface dataset:
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int A[M], B[M];
  int C[K], D[K];
  for (int i = 0; i < M; ++i) {
    cin >> A[i] >> B[i];
    --A[i];
    --B[i];
  }

  for (int i = 0; i < K; ++i) {
    cin >> C[i] >> D[i];
    --C[i];
    --D[i];
  }

  UnionFind uf(N);
  for (int i = 0; i < M; ++i) {
    uf.unite(A[i], B[i]);
  }

  int ans[N];
  fill(ans, ans + N, 0);
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      if (uf.same(i, j)) {
        ++ans[i];
        ++ans[j];
      }
    }
  }

  for (int i = 0; i < M; ++i) {
    if (uf.same(A[i], B[i])) {
      --ans[A[i]];
      --ans[B[i]];
    }
  }

  for (int i = 0; i < K; ++i) {
    if (uf.same(C[i], D[i])) {
      --ans[C[i]];
      --ans[D[i]];
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i != 0) {
      cout << " ";
    }
    cout << ans[i];
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int A[M], B[M];
  int C[K], D[K];
  for (int i = 0; i < M; ++i) {
    cin >> A[i] >> B[i];
    --A[i];
    --B[i];
  }

  for (int i = 0; i < K; ++i) {
    cin >> C[i] >> D[i];
    --C[i];
    --D[i];
  }

  UnionFind uf(N);
  for (int i = 0; i < M; ++i) {
    uf.unite(A[i], B[i]);
  }

  int ans[N];
  fill(ans, ans + N, 0);
  {
    int t[N];
    fill(t, t + N, 0);
    for (int i = 0; i < N; ++i) {
      ++t[uf.root(i)];
    }

    for (int i = 0; i < N; ++i) {
      ans[i] = t[uf.root(i)] - 1;
    }
  }

  for (int i = 0; i < M; ++i) {
    if (uf.same(A[i], B[i])) {
      --ans[A[i]];
      --ans[B[i]];
    }
  }

  for (int i = 0; i < K; ++i) {
    if (uf.same(C[i], D[i])) {
      --ans[C[i]];
      --ans[D[i]];
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i != 0) {
      cout << " ";
    }
    cout << ans[i];
  }
  cout << endl;

  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50010
struct uftree {
  int cnt;
  int rank;
  int parent;
} set[MAX];
int fr[100000 + 5];
void make_set(int n) {
  int i;
  for (i = 0; i <= n; i++) {
    set[i].cnt = 1;
    set[i].rank = 0;
    set[i].parent = i;
  }
}
int set_find(int x) {
  if (set[x].parent == x)
    return x;
  else
    return set[x].parent = set_find(set[x].parent);
}
void set_join(int x, int y) {
  int a = set_find(x);
  int b = set_find(y);
  if (set[a].rank > set[b].rank) {
    set[b].parent = a;
    if (a != b)
      set[a].cnt += set[b].cnt;
  } else {
    set[a].parent = b;
    if (a != b)
      set[b].cnt += set[a].cnt;
    if (set[a].rank == set[b].rank) {
      set[b].rank++;
    }
  }
}
int cmp(const void *a, const void *b) {
  uftree *p1 = (uftree *)a, *p2 = (uftree *)b;
  return p2->cnt - p1->cnt;
}
int query(int n) { return set[set_find(n)].cnt; }
int t, n, m, i, a, b, ans, k;
int main() {

  scanf("%d %d %d", &n, &m, &k);
  make_set(n);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    fr[a]++;
    fr[b]++;
    set_join(a, b);
  }
  for (int i = 1; i <= n; i++)
    fr[i] = query(i) - fr[i] - 1;
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &a, &b);
    if (set_find(a) == set_find(b))
      fr[a]--, fr[b]--;
  }
  for (int i = 1; i <= n; i++) {
    if (i != 1)
      printf(" ");
    printf("%d", fr[i]);
  }

  printf("\n");

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100010
struct uftree {
  int cnt;
  int rank;
  int parent;
} set[MAX];
int fr[100000 + 5];
void make_set(int n) {
  int i;
  for (i = 0; i <= n; i++) {
    set[i].cnt = 1;
    set[i].rank = 0;
    set[i].parent = i;
  }
}
int set_find(int x) {
  if (set[x].parent == x)
    return x;
  else
    return set[x].parent = set_find(set[x].parent);
}
void set_join(int x, int y) {
  int a = set_find(x);
  int b = set_find(y);
  if (set[a].rank > set[b].rank) {
    set[b].parent = a;
    if (a != b)
      set[a].cnt += set[b].cnt;
  } else {
    set[a].parent = b;
    if (a != b)
      set[b].cnt += set[a].cnt;
    if (set[a].rank == set[b].rank) {
      set[b].rank++;
    }
  }
}
int cmp(const void *a, const void *b) {
  uftree *p1 = (uftree *)a, *p2 = (uftree *)b;
  return p2->cnt - p1->cnt;
}
int query(int n) { return set[set_find(n)].cnt; }
int t, n, m, i, a, b, ans, k;
int main() {

  scanf("%d %d %d", &n, &m, &k);
  make_set(n);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    fr[a]++;
    fr[b]++;
    set_join(a, b);
  }
  for (int i = 1; i <= n; i++)
    fr[i] = query(i) - fr[i] - 1;
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &a, &b);
    if (set_find(a) == set_find(b))
      fr[a]--, fr[b]--;
  }
  for (int i = 1; i <= n; i++) {
    if (i != 1)
      printf(" ");
    printf("%d", fr[i]);
  }

  printf("\n");

  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

void dfs(int &m, int p, int t, vector<vector<int>> frd, vector<bool> &ald,
         vector<int> &group) {
  if (ald[p] == true) {
    return;
  }
  ald[p] = true;
  m++;
  group[p] = t;
  for (int x : frd[p]) {
    dfs(m, x, t, frd, ald, group);
  }
  return;
} // DFS　

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> frd(N + 1, vector<int>(0));
  vector<vector<int>> block(N + 1, vector<int>(0));
  for (int i = 0; i < M; i++) {
    int k, l;
    cin >> k >> l;
    frd[k].emplace_back(l);
    frd[l].emplace_back(k);
  }
  for (int i = 0; i < K; i++) {
    int k, l;
    cin >> k >> l;
    block[k].emplace_back(l);
    block[l].emplace_back(k);
  } // 友達関係、ブロック関係の配列取得
  vector<bool> ald_f(N + 1, false); // DFSしたかどうか
  vector<int> group(N + 1);
  vector<int> c(N); // 何回目のDFSで到達したか
  int t = 0;
  for (int i = 1; i < N + 1; i++) {
    if (ald_f[i] == false) {
      int m = 0;
      dfs(m, i, t, frd, ald_f, group);
      c[t] = m;
      t++;
    }
  } // DFSを行いｔ個にグルーピング
  for (int i = 1; i < N + 1; i++) {
    int max = c[group[i]];
    int h = group[i];
    int minus = frd[i].size();
    for (int x : block[i]) {
      if (group[x] == h) {
        minus++;
      }
    }
    cout << max - minus - 1 << " ";
  }
  // 所属するグループの要素数ー直接の友達ー同じグループでブロック関係の人数－１（自分）
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

void dfs(int &m, int p, int t, const vector<vector<int>> &frd,
         vector<bool> &ald, vector<int> &group) {
  if (ald[p] == true) {
    return;
  }
  ald[p] = true;
  m++;
  group[p] = t;
  for (int x : frd[p]) {
    dfs(m, x, t, frd, ald, group);
  }
  return;
} // DFS　

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> frd(N + 1, vector<int>(0));
  vector<vector<int>> block(N + 1, vector<int>(0));
  for (int i = 0; i < M; i++) {
    int k, l;
    cin >> k >> l;
    frd[k].emplace_back(l);
    frd[l].emplace_back(k);
  }
  for (int i = 0; i < K; i++) {
    int k, l;
    cin >> k >> l;
    block[k].emplace_back(l);
    block[l].emplace_back(k);
  } // 友達関係、ブロック関係の配列取得
  vector<bool> ald_f(N + 1, false); // DFSしたかどうか
  vector<int> group(N + 1);
  vector<int> c(N); // 何回目のDFSで到達したか
  int t = 0;
  for (int i = 1; i < N + 1; i++) {
    if (ald_f[i] == false) {
      int m = 0;
      dfs(m, i, t, frd, ald_f, group);
      c[t] = m;
      t++;
    }
  } // DFSを行いｔ個にグルーピング
  for (int i = 1; i < N + 1; i++) {
    int max = c[group[i]];
    int h = group[i];
    int minus = frd[i].size();
    for (int x : block[i]) {
      if (group[x] == h) {
        minus++;
      }
    }
    cout << max - minus - 1 << " ";
  }
  // 所属するグループの要素数ー直接の友達ー同じグループでブロック関係の人数－１（自分）
  cout << endl;
}
Problem p02762 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

struct Union_find {
  vector<int> par;
  vector<int> cnt;
  Union_find(int p) {
    par = vector<int>(p + 1);
    cnt = vector<int>(p + 1, 1);
    for (int i = 1; i <= p; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (cnt[x] < cnt[y]) {
      par[x] = y;
      cnt[y] += cnt[x];
    } else {
      par[y] = x;
      cnt[x] += cnt[y];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int get_cnt(int x) { return cnt[find(x)]; }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<bool>> b(n, vector<bool>(n + 1, false));
  vector<int> c(n + 1);
  Union_find f(n + 1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    f.unite(x, y);
    c[x]++;
    c[y]++;
  }
  vector<int> ans(n + 1);
  for (int i = 1; i <= n; i++) {
    ans[i] = f.get_cnt(i) - c[i] - 1;
  }
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    if (f.same(x, y)) {
      ans[x]--;
      ans[y]--;
    }
  }
  cout << ans[1];
  for (int i = 2; i <= n; i++) {
    cout << " " << ans[i];
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;

struct Union_find {
  vector<int> par;
  vector<int> cnt;
  Union_find(int p) {
    par = vector<int>(p + 1);
    cnt = vector<int>(p + 1, 1);
    for (int i = 1; i <= p; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (cnt[x] < cnt[y]) {
      par[x] = y;
      cnt[y] += cnt[x];
    } else {
      par[y] = x;
      cnt[x] += cnt[y];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int get_cnt(int x) { return cnt[find(x)]; }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> c(n + 1);
  Union_find f(n + 1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    f.unite(x, y);
    c[x]++;
    c[y]++;
  }
  vector<int> ans(n + 1);
  for (int i = 1; i <= n; i++) {
    ans[i] = f.get_cnt(i) - c[i] - 1;
  }
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    if (f.same(x, y)) {
      ans[x]--;
      ans[y]--;
    }
  }
  cout << ans[1];
  for (int i = 2; i <= n; i++) {
    cout << " " << ans[i];
  }
  cout << endl;
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1000000;

struct Unionfind {
  vector<int> parent;

  Unionfind(int N) { parent = vector<int>(N, -1); }

  int root(int x) { // 親の番号を検索する
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }

  int size(int x) { // 自分のグループの大きさを返す
    return -parent[root(x)];
  }

  bool unite(int x, int y) { // XとYを同じグループにする
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (size(x) < size(y))
      swap(x, y);
    parent[x] += parent[y];
    parent[y] = x;
    return true;
  }
  bool same(int x, int y) {
    if (root(x) == root(y))
      return true;
    return false;
  }
};
int fri[100005];
vector<int> bl[100005];

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  Unionfind uni(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    fri[a]++;
    fri[b]++;
    uni.unite(a, b);
  }
  rep(i, K) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }
  rep(i, N) {
    int ans;
    ans = uni.size(i) - 1 - fri[i];
    for (int u : bl[i]) {
      if (uni.same(i, bl[i][u]))
        ans--;
    }
    cout << ans << " ";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1000000;

struct Unionfind {
  vector<int> parent;

  Unionfind(int N) { parent = vector<int>(N, -1); }

  int root(int x) { // 親の番号を検索する
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }

  int size(int x) { // 自分のグループの大きさを返す
    return -parent[root(x)];
  }

  bool unite(int x, int y) { // XとYを同じグループにする
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (size(x) < size(y))
      swap(x, y);
    parent[x] += parent[y];
    parent[y] = x;
    return true;
  }
  bool same(int x, int y) {
    if (root(x) == root(y))
      return true;
    return false;
  }
};
int fri[100005];
vector<int> bl[100005];

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  Unionfind uni(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    fri[a]++;
    fri[b]++;
    uni.unite(a, b);
  }
  rep(i, K) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }
  rep(i, N) {
    int ans;
    ans = uni.size(i) - 1 - fri[i];
    for (int u : bl[i]) {
      if (uni.same(i, u))
        ans--;
    }
    cout << ans << " ";
  }
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};
// 直接友達な数
int deg[2002] = {0};
// to[i]がもつブロック関係な人
vector<int> to[100005];

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  // m--;k--;
  UnionFind node(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    deg[a]++;
    deg[b]++;
    node.unite(a, b);
  }
  for (int i = 0; i < k; i++) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    to[c].push_back(d);
    to[d].push_back(c);
  }
  for (int i = 0; i < n; i++) {
    int ans = node.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      // iがブロック関係の人数分引く
      if (node.same(i, u))
        --ans;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};
// 直接友達な数
int deg[100001] = {0};
// to[i]がもつブロック関係な人
vector<int> to[100005];

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  // m--;k--;
  UnionFind node(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    deg[a]++;
    deg[b]++;
    node.unite(a, b);
  }
  for (int i = 0; i < k; i++) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    to[c].push_back(d);
    to[d].push_back(c);
  }
  for (int i = 0; i < n; i++) {
    int ans = node.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      // iがブロック関係の人数分引く
      if (node.same(i, u))
        --ans;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int par[100003], fz[100005], bz[100005], cnt, dsz[100005];
vector<int> adj[100005], bdj[100005];

int dfs(int src, int pr) {
  par[src] = pr;
  int sz = fz[src];
  cnt++;

  for (int lp = 0; lp < sz; lp++) {
    int u = adj[src][lp];

    if (par[u] == 0) {
      dfs(u, pr);
    }
  }
}

int main() {

  int n, m, k, a, b, c, i, j, fr, sc, tr;
  scanf("%d %d %d", &n, &m, &k);

  for (i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    fz[a]++;
    adj[b].push_back(a);
    fz[b]++;
  }

  for (i = 1; i <= k; i++) {
    scanf("%d %d", &a, &b);
    bdj[a].push_back(b);
    bz[a]++;
    bdj[b].push_back(a);
    bz[b]++;
  }

  for (i = 1; i <= n; i++) {
    if (par[i] == 0) {
      cnt = 0;
      dfs(i, i);
      cnt--;
      dsz[i] = cnt;
    }
  }

  for (i = 1; i <= n; i++) {
    fr = dsz[par[i]] - fz[i];
    int sz = bz[i];

    for (int lp = 0; lp < sz; lp++) {
      int u = bdj[i][lp];
      if (par[u] == par[i])
        fr--;
      if (fr == 0)
        break;
    }

    printf("%d ", fr);

    // cout<<i<<"  "<<dsz[par[i]]<<"   "<<fz[i]<<endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int par[100003], fz[100005], bz[100005], cnt, dsz[100005];
vector<int> adj[100005], bdj[100005];

void dfs(int src, int pr) {
  par[src] = pr;
  int sz = fz[src];
  cnt++;

  for (int lp = 0; lp < sz; lp++) {
    int u = adj[src][lp];

    if (par[u] == 0) {
      dfs(u, pr);
    }
  }
}

int main() {

  int n, m, k, a, b, c, i, j, fr, sc, tr;
  scanf("%d %d %d", &n, &m, &k);

  for (i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    fz[a]++;
    adj[b].push_back(a);
    fz[b]++;
  }

  for (i = 1; i <= k; i++) {
    scanf("%d %d", &a, &b);
    bdj[a].push_back(b);
    bz[a]++;
    bdj[b].push_back(a);
    bz[b]++;
  }

  for (i = 1; i <= n; i++) {
    if (par[i] == 0) {
      cnt = 0;
      dfs(i, i);
      cnt--;
      dsz[i] = cnt;
    }
  }

  for (i = 1; i <= n; i++) {
    fr = dsz[par[i]] - fz[i];
    int sz = bz[i];

    for (int lp = 0; lp < sz; lp++) {
      int u = bdj[i][lp];
      if (par[u] == par[i])
        fr--;
      if (fr == 0)
        break;
    }

    printf("%d ", fr);

    // cout<<i<<"  "<<dsz[par[i]]<<"   "<<fz[i]<<endl;
  }

  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

const int MAX_N = 100001;

int par[MAX_N];
int rank[MAX_N];
int nele[MAX_N];

void init() {
  for (int i = 0; i < MAX_N; ++i) {
    par[i] = i;
    rank[i] = 0;
    nele[i] = 1;
  }
}

int root(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = root(par[x]);
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;

  nele[x] += nele[y];
  nele[y] = nele[x];

  if (rank[x] < rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank[x] == rank[y])
      ++rank[x];
  }
  return;
}

bool same(int x, int y) { return root(x) == root(y); }

int main() {
  init();

  int N, M, K;
  std::cin >> N >> M >> K;
  int A, B;
  int old_friend[N];
  std::fill(old_friend, old_friend + N, 0);
  for (int i = 0; i < M; ++i) {
    std::cin >> A >> B;
    --A;
    --B;
    unite(A, B);
    ++old_friend[A];
    ++old_friend[B];
  }
  std::vector<int> blocked[N];
  int C, D;
  for (int i = 0; i < K; ++i) {
    std::cin >> C >> D;
    --C;
    --D;
    blocked[C].push_back(D);
    blocked[D].push_back(C);
  }

  for (int i = 0; i < N; ++i) {
    int count = 0;
    for (int j = 0; j < N; ++j) {
      if (same(i, j))
        ++count;
    }
    for (int j = 0; j < blocked[i].size(); ++j) {
      if (same(i, blocked[i][j]))
        --count;
    }
    count -= old_friend[i] + 1;
    std::cout << count << ' ';
  }
  std::cout << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

const int MAX_N = 100001;

int par[MAX_N];
int rank[MAX_N];
int nele[MAX_N];

void init() {
  for (int i = 0; i < MAX_N; ++i) {
    par[i] = i;
    rank[i] = 0;
    nele[i] = 1;
  }
}

int root(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = root(par[x]);
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;

  nele[x] += nele[y];
  nele[y] = nele[x];

  if (rank[x] < rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank[x] == rank[y])
      ++rank[x];
  }
  return;
}

bool same(int x, int y) { return root(x) == root(y); }

int main() {
  init();

  int N, M, K;
  std::cin >> N >> M >> K;
  int A, B;
  int old_friend[N];
  std::fill(old_friend, old_friend + N, 0);
  for (int i = 0; i < M; ++i) {
    std::cin >> A >> B;
    --A;
    --B;
    unite(A, B);
    ++old_friend[A];
    ++old_friend[B];
  }
  std::vector<int> blocked[N];
  int C, D;
  for (int i = 0; i < K; ++i) {
    std::cin >> C >> D;
    --C;
    --D;
    blocked[C].push_back(D);
    blocked[D].push_back(C);
  }

  for (int i = 0; i < N; ++i) {
    int count = nele[root(i)];
    for (int j = 0; j < blocked[i].size(); ++j) {
      if (same(i, blocked[i][j]))
        --count;
    }
    count -= old_friend[i] + 1;
    std::cout << count << ' ';
  }
  std::cout << std::endl;
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using llint = long long int;

#define rep(i, c) for (int i = 0; i < (int)c; ++i)
// c 回繰り返す

//////////////////////////クラス構造体///////////////////////////
class UnionFind { // UnionFind木 Nの数に注意　0=<x<N　まで作られる
private:
  vector<int> par;

public:
  UnionFind(int N) : par(N, -1) {} // 初期化コンストラクタ 根を初期化
  // par[i]は根のとき負　それ以外非負　
  int root(int x) { // データxの根を返す　root(i)は必ず非負
    if (par[x] < 0)
      return x; // par[x]が負のときｘは木の根である
    return par[x] = root(par[x]); // 経路圧縮
  }

  void unite(int x, int y) { // xの根とyの根を同じにする
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return; // xの根とyの根が同じとき　なにもしない
    if (par[rx] > par[ry])
      swap(rx, ry); // ランク処理
    par[rx] += par[ry];
    par[ry] = rx; // 違うとき　根をおなじにする
  }

  bool same(int x, int y) { // 根が同じとき TRUE
    return root(x) == root(y);
  }
  int size(int x) { return -par[root(x)]; } // サイズを返す
};
///////////////////////////////////////////////////////////

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  UnionFind frie(N);
  int count[100010];
  vector<int> v[10010];

  rep(i, M) {
    int A, B;
    cin >> A >> B;
    --A;
    --B;
    count[A]++;
    count[B]++;
    frie.unite(A, B);
  }
  rep(i, K) {
    int C, D;
    cin >> C >> D;
    --C;
    --D;
    v[C].push_back(D);
    v[D].push_back(C);
  }
  rep(i, N) {
    int ans = frie.size(i) - 1 - count[i];
    for (int e : v[i]) {
      if (frie.same(i, e))
        --ans;
    }
    cout << ans << " ";
  }

  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using llint = long long int;

#define rep(i, c) for (int i = 0; i < (int)c; ++i)
// c 回繰り返す

//////////////////////////クラス構造体///////////////////////////
class UnionFind { // UnionFind木 Nの数に注意　0=<x<N　まで作られる
private:
  vector<int> par;

public:
  UnionFind(int N) : par(N, -1) {} // 初期化コンストラクタ 根を初期化
  // par[i]は根のとき負　それ以外非負　
  int root(int x) { // データxの根を返す　root(i)は必ず非負
    if (par[x] < 0)
      return x; // par[x]が負のときｘは木の根である
    return par[x] = root(par[x]); // 経路圧縮
  }

  void unite(int x, int y) { // xの根とyの根を同じにする
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return; // xの根とyの根が同じとき　なにもしない
    if (par[rx] > par[ry])
      swap(rx, ry); // ランク処理
    par[rx] += par[ry];
    par[ry] = rx; // 違うとき　根をおなじにする
  }

  bool same(int x, int y) { // 根が同じとき TRUE
    return root(x) == root(y);
  }
  int size(int x) { return -par[root(x)]; } // サイズを返す
};
///////////////////////////////////////////////////////////

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  UnionFind frie(N);
  int count[100010];
  vector<int> v[100010];

  rep(i, M) {
    int A, B;
    cin >> A >> B;
    --A;
    --B;
    count[A]++;
    count[B]++;
    frie.unite(A, B);
  }
  rep(i, K) {
    int C, D;
    cin >> C >> D;
    --C;
    --D;
    v[C].push_back(D);
    v[D].push_back(C);
  }
  rep(i, N) {
    int ans = frie.size(i) - 1 - count[i];
    for (int e : v[i]) {
      if (frie.same(i, e))
        --ans;
    }
    cout << ans << " ";
  }

  cout << endl;

  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int deg[10005];
vector<int> to[10005];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      if (uf.same(i, u))
        ans--;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int deg[100005];
vector<int> to[100005];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      if (uf.same(i, u))
        ans--;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)(x).size();
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
vector<int> siz;
vector<int> par;
vector<set<int>> f;
vector<int> b;
int find(int node) {
  while (node != par[node])
    node = par[node];

  return node;
}
void uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  if (siz[a] > siz[b])
    swap(a, b);
  siz[a] += siz[b];
  par[b] = a;
}
int32_t main() {
  int n = in();
  int m = in();
  int k = in();
  siz = vector<int>(n, 1);
  par = vector<int>(n);
  b = siz;
  f = vector<set<int>>(n);

  iota(par.begin(), par.end(), 0);
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--;
    v--;
    uni(u, v);
    f[u].insert(v);
    f[v].insert(u);
  }
  for (int i = 0; i < k; i++) {
    cin >> u >> v;
    u--;
    v--;
    if (find(u) == find(v)) {
      if (f[u].find(v) != f[u].end()) {
        f[v].erase(u);
        f[u].erase(v);
      }
      b[u]++;
      b[v]++;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << siz[find(i)] - (f[i].size() + b[i]) << " ";
  }
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)(x).size();
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
vector<int> siz;
vector<int> par;
vector<set<int>> f;
vector<int> b;
int find(int node) {
  while (node != par[node])
    node = par[node];

  return node;
}
void uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  if (siz[a] < siz[b]) {
    par[a] = b;
    siz[b] += siz[a];
  } else {
    par[b] = a;
    siz[a] += siz[b];
  }
}
int32_t main() {
  int n = in();
  int m = in();
  int k = in();
  siz = vector<int>(n, 1);
  par = vector<int>(n);
  b = siz;
  f = vector<set<int>>(n);

  iota(par.begin(), par.end(), 0);
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--;
    v--;
    uni(u, v);
    f[u].insert(v);
    f[v].insert(u);
  }
  for (int i = 0; i < k; i++) {
    cin >> u >> v;
    u--;
    v--;
    if (find(u) == find(v)) {
      if (f[u].find(v) != f[u].end()) {
        f[v].erase(u);
        f[u].erase(v);
      }
      b[u]++;
      b[v]++;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << siz[find(i)] - (f[i].size() + b[i]) << " ";
  }
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int s) {
    par.resize(s);
    rank.resize(s, 0);
    for (int i = 0; i < s; ++i)
      par[i] = i;
  }
  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
};

const int MAX_N = 100;
int Friend[MAX_N], Group[MAX_N];
int N, M, K;

int main() {
  scanf("%d %d %d", &N, &M, &K);
  UnionFind List(MAX_N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    --a, --b;
    List.unite(a, b);
    ++Friend[a], ++Friend[b];
  }
  for (int i = 0; i < N; ++i) {
    List.root(i);
  }
  for (int i = 0; i < N; ++i) {
    ++Group[List.par[i]];
  }
  for (int i = 0; i < N; ++i) {
    Group[i] = Group[List.par[i]];
  }
  for (int i = 0; i < K; ++i) {
    int c, d;
    scanf("%d %d", &c, &d);
    --c, --d;
    if (List.same(c, d)) {
      ++Friend[c], ++Friend[d];
    }
  }
  for (int i = 0; i < N; ++i) {
    printf("%d ", Group[i] - 1 - Friend[i]);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int s) {
    par.resize(s);
    rank.resize(s, 0);
    for (int i = 0; i < s; ++i)
      par[i] = i;
  }
  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
};

const int MAX_N = 100100;
int Friend[MAX_N], Group[MAX_N];
int N, M, K;

int main() {
  scanf("%d %d %d", &N, &M, &K);
  UnionFind List(MAX_N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    --a, --b;
    List.unite(a, b);
    ++Friend[a], ++Friend[b];
  }
  for (int i = 0; i < N; ++i) {
    List.root(i);
  }
  for (int i = 0; i < N; ++i) {
    ++Group[List.par[i]];
  }
  for (int i = 0; i < N; ++i) {
    Group[i] = Group[List.par[i]];
  }
  for (int i = 0; i < K; ++i) {
    int c, d;
    scanf("%d %d", &c, &d);
    --c, --d;
    if (List.same(c, d)) {
      ++Friend[c], ++Friend[d];
    }
  }
  for (int i = 0; i < N; ++i) {
    printf("%d ", Group[i] - 1 - Friend[i]);
  }
  printf("\n");
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;

// UnionFind Template
template <class T> struct UnionFind { // 構造体の定義
  vector<T> d;                        // メンバ変数
  UnionFind(T n = 0)
      : d(n, -1) {
  } // コンストラクタ（構造体が呼ばれたときにすぐに実行される処理）
  T find(T x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(T x, T y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] < d[y])
      swap(x, y); // d[x] is smaller. Then, connect d[x] to d[y].
    d[y] += d[x];
    d[x] = y;
    return true;
  }
  bool same(T x, T y) { return find(x) == find(y); }
  T size(T x) { return -(d[find(x)]); }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> fr(m);
  vector<int> bl[100005];
  UnionFind<int> uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    fr[a]++;
    fr[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }
  rep(i, n) {
    int ans;
    ans = uf.size(i) - 1 - fr[i];
    for (int a : bl[i]) {
      if (uf.same(i, a))
        ans--;
    }
    cout << ans << " ";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;

// UnionFind Template
template <class T> struct UnionFind { // 構造体の定義
  vector<T> d;                        // メンバ変数
  UnionFind(T n = 0)
      : d(n, -1) {
  } // コンストラクタ（構造体が呼ばれたときにすぐに実行される処理）
  T find(T x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(T x, T y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] < d[y])
      swap(x, y); // d[x] is smaller. Then, connect d[x] to d[y].
    d[y] += d[x];
    d[x] = y;
    return true;
  }
  bool same(T x, T y) { return find(x) == find(y); }
  T size(T x) { return -(d[find(x)]); }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> fr(100005);
  vector<int> bl[100005];
  UnionFind<int> uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    fr[a]++;
    fr[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }
  rep(i, n) {
    int ans;
    ans = uf.size(i) - 1 - fr[i];
    for (int a : bl[i]) {
      if (uf.same(i, a))
        ans--;
    }
    cout << ans << " ";
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll, ll>

vector<int> to[10010];
vector<int> group;
vector<int> block;

void dfs(int i, int gr, vector<int> &vec) {
  if (group[i] != -1)
    return;
  vec[gr]++;
  group[i] = gr;
  for (auto x : to[i]) {
    dfs(x, gr, vec);
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  group.resize(n, -1);
  block.resize(n, 0);
  vector<int> group_cnt;
  int tmp = 0;
  rep(i, n) {
    if (group[i] == -1) {
      group_cnt.push_back(0);
      dfs(i, tmp, group_cnt);
      tmp++;
    }
  }
  rep(i, k) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    if (group[c] == group[d]) {
      block[c]++;
      block[d]++;
    }
  }
  rep(i, n) {
    cout << group_cnt[group[i]] - to[i].size() - block[i] - 1 << " ";
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll, ll>

vector<int> to[100010];
vector<int> group;
vector<int> block;

void dfs(int i, int gr, vector<int> &vec) {
  if (group[i] != -1)
    return;
  vec[gr]++;
  group[i] = gr;
  for (auto x : to[i]) {
    dfs(x, gr, vec);
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  group.resize(n, -1);
  block.resize(n, 0);
  vector<int> group_cnt;
  int tmp = 0;
  rep(i, n) {
    if (group[i] == -1) {
      group_cnt.push_back(0);
      dfs(i, tmp, group_cnt);
      tmp++;
    }
  }
  rep(i, k) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    if (group[c] == group[d]) {
      block[c]++;
      block[d]++;
    }
  }
  rep(i, n) {
    cout << group_cnt[group[i]] - to[i].size() - block[i] - 1 << " ";
  }
  cout << endl;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;

struct unionfind { // 構造体の定義
  vector<int> d;   // メンバ変数
  unionfind(int n = 0)
      : d(n, -1) {
  } // コンストラクタ（構造体が呼ばれたときにすぐに実行される処理）
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] < d[y])
      swap(x, y); // d[x] is smaller. Then, connect d[x] to d[y].
    d[y] += d[x];
    d[x] = y;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -(d[find(x)]); }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int fr[10005];
  vector<int> bl[10005];
  unionfind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    fr[a]++;
    fr[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }
  rep(i, n) {
    int ans;
    ans = uf.size(i) - 1 - fr[i];
    for (int a : bl[i]) {
      if (uf.same(i, a))
        ans--;
    }
    cout << ans << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;

struct unionfind { // 構造体の定義
  vector<int> d;   // メンバ変数
  unionfind(int n = 0)
      : d(n, -1) {
  } // コンストラクタ（構造体が呼ばれたときにすぐに実行される処理）
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] < d[y])
      swap(x, y); // d[x] is smaller. Then, connect d[x] to d[y].
    d[y] += d[x];
    d[x] = y;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -(d[find(x)]); }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int fr[100005];
  vector<int> bl[100005];
  unionfind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    fr[a]++;
    fr[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    bl[a].push_back(b);
    bl[b].push_back(a);
  }
  rep(i, n) {
    int ans;
    ans = uf.size(i) - 1 - fr[i];
    for (int a : bl[i]) {
      if (uf.same(i, a))
        ans--;
    }
    cout << ans << " ";
  }
  cout << endl;
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> deg(m);
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
    deg[a]++;
    deg[b]++;
  }
  vector<vector<int>> block(n);
  rep(i, k) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    block[c].push_back(d);
    block[d].push_back(c);
  }
  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int x : block[i]) {
      if (uf.same(i, x))
        ans--;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> deg(n);
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
    deg[a]++;
    deg[b]++;
  }
  vector<vector<int>> block(n);
  rep(i, k) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    block[c].push_back(d);
    block[d].push_back(c);
  }
  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int x : block[i]) {
      if (uf.same(i, x))
        ans--;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}
const int INF = 1001001001;
const int mod = 1e9 + 7;

struct Unionfind {
  vector<int> d;
  Unionfind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0) {
      return x;
    }
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (d[x] > d[y]) {
      swap(x, y);
    }
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  Unionfind uf(N);

  vector<int> fre(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    fre[a]++;
    fre[b]++;
    uf.unite(a, b);
  }
  vector<vector<int>> block(N);
  for (int i = 0; i < K; i++) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    block[c].push_back(d);
    block[d].push_back(c);
  }
  for (int i = 0; i < N; i++) {
    int sum = uf.size(i) - 1 - fre[i];
    for (int c : block[i]) {
      if (uf.same(i, c)) {
        sum--;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}
const int INF = 1001001001;
const int mod = 1e9 + 7;

struct Unionfind {
  vector<int> d;
  Unionfind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0) {
      return x;
    }
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (d[x] > d[y]) {
      swap(x, y);
    }
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  Unionfind uf(N);

  vector<int> fre(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    fre[a]++;
    fre[b]++;
    uf.unite(a, b);
  }
  vector<vector<int>> block(N);
  for (int i = 0; i < K; i++) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    block[c].push_back(d);
    block[d].push_back(c);
  }
  for (int i = 0; i < N; i++) {
    int sum = uf.size(i) - 1 - fre[i];
    for (int c : block[i]) {
      if (uf.same(i, c)) {
        sum--;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

struct UnionFind {
  vector<int> p;
  UnionFind(int n) : p(n, 1) {}

  int root(int x) {
    if (p.at(x) < 0)
      return x;
    return p.at(x) = root(p.at(x));
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x), y = root(y);

    if (x == y)
      return 0;
    if (p.at(x) > p.at(y))
      swap(x, y);

    p.at(x) += p.at(y);
    p.at(y) = x;
    return 1;
  }

  int size(int x) { return -p.at(root(x)); }
};

int main() {
  int N, M, K, A, B;
  cin >> N >> M >> K;

  UnionFind uf(N);
  vector<set<int>> fb(N);

  rep(i, M) {
    cin >> A >> B;
    uf.merge(--A, --B);
    fb.at(A).insert(B);
    fb.at(B).insert(A);
  }

  rep(i, K) {
    cin >> A >> B;
    if (uf.issame(--A, --B)) {
      fb.at(A).insert(B);
      fb.at(B).insert(A);
    }
  }

  rep(i, N) cout << uf.size(i) - fb.at(i).size() - 1 << " ";
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

struct UnionFind {
  vector<int> p;
  UnionFind(int n) : p(n, -1) {}

  int root(int x) {
    if (p.at(x) < 0)
      return x;
    return p.at(x) = root(p.at(x));
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x), y = root(y);

    if (x == y)
      return 0;
    if (p.at(x) > p.at(y))
      swap(x, y);

    p.at(x) += p.at(y);
    p.at(y) = x;
    return 1;
  }

  int size(int x) { return -p.at(root(x)); }
};

int main() {
  int N, M, K, A, B;
  cin >> N >> M >> K;

  UnionFind uf(N);
  vector<set<int>> fb(N);

  rep(i, M) {
    cin >> A >> B;
    uf.merge(--A, --B);
    fb.at(A).insert(B);
    fb.at(B).insert(A);
  }

  rep(i, K) {
    cin >> A >> B;
    if (uf.issame(--A, --B)) {
      fb.at(A).insert(B);
      fb.at(B).insert(A);
    }
  }

  rep(i, N) cout << uf.size(i) - fb.at(i).size() - 1 << " ";
  cout << endl;
}