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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

string s;
int n;
map<string, bool> mp;

int main() {
  cin >> s >> n;
  int m = s.size();

  rep(i, m) {
    string t = {};
    FOR(j, i, m) {
      t = t + s[j];
      mp[t] = true;
    }
  }

  int c = 0;

  for (auto itr = mp.begin(); itr != mp.end(); itr++, c++) {
    if (c == n - 1) {
      cout << itr->first << endl;
      break;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

string s;
int n;
map<string, bool> mp;

int main() {
  cin >> s >> n;
  int m = s.size();

  rep(i, m) {
    string t = {};
    for (int j = i; j < i + 10 && j < m; j++) {
      t = t + s[j];
      mp[t] = true;
    }
  }

  int c = 0;

  for (auto itr = mp.begin(); itr != mp.end(); itr++, c++) {
    if (c == n - 1) {
      cout << itr->first << endl;
      break;
    }
  }

  return 0;
}
Problem p03355 (C++)

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

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

  map<string, int> mp;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= s.size() - i; j++) {
      mp[s.substr(i, j)]++;
    }
  }

  int cnt = 1;
  for (auto x : mp) {
    if (cnt == k) {
      cout << x.first << endl;
      return 0;
    }
    cnt++;
  }
  return 0;
}


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

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

  map<string, int> mp;
  for (int i = 0; i < s.size(); i++) {
    int maxlen = min(k, (ll)(s.size() - i));
    for (int j = 1; j <= maxlen; j++) {
      mp[s.substr(i, j)]++;
    }
  }

  int cnt = 1;
  for (auto x : mp) {
    if (cnt == k) {
      cout << x.first << endl;
      return 0;
    }
    cnt++;
  }
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int main() {

  freopen("input.txt", "r", stdin);

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

  int n = (int)s.size();
  vector<string> suff;
  for (int i = 0; i < n; ++i)
    suff.push_back(s.substr(i, n - i));

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

  if ((int)suff[0].size() >= k) {
    cout << suff[0].substr(0, k);
    return 0;
  } else
    k -= (int)suff[0].size();

  for (int i = 1, ind; i < n; ++i) {
    ind = 0;
    while (ind < min((int)suff[i].size(), (int)suff[i - 1].size()) &&
           suff[i][ind] == suff[i - 1][ind])
      ++ind;
    if (ind == (int)suff[i].size())
      continue;
    if ((int)suff[i].size() - ind >= k) {
      cout << suff[i].substr(0, ind + k);
      return 0;
    } else
      k -= (int)suff[i].size() - ind;
  }
}


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

int main() {

  // freopen("input.txt", "r", stdin);

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

  int n = (int)s.size();
  vector<string> suff;
  for (int i = 0; i < n; ++i)
    suff.push_back(s.substr(i, n - i));

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

  if ((int)suff[0].size() >= k) {
    cout << suff[0].substr(0, k);
    return 0;
  } else
    k -= (int)suff[0].size();

  for (int i = 1, ind; i < n; ++i) {
    ind = 0;
    while (ind < min((int)suff[i].size(), (int)suff[i - 1].size()) &&
           suff[i][ind] == suff[i - 1][ind])
      ++ind;
    if (ind == (int)suff[i].size())
      continue;
    if ((int)suff[i].size() - ind >= k) {
      cout << suff[i].substr(0, ind + k);
      return 0;
    } else
      k -= (int)suff[i].size() - ind;
  }
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define MAX 510000
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  ll n = s.size();
  set<string> se;
  REP(i, 1, n + 1) {
    rep(j, n - i + 1) { se.insert(s.substr(j, i)); }
  }
  auto itr = se.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define MAX 510000
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  ll n = s.size();
  set<string> se;
  rep(i, n) {
    REP(j, 1, k + 1) {
      if (i + j <= n)
        se.insert(s.substr(i, j));
    }
  }
  auto itr = se.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
}
Problem p03355 (C++)

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

