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

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

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

void printVec(std::vector<string> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  vector<string> subs;
  REP(i, s.size()) {
    FOR(j, 1, s.size() - i + 1) { subs.push_back(s.substr(i, j)); }
  }
  sort(ALL(subs));
  subs.erase(unique(ALL(subs)), subs.end());

  // printVec(subs);
  cout << subs[K - 1] << endl;

  return 0;
}


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

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

void printVec(std::vector<string> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  vector<string> subs;
  REP(i, s.size()) {
    FOR(j, 1, min(6, int(s.size() - i + 1))) { subs.push_back(s.substr(i, j)); }
  }
  sort(ALL(subs));
  subs.erase(unique(ALL(subs)), subs.end());

  // printVec(subs);
  cout << subs[K - 1] << endl;

  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;

  int num = 0;
  set<string> st;
  for (int l = 1; l <= s.size(); l++) {
    for (int i = 0; i < s.length() - (l - 1); i++) {
      st.insert(s.substr(i, l));
    }
  }

  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    if (num == K - 1) {
      cout << *itr << endl;
      return 0;
    }
    num++;
  }
}


Fixed submission:
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;

  int num = 0;
  set<string> st;
  for (int l = 1; l <= K; l++) {
    for (int i = 0; i < s.length() - (l - 1); i++) {
      st.insert(s.substr(i, l));
    }
  }

  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    if (num == K - 1) {
      cout << *itr << endl;
      return 0;
    }
    num++;
  }
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// using namespace std;

template <typename T>
using gtree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
  string s;
  cin >> s;
  vector<string> v;
  int k;
  cin >> k;
  gtree<string> t;
  rep(i, s.size()) {
    string a = "";
    for (int j = i; j < s.size(); ++j) {
      a += s[j];
      t.insert(a);
    }
  }

  cout << *t.find_by_order(k - 1) << endl;
}


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

template <typename T>
using gtree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
  string s;
  cin >> s;
  vector<string> v;
  int k;
  cin >> k;
  gtree<string> t;
  rep(i, s.size()) {
    string a = "";
    for (int j = i; j < s.size(); ++j) {
      a += s[j];
      if (a.size() > k)
        break;
      t.insert(a);
    }
  }

  cout << *t.find_by_order(k - 1) << endl;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int main() {
  set<string> st;
  string s;
  int k;
  cin >> s >> k;
  rep(i, s.size()) {
    string tmp = "";
    tmp += s[i];
    st.insert(tmp);
    for (int j = i + 1; j < min((int)s.size(), j + k - 1); ++j) {
      tmp += s[j];
      st.insert(tmp);
    }
  }
  int cnt = 0;
  string ret = "";
  for (auto &ans : st) {
    ret = ans;
    if (cnt + 1 == k)
      break;
    cnt++;
  }
  cout << ret << endl;
  return 0;
}


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

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int main() {
  set<string> st;
  string s;
  int k;
  cin >> s >> k;
  rep(i, s.size()) {
    string tmp = "";
    tmp += s[i];
    st.insert(tmp);
    for (int j = i + 1; j < min((int)s.size(), i + k); ++j) {
      tmp += s[j];
      st.insert(tmp);
    }
  }
  int cnt = 0;
  string ret = "";
  for (auto &ans : st) {
    ret = ans;
    if (cnt + 1 == k)
      break;
    cnt++;
  }
  cout << ret << endl;
  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;

string s;
int K;
map<string, int> mp;

int main(void) {

  cin >> s;
  scanf("%d", &K);

  vector<P> v(s.size());
  for (int i = 0; i < s.size(); ++i)
    v[i].first = s[i] - 'a', v[i].second = i;
  sort(v.begin(), v.end());

  priority_queue<string, vector<string>, greater<string>> que;
  int cnt = 0, num = 0;
  for (auto p : v) {
    if (cnt >= K && num != p.first)
      break;
    num = p.first;
    int idx = p.second;
    string temp;
    for (int i = idx; i < s.size(); ++i) {
      temp = temp + s[i];
      if (mp.find(temp) == mp.end())
        ++cnt, que.push(temp);
      ++mp[temp];
    }
  }

  for (int i = 0; i < K - 1; ++i)
    que.pop();
  printf("%s\n", que.top().c_str());

  return 0;
}


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

string s;
int K;
map<string, int> mp;

int main(void) {

  cin >> s;
  scanf("%d", &K);

  vector<P> v(s.size());
  for (int i = 0; i < s.size(); ++i)
    v[i].first = s[i] - 'a', v[i].second = i;
  sort(v.begin(), v.end());

  priority_queue<string, vector<string>, greater<string>> que;
  int cnt = 0, num = 0;
  for (auto p : v) {
    if (cnt >= K && num != p.first)
      break;
    num = p.first;
    int idx = p.second;
    string temp;
    for (int i = idx; i < s.size() && i < idx + K; ++i) {
      temp = temp + s[i];
      if (mp.find(temp) == mp.end())
        ++cnt, que.push(temp);
      ++mp[temp];
    }
  }

  for (int i = 0; i < K - 1; ++i)
    que.pop();
  printf("%s\n", que.top().c_str());

  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define dum(x) cout << #x << '=' << x << endl
#define ll long long
using namespace std;

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> vs;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= s.size() - i; j++) {
      string str = s.substr(i, min(j, 5));
      vs.push_back(str);
    }
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  cout << vs[k - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define dum(x) cout << #x << '=' << x << endl
#define ll long long
using namespace std;

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> vs;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= k; j++) {
      string str = s.substr(i, j);
      vs.push_back(str);
    }
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  cout << vs[k - 1] << endl;
}
Problem p03353 (C++)

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

