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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

std::vector<int> split(int x) {
  if (x == 0)
    return {0};
  std::vector<int> ans;
  while (x)
    ans.push_back(x % 10), x /= 10;
  std::reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::pair<int, int>> condition;
  for (int i = 0; i < m; ++i)
    std::cin >> condition[i].first >> condition[i].second;
  for (int ans = 0; ans <= 999; ++ans) {
    auto res = split(ans);
    if (res.size() != n)
      continue;
    bool flag = true;
    for (int i = 0; i < m; ++i)
      if (res[condition[i].first - 1] != condition[i].second)
        flag = false;
    if (!flag)
      continue;
    std::cout << ans << "\n";
    return 0;
  }
  std::cout << -1 << "\n";
}


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

std::vector<int> split(int x) {
  if (x == 0)
    return {0};
  std::vector<int> ans;
  while (x)
    ans.push_back(x % 10), x /= 10;
  std::reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::pair<int, int>> condition(m);
  for (int i = 0; i < m; ++i)
    std::cin >> condition[i].first >> condition[i].second;
  for (int ans = 0; ans <= 999; ++ans) {
    auto res = split(ans);
    if (res.size() != n)
      continue;
    bool flag = true;
    for (int i = 0; i < m; ++i)
      if (res[condition[i].first - 1] != condition[i].second)
        flag = false;
    if (!flag)
      continue;
    std::cout << ans << "\n";
    return 0;
  }
  std::cout << -1 << "\n";
}
Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// 0 <= unsigned int <= 4.2e9

int main() {
  size_t N, M;
  cin >> N >> M;
  string S(N, '0');
  if (N > 1) {
    S[0] = 1;
  }
  vector<int> c(N + 1, -1);
  for (size_t i = 0; i < M; i++) {
    size_t si;
    int temp;
    cin >> si >> temp;
    if (c[si] == -1) {
      c[si] = temp;
    } else {
      if (c[si] != temp) {
        cout << -1 << endl;
        return 0;
      } else {
        c[si] = temp;
      }
    }
    string ss = to_string(temp);
    S[si - 1] = ss[0];
  }

  size_t len = S.length();
  if ((S.length() > 1) && (S[0] == '0')) {
    cout << -1 << endl;
    return 0;
  }

  int ans = stoi(S);
  cout << ans << endl;
  bool check = true;
}


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

typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// 0 <= unsigned int <= 4.2e9

int main() {
  size_t N, M;
  cin >> N >> M;
  string S(N, '0');
  if (N > 1) {
    S[0] = '1';
  }
  vector<int> c(N + 1, -1);
  for (size_t i = 0; i < M; i++) {
    size_t si;
    int temp;
    cin >> si >> temp;
    if (c[si] == -1) {
      c[si] = temp;
    } else {
      if (c[si] != temp) {
        cout << -1 << endl;
        return 0;
      } else {
        c[si] = temp;
      }
    }
    string ss = to_string(temp);
    S[si - 1] = ss[0];
  }

  size_t len = S.length();
  if ((S.length() > 1) && (S[0] == '0')) {
    cout << -1 << endl;
    return 0;
  }

  int ans = stoi(S);
  cout << ans << endl;
  bool check = true;
}
Problem p02761 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  string num(M, '.');
  for (int i = 0; i < M; i++) {
    int c, s;
    cin >> s >> c;
    if (N > 1 && s == 1 && c == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (num[s - 1] != '.' && num[s - 1] != (char)(c + '0')) {
      cout << -1 << endl;
      return 0;
    }
    num[s - 1] = (char)(c + '0');
  }
  for (char &ch : num) {
    if (ch == '.') {
      ch = '0';
    }
  }
  if (num[0] == '0') {
    if (N == 1) {
      cout << 0 << endl;
      return 0;
    } else {
    }
    num[0] = '1';
  }
  int min_num = stoi(num);
  cout << min_num << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  int N, M;
  cin >> N >> M;
  string num(N, '.');
  for (int i = 0; i < M; i++) {
    int c, s;
    cin >> s >> c;
    if (N > 1 && s == 1 && c == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (num[s - 1] != '.' && num[s - 1] != (char)(c + '0')) {
      cout << -1 << endl;
      return 0;
    }
    num[s - 1] = (char)(c + '0');
  }
  for (char &ch : num) {
    if (ch == '.') {
      ch = '0';
    }
  }
  if (num[0] == '0') {
    if (N == 1) {
      cout << 0 << endl;
      return 0;
    } else {
    }
    num[0] = '1';
  }
  int min_num = stoi(num);
  cout << min_num << endl;
  return 0;
}
Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())
#define SORT(A) sort(A.begin(), A.end())
#define REV(A) reverse(A.begin(), A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int, int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second