ll max(ll a, ll b) {
  if (a >= b)
    return a;
  else if (a < b)
    return b;
}
ll min(ll a, ll b) {
  if (a <= b)
    return a;
  else if (a > b)
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fact(ll n) {
  ll res = 1;
  rep(i, n) {
    res *= i + 1;
    res = res % ((ll)(1e9 + 7));
  }
  return res;
}
ll digit(const ll n) {
  ll res = 0;
  ll i = 1;
  while (n % i != n) {
    res += 1;
    i *= 10;
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> substrings;
  int length = 1;
  while (length <= s.size()) {
    for (int i = 0; i < s.size() - length + 1; i++) {
      string temp = {};
      for (int j = i; j < i + length; j++) {
        temp += s[j];
      }
      substrings.push_back(temp);
    }
    length += 1;
  }

  sort(substrings.begin(), substrings.end());
  substrings.push_back("-1");
  vector<string> ansstrings;
  for (int i = 0; i < substrings.size() - 1; i++) {
    if (substrings[i] != substrings[i + 1])
      ansstrings.push_back(substrings[i]);
  }
  /*
  rep(i, substrings.size())cout << substrings[i] << " ";
  cout << endl;
  rep(i, ansstrings.size())cout << ansstrings[i] << " ";
  cout << endl;
  */
  cout << ansstrings[k - 1] << endl;
}


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

ll max(ll a, ll b) {
  if (a >= b)
    return a;
  else if (a < b)
    return b;
}
ll min(ll a, ll b) {
  if (a <= b)
    return a;
  else if (a > b)
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fact(ll n) {
  ll res = 1;
  rep(i, n) {
    res *= i + 1;
    res = res % ((ll)(1e9 + 7));
  }
  return res;
}
ll digit(const ll n) {
  ll res = 0;
  ll i = 1;
  while (n % i != n) {
    res += 1;
    i *= 10;
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> substrings;
  int length = 1;
  while (length <= k) {
    for (int i = 0; i < s.size() - length + 1; i++) {
      string temp = {};
      for (int j = i; j < i + length; j++) {
        temp += s[j];
      }
      substrings.push_back(temp);
    }
    length += 1;
  }

  sort(substrings.begin(), substrings.end());
  substrings.push_back("-1");
  vector<string> ansstrings;
  for (int i = 0; i < substrings.size() - 1; i++) {
    if (substrings[i] != substrings[i + 1])
      ansstrings.push_back(substrings[i]);
  }
  /*
  rep(i, substrings.size())cout << substrings[i] << " ";
  cout << endl;
  rep(i, ansstrings.size())cout << ansstrings[i] << " ";
  cout << endl;
  */
  cout << ansstrings[k - 1] << endl;
}
Problem p03355 (C++)

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

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  string s;
  int K;
  cin >> s >> K;
  vector<string> subs;
  for (int i = 0; i < (int)s.size(); i++) {
    for (int j = 1; i + j <= (int)s.size(); j++)
      subs.push_back(s.substr(i, j));
  }
  sort(subs.begin(), subs.end());
  subs.erase(unique(subs.begin(), subs.end()), subs.end());
  //~ for (string ss: subs) cout << ss << " ";
  cout << subs[K - 1] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  string s;
  int K;
  cin >> s >> K;
  vector<string> subs;
  for (int i = 0; i < (int)s.size(); i++) {
    for (int j = 1; j <= min((int)s.size() - i, 5); j++)
      subs.push_back(s.substr(i, j));
  }
  sort(subs.begin(), subs.end());
  subs.erase(unique(subs.begin(), subs.end()), subs.end());
  //~ for (string ss: subs) cout << ss << " ";
  cout << subs[K - 1] << "\n";
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> ss;
  for (int i = 0; i < s.length(); i++) {
    string tmp = "";
    for (int j = i; j < s.length(); j++) {
      tmp += s[j];
      ss.insert(tmp);
    }
  }
  auto itr = ss.begin();
  for (int i = 0; i < k - 1; i++) {
    itr++;
  }
  cout << *itr << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> ss;
  for (int i = 0; i < s.length(); i++) {
    string tmp = "";
    for (int j = 0; j < k && i + j < s.length(); j++) {
      tmp += s[i + j];
      ss.insert(tmp);
    }
  }
  auto itr = ss.begin();
  for (int i = 0; i < k - 1; i++) {
    itr++;
  }
  cout << *itr << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

std::string s;
int K;
std::set<std::string> S;

int main() {
  std::cin >> s >> K;

  S.insert(s);

  int N = s.size();
  rep(i, N) rep(j, N - 1) S.insert(s.substr(i, j + 1));

  int cnt = 0;
  for (auto it = S.begin(); it != S.end(); ++it) {
    ++cnt;

    if (cnt == K) {
      std::cout << *it << std::endl;
      break;
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

std::string s;
int K;
std::set<std::string> S;

int main() {
  std::cin >> s >> K;

  S.insert(s);

  int N = s.size();
  rep(i, N) rep(j, 100) S.insert(s.substr(i, j + 1));

  int cnt = 0;
  for (auto it = S.begin(); it != S.end(); ++it) {
    ++cnt;

    if (cnt == K) {
      std::cout << *it << std::endl;
      break;
    }
  }

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void READ() {
#define R READ();
#define INFI 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define MEMX(x, num) memset(x, num, sizeof(x))
#define prln(x) cout << #x << "=" << x << endl
#define prt(x) cout << x << endl
#define prk(x) x << " "
#define sz(x) int(x.size())
#define PB(x) push_back(x)
#define LINK(x, y) G[x].PB(y), G[y].PB(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
#endif
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(6);
}
///////////////////////////
const int N = 5 + 1e5;
const int M = 7 + 3e2;

int t, n, k;
unordered_set<string> st;
string s;
vector<string> a;
int main() {

  cin >> s >> k;
  int sz = s.size();
  rep(i, 0, sz - 1) {
    rep(j, 1, sz - i) { st.insert(s.substr(i, j)); }
  }
  for (auto it = st.begin(); it != st.end(); it++) {
    a.push_back(*it);
  }
  nth_element(a.begin(), a.begin() + k - 1, a.end());
  cout << a[k - 1];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void READ() {
#define R READ();
#define INFI 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define MEMX(x, num) memset(x, num, sizeof(x))
#define prln(x) cout << #x << "=" << x << endl
#define prt(x) cout << x << endl
#define prk(x) x << " "
#define sz(x) int(x.size())
#define PB(x) push_back(x)
#define LINK(x, y) G[x].PB(y), G[y].PB(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
#endif
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(6);
}
///////////////////////////
const int N = 5 + 1e5;
const int M = 7 + 3e2;

int t, n, k;
unordered_set<string> st;
string s;
vector<string> a;
int main() {

  cin >> s >> k;
  int sz = s.size();
  rep(i, 0, sz - 1) {
    rep(j, 1, min(k, sz - i)) { st.insert(s.substr(i, j)); }
  }
  for (auto it = st.begin(); it != st.end(); it++) {
    a.push_back(*it);
  }
  nth_element(a.begin(), a.begin() + k - 1, a.end());
  cout << a[k - 1];

  return 0;
}
Problem p03355 (C++)

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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> d;
  rep(i, s.size()) {
    for (int j = i; j < s.size(); j++) {
      d.pb(s.substr(i, j - i + 1));
    }
  }
  sort(d.begin(), d.end());
  int p = 1;
  int now = 0;
  while (p < k) {
    if (d[now + 1] != d[now])
      p++;
    now++;
  }
  cout << d[now] << endl;
}

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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> d;
  rep(i, s.size()) {
    for (int j = i; j < min(i + 5, (int)s.size()); j++) {
      d.pb(s.substr(i, j - i + 1));
    }
  }
  sort(d.begin(), d.end());
  int p = 1;
  int now = 0;
  while (p < k) {
    if (d[now + 1] != d[now])
      p++;
    now++;
  }
  cout << d[now] << endl;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {

  string s;
  int K;
  set<string> substring;

  cin >> s;
  cin >> K;

  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < K; j++) {
      substring.insert(s.substr(i, j));
    }
  }

  auto itr = substring.begin();
  for (int i = 0; i < K; i++)
    itr++;
  cout << *itr << endl;

  return 0;
}

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

int main() {

  string s;
  int K;
  set<string> substring;

  cin >> s;
  cin >> K;

  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j <= K; j++) {
      substring.insert(s.substr(i, j));
    }
  }

  auto itr = substring.begin();
  for (int i = 0; i < K; i++)
    itr++;
  cout << *itr << endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> vt;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string a;
  cin >> a;
  int t = a.size();
  for (int i = 0; i < t; i++) {
    string c;
    for (int y = 0; y < t; y++) {
      if (i + y >= t)
        continue;
      c += a[i + y];
      vt.push_back(c);
    }
  }
  sort(vt.begin(), vt.end());
  vt.erase(unique(vt.begin(), vt.end()), vt.end());
  int m;
  cin >> m;
  cout << vt[m - 1];
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> vt;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string a;
  cin >> a;
  int t = a.size();
  for (int i = 0; i < t; i++) {
    string c;
    for (int y = 0; y < 5; y++) {
      if (i + y >= t)
        continue;
      c += a[i + y];
      vt.push_back(c);
    }
  }
  sort(vt.begin(), vt.end());
  vt.erase(unique(vt.begin(), vt.end()), vt.end());
  int m;
  cin >> m;
  cout << vt[m - 1];
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
priority_queue<string> vs;
set<string> S;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = s.length();
  FOR(i, 0, n) {
    string str = "";
    str += s[i];
    if (vs.size() == K) {
      if (str > vs.top())
        continue;
    }
    if (S.count(str) == 0) {
      vs.push(str);
      if (vs.size() == K + 1)
        vs.pop();
    }
    S.insert(str);
    FOR(j, i + 1, n) {
      str += s[j];
      if (S.count(str) == 0) {
        vs.push(str);
        if (vs.size() == K + 1)
          vs.pop();
      }
      S.insert(str);
    }
  }
  vector<string> ans;
  while (!vs.empty()) {
    ans.push_back(vs.top());
    vs.pop();
  }
  sort(ans.begin(), ans.end());
  cout << ans[K - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
priority_queue<string> vs;
set<string> S;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = s.length();
  FOR(i, 0, n) {
    string str = "";
    str += s[i];
    if (vs.size() == K) {
      if (str > vs.top())
        continue;
    }
    if (S.count(str) == 0) {
      vs.push(str);
      if (vs.size() == K + 1)
        vs.pop();
    }
    S.insert(str);
    FOR(j, i + 1, n) {
      str += s[j];
      if (vs.size() == K) {
        if (str > vs.top())
          continue;
      }
      if (S.count(str) == 0) {
        vs.push(str);
        if (vs.size() == K + 1)
          vs.pop();
      }
      S.insert(str);
    }
  }
  vector<string> ans;
  while (!vs.empty()) {
    ans.push_back(vs.top());
    vs.pop();
  }
  sort(ans.begin(), ans.end());
  cout << ans[K - 1] << endl;
}
Problem p03355 (C++)

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  map<string, int> m;
  for (int i = 0; i < s.size(); ++i) {
    string t = char_to_string(s[i]);
    m[t]++;
    for (int k = i + 1; k < s.size(); ++k) {
      t += s[k];
      m[t]++;
    }
  }
  vector<string> ans;
  for (auto x : m) {
    ans.push_back(x.first);
  }
  sort(all(ans));
  cout << ans[K - 1] << endl;
}


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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  map<string, int> m;
  for (int i = 0; i < s.size(); ++i) {
    string t = char_to_string(s[i]);
    m[t]++;
    for (int k = i + 1; k < s.size(); ++k) {
      t += s[k];
      m[t]++;
      if (t.size() == K)
        break;
    }
  }
  vector<string> ans;
  for (auto x : m) {
    ans.push_back(x.first);
  }
  sort(all(ans));
  cout << ans[K - 1] << endl;
}
Problem p03355 (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(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

string solve(string s, int k) {
  int n = s.size();
  vector<string> v;
  map<string, int> mp;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= n - i; j++) {
      string t = s.substr(i, j);
      if (mp[t])
        continue;
      mp[t]++;
      v.push_back(t);
    }
  }
  sort(ALL(v));
  return v[k - 1];
}

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  cout << solve(s, k) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

string solve(string s, int k) {
  int n = s.size();
  vector<string> v;
  map<string, int> mp;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 5; j++) {
      if (i + j - 1 > n)
        continue;
      string t = s.substr(i, j);
      if (mp[t])
        continue;
      mp[t]++;
      v.push_back(t);
    }
  }
  sort(ALL(v));
  return v[k - 1];
}

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  cout << solve(s, k) << endl;
}
Problem p03356 (C++)

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