using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define AND(x, y, z)                                                           \
  set_intersection((x).begin(), (x).end(), (y).begin(), (y).end(),             \
                   inserter((z), (z).end()))
#define CONCAT(x, y) (x).insert((x).end(), (y).begin(), (y).end())
#define DUMP(xs)                                                               \
  for (auto x : xs)                                                            \
    cout << x << ' ';                                                          \
  cout << endl
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define OR(x, y, z)                                                            \
  set_union((x).begin(), (x).end(), (y).begin(), (y).end(),                    \
            inserter((z), (z).end()))
#define OUT(x) cout << x << endl
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> VECINT;
typedef vector<ll> VECLL;

int main() {
  string S;
  cin >> S;
  int N;
  N = S.size();
  int K;
  cin >> K;
  set<string> dict;
  // 0~N-1
  REP(i, N) {
    // 1~N-i
    FOR(j, 1, N - i + 1) { dict.insert(S.substr(i, j)); }
  }
  vector<string> vec(ALL(dict));
  OUT(vec[K - 1]);

  return 0;
}

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

using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define AND(x, y, z)                                                           \
  set_intersection((x).begin(), (x).end(), (y).begin(), (y).end(),             \
                   inserter((z), (z).end()))
#define CONCAT(x, y) (x).insert((x).end(), (y).begin(), (y).end())
#define DUMP(xs)                                                               \
  for (auto x : xs)                                                            \
    cout << x << ' ';                                                          \
  cout << endl
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define OR(x, y, z)                                                            \
  set_union((x).begin(), (x).end(), (y).begin(), (y).end(),                    \
            inserter((z), (z).end()))
#define OUT(x) cout << x << endl
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> VECINT;
typedef vector<ll> VECLL;

int main() {
  string S;
  cin >> S;
  int N;
  N = S.size();
  int K;
  cin >> K;
  set<string> dict;
  // 0~N-1
  REP(i, N) {
    // 1~N-i
    FOR(j, 1, min(6, N - i + 1)) { dict.insert(S.substr(i, j)); }
  }
  vector<string> vec(ALL(dict));
  OUT(vec[K - 1]);

  return 0;
}
Problem p03353 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  string s;
  int k;
  cin >> s >> k;
  map<string, int> maps;
  for (int i = 1; i <= s.length(); i++) {
    for (int j = 0; i + j <= s.length(); j++) {
      string str = s.substr(j, i);
      maps[str]++;
    }
  }
  int index = 0;
  for (const auto &item : maps) {
    index++;
    if (index == k) {
      cout << item.first << endl;
      return 0;
    }
  }
}

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