int main(void) {
  int n, m;
  cin >> n >> m;
  vp sc(m);
  rep(i, m) {
    int s, c;
    cin >> s >> c;
    s--;
    sc[i].fi = s;
    sc[i].se = c;
  }
  SORT(sc);
  rep(i, m - 1) {
    if (sc[i].fi == sc[i + 1].fi) {
      if (sc[i].se != sc[i + 1].se) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  vi ans(n, 0);
  rep(i, m) ans[sc[i].fi] = sc[i].se;
  if (sc[0].fi == 0 && sc[0].se == 0) {
    if (n == 1) {
      cout << 0 << endl;
      return 0;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  if (ans[0] == 0)
    ans[0] = 1;
  rep(i, n) cout << ans[i];
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())
#define SORT(A) sort(A.begin(), A.end())
#define REV(A) reverse(A.begin(), A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int, int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second

int main(void) {
  int n, m;
  cin >> n >> m;
  if (m == 0) {
    if (n == 1) {
      cout << 0;
    } else {
      cout << 1;
      rep(i, n - 1) cout << 0;
    }
    cout << endl;
    return 0;
  }
  vp sc(m);
  rep(i, m) {
    int s, c;
    cin >> s >> c;
    s--;
    sc[i].fi = s;
    sc[i].se = c;
  }
  SORT(sc);
  rep(i, m - 1) {
    if (sc[i].fi == sc[i + 1].fi) {
      if (sc[i].se != sc[i + 1].se) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  vi ans(n, 0);
  rep(i, m) ans[sc[i].fi] = sc[i].se;
  if (sc[0].fi == 0 && sc[0].se == 0) {
    if (n == 1) {
      cout << 0 << endl;
      return 0;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  if (ans[0] == 0)
    ans[0] = 1;
  rep(i, n) cout << ans[i];
  cout << endl;
}
Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> p(m);
  rep(i, m) cin >> p[i].first >> p[i].second;
  rep(x, 1000) {
    int keta = 1;
    int nx = x / 10;
    vector<int> d(1, x % 10);
    while (nx) {
      keta++;
      d.push_back(nx % 10);
      nx /= 10;
    }
    if (keta != n)
      continue;
    bool ok = true;
    reverse(d.begin(), d.end());
    rep(i, n) {
      if (d[p[i].first - 1] != p[i].second)
        ok = false;
    }
    if (ok) {
      cout << x << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> p(m);
  rep(i, m) cin >> p[i].first >> p[i].second;
  rep(x, 1000) {
    int keta = 1;
    int nx = x / 10;
    vector<int> d(1, x % 10);
    while (nx) {
      keta++;
      d.push_back(nx % 10);
      nx /= 10;
    }
    if (keta != n)
      continue;
    bool ok = true;
    reverse(d.begin(), d.end());
    rep(i, m) {
      if (d[p[i].first - 1] != p[i].second)
        ok = false;
    }
    if (ok) {
      cout << x << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }

  int st = 1;
  for (int i = 1; i < N; i++) {
    st *= 10;
  }
  int ed = st * 10;
  if (N == 1) {
    st = 0;
  }
  int ans = -1;
  for (int i = st; i < ed; i++) {
    vector<int> a;
    int tmp = i;
    while (tmp != 0) {
      a.push_back(tmp % 10);
      tmp /= 10;
    }
    int flag = 1;
    for (int j = 0; j < M; j++) {
      if (a[N - s[j]] != c[j]) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }

  int st = 1;
  for (int i = 1; i < N; i++) {
    st *= 10;
  }
  int ed = st * 10;
  if (N == 1) {
    st = 0;
  }
  int ans = -1;
  for (int i = st; i < ed; i++) {
    vector<int> a;
    int tmp = i;
    if (tmp == 0) {
      a.push_back(0);
    }
    while (tmp != 0) {
      a.push_back(tmp % 10);
      tmp /= 10;
    }
    int flag = 1;
    for (int j = 0; j < M; j++) {
      if (a[N - s[j]] != c[j]) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02761 (C++)

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

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m), a(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
    a[s[i]] = c[i];
  }
  for (int i = 0; i < m; i++) {
    if (a[s[i]] != c[i] || (n > 1 && s[i] == 1 && c[i] == 0)) {
      cout << -1;
      return 0;
    }
  }
  if (s[1] == 0 && n > 1)
    s[1] = 1;
  for (int i = 1; i <= n; i++)
    cout << a[i];
}

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

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m), a(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
    a[s[i]] = c[i];
  }
  for (int i = 0; i < m; i++) {
    if (a[s[i]] != c[i] || (n > 1 && s[i] == 1 && c[i] == 0)) {
      cout << -1;
      return 0;
    }
  }
  if (a[1] == 0 && n > 1)
    a[1] = 1;
  for (int i = 1; i <= n; i++)
    cout << a[i];
}
Problem p02761 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

n, m = map(int, readline().split())
if m == 0:
    if n == 1:
        print(0)
    else:
        print("1" + "0" * (n - 1))
sc = [list(map(int, readline().split())) for i in range(m)]
sc.sort()
memo = sc[0]
if sc[0] == [1, 0] and n != 1:
    print(-1)
    exit()
for i in range(1, m):
    if memo[0] == sc[i][0]:
        if memo[1] != sc[i][1]:
            print(-1)
            exit()
    else:
        memo = sc[i]
ans = ""
for i in range(n):
    for j in range(m):
        if i + 1 == sc[j][0]:
            ans += str(sc[j][1])
            break
    if len(ans) != i + 1:
        if len(ans) == 0:
            ans += "1"
        else:
            ans += "0"
print(ans)


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

n, m = map(int, readline().split())
if m == 0:
    if n == 1:
        print(0)
    else:
        print("1" + "0" * (n - 1))
    exit()
sc = [list(map(int, readline().split())) for i in range(m)]
sc.sort()
memo = sc[0]
if sc[0] == [1, 0] and n != 1:
    print(-1)
    exit()
for i in range(1, m):
    if memo[0] == sc[i][0]:
        if memo[1] != sc[i][1]:
            print(-1)
            exit()
    else:
        memo = sc[i]
ans = ""
for i in range(n):
    for j in range(m):
        if i + 1 == sc[j][0]:
            ans += str(sc[j][1])
            break
    if len(ans) != i + 1:
        if len(ans) == 0:
            ans += "1"
        else:
            ans += "0"
print(ans)
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define inf 1000000007
using namespace std;
int n, m, k, fa[100009], num[100009];
vector<int> w[100009];
vector<int> w1[100009];
int findl(int x) { return fa[x] == x ? x : findl(fa[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    fa[i] = i, num[i] = 1;
  for (int i = 0; i < m; i++) {
    int x, y, tmp, tmp1;
    scanf("%d%d", &x, &y);
    w[x].push_back(y), w[y].push_back(x);
    tmp = findl(x), tmp1 = findl(y);
    if (tmp != tmp1)
      fa[tmp] = tmp1, num[tmp1] += num[tmp];
  }
  for (int i = 0; i < k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    w1[x].push_back(y), w1[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    int ans = num[findl(i)] - w[i].size() - 1;
    for (int j = 0; j < w1[i].size(); j++) {
      int tmp = findl(i), tmp1 = findl(w1[i][j]);
      if (tmp == tmp1)
        ans--;
    }
    printf("%d ", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define inf 1000000007
using namespace std;
int n, m, k, fa[100009], num[100009];
vector<int> w[100009];
vector<int> w1[100009];
int findl(int x) { return fa[x] == x ? x : fa[x] = findl(fa[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    fa[i] = i, num[i] = 1;
  for (int i = 0; i < m; i++) {
    int x, y, tmp, tmp1;
    scanf("%d%d", &x, &y);
    w[x].push_back(y), w[y].push_back(x);
    tmp = findl(x), tmp1 = findl(y);
    if (tmp != tmp1)
      fa[tmp] = tmp1, num[tmp1] += num[tmp];
  }
  for (int i = 0; i < k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    w1[x].push_back(y), w1[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    int ans = num[findl(i)] - w[i].size() - 1;
    for (int j = 0; j < w1[i].size(); j++) {
      int tmp = findl(i), tmp1 = findl(w1[i][j]);
      if (tmp == tmp1)
        ans--;
    }
    printf("%d ", ans);
  }
  return 0;
}
Problem p02762 (Python)

Buggy submission (status: Runtime Error):
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        self.parents[x] = self.find(self.parents[x])
        return self.parents[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = x, y
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.parents[self.find(x)]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]


N, M, K = map(int, input().split())
uf = UnionFind(N)
friends = [[] for _ in range(N)]
blocks = [[] for _ in range(N)]
root = {}

for i in range(M):
    a, b = map(int, input().split())
    friends[a - 1].append(b - 1)
    friends[b - 1].append(a - 1)
    uf.unite(a - 1, b - 1)

roots = uf.roots()
for r in roots:
    size = uf.size(r)
    root[r] = size - 1

for i in range(K):
    c, d = map(int, input().split())
    if uf.same(c - 1, d - 1):
        blocks[c - 1].append(d - 1)
        blocks[d - 1].append(c - 1)

ans = []
for i in range(N):
    ans.append(root[uf.find(i)] - len(blocks[i]) - len(friends[i]))
print(*ans)


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        self.parents[x] = self.find(self.parents[x])
        return self.parents[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.parents[self.find(x)]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]


N, M, K = map(int, input().split())
uf = UnionFind(N)
friends = [[] for _ in range(N)]
blocks = [[] for _ in range(N)]
root = {}

for i in range(M):
    a, b = map(int, input().split())
    friends[a - 1].append(b - 1)
    friends[b - 1].append(a - 1)
    uf.unite(a - 1, b - 1)

roots = uf.roots()
for r in roots:
    size = uf.size(r)
    root[r] = size - 1

for i in range(K):
    c, d = map(int, input().split())
    if uf.same(c - 1, d - 1):
        blocks[c - 1].append(d - 1)
        blocks[d - 1].append(c - 1)

ans = []
for i in range(N):
    ans.append(root[uf.find(i)] - len(blocks[i]) - len(friends[i]))
print(*ans)
Problem p02762 (Python)

Buggy submission (status: Runtime Error):
# https://atcoder.jp/contests/abc157/submissions/10475457

import sys

readline = sys.stdin.readine

N, M, K = map(int, readline().split())

F = [[] for _ in range(N + 1)]
for _ in range(M):
    a, b = map(int, readline().split())
    F[a].append(b)
    F[b].append(a)

B = [[] for _ in range(N + 1)]
for _ in range(K):
    c, d = map(int, readline().split())
    B[c].append(d)
    B[d].append(c)

seen = [-1] * (N + 1)
group = 0


def dfs(graph, s):
    stack = [s]
    seen[s] = group
    n_connection = 1

    while stack:
        u = stack.pop()
        for v in graph[u]:
            if seen[v] == -1:
                n_connection += 1
                seen[v] = group
                stack.append(v)

    return n_connection


union = {}
for i in range(1, N + 1):
    if seen[i] == -1:
        n = dfs(F, i)
        union[group] = n
        group += 1

ans = []
for i in range(1, N + 1):
    tmp = 0
    tmp += union[seen[i]] - 1
    tmp -= len(F[i])
    for j in B[i]:
        if seen[j] == seen[i]:
            tmp -= 1
    ans.append(tmp)

print(" ".join(map(str, ans)))


Fixed submission:
# https://atcoder.jp/contests/abc157/submissions/10475457

import sys

readline = sys.stdin.readline

N, M, K = map(int, readline().split())

F = [[] for _ in range(N + 1)]
for _ in range(M):
    a, b = map(int, readline().split())
    F[a].append(b)
    F[b].append(a)

B = [[] for _ in range(N + 1)]
for _ in range(K):
    c, d = map(int, readline().split())
    B[c].append(d)
    B[d].append(c)

seen = [-1] * (N + 1)
group = 0


def dfs(graph, s):
    stack = [s]
    seen[s] = group
    n_connection = 1

    while stack:
        u = stack.pop()
        for v in graph[u]:
            if seen[v] == -1:
                n_connection += 1
                seen[v] = group
                stack.append(v)

    return n_connection


union = {}
for i in range(1, N + 1):
    if seen[i] == -1:
        n = dfs(F, i)
        union[group] = n
        group += 1

ans = []
for i in range(1, N + 1):
    tmp = 0
    tmp += union[seen[i]] - 1
    tmp -= len(F[i])
    for j in B[i]:
        if seen[j] == seen[i]:
            tmp -= 1
    ans.append(tmp)

print(" ".join(map(str, ans)))
Problem p02762 (C++)

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

void Main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> frig(n); // friend group tree
  vector<int> numf(n); // number of friend
  vector<int> numb(n); // number of ban
  vector<int> numc(n); // size of group
  for (int i = 0; i < n; i++) {
    frig[i] = i;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    numf[a]++;
    numf[b]++;
    int p1 = a, p2 = b;
    while (frig[p1] != p1)
      p1 = frig[p1];
    while (frig[p2] != p2)
      p2 = frig[p2];
    frig[p1] = p2;
  }
  for (int i = 0; i < n; i++) {
    int p = i;
    while (frig[p] != p)
      p = frig[p];
    frig[i] = p;
    numc[p]++;
  }

  for (int i = 0; i < k; i++) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    if (frig[c] == frig[d]) {
      numb[c]++;
      numb[d]++;
    }
  }

  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << numc[frig[i]] - numf[i] - numb[i] - 1;
  }
  cout << endl;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}


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

void Main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> frig(n); // friend group tree
  vector<int> numf(n); // number of friend
  vector<int> numb(n); // number of ban
  vector<int> numc(n); // size of group
  for (int i = 0; i < n; i++) {
    frig[i] = i;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    numf[a]++;
    numf[b]++;
    int p1 = a, p2 = b;
    while (frig[p1] != p1)
      p1 = frig[p1];
    while (frig[p2] != p2)
      p2 = frig[p2];
    frig[p1] = p2;
    frig[a] = p2;
    frig[b] = p2;
  }
  for (int i = 0; i < n; i++) {
    int p = i;
    while (frig[p] != p)
      p = frig[p];
    frig[i] = p;
    numc[p]++;
  }

  for (int i = 0; i < k; i++) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    if (frig[c] == frig[d]) {
      numb[c]++;
      numb[d]++;
    }
  }

  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << numc[frig[i]] - numf[i] - numb[i] - 1;
  }
  cout << endl;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}
Problem p02762 (C++)

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

#define FILE
#define fr first
#define se second

using namespace std;

const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int m;
int k;
int x;
int y;
int c;
vector<int> g[N];
vector<int> num(N, 0);
vector<int> comp(N, -1);
void dfs(int v, int c) {
  if (comp[v] != c) {
    return;
  }
  comp[v] = c;
  for (auto to : g[v]) {
    dfs(to, c);
  }
}
int main() {
#ifdef FILEs
  freopen("input.txt", "r", stdin);
  /// freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (comp[i] == -1) {
      dfs(i, ++c);
    }
  }
  for (int i = 1; i <= n; i++) {
    num[comp[i]]++;
  }
  vector<int> res(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    res[i] = num[comp[i]] - 1 - (int)g[i].size();
  }
  for (int i = 1; i <= k; i++) {
    cin >> x >> y;
    if (comp[x] == comp[y]) {
      res[x]--, res[y]--;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << res[i] << " ";
  }
  cout << "\n";
}


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

#define FILE
#define fr first
#define se second

using namespace std;

const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int m;
int k;
int x;
int y;
int c;
vector<int> g[N];
vector<int> num(N, 0);
vector<int> comp(N, -1);
void dfs(int v, int c) {
  if (comp[v] != -1) {
    return;
  }
  comp[v] = c;
  for (auto to : g[v]) {
    dfs(to, c);
  }
}
int main() {
#ifdef FILEs
  freopen("input.txt", "r", stdin);
  /// freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);

  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (comp[i] == -1) {
      dfs(i, ++c);
    }
  }
  for (int i = 1; i <= n; i++) {
    num[comp[i]]++;
  }
  vector<int> res(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    res[i] = num[comp[i]] - 1 - (int)g[i].size();
  }
  for (int i = 1; i <= k; i++) {
    cin >> x >> y;
    if (comp[x] == comp[y]) {
      res[x]--, res[y]--;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << res[i] << " ";
  }
  cout << "\n";
}
Problem p02762 (Python)

Buggy submission (status: Runtime Error):
# 同じ木に属する(==そこまでたどりつけるかどうか）の判定にunionfind
# 同じ木に属しているノードはあとで数えることができる（そうだね）
# 除外するやつが出てくる。直の友達+ブロック関係にあるやつ（と自分）
# 同じ木に属しているノードの総数 - (直の友達+ブロック関係+自分自身)が答え
n, m, k = map(int, input().split())
par = [i for i in range(n)]
ans = [0 for _ in range(n)]


def root(x):
    if x == par[x]:
        return x
    y = root(par[x])
    par[x] = y
    return y


def union(x, y):
    if x > y:
        x, y = y, x
    rx, ry = root(x), root(y)
    if rx != ry:
        par[ry] = rx


friends = [[] for _ in range(n)]
blocks = [[] for _ in range(n)]

for _ in range(m):
    a, b = map(int, input().split())
    union(a - 1, b - 1)
    friends[a - 1].append(b - 1)
    friends[b - 1].append(a - 1)
for _ in range(k):
    c, d = map(int, input().split())
    blocks[c - 1].append(d - 1)
    blocks[d - 1].append(c - 1)

# 木に属しているやつを数えるぜ
group_count = [0 for _ in range(n)]
for i in range(n):
    group_count[root(i)] += 1
# 答えを作るぜ
for i in range(n):
    # 初期化（じゃないけど）
    ans[i] += group_count[root(i)]
    # 直の友達を除く
    ans[i] -= len(friends[i])
    # ブロックしてるやつを除く
    for b in blocks[i]:
        ra, rb = root(i), root(b)
        if ra == rb:
            ans[i] -= 1
    # 自分自身を除く
    ans[i] -= 1
print(" ".join(list(map(str, ans))))


Fixed submission:
# 同じ木に属する(==そこまでたどりつけるかどうか）の判定にunionfind
# 同じ木に属しているノードはあとで数えることができる（そうだね）
# 除外するやつが出てくる。直の友達+ブロック関係にあるやつ（と自分）
# 同じ木に属しているノードの総数 - (直の友達+ブロック関係+自分自身)が答え
from sys import setrecursionlimit

setrecursionlimit(10**8)
n, m, k = map(int, input().split())
par = [i for i in range(n)]
ans = [0 for _ in range(n)]


def root(x):
    if x == par[x]:
        return x
    y = root(par[x])
    par[x] = y
    return y


def union(x, y):
    if x > y:
        x, y = y, x
    rx, ry = root(x), root(y)
    if rx != ry:
        par[ry] = rx


friends = [[] for _ in range(n)]
blocks = [[] for _ in range(n)]

for _ in range(m):
    a, b = map(int, input().split())
    union(a - 1, b - 1)
    friends[a - 1].append(b - 1)
    friends[b - 1].append(a - 1)
for _ in range(k):
    c, d = map(int, input().split())
    blocks[c - 1].append(d - 1)
    blocks[d - 1].append(c - 1)

# 木に属しているやつを数えるぜ
group_count = [0 for _ in range(n)]
for i in range(n):
    group_count[root(i)] += 1
# 答えを作るぜ
for i in range(n):
    # 初期化（じゃないけど）
    ans[i] += group_count[root(i)]
    # 直の友達を除く
    ans[i] -= len(friends[i])
    # ブロックしてるやつを除く
    for b in blocks[i]:
        ra, rb = root(i), root(b)
        if ra == rb:
            ans[i] -= 1
    # 自分自身を除く
    ans[i] -= 1
print(" ".join(list(map(str, ans))))
Problem p02762 (C++)

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

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

struct ps {
  long long par, sz;
};

#define N 1e5 + 5
vector<ps> par(N);
long long Find(long long node) {
  return par[node].par == node ? node : par[node].par = Find(par[node].par);
}
bool Union(long long u, long long v) {
  long long pu = Find(u);
  long long pv = Find(v);
  if (pu != pv) {
    par[pu].par = pv;
    par[pv].sz += par[pu].sz;
    par[pu].sz = 1;
    return true;
  }
  return false;
}

vector<int> arr[100005];

int main() {
  textIO();
  fastIO();

  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    par[i].par = i;
    par[i].sz = 1;
  }
  for (int i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v;
    Union(u, v);
    arr[u].push_back(v);
    arr[v].push_back(u);
  }
  for (int i = 0; i < k; i++) {
    long long u, v;
    cin >> u >> v;
    if (Find(u) == Find(v)) {
      arr[u].push_back(v);
      arr[v].push_back(u);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << par[Find(i)].sz - arr[i].size() - 1 << " ";
  }
  cout << '\n';
  return 0;
}


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

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

struct ps {
  long long par, sz;
};

#define N 1e5 + 5
vector<ps> par(N);
long long Find(long long node) {
  return par[node].par == node ? node : par[node].par = Find(par[node].par);
}
bool Union(long long u, long long v) {
  long long pu = Find(u);
  long long pv = Find(v);
  if (pu != pv) {
    par[pu].par = pv;
    par[pv].sz += par[pu].sz;
    par[pu].sz = 1;
    return true;
  }
  return false;
}

vector<int> arr[100005];

int main() {
  // textIO();
  fastIO();

  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    par[i].par = i;
    par[i].sz = 1;
  }
  for (int i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v;
    Union(u, v);
    arr[u].push_back(v);
    arr[v].push_back(u);
  }
  for (int i = 0; i < k; i++) {
    long long u, v;
    cin >> u >> v;
    if (Find(u) == Find(v)) {
      arr[u].push_back(v);
      arr[v].push_back(u);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << par[Find(i)].sz - arr[i].size() - 1 << " ";
  }
  cout << '\n';
  return 0;
}
Problem p02762 (C++)

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

using namespace std;

#define sqr(x) ((x) * (x))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef long long ll;

const int MOD = 1000000007;
const int INF = 1000000007;
const ll INFLL = 1000000000000000007LL;

struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N, -1) {}

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

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

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

  int size(int x) { return -par[root(x)]; }
};

void solve() {
  int N, M, K;
  cin >> N >> M >> K;
  UnionFind uf(N);
  vector<int> deg(N, 0);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  vector<vector<int>> block(N);
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    block[a].push_back(b);
    block[b].push_back(a);
  }
  for (int i = 0; i < N; i++) {
    int ans = uf.size(i) - deg[i] - 1;
    for (int b : block[i]) {
      if (uf.same(i, b))
        ans--;
    }
    cout << ans << ' ';
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(12);

  solve();

  return 0;
}


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

using namespace std;

#define sqr(x) ((x) * (x))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef long long ll;

const int MOD = 1000000007;
const int INF = 1000000007;
const ll INFLL = 1000000000000000007LL;

struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N, -1) {}

  int root(int x) {
    if (par[x] < 0)
      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] += par[ry];
    par[ry] = rx;
  }

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

  int size(int x) { return -par[root(x)]; }
};

void solve() {
  int N, M, K;
  cin >> N >> M >> K;
  UnionFind uf(N);
  vector<int> deg(N, 0);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  vector<vector<int>> block(N);
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    block[a].push_back(b);
    block[b].push_back(a);
  }
  for (int i = 0; i < N; i++) {
    int ans = uf.size(i) - deg[i] - 1;
    for (int b : block[i]) {
      if (uf.same(i, b))
        ans--;
    }
    cout << ans << ' ';
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(12);

  solve();

  return 0;
}
Problem p02762 (C++)

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

class UnionFind {
private:
  vector<int> par;

public:
  UnionFind(int N) {
    par.resize(N);
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) { return (x == par[x]) ? x : (par[x] = root(par[x])); }
  void unite(int x, int y) { par[root(y)] = root(x); }
  bool same(int x, int y) { return (root(x) == root(y)); }
};

int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  int K;
  cin >> K;

  UnionFind UF(N);

  vector<ll> FA(M), FB(M), BA(M), BB(M);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    UF.unite(A, B);
    FA[i] = A, FB[i] = B;
  }
  for (ll i = 0; i < K; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    BA[i] = A, BB[i] = B;
  }
  vector<int> CNT(N, 0);
  for (ll i = 0; i < N; i++) {
    CNT[UF.root(i)]++;
  }

  vector<int> ans(N);
  for (ll i = 0; i < N; i++) {
    ans[i] = CNT[UF.root(i)];
    ans[i]--; // 除く自分
  }
  for (int i = 0; i < M; i++) {
    if (UF.same(FA[i], FB[i])) {
      ans[FA[i]]--;
      ans[FB[i]]--;
    }
  }
  for (ll i = 0; i < K; i++) {
    if (UF.same(BA[i], BB[i])) {
      ans[BA[i]]--;
      ans[BB[i]]--;
    }
  }
  cout << ans[0];
  for (int i = 1; i < ans.size(); i++) {
    cout << ' ' << ans[i];
  }

  return 0;
}

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

class UnionFind {
private:
  vector<int> par;

public:
  UnionFind(int N) {
    par.resize(N);
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) { return (x == par[x]) ? x : (par[x] = root(par[x])); }
  void unite(int x, int y) { par[root(y)] = root(x); }
  bool same(int x, int y) { return (root(x) == root(y)); }
};

int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  int K;
  cin >> K;

  UnionFind UF(N);

  vector<ll> FA(M), FB(M), BA(K), BB(K);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    UF.unite(A, B);
    FA[i] = A, FB[i] = B;
  }
  for (ll i = 0; i < K; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    BA[i] = A, BB[i] = B;
  }
  vector<int> CNT(N, 0);
  for (ll i = 0; i < N; i++) {
    CNT[UF.root(i)]++;
  }

  vector<int> ans(N);
  for (ll i = 0; i < N; i++) {
    ans[i] = CNT[UF.root(i)];
    ans[i]--; // 除く自分
  }
  for (int i = 0; i < M; i++) {
    if (UF.same(FA[i], FB[i])) {
      ans[FA[i]]--;
      ans[FB[i]]--;
    }
  }
  for (ll i = 0; i < K; i++) {
    if (UF.same(BA[i], BB[i])) {
      ans[BA[i]]--;
      ans[BB[i]]--;
    }
  }
  cout << ans[0];
  for (int i = 1; i < ans.size(); i++) {
    cout << ' ' << ans[i];
  }

  return 0;
}
Problem p02762 (C++)

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

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

using namespace std;

set<int> f[maxn], b[maxn], ts;
vector<set<int>> path;
bool vis[maxn];
int ans[maxn];

void dfs(int cur) {
  if (vis[cur])
    return;
  vis[cur] = 1;
  for (auto &x : f[cur])
    dfs(x);
  ts.insert(cur);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    f[x].insert(y);
    f[y].insert(x);
  }
  for (int i = 0, x, y; i < k; ++i) {
    cin >> x >> y;
    b[x].insert(y);
    b[y].insert(x);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      ts.clear();
      dfs(i);
    }
    int full = ts.size();
    for (auto x : ts) {
      int value = full - f[x].size();
      for (auto v : b[x])
        if (ts.count(v))
          --value;
      ans[x] = value - 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " \n"[i == n];
  }
  return 0;
}

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

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

using namespace std;

set<int> f[maxn], b[maxn], ts;
vector<set<int>> path;
bool vis[maxn];
int ans[maxn];

void dfs(int cur) {
  if (vis[cur])
    return;
  vis[cur] = 1;
  for (auto &x : f[cur])
    dfs(x);
  ts.insert(cur);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    f[x].insert(y);
    f[y].insert(x);
  }
  for (int i = 0, x, y; i < k; ++i) {
    cin >> x >> y;
    b[x].insert(y);
    b[y].insert(x);
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[i])
      continue;
    if (!vis[i]) {
      ts.clear();
      dfs(i);
    }
    int full = ts.size();
    for (auto x : ts) {
      int value = full - f[x].size();
      for (auto v : b[x])
        if (ts.count(v))
          --value;
      ans[x] = value - 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " \n"[i == n];
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int id;
vector<int> cc_id;
void dfs(vector<vector<int>> G, int v, int &cnt) {
  cc_id[v] = id;
  cnt++;
  for (auto i : G[v]) {
    if (cc_id[i] != -1)
      continue;
    dfs(G, i, cnt);
  }
}
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> G(N);
  vector<int> cc_num;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cc_id.assign(N, -1);
  id = 0;
  rep(i, N) {
    if (cc_id[i] == -1) {
      int cnt = 0;
      dfs(G, i, cnt);
      cc_num.push_back(cnt);
      id++;
    }
  }
  vector<int> ans(N);
  rep(i, N) ans[i] = cc_num[cc_id[i]] - (int)G[i].size() - 1;
  rep(i, K) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    if (cc_id[c] == cc_id[d]) {
      ans[c]--;
      ans[d]--;
    }
  }
  rep(i, N) cout << ans[i] << ' ';
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int id;
vector<int> cc_id;
void dfs(vector<vector<int>> &G, int v, int &cnt) {
  cc_id[v] = id;
  cnt++;
  for (auto i : G[v]) {
    if (cc_id[i] != -1)
      continue;
    dfs(G, i, cnt);
  }
}
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> G(N);
  vector<int> cc_num;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cc_id.assign(N, -1);
  id = 0;
  rep(i, N) {
    if (cc_id[i] == -1) {
      int cnt = 0;
      dfs(G, i, cnt);
      cc_num.push_back(cnt);
      id++;
    }
  }
  vector<int> ans(N);
  rep(i, N) ans[i] = cc_num[cc_id[i]] - (int)G[i].size() - 1;
  rep(i, K) {
    int c, d;
    cin >> c >> d;
    c--;
    d--;
    if (cc_id[c] == cc_id[d]) {
      ans[c]--;
      ans[d]--;
    }
  }
  rep(i, N) cout << ans[i] << ' ';
  cout << endl;
}
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#define N 100005
using namespace std;

int n, m, k, a[N], b[N], pr[N], cp[N], cm[N];

int fnd(int p) { return pr[p] == p ? p : fnd(pr[p]); }

int main() {
  int i, x, y;
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++)
    pr[i] = i, cp[i] = 1;
  while (m--) {
    scanf("%d%d", &x, &y);
    cm[x]++, cm[y]++;
    x = fnd(x);
    y = fnd(y);
    if (x != y) {
      pr[y] = x;
      cp[x] += cp[y];
    }
  }
  while (k--) {
    scanf("%d%d", &x, &y);
    if (fnd(x) == fnd(y))
      cm[x]++, cm[y]++;
  }
  for (i = 1; i <= n; i++) {
    printf("%d ", cp[fnd(i)] - cm[i] - 1);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define N 100005
using namespace std;

int n, m, k, a[N], b[N], pr[N], cp[N], cm[N];

int fnd(int p) { return pr[p] == p ? p : pr[p] = fnd(pr[p]); }

int main() {
  int i, x, y;
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++)
    pr[i] = i, cp[i] = 1;
  while (m--) {
    scanf("%d%d", &x, &y);
    cm[x]++, cm[y]++;
    x = fnd(x);
    y = fnd(y);
    if (x != y) {
      pr[y] = x;
      cp[x] += cp[y];
    }
  }
  while (k--) {
    scanf("%d%d", &x, &y);
    if (fnd(x) == fnd(y))
      cm[x]++, cm[y]++;
  }
  for (i = 1; i <= n; i++) {
    printf("%d ", cp[fnd(i)] - cm[i] - 1);
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include<iostream>
using namespace std;
const int N = 1e5 + 5;
int pa[N], cnt[N], fr[N];
int n, m, k;
int search(int x) { return pa[x] == x ? x : search(pa[x]); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> k;
  // while(cin>>n>>m>>k)

  for (int i = 1; i <= n; i++) {
    pa[i] = i;
    cnt[i] = 1;
    fr[i] = 0;
  }
  int x, y;
  while (m--) {
    cin >> x >> y;
    int a = search(x);
    int b = search(y);
    fr[x]++;
    fr[y]++;
    if (a != b) {
      pa[a] = b;
      cnt[b] += cnt[a];
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt[i] = cnt[search(i)];
  }
  while (k--) {
    cin >> x >> y;
    int a = search(x);
    int b = search(y);
    if (a == b) {
      cnt[x]--, cnt[y]--;
    }
  }
  for (int i = 1; i <= n; i++)
    cout << cnt[i] - fr[i] - 1 << ' ';
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include<iostream>
using namespace std;
const int N = 1e5 + 5;
int pa[N], cnt[N], fr[N];
int n, m, k;
int search(int x) {
  // return pa[x]==pa[pa[x]]?pa[x]:pa[x]=search(pa[x]);
  if (pa[x] != pa[pa[x]])
    pa[x] = search(pa[x]);
  return pa[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> k;
  // while(cin>>n>>m>>k)

  for (int i = 1; i <= n; i++) {
    pa[i] = i;
    cnt[i] = 1;
    fr[i] = 0;
  }
  int x, y;
  while (m--) {
    cin >> x >> y;
    int a = search(x);
    int b = search(y);
    fr[x]++;
    fr[y]++;
    if (a != b) {
      pa[a] = b;
      cnt[b] += cnt[a];
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt[i] = cnt[search(i)];
  }
  while (k--) {
    cin >> x >> y;
    int a = search(x);
    int b = search(y);
    if (a == b) {
      cnt[x]--, cnt[y]--;
    }
  }
  for (int i = 1; i <= n; i++)
    cout << cnt[i] - fr[i] - 1 << ' ';
  cout << endl;

  return 0;
}
Problem p02762 (Python)

Buggy submission (status: Runtime Error):
class UnionFind:
    def __init__(self, n_nodes):
        self.parent = [i for i in range(n_nodes)]
        self.rank = [0] * n_nodes
        self.size = [1] * n_nodes

    def find(self, x):
        if x == self.parent[x]:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
            self.size[x] += self.size[y]
        else:
            self.parent[x] = y
            self.size[y] += self.size[x]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def check(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]


N, M, K = map(int, input().split())
tree = UnionFind(N)
exclusion = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    tree.unite(a, b)
    exclusion[a].append(b)
    exclusion[b].append(a)

for _ in range(K):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    if not tree.check(c, d):
        continue
    exclusion[c].append(d)
    exclusion[d].append(c)

for i in range(N):
    # 同一グループで自分以外の人数
    n = tree.get_size(i) - 1
    ans = n - len(exclusion[i])
    print(ans, end=" ")


Fixed submission:
import sys

sys.setrecursionlimit(10**6)


class UnionFind:
    def __init__(self, n_nodes):
        self.parent = [i for i in range(n_nodes)]
        self.rank = [0] * n_nodes
        self.size = [1] * n_nodes

    def find(self, x):
        if x == self.parent[x]:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
            self.size[x] += self.size[y]
        else:
            self.parent[x] = y
            self.size[y] += self.size[x]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def check(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]


N, M, K = map(int, input().split())
tree = UnionFind(N)
exclusion = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    tree.unite(a, b)
    exclusion[a].append(b)
    exclusion[b].append(a)

for _ in range(K):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    if not tree.check(c, d):
        continue
    exclusion[c].append(d)
    exclusion[d].append(c)

for i in range(N):
    # 同一グループで自分以外の人数
    n = tree.get_size(i) - 1
    ans = n - len(exclusion[i])
    print(ans, end=" ")