using namespace std;

int n, m, x, y;
int Rank[100001];
int parent[100001];

int find(int x);
bool isSame(int x, int y);
void unite(int x, int y);

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

  vector<int> p(n, 0);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }

  for (int i = 1; i <= n; i++) {
    Rank[i] = 0;
    parent[i] = i;
  }

  for (int i = 0; i < m; i++) {
    cin >> x >> y;

    if (!isSame(x, y))
      unite(x, y);
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (isSame(i, p[i]))
      ans++;
  }

  cout << ans << '\n';
}

int find(int x) {
  if (x == parent[x])
    return x;
  else {
    return parent[x] = find(parent[x]);
  }
}

bool isSame(int x, int y) { return find(x) == find(y); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);

  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    parent[x] = y;
  } else {
    parent[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}


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

using namespace std;

int n, m, x, y;
int Rank[100001];
int parent[100001];

int find(int x);
bool isSame(int x, int y);
void unite(int x, int y);

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

  vector<int> p(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }

  for (int i = 1; i <= n; i++) {
    Rank[i] = 0;
    parent[i] = i;
  }

  for (int i = 0; i < m; i++) {
    cin >> x >> y;

    if (!isSame(x, y))
      unite(x, y);
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (isSame(i, p[i]))
      ans++;
  }

  cout << ans << '\n';
}