using namespace std;
typedef long long ll;

int main() {
  string s;
  int k;
  cin >> s >> k;
  map<string, int> maps;
  for (int i = 1; i <= k; i++) {
    for (int j = 0; i + j <= s.length(); j++) {
      string str = s.substr(j, i);
      maps[str]++;
    }
  }
  int index = 0;
  for (const auto &item : maps) {
    index++;
    if (index == k) {
      cout << item.first << endl;
      return 0;
    }
  }
}
Problem p03353 (C++)

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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  int n = s.size();
  vector<string> v;
  int t = min(5, n);

  for (int i = 1; i <= t; i++) { // i文字分のsubstring
    rep(j, n - i + 1) { v.push_back(s.substr(j, i)); }
  }

  sort(v.begin(), v.end());
  int i = 0, cnt = 1;
  while (true) {
    if (v[i] != v[i + 1]) {
      if (cnt == k) {
        cout << v[i] << endl;
        break;
      }
      cnt++;
      i++;
    } else
      i++;
  }
}

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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  int n = s.size();
  vector<string> v;
  int t = min(5, n);

  for (int i = 1; i <= t; i++) { // i文字分のsubstring
    rep(j, n - i + 1) { v.push_back(s.substr(j, i)); }
  }

  sort(v.begin(), v.end());

  v.push_back(" ");

  int i = 0, cnt = 1;
  while (true) {
    if (v[i] != v[i + 1]) {
      if (cnt == k) {
        cout << v[i] << endl;
        break;
      }
      cnt++;
      i++;
    } else
      i++;
  }
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long int ll;
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < signed(n); i++)
#define EREP(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a).begin(), (a).end()
// #define EVEL 1

#ifdef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\n";
#else
#define DEB(X)                                                                 \
  {}
#define TF(f)                                                                  \
  {}
#define END                                                                    \
  {}
#endif
const int MOD = 1000000007;
const ll INF = 20000000000;
typedef std::pair<int, int> P;

ll ans;
ll N, A[200010];
ll K;
string S;
set<string> Q;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> S >> K;
  REP(i, S.size()) {
    string t = "";
    for (int j = 0; j < S.size() - i; j++) {
      t += S[i + j];
      Q.insert(t);
    }
  }
  int cnt = 1;
  for (auto x : Q) {
    if (cnt == K) {
      cout << x;
      break;
    }
    cnt++;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long int ll;
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < signed(n); i++)
#define EREP(i, n) for (int i = 1; i <= (n); i++)
#define ALL(a) (a).begin(), (a).end()
// #define EVEL 1

#ifdef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\n";
#else
#define DEB(X)                                                                 \
  {}
#define TF(f)                                                                  \
  {}
#define END                                                                    \
  {}
#endif
const int MOD = 1000000007;
const ll INF = 20000000000;
typedef std::pair<int, int> P;

ll ans;
ll N, A[200010];
ll K;
string S;
set<string> Q;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> S >> K;
  REP(i, S.size()) {
    for (int j = 0; j < 5; j++) {
      Q.insert(S.substr(i, j + 1));
    }
  }
  int cnt = 1;
  for (auto x : Q) {
    if (cnt == K) {
      cout << x;
      break;
    }
    cnt++;
  }
}
Problem p03353 (C++)

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

using namespace std;

int main() {
  string s;
  vector<string> S;
  int K;
  cin >> s >> K;
  for (int i = 1; i <= 5; i++) {
    for (int j = 0; j < s.size() - i; j++) {
      S.push_back(s.substr(j, i));
    }
  }
  sort(S.begin(), S.end());
  S.erase(unique(S.begin(), S.end()), S.end());
  // for(int i = 0; i < K; i++){
  //     cout << S[i] << endl;
  // }
  cout << S[K - 1] << endl;
}

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

using namespace std;

