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

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

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) { rep(i, N) 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) {
    if (root(x) == root(y))
      return;
    par[root(x)] = root(y);
  }

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

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    tree.unite(a - 1, b - 1);
  }
  int ROOT[N];
  rep(i, N) ROOT[i] = 0;
  rep(i, N) {
    int r = tree.root(i + 1);
    ROOT[r]++;
  }
  sort(ROOT, ROOT + N);
  cout << ROOT[N - 1] << endl;
  return 0;
}

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

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) { rep(i, N) 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) {
    if (root(x) == root(y))
      return;
    par[root(x)] = root(y);
  }

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

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    tree.unite(a - 1, b - 1);
  }
  int ROOT[N];
  rep(i, N) ROOT[i] = 0;
  rep(i, N) {
    int r = tree.root(i);
    ROOT[r]++;
  }
  sort(ROOT, ROOT + N);
  cout << ROOT[N - 1] << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
/*	[]__________[][][][]____[][][][][]__{}__[][][][]____[][][][][]__[][][][]
    []__________[]______________[]__________[]__________[]__________[]____[]
    []__________[][][][]________[]__________[][][][]____[]__________[]____[]
    []__________[]______________[]________________[]____[]__[][][]__[]____[]
    []__________[]______________[]________________[]____[]______[]__[]____[]
    [][][][]____[][][][]________[]__________[][][][]____[][][][][]__[][][][]
*/
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define endl '\n'
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define decimal(x) cout << fixed << setprecision(x);
vector<int> adj[100005];
bool vis[100005];
int n;
vector<int> temp;
int c;
void dfs(int x) {
  vis[x] = true;
  c++;
  for (auto i : adj[x]) {
    if (!vis[i])
      dfs(i);
  }
}
void helper() {
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      dfs(i);
      ans = max(ans, c);
      c = 0;
    }
  }
  cout << ans << endl;
}
void solve() {
  int m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  helper();
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Fixed submission:
/*	[]__________[][][][]____[][][][][]__{}__[][][][]____[][][][][]__[][][][]
    []__________[]______________[]__________[]__________[]__________[]____[]
    []__________[][][][]________[]__________[][][][]____[]__________[]____[]
    []__________[]______________[]________________[]____[]__[][][]__[]____[]
    []__________[]______________[]________________[]____[]______[]__[]____[]
    [][][][]____[][][][]________[]__________[][][][]____[][][][][]__[][][][]
*/
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define endl '\n'
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define decimal(x) cout << fixed << setprecision(x);
vector<int> adj[200005];
bool vis[200005];
int n;
vector<int> temp;
int c;
void dfs(int x) {
  vis[x] = true;
  c++;
  for (auto i : adj[x]) {
    if (!vis[i])
      dfs(i);
  }
}
void helper() {
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      dfs(i);
      ans = max(ans, c);
      c = 0;
    }
  }
  cout << ans << endl;
}
void solve() {
  int m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  helper();
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
struct UnionFind {
  vector<int> r;
  UnionFind(int N) { r = vector<int>(N, -1); }
  int root(int x) {
    if (r[x] < 0)
      return x;
    return root(r[x]);
  }
  bool unite(int a, int b) {
    int x = root(a);
    int y = root(b);
    if (x == y)
      return false;
    if (x > y)
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    UF.unite(a, b);
  }
  int res = 1;
  for (int i = 0; i < n; i++) {
    res = max(res, UF.size(i));
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
struct UnionFind {
  vector<int> r;
  UnionFind(int N) { r = vector<int>(N, -1); }
  int root(int x) {
    if (r[x] < 0)
      return x;
    r[x] = root(r[x]);
    return r[x];
  }
  bool unite(int a, int b) {
    int x = root(a);
    int y = root(b);
    if (x == y)
      return false;
    if (x > y)
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    UF.unite(a, b);
  }
  int res = 1;
  for (int i = 0; i < n; i++) {
    res = max(res, UF.size(i));
  }
  cout << res << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
vector<int> adj[100001];
bool visited[100001];
int groupsize = 0;
int maxsize = 0;
void dfs(int s) {
  visited[s] = true;
  groupsize += 1;
  for (int i = 0; i < adj[s].size(); ++i) {
    if (visited[adj[s][i]] == false)
      dfs(adj[s][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    // cout << a << ' ' << b << '\n';
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; ++i) {
    groupsize = 0;
    if (!visited[i]) {
      dfs(i);
      if (groupsize > maxsize) {
        maxsize = groupsize;
      }
    }
  }
  cout << maxsize;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
vector<int> adj[1000001];
bool visited[1000001];
int groupsize = 0;
int maxsize = 0;
void dfs(int s) {
  visited[s] = true;
  groupsize += 1;
  for (int i = 0; i < adj[s].size(); ++i) {
    if (visited[adj[s][i]] == false)
      dfs(adj[s][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    // cout << a << ' ' << b << '\n';
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; ++i) {
    groupsize = 0;
    if (!visited[i]) {
      dfs(i);
      if (groupsize > maxsize) {
        maxsize = groupsize;
      }
    }
  }
  cout << maxsize;
}
Problem p02573 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
grid = [set() for i in range(n)]
for i in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    grid[u].add(v)
    grid[v].add(u)

cnt = 0
best = 0
visited = set()


def dfs(x):
    global cnt
    if x in visited:
        return
    cnt += 1
    visited.add(x)
    for i in grid[x]:
        dfs(i)


for i in range(n):
    cnt = 0
    dfs(i)
    best = max(best, cnt)

print(best)


Fixed submission:
n, m = map(int, input().split())
grid = [set() for i in range(n)]
for i in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    grid[u].add(v)
    grid[v].add(u)

cnt = 0
best = 0
visited = set()

import sys

sys.setrecursionlimit(1000000)


def dfs(x):
    global cnt
    if x in visited:
        return
    cnt += 1
    visited.add(x)
    for i in grid[x]:
        dfs(i)


for i in range(n):
    cnt = 0
    dfs(i)
    best = max(best, cnt)

print(best)
Problem p02573 (Python)

Buggy submission (status: Runtime Error):
N, M = list(map(int, input().split()))

d = {i: [] for i in range(1, N + 1)}
for _ in range(M):
    a, b = list(map(int, input().split()))
    d[a].append(b)
    d[b].append(a)

visited = [False for _ in range(N + 1)]


def visit(x):
    if visited[x]:
        return 0
    visited[x] = True

    z = 1
    for y in d[x]:
        z += visit(y)
    return z


m = 1
for i in range(1, N + 1):
    r = visit(i)
    if m < r:
        m = r

print(m)


Fixed submission:
import sys

sys.setrecursionlimit(10000000)

N, M = list(map(int, input().split()))

d = {i: [] for i in range(1, N + 1)}
for _ in range(M):
    a, b = list(map(int, input().split()))
    d[a].append(b)
    d[b].append(a)

visited = [False for _ in range(N + 1)]


def visit(x):
    if visited[x]:
        return 0
    visited[x] = True

    z = 1
    for y in d[x]:
        z += visit(y)
    return z


m = 1
for i in range(1, N + 1):
    r = visit(i)
    if m < r:
        m = r

print(m)
Problem p02573 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N, M = map(int, input().split())
groups = [0] * (N + 1)
group_num_list = [[0]]
group_num = 1
for _ in range(M):
    A, B = map(int, input().split())
    if groups[A] != 0 and groups[B] != 0:
        if groups[A] != groups[B]:
            lower = min(groups[A], groups[B])
            higher = max(groups[A], groups[B])
            for i in group_num_list[higher]:
                groups[i] = lower
            tmp = group_num_list[higher]
            group_num_list[lower].extend(tmp)
            # del group_num_list[higher]
    elif groups[A] != 0:
        groups[B] = groups[A]
        group_num_list[groups[A]].append(int(B))
    elif groups[B] != 0:
        groups[A] = groups[B]
        group_num_list[groups[B]].append(int(A))
    else:
        groups[A] = group_num
        groups[B] = group_num
        group_num_list.append([int(A), int(B)])
        group_num += 1
counter = Counter(groups)
# print(collection)
# print(group_num_list)
if len(groups) == 1:
    print(1)
elif int(counter.most_common()[0][0]) != 0:
    print(counter.most_common()[0][1])
else:
    print(counter.most_common()[1][1])


Fixed submission:
from collections import Counter

N, M = map(int, input().split())
groups = [0] * (N + 1)
group_num_list = [[0]]
group_num = 1
for _ in range(M):
    A, B = map(int, input().split())
    if groups[A] != 0 and groups[B] != 0:
        if groups[A] != groups[B]:
            lower = min(groups[A], groups[B])
            higher = max(groups[A], groups[B])
            for i in group_num_list[higher]:
                groups[i] = lower
            tmp = group_num_list[higher]
            group_num_list[lower].extend(tmp)
            # del group_num_list[higher]
    elif groups[A] != 0:
        groups[B] = groups[A]
        group_num_list[groups[A]].append(int(B))
    elif groups[B] != 0:
        groups[A] = groups[B]
        group_num_list[groups[B]].append(int(A))
    else:
        groups[A] = group_num
        groups[B] = group_num
        group_num_list.append([int(A), int(B)])
        group_num += 1
counter = Counter(groups)
# print(collection)
# print(group_num_list)
if len(group_num_list) == 1:
    print(1)
elif int(counter.most_common()[0][0]) != 0:
    print(counter.most_common()[0][1])
else:
    print(counter.most_common()[1][1])
Problem p02573 (Python)

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

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

    def union(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 size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

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

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {root: self.members(root) for root in self.roots()}

    def __str__(self):
        return "\n".join(f"{root}: {self.members(root)}" for root in self.roots())


N, M = map(int, input().split())

uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.union(a - 1, b - 1)

print(max(len(members) for members in uf.all_group_members().values()))


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
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(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 size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

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

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {root: self.members(root) for root in self.roots()}

    def __str__(self):
        return "\n".join(f"{root}: {self.members(root)}" for root in self.roots())


N, M = map(int, input().split())

uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.union(a - 1, b - 1)

print(max(uf.size(i) for i in range(N)))
Problem p02573 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
C = [set([]) for i in range(N)]


def dfs(n, y):
    seen[n] = True
    y += 1
    for c in C[n]:
        if seen[c]:
            continue
        else:
            y = dfs(c, y)
    return y


for i in range(M):
    a, b = map(int, input().split())
    C[a - 1].add(b - 1)
    C[b - 1].add(a - 1)

ans = 0
seen = [False] * N
for i in range(N):
    if seen[i]:
        continue
    x = 0
    x = dfs(i, x)
    ans = max(ans, x)

print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(1000000)


N, M = map(int, input().split())
C = [set([]) for i in range(N)]


def dfs(n, y):
    seen[n] = True
    y += 1
    for c in C[n]:
        if seen[c]:
            continue
        else:
            y = dfs(c, y)
    return y


for i in range(M):
    a, b = map(int, input().split())
    C[a - 1].add(b - 1)
    C[b - 1].add(a - 1)

ans = 0
seen = [False] * N
for i in range(N):
    if seen[i]:
        continue
    x = 0
    x = dfs(i, x)
    ans = max(ans, x)

print(ans)
Problem p02573 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**9)

n, m = map(int, input().split())

root = [-1] * n


def r(x):
    if root[x] < 0:
        return x

    return r(root[x])


def unite(x, y):
    x = r(x)
    y = r(y)
    if x == y:
        return
    root[x] += root[y]
    root[y] = x


def size(x):
    return -1 * root[r(x)]


for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    unite(a, b)

ans = 0
for i in range(n):
    ans = max(ans, size(i))

print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**9)

n, m = map(int, input().split())

root = [-1] * n


def r(x):
    if root[x] < 0:
        return x
    else:
        root[x] = r(root[x])
        return root[x]


def unite(x, y):
    x = r(x)
    y = r(y)
    if x == y:
        return
    root[x] += root[y]
    root[y] = x


def size(x):
    return -1 * root[r(x)]


for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    unite(a, b)

ans = 0
for i in range(n):
    ans = max(ans, size(i))

print(ans)
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define int long long

using namespace std;

const int N = 1e5 + 7;
const int mod = 1e9 + 7;

vector<int> adj[N];
int used[N], cnt, mx;

void dfs(int v) {
  used[v] = 1;
  cnt++;
  for (auto u : adj[v]) {
    if (!used[u]) {
      dfs(u);
    }
  }
}

int32_t main() {
  speed;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cnt = 0;
      dfs(i);
      mx = max(mx, cnt);
    }
  }
  cout << mx;
}


Fixed submission:
#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define int long long

using namespace std;

const int N = 2e5 + 7;
const int mod = 1e9 + 7;

vector<int> adj[N];
int used[N], cnt, mx;

void dfs(int v) {
  used[v] = 1;
  cnt++;
  for (auto u : adj[v]) {
    if (!used[u]) {
      dfs(u);
    }
  }
}

int32_t main() {
  speed;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cnt = 0;
      dfs(i);
      mx = max(mx, cnt);
    }
  }
  cout << mx;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 7e18;
using namespace std;

ll par[100001];
ll ran[100001];
ll siz[100001];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    ran[i] = 0;
    siz[i] = 1;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    par[x] = find(par[x]);
    return par[x];
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (ran[x] < ran[y]) {
    par[x] = y;
    siz[y] += siz[x];
  } else {
    par[y] = x;
    siz[x] += siz[y];
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return siz[find(x)]; }

int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    unite(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, size(i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 7e18;
using namespace std;

ll par[200001];
ll ran[200001];
ll siz[200001];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    ran[i] = 0;
    siz[i] = 1;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    par[x] = find(par[x]);
    return par[x];
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (ran[x] < ran[y]) {
    par[x] = y;
    siz[y] += siz[x];
  } else {
    par[y] = x;
    siz[x] += siz[y];
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return siz[find(x)]; }

int main() {
  int n, m;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    unite(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, size(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int father[maxn], cnt[maxn];
int findFather(int x) { return x == father[x] ? x : findFather(father[x]); }
int main() {
  fastio();
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) father[i] = i;
  rep(i, 1, m) {
    int u, v;
    cin >> u >> v;
    int fu = findFather(u), fv = findFather(v);
    if (fu != fv)
      father[fu] = fv;
  }
  int ans = 0;
  rep(i, 1, n) {
    int f = findFather(i);
    cnt[f]++;
    ans = max(cnt[f], ans);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int father[maxn], cnt[maxn];
int findFather(int x) {
  return x == father[x] ? x : father[x] = findFather(father[x]);
}
int main() {
  fastio();
  int n, m;
  cin >> n >> m;
  rep(i, 1, n) father[i] = i;
  rep(i, 1, m) {
    int u, v;
    cin >> u >> v;
    int fu = findFather(u), fv = findFather(v);
    if (fu != fv)
      father[fu] = fv;
  }
  int ans = 0;
  rep(i, 1, n) {
    int f = findFather(i);
    cnt[f]++;
    ans = max(cnt[f], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define mes(a, b) memset((a), (b), sizeof((a)))
#define syio                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);                                                             \
  std::cout.tie(0);
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
typedef long double real;
typedef pair<int, int> pii;

const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mxn = 200033;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-' ? -1 : 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int n, m, f[mxn], cnt[mxn];
void init() {
  for (int i = 1; i <= n; ++i)
    f[i] = i, cnt[i] = 1;
}
int find(int x) { return f[x] == x ? x : find(f[x]); }
void join(int u, int v) {
  int fu = find(u), fv = find(v);
  if (fu != fv) {
    f[fu] = f[fv];
    cnt[fv] += cnt[fu];
  }
}
int main() {
  syio;
  cin >> n >> m;
  init();
  int a, b;
  while (m--) {
    cin >> a >> b;
    join(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; ++i)
    if (find(i) == i)
      mx = max(mx, cnt[i]);
  cout << mx << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define mes(a, b) memset((a), (b), sizeof((a)))
#define syio                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);                                                             \
  std::cout.tie(0);
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
typedef long double real;
typedef pair<int, int> pii;

const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mxn = 200033;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-' ? -1 : 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int n, m, f[mxn], cnt[mxn];
void init() {
  for (int i = 1; i <= n; ++i)
    f[i] = i, cnt[i] = 1;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void join(int u, int v) {
  int fu = find(u), fv = find(v);
  if (fu != fv) {
    f[fu] = f[fv];
    cnt[fv] += cnt[fu];
  }
}
int main() {
  syio;
  cin >> n >> m;
  init();
  int a, b;
  while (m--) {
    cin >> a >> b;
    join(a, b);
  }
  int mx = 0;
  for (int i = 1; i <= n; ++i)
    if (find(i) == i)
      mx = max(mx, cnt[i]);
  cout << mx << endl;
  return 0;
}
Problem p02573 (C++)

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

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

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

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

  // 友達集合を作る
  UnionFind UF(N);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    // A -= 1; B -= 1;
    UF.unite(A, B);
  }

  // 最大の友達集合を求める
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, UF.size(i));
  }

  cout << ans << endl;
}


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

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

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

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

  // 友達集合を作る
  UnionFind UF(N + 1);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    // A -= 1; B -= 1;
    UF.unite(A, B);
  }

  // 最大の友達集合を求める
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, UF.size(i));
  }

  cout << ans << endl;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll read() {
  ll x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int f[100005], n, m, maxn, cnt[100005];
int find(int x) {
  if (x == f[x])
    return f[x];
  return f[x] = find(f[x]);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read();
    int x = find(a), y = find(b);
    if (x != y)
      f[x] = y;
  }
  for (int i = 1; i <= n; i++)
    cnt[find(i)]++;
  for (int i = 1; i <= n; i++)
    maxn = max(maxn, cnt[i]);
  cout << maxn;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll read() {
  ll x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int f[500005], n, m, maxn, cnt[500005];
int find(int x) {
  if (x == f[x])
    return f[x];
  return f[x] = find(f[x]);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read();
    int x = find(a), y = find(b);
    if (x != y)
      f[x] = y;
  }
  for (int i = 1; i <= n; i++)
    cnt[find(i)]++;
  for (int i = 1; i <= n; i++)
    maxn = max(maxn, cnt[i]);
  cout << maxn;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int p[100005], n, m, g[100005], ans = 0, nc = 0;
vector<int> v;
int fa(int x) { return (p[x] == x) ? x : (p[x] = fa(p[x])); }
void merge(int x, int y) {
  int fx = fa(x), fy = fa(y);
  p[fx] = fy;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    merge(a, b);
  }
  for (int i = 1; i <= n; i++) {
    g[fa(i)]++;
  }
  for (int i = 1; i <= n; i++)
    if (g[i] != 0)
      v.push_back(g[i]);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i != 0 && v[i] == v[i - 1])
      continue;
    ans += (v[i] - nc);
    nc += (v[i] - nc);
    // cout<<i<<' '<<ans<<' '<<nc<<endl;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int p[200005], n, m, g[200005], ans = 0, nc = 0;
vector<int> v;
int fa(int x) { return (p[x] == x) ? x : (p[x] = fa(p[x])); }
void merge(int x, int y) {
  int fx = fa(x), fy = fa(y);
  p[fx] = fy;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    merge(a, b);
  }
  for (int i = 1; i <= n; i++) {
    g[fa(i)]++;
  }
  for (int i = 1; i <= n; i++)
    if (g[i] != 0)
      v.push_back(g[i]);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (i != 0 && v[i] == v[i - 1])
      continue;
    ans += (v[i] - nc);
    nc += (v[i] - nc);
    // cout<<i<<' '<<ans<<' '<<nc<<endl;
  }
  cout << ans;
  return 0;
}
Problem p02573 (C++)

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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

class UnionFind {
private:
  ll num;
  vector<ll> par;
  vector<ll> sizes;
  vector<ll> rank;

public:
  UnionFind(ll n) { init(n); }

  ~UnionFind() {}

  ll init(ll n) {
    num = n;
    par.resize(n);
    sizes.resize(n);
    rank.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
      sizes[i] = 1;
      rank[i] = 0;
    }
  }

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

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
    ll temp = sizes[rx] + sizes[ry];
    sizes[rx] = temp;
    sizes[ry] = temp;
  }

  ll get_size(ll x) { return sizes[root(x)]; }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  ll max_size = 0;
  rep(i, n) { max_size = max(max_size, u.get_size(i)); }
  cout << max_size << endl;
  return 0;
}


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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

class UnionFind {
private:
  ll num;
  vector<ll> par;
  vector<ll> sizes;
  vector<ll> rank;

public:
  UnionFind(ll n) { init(n); }

  ~UnionFind() {}

  void init(ll n) {
    num = n;
    par.resize(n);
    sizes.resize(n);
    rank.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
      sizes[i] = 1;
      rank[i] = 0;
    }
  }

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

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
    ll temp = sizes[rx] + sizes[ry];
    sizes[rx] = temp;
    sizes[ry] = temp;
  }

  ll get_size(ll x) { return sizes[root(x)]; }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  ll max_size = 0;
  rep(i, n) { max_size = max(max_size, u.get_size(i)); }
  cout << max_size << endl;
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <time.h>
#include <vector>
#define h 200010
#define lli long long int
#define pb push_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
using namespace std;
vector<lli> par(h);
lli func(lli a) {
  if (par[a] == a)
    return a;
  else
    par[a] = func(par[a]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  lli n, e, i, x, y, u, v, ans = 0, l = 0, cnt;
  set<lli> c;
  cin >> n >> e;
  for (i = 0; i <= n; i++)
    par[i] = i;
  for (i = 0; i < e; i++) {
    cin >> x >> y;
    u = func(x);
    v = func(y);
    if (u != v)
      par[u] = v;
  }
  for (i = 1; i <= n; i++)
    u = func(i);
  sort(par.begin() + 1, par.begin() + n + 1);
  cnt = 1;
  for (i = 1; i <= n; i++) {
    c.insert(par[i]);
    if (l == c.size())
      cnt += 1;
    else {
      l += 1;
      ans = max(ans, cnt);
      cnt = 1;
    }
  }
  ans = max(ans, cnt);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <time.h>
#include <vector>
#define h 200010
#define lli long long int
#define pb push_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
using namespace std;
vector<lli> par(h);
lli func(lli a) {
  if (par[a] == a)
    return a;
  else
    return par[a] = func(par[a]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  lli n, e, i, x, y, u, v, ans = 0, l = 0, cnt;
  set<lli> c;
  cin >> n >> e;
  for (i = 0; i <= n; i++)
    par[i] = i;
  for (i = 0; i < e; i++) {
    cin >> x >> y;
    u = func(x);
    v = func(y);
    if (u != v)
      par[u] = v;
  }
  for (i = 1; i <= n; i++)
    u = func(i);
  sort(par.begin() + 1, par.begin() + n + 1);
  cnt = 1;
  for (i = 1; i <= n; i++) {
    c.insert(par[i]);
    if (l == c.size())
      cnt += 1;
    else {
      l += 1;
      ans = max(ans, cnt);
      cnt = 1;
    }
  }
  ans = max(ans, cnt);
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++)

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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long double pi = acos(-1);
#define x first
#define y second
#define read(n, a)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define rep(z, n) for (int i = z; i < n; i++)

// pair<int,int> a[200002]={};
// ll b[200003]={};
ll n, m;
#define MAXNODES 100000 + 9
#define PB push_back
typedef vector<int> vi;
vector<vi> edges(MAXNODES);
bool visited[MAXNODES];
int dfs(int node) {
  int ans = 0;
  visited[node] = true;
  for (int neighbor : edges[node]) {
    if (!visited[neighbor]) {
      ans++;
      ans += dfs(neighbor);
    }
  }
  return ans;
}
// vector<set<int> >v(200002);
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  /// freopen("ss.txt","r",stdin);
  /// freopen("out.txt","w",stdout);
  int t = 1;
  // cin>>t;
  while (t--) {
    cin >> n >> m;
    rep(0, m) {
      int u, v;
      cin >> u >> v;
      edges[u].PB(v);
      edges[v].PB(u);
    }
    ll ans = 0;
    rep(1, n + 1) {
      if (!visited[i]) {
        ans = max(ans, (ll)dfs(i) + 1);
      }
    }
    cout << ans;
  }
  return 0;
}


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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long double pi = acos(-1);
#define x first
#define y second
#define read(n, a)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define rep(z, n) for (int i = z; i < n; i++)

// pair<int,int> a[200002]={};
// ll b[200003]={};
ll n, m;
#define MAXNODES 200000 + 9
#define PB push_back
typedef vector<int> vi;
vector<vi> edges(MAXNODES);
bool visited[MAXNODES];
int dfs(int node) {
  int ans = 0;
  visited[node] = true;
  for (int neighbor : edges[node]) {
    if (!visited[neighbor]) {
      ans++;
      ans += dfs(neighbor);
    }
  }
  return ans;
}
// vector<set<int> >v(200002);
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  /// freopen("ss.txt","r",stdin);
  /// freopen("out.txt","w",stdout);
  int t = 1;
  // cin>>t;
  while (t--) {
    cin >> n >> m;
    rep(0, m) {
      int u, v;
      cin >> u >> v;
      edges[u].PB(v);
      edges[v].PB(u);
    }
    ll ans = 0;
    rep(1, n + 1) {
      if (!visited[i]) {
        ans = max(ans, (ll)dfs(i) + 1);
      }
    }
    cout << ans;
  }
  return 0;
}
Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
bool vis[100005];
vector<ll> gr[100005];

ll dfs(ll u) {
  vis[u] = true;
  ll ans = 1;
  for (auto v : gr[u]) {
    if (!vis[v]) {
      ans += dfs(v);
    }
  }
  return ans;
}

int main() {
  ll n, m;
  cin >> n >> m;
  memset(vis, false, sizeof(vis));

  ll a, b;
  map<pair<ll, ll>, ll> ma;
  while (m--) {
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (!ma[{a, b}]) {
      gr[a].push_back(b);
      gr[b].push_back(a);
    }
  }
  ll mx = 0;
  for (ll i = 1; i <= n; ++i) {
    if (vis[i] == false) {
      mx = max(mx, dfs(i));
    }
  }
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
bool vis[200005];
vector<ll> gr[200005];

ll dfs(ll u) {
  vis[u] = true;
  ll ans = 1;
  for (auto v : gr[u]) {
    if (!vis[v]) {
      ans += dfs(v);
    }
  }
  return ans;
}

int main() {
  ll n, m;
  cin >> n >> m;
  memset(vis, false, sizeof(vis));

  ll a, b;
  map<pair<ll, ll>, ll> ma;
  while (m--) {
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (!ma[{a, b}]) {
      gr[a].push_back(b);
      gr[b].push_back(a);
    }
  }
  ll mx = 0;
  for (ll i = 1; i <= n; ++i) {
    if (vis[i] == false) {
      mx = max(mx, dfs(i));
    }
  }
  cout << mx << endl;
}
Problem p02573 (C++)

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

using namespace std;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
const int LIT = 1e9 + 7;
int n, m;

vector<int> Map[20001];
set<pair<int, int>> s;

void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (s.count({a, b}) == 0) {
      s.insert({a, b});
      Map[a].push_back(b);
      Map[b].push_back(a);
    }
  }

  vector<bool> bfs_visited(n + 1, false);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int cnt = 1;
    if (!bfs_visited[i]) {
      queue<int> q;
      q.push(i);
      bfs_visited[i] = true;
      while (!q.empty()) {
        int cur = q.front();
        q.pop();

        for (int j = 0; j < Map[cur].size(); j++) {
          if (!bfs_visited[Map[cur][j]]) {
            bfs_visited[Map[cur][j]] = true;
            cnt++;
            q.push(Map[cur][j]);
          }
        }
      }
    }
    ans = max(ans, cnt);
  }

  cout << ans;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
const int LIT = 1e9 + 7;
int n, m;

vector<int> Map[200001];
set<pair<int, int>> s;

void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (s.count({a, b}) == 0) {
      s.insert({a, b});
      Map[a].push_back(b);
      Map[b].push_back(a);
    }
  }

  vector<bool> bfs_visited(n + 1, false);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int cnt = 1;
    if (!bfs_visited[i]) {
      queue<int> q;
      q.push(i);
      bfs_visited[i] = true;
      while (!q.empty()) {
        int cur = q.front();
        q.pop();

        for (int j = 0; j < Map[cur].size(); j++) {
          if (!bfs_visited[Map[cur][j]]) {
            bfs_visited[Map[cur][j]] = true;
            cnt++;
            q.push(Map[cur][j]);
          }
        }
      }
    }
    ans = max(ans, cnt);
  }

  cout << ans;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02573 (C++)

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

struct UnionFind {
  vector<int> p;

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

  int root(int c) {
    if (p.at(c) < 0)
      return c;
    else
      return root(p.at(c));
  }

  void unite(int a, int b) {
    int ra = root(a);
    int rb = root(b);
    if (ra == rb)
      return;
    if (p.at(ra) > p.at(rb))
      swap(p.at(ra), p.at(rb));
    p.at(ra) += p.at(rb);
    p.at(rb) = ra;
  }

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

int main() {
  int n, m;
  cin >> n >> m;

  UnionFind fuf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    fuf.unite(a - 1, b - 1);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, fuf.size(i));
  }
  cout << ans << endl;
}

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

struct UnionFind {
  vector<int> p;

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

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

  void unite(int a, int b) {
    int ra = root(a);
    int rb = root(b);
    if (ra == rb)
      return;
    if (p.at(ra) > p.at(rb))
      swap(p.at(ra), p.at(rb));
    p.at(ra) += p.at(rb);
    p.at(rb) = ra;
  }

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

int main() {
  int n, m;
  cin >> n >> m;

  UnionFind fuf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    fuf.unite(a - 1, b - 1);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, fuf.size(i));
  }
  cout << ans << endl;
}