int find(int x) {
  if (x == parent[x])
    return x;
  else {
    return parent[x] = find(parent[x]);
  }
}

bool isSame(int x, int y) { return find(x) == find(y); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);

  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    parent[x] = y;
  } else {
    parent[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}
Problem p03356 (C++)

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

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    tree.unite(x - 1, y - 1);
  }
  int sum = 0;
  for (int i = 0; i < N; i++)
    if (tree.same(i + 1, p[i]))
      sum++;
  cout << sum;
}


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

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    tree.unite(x - 1, y - 1);
  }
  int sum = 0;
  for (int i = 0; i < N; i++)
    if (tree.same(i, p[i] - 1))
      sum++;
  cout << sum;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define REP(i, x, y) for (int i = (int)(x); i < (int)(y); i++)
#define RREP(i, x, y) for (int i = (int)(x); i > (int)(y); i--)
#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long ll;

using namespace std;

const int MAX_N = 100000;
int N, M;
int p[MAX_N], x[MAX_N], y[MAX_N];

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)]; }
};

void solve() {
  int answer = 0;
  UnionFind uf(N);
  REP(i, 0, M) { uf.unionSet(x[i], y[i]); }
  REP(i, 0, N) {
    if (uf.findSet(p[i], i + 1))
      answer++;
  }
  cout << answer << "\n";
  return;
}