int main() {
  string s;
  vector<string> S;
  int K;
  cin >> s >> K;
  int tmp = min(5, (int)s.size());
  for (int i = 1; i <= tmp; i++) {
    for (int j = 0; j < s.size(); j++) {
      S.push_back(s.substr(j, i));
    }
  }
  sort(S.begin(), S.end());
  S.erase(unique(S.begin(), S.end()), S.end());
  // for(int i = 0; i < K; i++){
  //     cout << S[i] << endl;
  // }
  cout << S[K - 1] << endl;
}
Problem p03353 (C++)

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

int main() {
  string s;
  int k;
  cin >> s;
  cin >> k;
  set<string> dp;
  for (int t = 0; t < s.length(); ++t)
    for (int i = 0, j = t; j < s.length(); ++i, ++j)
      dp.insert(s.substr(i, j - i + 1));
  auto iter = dp.begin();
  for (int i = 0; i < k - 1; ++i)
    ++iter;
  cout << *iter << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  int k;
  cin >> s;
  cin >> k;
  set<string> dp;
  for (int t = 0; t < s.length(); ++t)
    for (int i = 0, j = t; j < s.length(); ++i, ++j) {
      string t = s.substr(i, j - i + 1);
      if (dp.size() < 5)
        dp.insert(t);
      else {
        auto ii = --dp.end();
        string t2 = *(ii);
        if (t < t2) {
          dp.erase(ii);
          dp.insert(t);
        }
      }
    }
  auto iter = dp.begin();
  for (int i = 0; i < k - 1; ++i)
    ++iter;
  cout << *iter << endl;
  return 0;
}
Problem p03353 (C++)

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

typedef long long ll;

#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
#define all(a) (a).begin(), (a).end()

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  int k;
  cin >> s >> k;

  int N = s.size();
  map<string, int> mp;
  REP(i, 0, N) {
    for (int j = 1; i + j <= N; j++) {
      string subs = s.substr(i, j);
      mp[subs]++;
    }
  }
  int cnt = 0;
  for (auto e : mp) {
    cnt++;
    if (cnt == k) {
      cout << e.first << '\n';
      break;
    }
  }
  return 0;
}

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

typedef long long ll;

#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
#define all(a) (a).begin(), (a).end()

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  int k;
  cin >> s >> k;

  int N = s.size();
  map<string, int> mp;
  REP(i, 0, N) {
    for (int j = 1; i + j <= N && j <= k; j++) {
      string subs = s.substr(i, j);
      mp[subs]++;
    }
  }
  int cnt = 0;
  for (auto e : mp) {
    cnt++;
    if (cnt == k) {
      cout << e.first << '\n';
      break;
    }
  }
  return 0;
}
Problem p03353 (C++)

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

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  int k;
  cin >> s >> k;
  vector<string> ans;

  rep(i, s.length()) {
    for (int j = 1; i + j <= s.length(); ++j) {
      ans.push_back(s.substr(i, j));
    }
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());

  cout << ans[k - 1] << '\n';
  return 0;
}


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

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  int k;
  cin >> s >> k;
  vector<string> ans;

  rep(i, s.length()) {
    for (int j = 1; i + j <= s.length() && j <= k; ++j) {
      ans.push_back(s.substr(i, j));
    }
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());

  cout << ans[k - 1] << '\n';
  return 0;
}
Problem p03353 (C++)

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

using namespace std;
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

int main() {

  int n;

  string str;

  cin >> str >> n;

  vector<string> ans;

  map<string, int> used;
  for (int i = 0; str[i]; i++) {

    string add = "";
    for (int j = i; str[j]; j++) {

      add += str[j];
      if (!used[add])
        ans.pb(add), used[add] = 1;
    }
  }

  sort(all(ans));

  cout << ans[n - 1];

  return 0;
}


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

using namespace std;
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

int main() {

  int n;

  string str;

  cin >> str >> n;

  vector<string> ans;

  map<string, int> used;
  for (int i = 0; str[i]; i++) {

    string add = "";
    for (int j = i; str[j] && j < i + n; j++) {

      add += str[j];
      if (!used[add])
        ans.pb(add), used[add] = 1;
    }
  }

  sort(all(ans));

  cout << ans[n - 1];

  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
string s;
int k;
set<string> ses;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> s >> k;
  REP(i, s.length()) {
    string ss;
    ss.push_back(s[i]);
    ses.insert(ss);
    FOR(j, i + 1, s.length()) {
      ss.push_back(s[j]);
      ses.insert(ss);
    }
  }

  int i = 1;
  for (auto x : ses) {
    if (i++ == k)
      cout << x << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
string s;
int k;
set<string> ses;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> s >> k;
  REP(i, s.length()) {
    string ss;
    ss.push_back(s[i]);
    ses.insert(ss);
    for (int j = 1; i + j < s.length() && j < k; j++) {
      ss.push_back(s[i + j]);
      ses.insert(ss);
    }
  }

  int i = 1;
  for (auto x : ses) {
    if (i++ == k)
      cout << x << endl;
  }
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifdef LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
        cin.tie(NULL);
        ios::sync_with_stdio(false);
}
*/

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  int k;

  cin >> k;

  set<string> se;

  int len = s.size();

  rep(i, len) {
    for (int j = i; j < len; j++) {
      se.insert(s.substr(i, j - i + 1));
    }
  }

  set<string>::iterator it = se.begin();

  int t = 1;

  for (; it != se.end(); it++) {
    if (t == k) {
      cout << *it << endl;
      break;
    }
    t++;
  }

  return 0;
}


Fixed submission:
#ifdef LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
        cin.tie(NULL);
        ios::sync_with_stdio(false);
}
*/

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  int k;

  cin >> k;

  set<string> se;

  int len = s.size();

  rep(i, len) {
    for (int j = i; j < k + i; j++) {
      se.insert(s.substr(i, j - i + 1));
    }
  }

  set<string>::iterator it = se.begin();

  int t = 1;

  for (; it != se.end(); it++) {
    if (t == k) {
      cout << *it << endl;
      break;
    }
    t++;
  }

  return 0;
}
Problem p03353 (C++)

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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;

  set<string> st;
  rep(i, 0, s.size()) {
    rep(j, 0, s.size() - i) { st.insert(s.substr(i, j + 1)); }
  }
  auto itr = st.begin();
  rep(i, 1, k)++ itr;

  cout << *itr << endl;

  return 0;
}

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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;

  set<string> st;
  rep(i, 0, s.size()) {
    rep(j, 0, k) {
      if (j + 1 > s.size())
        break;
      st.insert(s.substr(i, j + 1));
    }
  }
  auto itr = st.begin();
  rep(i, 1, k)++ itr;

  cout << *itr << endl;

  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int k;
  cin >> s >> k;
  vector<string> ans;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= s.size(); j++) {
      ans.push_back(s.substr(i, j));
    }
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());
  // for(int i=0;i<ans.size();i++)cout<<ans[i]<<endl;
  cout << ans[k - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int k;
  cin >> s >> k;
  vector<string> ans;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= k; j++) {
      ans.push_back(s.substr(i, j));
    }
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());
  // for(int i=0;i<ans.size();i++)cout<<ans[i]<<endl;
  cout << ans[k - 1] << endl;
  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = s.length();
  int i, j;
  set<string> str;
  for (i = 0; i < n; i++) {
    for (j = 1; i + j <= n; j++) {
      str.insert(s.substr(i, j));
    }
  }
  i = 1;
  for (auto kk : str) {
    if (i == k) {
      cout << kk << endl;
      break;
    }
    i++;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = s.length();
  int i, j;
  set<string> str;
  for (i = 0; i < n; i++) {
    for (j = 1; i + j <= n && j <= k; j++) {
      str.insert(s.substr(i, j));
    }
  }
  i = 1;
  for (auto kk : str) {
    if (i == k) {
      cout << kk << endl;
      break;
    }
    i++;
  }
}
Problem p03353 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define int long long

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a) for (int i = (a); i >= 0; --i)
#define CLR(a) memset((a), 0, sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF (3e15)