int main() {
  // to shorten execution time for iostream
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  REP(i, 0, N) cin >> p[i];
  REP(i, 0, M) { cin >> x[i] >> y[i]; }
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define REP(i, x, y) for (int i = (int)(x); i < (int)(y); i++)
#define RREP(i, x, y) for (int i = (int)(x); i > (int)(y); i--)
#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long ll;

using namespace std;

const int MAX_N = 100000;
int N, M;
int p[MAX_N], x[MAX_N], y[MAX_N];

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)]; }
};

void solve() {
  int answer = 0;
  UnionFind uf(N + 1);
  REP(i, 0, M) { uf.unionSet(x[i], y[i]); }
  REP(i, 0, N) {
    if (uf.findSet(p[i], i + 1))
      answer++;
  }
  cout << answer << "\n";
  return;
}

int main() {
  // to shorten execution time for iostream
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  REP(i, 0, N) cin >> p[i];
  REP(i, 0, M) { cin >> x[i] >> y[i]; }
  solve();
  return 0;
}
Problem p03356 (C++)

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

const int N = 100005;

int n, m, p[N], par[N];
vector<int> vec[N];
bool c1[N], c2[N];

int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); }
void join(int p, int q) { par[anc(p)] = q; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> p[i], par[i] = i;

  while (m--) {
    int u, v;
    cin >> u >> v;
    join(u, v);
  }

  for (int i = 1; i <= n; ++i)
    vec[anc(i)].push_back(i);

  int res = 0;
  for (int i = 1; i <= n; ++i)
    if (i == anc(i)) {
      for (auto &j : vec[i])
        c1[j] = 1, c2[p[j]] = 1;
      for (auto &j : vec[i])
        if (c1[j] && c2[j]) {
          ++res;
        }

      for (auto &j : vec[i])
        c1[j] = 0, c2[p[j]] = 0;
    }

  cout << res << '\n';
}

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