using namespace std;

string S;
int K;
signed main() {
  cin >> S;
  cin >> K;

  vector<string> vs;
  REP(i, S.size())
  FOR(j, i + 1, S.size() + 1) { vs.push_back(S.substr(i, j - i)); }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  // REP(i, vs.size()) { DUMP(vs[i]); }
  cout << vs[K - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define int long long

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a) for (int i = (a); i >= 0; --i)
#define CLR(a) memset((a), 0, sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF (3e15)

using namespace std;

string S;
int K;
signed main() {
  cin >> S;
  cin >> K;

  vector<string> vs;
  REP(i, S.size()) {
    FOR(j, i + 1, i + 10) { vs.push_back(S.substr(i, j - i)); }
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  // REP(i, vs.size()) { DUMP(vs[i]); }
  cout << vs[K - 1] << endl;

  return 0;
}
Problem p03353 (C++)

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> da;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= s.size() - i; j++) {
      string t = s.substr(i, j);
      da.insert(t);
    }
  }
  auto pos = da.begin();
  for (int i = 0; i < k - 1; i++)
    pos++;
  cout << *pos << endl;
}

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> da;
  int n = s.size();
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= min(k, n - i); j++) {
      string t = s.substr(i, j);
      da.insert(t);
    }
  }
  auto pos = da.begin();
  for (int i = 0; i < k - 1; i++)
    pos++;
  cout << *pos << endl;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;

  set<string> stset;
  for (int i = 0; i < s.length(); i++) {
    for (int l = 1; l <= s.length(); l++) {
      if (i + l > s.length())
        continue;
      string t = s.substr(i, l);
      stset.insert(t);
    }
  }

  int cnt = 1;
  for (auto st : stset) {
    if (cnt == K) {
      cout << st << endl;
      break;
    }
    cnt++;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <set>
#include <string>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;

  set<string> stset;
  for (int i = 0; i < s.length(); i++) {
    for (int l = 1; l <= 5; l++) {
      if (i + l > s.length())
        continue;
      string t = s.substr(i, l);
      stset.insert(t);
    }
  }

  int cnt = 1;
  for (auto st : stset) {
    if (cnt == K) {
      cout << st << endl;
      break;
    }
    cnt++;
  }

  return 0;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  set<string> t;
  for (int i = 0; i < s.size(); i++) {
    string h = "";
    h += s[i];
    t.insert(h);
    for (int j = i + 1; j < s.size(); j++) {
      h += s[j];
      t.insert(h);
    }
  }
  auto itr = t.begin();
  for (int i = 1; i < k; i++)
    itr++;
  cout << *itr << endl;
  system("pause");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  set<string> t;
  for (int i = 0; i < s.size(); i++) {
    string h = "";
    h += s[i];
    t.insert(h);
    for (int j = i + 1; j < min(i + k, (int)s.size()); j++) {
      h += s[j];
      t.insert(h);
    }
  }
  auto itr = t.begin();
  for (int i = 1; i < k; i++)
    itr++;
  cout << *itr << endl;
  system("pause");
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  set<string> st;
  string s;
  cin >> s;
  int k;
  cin >> k;
  rep(i, s.size()) {
    rep(j, s.size() - i) { st.insert(s.substr(i, j + 1)); }
  }
  set<string>::iterator itr = st.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  set<string> st;
  string s;
  cin >> s;
  int k;
  cin >> k;
  rep(i, s.size()) {
    rep(j, s.size() - i) {
      if (j == 5)
        break;
      st.insert(s.substr(i, j + 1));
    }
  }
  set<string>::iterator itr = st.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
}
Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

int main() {
  string s;
  ll k, res = 0;
  cin >> s >> k;
  string sub, ans;
  map<string, int> mp;
  for (int i = 1; i <= s.size(); i++) { // 引き抜く文字数
    rep(j, s.size() - i + 1) {          // 開始地点
      sub = s.substr(j, i);
      if (mp.find(sub) == mp.end()) {
        mp[sub] = 1;
        //    cout << sub << endl;
      }
    }
  }
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    res++;
    //  cout << res << ":"<< key << endl;
    if (res == k) {
      ans = itr->first;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

int main() {
  string s;
  ll k, res = 0;
  cin >> s >> k;
  string sub, ans;
  map<string, int> mp;
  for (int i = 1; i <= k; i++) { // 引き抜く文字数
    rep(j, s.size() - i + 1) {   // 開始地点
      sub = s.substr(j, i);
      if (mp.find(sub) == mp.end()) {
        mp[sub] = 1;
        //    cout << sub << endl;
      }
    }
  }
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    res++;
    //  cout << res << ":"<< key << endl;
    if (res == k) {
      ans = itr->first;
    }
  }
  cout << ans << endl;
}
Problem p03354 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
vector<int> par, sz;
int find(int x) {
  while (x != par[x])
    x = par[x];
  return x;
}
void uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (sz[a] < sz[b])
    swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
}
bool same(int a, int b) { return find(a) == find(b); }

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  int n, m;
  scanf("%lld%lld", &n, &m);
  sz = vector<int>(n + 1, 1);
  par = vector<int>(n + 1);
  iota(par.begin(), par.end(), 0);

  vector<int> p(n + 1);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &p[i]);

  int u, v;
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld", &u, &v);
    uni(u, v);
  }

  int ct = 0;
  for (int i = 1; i <= n; i++) {
    if (same(i, p[i]))
      ct++;
  }
  printf("%lld\n", ct);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
vector<int> par, sz;
int find(int x) {
  while (x != par[x])
    x = par[x];
  return x;
}
void uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
}
bool same(int a, int b) { return find(a) == find(b); }

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  int n, m;
  scanf("%lld%lld", &n, &m);
  sz = vector<int>(n + 1, 1);
  par = vector<int>(n + 1);
  iota(par.begin(), par.end(), 0);

  vector<int> p(n + 1);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &p[i]);

  int u, v;
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld", &u, &v);
    uni(u, v);
  }

  int ct = 0;
  for (int i = 1; i <= n; i++) {
    if (same(i, p[i]))
      ct++;
  }
  printf("%lld\n", ct);
}
Problem p03354 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xy = [list(map(int, input().split())) for _ in range(m)]

par = [i for i in range(n)]
rank = [0 for _ in range(n)]


def root(x):
    if x == par[x]:
        return x
    par[x] = root(par[x])  # 経路圧縮
    return par[x]


def is_same(x, y):
    return root(x) == root(y)