const int N = 100005;

int n, m, p[N], par[N];
vector<int> vec[N];
bool c1[N], c2[N];

int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); }
void join(int p, int q) { par[anc(p)] = anc(q); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> p[i], par[i] = i;

  while (m--) {
    int u, v;
    cin >> u >> v;
    join(u, v);
  }

  for (int i = 1; i <= n; ++i)
    vec[anc(i)].push_back(i);

  int res = 0;
  for (int i = 1; i <= n; ++i)
    if (i == anc(i)) {
      for (auto &j : vec[i])
        c1[j] = 1, c2[p[j]] = 1;
      for (auto &j : vec[i])
        if (c1[j] && c2[j]) {
          ++res;
        }

      for (auto &j : vec[i])
        c1[j] = 0, c2[p[j]] = 0;
    }

  cout << res << '\n';
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define N 100010

int n, m, x, y, cnt, ans, p[N], mark1[N], mark2[N]; // 1:weizhi 2:shuzi

struct edge {
  int e;
  edge *next;
} *v[N << 1], ed[N];

void add_edge(int s, int e) {
  ed[++cnt].next = v[s];
  v[s] = ed + cnt;
  v[s]->e = e;
}

void dfs(int x, int fa, int k) {
  if (mark1[x])
    return;
  mark1[x] = k;
  mark2[p[x]] = k;
  for (edge *e = v[x]; e; e = e->next) {
    if (e->e == fa)
      continue;
    dfs(e->e, x, k);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  cnt = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add_edge(x, y);
    add_edge(y, x);
  }
  for (int i = 1; i <= n; i++)
    if (!mark1[i])
      dfs(i, 0, i);
  for (int i = 1; i <= n; i++)
    if (mark1[i] == mark2[i])
      ans++;
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define N 100010

int n, m, x, y, cnt, ans, p[N], mark1[N], mark2[N]; // 1:weizhi 2:shuzi

struct edge {
  int e;
  edge *next;
} *v[N], ed[N << 1];

void add_edge(int s, int e) {
  ed[++cnt].next = v[s];
  v[s] = ed + cnt;
  v[s]->e = e;
}

void dfs(int x, int fa, int k) {
  if (mark1[x])
    return;
  mark1[x] = k;
  mark2[p[x]] = k;
  for (edge *e = v[x]; e; e = e->next) {
    if (e->e == fa)
      continue;
    dfs(e->e, x, k);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  cnt = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add_edge(x, y);
    add_edge(y, x);
  }
  for (int i = 1; i <= n; i++)
    if (!mark1[i])
      dfs(i, 0, i);
  for (int i = 1; i <= n; i++)
    if (mark1[i] == mark2[i])
      ans++;
  printf("%d", ans);
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

struct UnionFind {
  vector<int> par;

  // 要素nで初期化
  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  // xが属する集合の根を返す
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  // xとyが同じ集合に属しているかを判定
  bool issame(int x, int y) { return root(x) == root(y); }

  // xが属する集合とyが属する集合をマージ
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // 計算量が減るよ
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  // xが属する集合のサイズ
  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  vector<P> a(m);
  ll x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    a[i] = mp(x, y);
  }

  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    uf.merge(a[i].fs, a[i].sc);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if ((p[i] == i + 1) || (uf.issame(p[i], i + 1))) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

struct UnionFind {
  vector<int> par;

  // 要素nで初期化
  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  // xが属する集合の根を返す
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  // xとyが同じ集合に属しているかを判定
  bool issame(int x, int y) { return root(x) == root(y); }

  // xが属する集合とyが属する集合をマージ
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // 計算量が減るよ
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  // xが属する集合のサイズ
  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  vector<P> a(m);
  ll x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    a[i] = mp(x, y);
  }

  UnionFind uf(n + 1);
  for (int i = 0; i < m; i++) {
    uf.merge(a[i].fs, a[i].sc);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if ((p[i] == i + 1) || (uf.issame(p[i], i + 1))) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int pa[100005];
int t[100005];
vector<int> v1;

int search(int a) {
  if (pa[a] == -1) {
    for (int i = 0; i < v1.size(); i++) {
      pa[v1[i]] = a;
    }
    v1.clear();
    return a;
  }
  v1.push_back(a);
  return search(pa[a]);
}

void connect(int a, int b) {
  a = search(a), b = search(b);
  pa[b] = a;
}

int main() {
  int n, m;
  int i, j, k;
  int a, b, c;
  int s = 0;
  memset(pa, -1, sizeof(pa));
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a;
    t[a - 1] = i;
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    connect(a, b);
  }
  for (i = 0; i < n; i++) {
    a = search(i), b = search(t[i]);
    if (a == b)
      s++;
  }
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int pa[100005];
int t[100005];
vector<int> v1;

int search(int a) {
  if (pa[a] == -1) {
    for (int i = 0; i < v1.size(); i++) {
      pa[v1[i]] = a;
    }
    v1.clear();
    return a;
  }
  v1.push_back(a);
  return search(pa[a]);
}

void connect(int a, int b) {
  a = search(a), b = search(b);
  if (a != b)
    pa[b] = a;
}

int main() {
  int n, m;
  int i, j, k;
  int a, b, c;
  int s = 0;
  memset(pa, -1, sizeof(pa));
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a;
    t[a - 1] = i;
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    connect(a, b);
  }
  for (i = 0; i < n; i++) {
    a = search(i), b = search(t[i]);
    if (a == b)
      s++;
  }
  cout << s << endl;
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n, m;
int p[100000];
int x[100000], y[100000];
vector<int> et[100000];
bool used[100000];

signed main() {
  int i, j;

  cin >> n >> m;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  rep(i, m) {
    et[x[i]].push_back(y[i]);
    et[y[i]].push_back(x[i]);
  }

  int ans = 0;
  queue<int> que;
  rep(i, n) {
    if (used[i])
      continue;

    vector<int> pos;
    used[i] = true;
    pos.push_back(i);
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (i = 0; i < et[v].size(); i++) {
        int nv = et[v][i];
        if (!used[nv]) {
          used[nv] = true;
          pos.push_back(nv);
          que.push(nv);
        }
      }
    }

    // posとp[pos]でマッチング
    vector<int> val;
    rep(j, pos.size()) val.push_back(p[pos[j]]);
    sort(val.begin(), val.end());

    rep(j, pos.size()) {
      int id = lower_bound(val.begin(), val.end(), pos[j]) - val.begin();
      if (id == val.size() || val[id] != pos[j])
        continue;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n, m;
int p[100000];
int x[100000], y[100000];
vector<int> et[100000];
bool used[100000];

signed main() {
  int i, j;

  cin >> n >> m;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  rep(i, m) {
    et[x[i]].push_back(y[i]);
    et[y[i]].push_back(x[i]);
  }

  int ans = 0;
  queue<int> que;
  rep(i, n) {
    if (used[i])
      continue;

    vector<int> pos;
    used[i] = true;
    pos.push_back(i);
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (j = 0; j < et[v].size(); j++) {
        int nv = et[v][j];
        if (!used[nv]) {
          used[nv] = true;
          pos.push_back(nv);
          que.push(nv);
        }
      }
    }

    // posとp[pos]でマッチング
    vector<int> val;
    rep(j, pos.size()) val.push_back(p[pos[j]]);
    sort(val.begin(), val.end());

    rep(j, pos.size()) {
      int id = lower_bound(val.begin(), val.end(), pos[j]) - val.begin();
      if (id == val.size() || val[id] != pos[j])
        continue;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03356 (C++)

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

struct UnionFind {
  vector<int> parent, size;

  UnionFind(int n) : parent(n, -1), size(n, 1) {}
  void init(int n) {
    parent.assign(n, -1);
    size.assign(n, 1);
    return;
  }

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

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

  void unite(int x, int y) {
    int gx = root(x);
    int gy = root(y);
    if (gx == gy)
      return;
    else {
      if (size[gx] < size[gy]) {
        size[gy] += size[gx];
        parent[gx] = gy;
      } else {
        size[gx] += size[gy];
        parent[gy] = gx;
      }
    }
  }
  int get_size(int x) { return size[root(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    p[i] -= 1;
  }
  vector<pair<int, int>> xy(N);
  for (int i = 0; i < M; i++) {
    cin >> xy[i].first >> xy[i].second;
    xy[i].first -= 1;
    xy[i].second -= 1;
  }

  UnionFind UF(N);
  int res = 0;
  for (int i = 0; i < M; i++) {
    UF.unite(xy[i].first, xy[i].second);
  }
  for (int i = 0; i < N; i++) {
    if (UF.same(i, p[i]))
      res++;
  }
  cout << res << endl;
  return 0;
}

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

struct UnionFind {
  vector<int> parent, size;

  UnionFind(int n) : parent(n, -1), size(n, 1) {}
  void init(int n) {
    parent.assign(n, -1);
    size.assign(n, 1);
    return;
  }

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

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

  void unite(int x, int y) {
    int gx = root(x);
    int gy = root(y);
    if (gx == gy)
      return;
    else {
      if (size[gx] < size[gy]) {
        size[gy] += size[gx];
        parent[gx] = gy;
      } else {
        size[gx] += size[gy];
        parent[gy] = gx;
      }
    }
  }
  int get_size(int x) { return size[root(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    p[i] -= 1;
  }
  vector<pair<int, int>> xy(M);
  for (int i = 0; i < M; i++) {
    cin >> xy[i].first >> xy[i].second;
    xy[i].first -= 1;
    xy[i].second -= 1;
  }

  UnionFind UF(N);
  int res = 0;
  for (int i = 0; i < M; i++) {
    UF.unite(xy[i].first, xy[i].second);
  }
  for (int i = 0; i < N; i++) {
    if (UF.same(i, p[i]))
      res++;
  }
  cout << res << endl;
  return 0;
}