def unite(x, y):
    x = root(x)
    y = root(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1
    par[x] = y


for x, y in xy:
    unite(x - 1, y - 1)


count = 0
for i, p_i in enumerate(p):
    count += 1 if is_same(i, p_i - 1) else 0
print(count)


Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xy = [list(map(int, input().split())) for _ in range(m)]

par = [i for i in range(n)]
rank = [0 for _ in range(n)]


def root(x):
    if x == par[x]:
        return x
    par[x] = root(par[x])  # 経路圧縮
    return par[x]


def is_same(x, y):
    return root(x) == root(y)


def unite(x, y):
    x = root(x)
    y = root(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1


for x, y in xy:
    unite(x - 1, y - 1)


count = 0
for i, p_i in enumerate(p):
    count += 1 if is_same(i, p_i - 1) else 0
print(count)
Problem p03354 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
p = [0] + list(map(int, input().split()))
x, y = zip(*(map(int, input().split()) for _ in range(M)))
E = [[] for _ in range(N + 1)]
for a, b in zip(x, y):
    E[a].append(b)
    E[b].append(a)
v = [True for _ in range(N + 1)]


def f(i, P, Q):
    v[i] = False
    P.add(i)
    Q.add(p[i])
    for a in E[i]:
        if v[a]:
            f(a, P, Q)


ans = 0
for i in range(1, N + 1):
    if v[i]:
        P = set()
        Q = set()
        f(i, P, Q)
        ans += len(P & Q)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(2 * 10**5)

N, M = map(int, input().split())
p = [0] + list(map(int, input().split()))
x, y = zip(*(map(int, input().split()) for _ in range(M)))
E = [[] for _ in range(N + 1)]
for a, b in zip(x, y):
    E[a].append(b)
    E[b].append(a)
v = [True for _ in range(N + 1)]


def f(i, P, Q):
    v[i] = False
    P.add(i)
    Q.add(p[i])
    for a in E[i]:
        if v[a]:
            f(a, P, Q)


ans = 0
for i in range(1, N + 1):
    if v[i]:
        P = set()
        Q = set()
        f(i, P, Q)
        ans += len(P & Q)
print(ans)
Problem p03354 (C++)

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

class UnionFind {
private:
  vector<int> par;

public:
  UnionFind(int n) : par(n) {
    for (int i = 0; i < n; i++) {
      par.at(i) = i;
    }
  }

  int root(int x) {
    if (par.at(x) == x) {
      return x;
    } else {
      return root(par.at(x));
    }
  }

  void unite(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx != ry) {
      par.at(rx) = ry;
    }
  }

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

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

  vector<int> vec(n);
  UnionFind tree(n);

  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    tree.unite(x - 1, y - 1);
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    if (tree.same(i, vec.at(i) - 1)) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

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

class UnionFind {
private:
  vector<int> par;

public:
  UnionFind(int n) : par(n) {
    for (int i = 0; i < n; i++) {
      par.at(i) = i;
    }
  }

  int root(int x) {
    if (par.at(x) == x) {
      return x;
    } else {
      return par.at(x) = root(par.at(x));
    }
  }

  void unite(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx != ry) {
      par.at(rx) = ry;
    }
  }

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

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

  vector<int> vec(n);
  UnionFind tree(n);

  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    tree.unite(x - 1, y - 1);
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    if (tree.same(i, vec.at(i) - 1)) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03354 (C++)

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

using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

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

  int p[N];
  for (int i = 0; i < N; ++i)
    cin >> p[i];

  UnionFind uf(N);

  int x, y;
  for (int i = 0; i < M; ++i) {
    cin >> x >> y;
    uf.unionSet(x - 1, y - 1);
  }

  int ans{0};
  for (int i = 0; i < N; ++i)
    if (uf.findSet(i, p[i]))
      ++ans;

  cout << ans << endl;
}

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

using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

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

  int p[N];
  for (int i = 0; i < N; ++i)
    cin >> p[i];

  UnionFind uf(N);

  int x, y;
  for (int i = 0; i < M; ++i) {
    cin >> x >> y;
    uf.unionSet(x - 1, y - 1);
  }

  int ans{0};
  for (int i = 0; i < N; ++i)
    if (uf.findSet(i, p[i] - 1))
      ++ans;

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

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

using namespace std;
vector<int> dsu;

int parent(int a) {
  if (dsu[a] == a)
    return a;
  return parent(dsu[a]);
}

void join(int a, int b) {
  int pa = parent(a);
  int pb = parent(b);
  dsu[pa] = pb;
}

int main() {
  int n, m, a, b;
  scanf("%d %d", &n, &m);
  vector<int> p(n);
  dsu = vector<int>(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }

  for (int i = 0; i < n; i++)
    dsu[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &a, &b);
    --a;
    --b;
    join(a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += parent(p[i]) == parent(i);
  }

  printf("%d\n", ans);
}

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

using namespace std;
vector<int> dsu;

int parent(int a) {
  if (dsu[a] == a)
    return a;
  return dsu[a] = parent(dsu[a]);
}

void join(int a, int b) {
  int pa = parent(a);
  int pb = parent(b);
  dsu[pa] = pb;
}

int main() {
  int n, m, a, b;
  scanf("%d %d", &n, &m);
  vector<int> p(n);
  dsu = vector<int>(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }

  for (int i = 0; i < n; i++)
    dsu[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &a, &b);
    --a;
    --b;
    join(a, b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += parent(p[i]) == parent(i);
  }

  printf("%d\n", ans);
}