problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02467
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; class Prime { private: vector<long long> divisor; public: Prime(long long n) { for (long long i = 0; i < n; ++i) divisor.emplace_back(i); for (long long i = 2; i <= n / i; ++i) if (divisor[i] == i) { for (long long j = i * i; j < n; j += i) divisor[j] = i; } } bool is_prime(long long n) { if (n <= 1) return false; if (n < (long long)divisor.size()) return divisor[n] == n; for (long long i = 2; i <= n / i; ++i) if (n % i == 0) return false; return true; } vector<long long> factor(long long n) { vector<long long> res; for (long long i = 2; i <= n / i && n >= (long long)divisor.size(); ++i) { while (n % i == 0) { res.emplace_back(i); n /= i; } } if (n >= (long long)divisor.size()) { res.emplace_back(n); } else { while (n > 1) { res.emplace_back(divisor[n]); n /= divisor[n]; } } sort(res.begin(), res.end()); return res; } vector<long long> get_divisors(long long n) { vector<long long> res; for (long long i = 2; i <= n / i && n >= (long long)divisor.size(); ++i) { if (n % i) continue; res.emplace_back(i); while (n % i == 0) n /= i; } if (n >= (long long)divisor.size()) { res.emplace_back(n); } else { while (n > 1) { long long d = divisor[n]; res.emplace_back(d); while (n % d == 0) n /= d; } } sort(res.begin(), res.end()); return res; } }; int main() { int n; cin >> n; Prime prime(10000000); cout << n << ":"; for (int f : prime.factor(n)) cout << " " << f; cout << endl; }
#include <bits/stdc++.h> using namespace std; class Prime { private: vector<long long> divisor; public: Prime(long long n) { for (long long i = 0; i < n; ++i) divisor.emplace_back(i); for (long long i = 2; i <= n / i; ++i) if (divisor[i] == i) { for (long long j = i * i; j < n; j += i) divisor[j] = i; } } bool is_prime(long long n) { if (n <= 1) return false; if (n < (long long)divisor.size()) return divisor[n] == n; for (long long i = 2; i <= n / i; ++i) if (n % i == 0) return false; return true; } vector<long long> factor(long long n) { vector<long long> res; for (long long i = 2; i <= n / i && n >= (long long)divisor.size(); ++i) { while (n % i == 0) { res.emplace_back(i); n /= i; } } if (n >= (long long)divisor.size()) { res.emplace_back(n); } else { while (n > 1) { res.emplace_back(divisor[n]); n /= divisor[n]; } } sort(res.begin(), res.end()); return res; } vector<long long> get_divisors(long long n) { vector<long long> res; for (long long i = 2; i <= n / i && n >= (long long)divisor.size(); ++i) { if (n % i) continue; res.emplace_back(i); while (n % i == 0) n /= i; } if (n >= (long long)divisor.size()) { res.emplace_back(n); } else { while (n > 1) { long long d = divisor[n]; res.emplace_back(d); while (n % d == 0) n /= d; } } sort(res.begin(), res.end()); return res; } }; int main() { int n; cin >> n; Prime prime(1000000); cout << n << ":"; for (int f : prime.factor(n)) cout << " " << f; cout << endl; }
replace
77
78
77
78
MLE
p02467
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <string> using namespace std; int isprime(int n) { if (n < 2) return 0; else for (int i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; } // dfs, void solve(int n) { int sum = 0; cout << n << ":"; while (n > 1) { for (int i = 2; i <= n; i++) if (isprime(i) && (n % i == 0)) { cout << " "; // 格式 cout << i; n /= i; sum++; break; } } cout << endl; } int main() { int n; cin >> n; solve(n); return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> using namespace std; int isprime(int n) { if (n < 2) return 0; else for (int i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; } // dfs, void solve(int n) { if (isprime(n)) { cout << n << ": " << n << endl; // 特殊情况排除 return; } int sum = 0; cout << n << ":"; while (n > 1) { for (int i = 2; i <= n; i++) if (isprime(i) && (n % i == 0)) { cout << " "; // 格式 cout << i; n /= i; sum++; break; } } cout << endl; } int main() { int n; cin >> n; solve(n); return 0; }
insert
16
16
16
20
TLE
p02467
C++
Time Limit Exceeded
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int n, i; cin >> n; cout << n << ":"; for (i = 2; n != 1; i++) { if (i > n / 2) { cout << " " << n; n = 1; continue; } while (n % i == 0) { cout << " " << i; n /= i; } } cout << endl; return 0; }
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int n, i; cin >> n; cout << n << ":"; for (i = 2; n != 1; i++) { if (i > sqrt(n)) { cout << " " << n; n = 1; continue; } while (n % i == 0) { cout << " " << i; n /= i; } } cout << endl; return 0; }
replace
13
14
13
14
TLE
p02467
C++
Time Limit Exceeded
#include <cmath> #include <iostream> using namespace std; // 素数判定(素数ならtrue) bool Prime(int prime) { // 偶数除く if (prime == 2) { return true; } else if (prime % 2 == 0 || prime < 2) { return false; } float Asqrt = sqrtf(prime); for (int i = 3; i <= Asqrt; i += 2) { if (prime % i == 0) { return false; } } return true; } int main() { int n; cin >> n; cout << n << ":"; for (int prime = 2;; prime++) { if (Prime(n)) { cout << " " << n << endl; break; } while (true) { // 素数 かつ 素因数の場合 if (Prime(prime) && n % prime == 0) { n /= prime; cout << " " << prime; // 分解可能なら続ける continue; } // 分解不可でbreak; break; } } return 0; }
#include <cmath> #include <iostream> using namespace std; // 素数判定(素数ならtrue) bool Prime(int prime) { // 偶数除く if (prime == 2) { return true; } else if (prime % 2 == 0 || prime < 2) { return false; } float Asqrt = sqrtf(prime); for (int i = 3; i <= Asqrt; i += 2) { if (prime % i == 0) { return false; } } return true; } int main() { int n; cin >> n; cout << n << ":"; for (int prime = 2;; prime++) { if (Prime(n)) { cout << " " << n << endl; break; } while (true) { // 素数 かつ 素因数の場合 if (Prime(prime) && n % prime == 0) { n /= prime; cout << " " << prime; if (n == 1) { cout << endl; exit(0); } // 分解可能なら続ける continue; } // 分解不可でbreak; break; } } return 0; }
insert
44
44
44
49
TLE
p02467
C++
Time Limit Exceeded
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int n, i; cin >> n; cout << n << ":"; for (i = 2; n != 1; i++) { while (n % i == 0) { cout << " " << i; n /= i; } } cout << endl; return 0; }
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int n, i; cin >> n; cout << n << ":"; for (i = 2; n != 1; i++) { if (i > sqrt(n)) { cout << " " << n; n = 1; continue; } while (n % i == 0) { cout << " " << i; n /= i; } } cout << endl; return 0; }
insert
13
13
13
18
TLE
p02468
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int #define MOD(x) ((x % MOD_N) + MOD_N) % MOD_N #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORE(i, a, b) for (int i = (a); i <= (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define RFORE(i, a, b) for (int i = (b); i >= (a); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SZ(c) (int)((c).size()) #define EACH(i, v) for (auto i = v.begin(); i != v.end(); ++i) #define REACH(i, v) for (auto i = v.rbegin(); i != v.rend(); ++i) #define LB(c, x) distance((c).begin(), lower_bound(ALL(c), x)) #define UB(c, x) distance((c).begin(), upper_bound(ALL(c), x)) #define COUNT(c, x) (upper_bound(ALL(c), x) - lower_bound(ALL(c), x)) #define UNIQUE(c) \ SORT(c); \ (c).erase(unique(ALL(c)), (c).end()); #define COPY(c1, c2) copy(ALL(c1), (c2).begin()) #define EXIST(s, e) (bool)((s).find(e) != (s).end()) #define PB push_back #define MP make_pair #define DUMP(x) cerr << #x << " = " << (x) << endl; #define NL cerr << endl; using namespace std; template <typename T, typename U> using P = pair<T, U>; template <typename T> using V = vector<T>; template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> T sum(vector<T> &v) { return accumulate(ALL(v), T()); } template <typename T> T sum(vector<T> &v, int a, int b) { return accumulate(v.begin() + a, v.begin() + b, T()); } template <typename T> T max(vector<T> &v) { return *max_element(ALL(v)); } template <typename T> T min(vector<T> &v) { return *min_element(ALL(v)); } template <typename T> T max_index(vector<T> &v) { return distance((v).begin(), max_element(ALL(v))); } template <typename T> T min_index(vector<T> &v) { return distance((v).begin(), min_element(ALL(v))); } struct edge { int to, cost; }; template <typename T> auto &operator<<(ostream &s, const vector<T> &v) { s << "["; bool a = 1; for (auto e : v) { s << (a ? "" : " ") << e; a = 0; } s << "]"; return s; } template <typename T, typename U> auto &operator<<(ostream &s, const pair<T, U> &p) { s << "(" << p.first << "," << p.second << ")"; return s; } template <typename T> auto &operator<<(ostream &s, const set<T> &st) { s << "{"; bool a = 1; for (auto e : st) { s << (a ? "" : " ") << e; a = 0; } s << "}"; return s; } template <typename T, typename U> auto &operator<<(ostream &s, const map<T, U> &m) { s << "{"; bool a = 1; for (auto e : m) { s << (a ? "" : " ") << e.first << ":" << e.second; a = 0; } s << "}"; return s; } const int INF = 1e18; const int MOD_N = 1e9 + 7; int powM(int x, int n, int mod_n = MOD_N) { int res = 1; while (n > 0) { if ((n & 1) == 1) (res *= x) %= mod_n; (x *= x) %= mod_n; n >>= 1; } return res; } signed main() { int n, m; cin >> n >> m; DUMP(MP(n, m)) cout << powM(n, m) << endl; return 0; }
#include <bits/stdc++.h> #define int long long int #define MOD(x) ((x % MOD_N) + MOD_N) % MOD_N #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORE(i, a, b) for (int i = (a); i <= (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define RFORE(i, a, b) for (int i = (b); i >= (a); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SZ(c) (int)((c).size()) #define EACH(i, v) for (auto i = v.begin(); i != v.end(); ++i) #define REACH(i, v) for (auto i = v.rbegin(); i != v.rend(); ++i) #define LB(c, x) distance((c).begin(), lower_bound(ALL(c), x)) #define UB(c, x) distance((c).begin(), upper_bound(ALL(c), x)) #define COUNT(c, x) (upper_bound(ALL(c), x) - lower_bound(ALL(c), x)) #define UNIQUE(c) \ SORT(c); \ (c).erase(unique(ALL(c)), (c).end()); #define COPY(c1, c2) copy(ALL(c1), (c2).begin()) #define EXIST(s, e) (bool)((s).find(e) != (s).end()) #define PB push_back #define MP make_pair #define DUMP(x) cerr << #x << " = " << (x) << endl; #define NL cerr << endl; using namespace std; template <typename T, typename U> using P = pair<T, U>; template <typename T> using V = vector<T>; template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> T sum(vector<T> &v) { return accumulate(ALL(v), T()); } template <typename T> T sum(vector<T> &v, int a, int b) { return accumulate(v.begin() + a, v.begin() + b, T()); } template <typename T> T max(vector<T> &v) { return *max_element(ALL(v)); } template <typename T> T min(vector<T> &v) { return *min_element(ALL(v)); } template <typename T> T max_index(vector<T> &v) { return distance((v).begin(), max_element(ALL(v))); } template <typename T> T min_index(vector<T> &v) { return distance((v).begin(), min_element(ALL(v))); } struct edge { int to, cost; }; template <typename T> auto &operator<<(ostream &s, const vector<T> &v) { s << "["; bool a = 1; for (auto e : v) { s << (a ? "" : " ") << e; a = 0; } s << "]"; return s; } template <typename T, typename U> auto &operator<<(ostream &s, const pair<T, U> &p) { s << "(" << p.first << "," << p.second << ")"; return s; } template <typename T> auto &operator<<(ostream &s, const set<T> &st) { s << "{"; bool a = 1; for (auto e : st) { s << (a ? "" : " ") << e; a = 0; } s << "}"; return s; } template <typename T, typename U> auto &operator<<(ostream &s, const map<T, U> &m) { s << "{"; bool a = 1; for (auto e : m) { s << (a ? "" : " ") << e.first << ":" << e.second; a = 0; } s << "}"; return s; } const int INF = 1e18; const int MOD_N = 1e9 + 7; int powM(int x, int n, int mod_n = MOD_N) { int res = 1; while (n > 0) { if ((n & 1) == 1) (res *= x) %= mod_n; (x *= x) %= mod_n; n >>= 1; } return res; } signed main() { int n, m; cin >> n >> m; cout << powM(n, m) << endl; return 0; }
delete
116
117
116
116
0
MP(n,m) = (2,3)
p02468
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; bool judge(int s) { for (int i = 2; i * i <= s; i++) { if (s % i != 0 && (i + 1) * (i + 1) > s) { return true; } if (s % i == 0) { return false; break; } } } int main() { unsigned long long a; int m, n, b; cin >> m >> n; a = m; b = 1000000007; int l = n; int k[1000] = {}; if (judge(l)) { k[0] = l; } else { int count = 0; for (int j = 2;;) { if (l == 1) { break; } if (l % j == 0 && judge(j)) { l = l / j; k[count] = j; count++; continue; } j++; } } for (int j = 0; k[j] != 0; j++) { for (int i = 1; i < k[j]; i++) { a = a * m; if (a >= b) { a = a % b; if (a == m && k[j] - i > k[j] % i) { k[j] = k[j] % i; i = 0; } if (a == 1) { break; } } } m = a; } cout << a << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool judge(int s) { for (int i = 2; i * i <= s; i++) { if (s % i != 0 && (i + 1) * (i + 1) > s) { return true; } if (s % i == 0) { return false; break; } } } int main() { unsigned long long a; int m, n, b; cin >> m >> n; a = m; b = 1000000007; int l = n; int k[1000] = {}; if (judge(l)) { k[0] = l; } else { int count = 0; for (int j = 2;;) { if (l == 1) { break; } if (l % j == 0) { l = l / j; k[count] = j; count++; continue; } j++; } } for (int j = 0; k[j] != 0; j++) { for (int i = 1; i < k[j]; i++) { a = a * m; if (a >= b) { a = a % b; if (a == m && k[j] - i > k[j] % i) { k[j] = k[j] % i; i = 0; } if (a == 1) { break; } } } m = a; } cout << a << endl; return 0; }
replace
35
36
35
36
TLE
p02468
C++
Time Limit Exceeded
#include <iostream> using namespace std; typedef long long ll; /* AOJ power http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_B&lang=jp 繰り返し2乗法 */ ll power(int m, int n) { ll ans = 1; while (n > 0) { if (n == 1) return (ans * m) % ll(1e9 + 7); ans *= m * m; ans %= ll(1e9 + 7); n -= 2; } return ans; } int main() { int n, m; cin >> m >> n; cout << power(m, n) << "\n"; }
#include <iostream> using namespace std; typedef long long ll; /* AOJ power http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_B&lang=jp 繰り返し2乗法 */ ll power(int m, int n) { if (n == 0) return 1; if (n & 1) return m * power(m, n - 1) % ll(1e9 + 7); ll k = power(m, n / 2); return k * k % ll(1e9 + 7); } int main() { int n, m; cin >> m >> n; cout << power(m, n) << "\n"; }
replace
11
20
11
17
TLE
p02468
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; class Inverse { private: long long mod; vector<long long> inv; public: Inverse() {} Inverse(long long mod, long long n = 10000000) : mod(mod) { inv = vector<long long>(n, 1); for (int i = 2; i < n; ++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod; } long long operator()(long long a) const { if (a < (int)inv.size()) return inv[a]; long long b = mod, x = 1, y = 0; while (b) { long long t = a / b; swap(a -= t * b, b); swap(x -= t * y, y); } return (x %= mod) < 0 ? x + mod : x; } }; class Mint { private: static long long mod; static Inverse inverse; long long val; public: Mint() {} Mint(const long long &val) { this->val = val % mod; if (this->val < 0) this->val += mod; } static void setMod(const long long &m) { mod = m; inverse = Inverse(m); } Mint operator+=(const Mint &m) { val += m.val; if (val >= mod) val -= mod; return *this; } Mint operator-=(const Mint &m) { val -= m.val; if (val < 0) val += mod; return *this; } Mint operator*=(const Mint &m) { val *= m.val; val %= mod; return *this; } Mint operator/=(const Mint &m) { val *= inverse(m.val); val %= mod; return *this; } Mint operator+(const Mint &m) const { Mint res = *this; res += m; return res; } Mint operator-(const Mint &m) const { Mint res = *this; res -= m; return res; } Mint operator*(const Mint &m) const { Mint res = *this; res *= m; return res; } Mint operator/(const Mint &m) const { Mint res = *this; res /= m; return res; } Mint operator++() { return val += 1; } operator long long() { return val; } }; Mint pow(const Mint &m, long long n) { if (n == 0) { return 1; } else if (n < 0) { return pow(m, -n); } Mint mm = pow(m, n / 2); mm *= mm; if (n % 2) mm *= m; return mm; } long long Mint::mod; Inverse Mint::inverse; ostream &operator<<(ostream &os, Mint a) { os << (long long)a; return os; } istream &operator>>(istream &is, Mint &a) { long long n; is >> n; a = Mint(n); return is; } int main() { Mint::setMod(1000000007); Mint m; int n; cin >> m >> n; cout << pow(m, n) << endl; }
#include <bits/stdc++.h> using namespace std; class Inverse { private: long long mod; vector<long long> inv; public: Inverse() {} Inverse(long long mod, long long n = 1000000) : mod(mod) { inv = vector<long long>(n, 1); for (int i = 2; i < n; ++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod; } long long operator()(long long a) const { if (a < (int)inv.size()) return inv[a]; long long b = mod, x = 1, y = 0; while (b) { long long t = a / b; swap(a -= t * b, b); swap(x -= t * y, y); } return (x %= mod) < 0 ? x + mod : x; } }; class Mint { private: static long long mod; static Inverse inverse; long long val; public: Mint() {} Mint(const long long &val) { this->val = val % mod; if (this->val < 0) this->val += mod; } static void setMod(const long long &m) { mod = m; inverse = Inverse(m); } Mint operator+=(const Mint &m) { val += m.val; if (val >= mod) val -= mod; return *this; } Mint operator-=(const Mint &m) { val -= m.val; if (val < 0) val += mod; return *this; } Mint operator*=(const Mint &m) { val *= m.val; val %= mod; return *this; } Mint operator/=(const Mint &m) { val *= inverse(m.val); val %= mod; return *this; } Mint operator+(const Mint &m) const { Mint res = *this; res += m; return res; } Mint operator-(const Mint &m) const { Mint res = *this; res -= m; return res; } Mint operator*(const Mint &m) const { Mint res = *this; res *= m; return res; } Mint operator/(const Mint &m) const { Mint res = *this; res /= m; return res; } Mint operator++() { return val += 1; } operator long long() { return val; } }; Mint pow(const Mint &m, long long n) { if (n == 0) { return 1; } else if (n < 0) { return pow(m, -n); } Mint mm = pow(m, n / 2); mm *= mm; if (n % 2) mm *= m; return mm; } long long Mint::mod; Inverse Mint::inverse; ostream &operator<<(ostream &os, Mint a) { os << (long long)a; return os; } istream &operator>>(istream &is, Mint &a) { long long n; is >> n; a = Mint(n); return is; } int main() { Mint::setMod(1000000007); Mint m; int n; cin >> m >> n; cout << pow(m, n) << endl; }
replace
12
13
12
13
MLE
p02468
C++
Runtime Error
#include <iostream> using namespace std; #define MOD 1000000007LL long long power(int a, int b) { if (b == 0) { return 1LL; } return power(a, b - 1) * a % MOD; } int main() { int n, m; cin >> m >> n; cout << power(m, n) << endl; return 0; }
#include <iostream> using namespace std; #define MOD 1000000007LL long long power(int a, int b) { if (b == 0) { return 1LL; } if (b % 2 == 1) { return power(a, b / 2) * power(a, b / 2) % MOD * a % MOD; } return power(a, b / 2) * power(a, b / 2) % MOD; } int main() { int n, m; cin >> m >> n; cout << power(m, n) << endl; return 0; }
replace
11
12
11
16
0
p02468
C++
Time Limit Exceeded
#include <math.h> #include <stdio.h> int main(void) { long long n, i, m, mt, mt2; scanf("%lld%lld", &m, &n); mt = m, mt2 = m; m = 1; long long a = log2(n), b = n - (int)pow(2, a); while (a) { for (i = 0; i < a; ++i) { mt *= mt; mt %= 1000000007; } a = log2(b); m *= mt; m %= 1000000007; if (a) b = b - (int)pow(2, a); else if (b) { m *= mt2; m %= 1000000007; } mt = mt2; } printf("%lld\n", m); return 0; }
#include <math.h> #include <stdio.h> int main(void) { long long n, i, m, mt, mt2; scanf("%lld%lld", &m, &n); mt = m, mt2 = m; m = 1; long long a = log2(n), b = n - (int)pow(2, a); while (a) { for (i = 0; i < a; ++i) { mt *= mt; mt %= 1000000007; } a = log2(!b ? 1 : b); m *= mt; m %= 1000000007; if (a) b = b - (int)pow(2, a); else if (b) { m *= mt2; m %= 1000000007; } mt = mt2; } printf("%lld\n", m); return 0; }
replace
14
15
14
15
TLE
p02468
C++
Time Limit Exceeded
#include <iostream> int main() { int64_t m, n, q = 1000000007; std::cin >> m >> n; int64_t r = m % q; for (int64_t i = 1; i < n; i++) { r = (r * m) % q; } std::cout << r << "\n"; return 0; }
#include <iostream> int main() { int64_t m, n, q = 1000000007; std::cin >> m >> n; int64_t r = 1; int64_t m2 = m * m; int64_t m4 = m * m * m * m; int64_t i = 0; for (int64_t j = 0; j < n / 4; j++) { r = (r * m4) % q; i += 4; } for (; i < n; i++) { r = (r * m) % q; } std::cout << r << "\n"; return 0; }
replace
6
8
6
15
TLE
p02469
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define int long long #define MOD 1000000007 int gcd(int a, int b) { if (a % b == 0) return b; return gcd(a, b % a); } int lcm(int a, int b) { return a / gcd(a, b) * b; } signed main() { int n; int a[11]; cin >> n; rep(i, n) cin >> a[i]; int ans = a[0]; rep(i, n) ans = lcm(ans, a[i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define int long long #define MOD 1000000007 int gcd(int a, int b) { if (a % b == 0) return b; return gcd(b, a % b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } signed main() { int n; int a[11]; cin >> n; rep(i, n) cin >> a[i]; int ans = a[0]; rep(i, n) ans = lcm(ans, a[i]); cout << ans << endl; return 0; }
replace
8
9
8
9
TLE
p02469
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; inline int gcd(int x, int y) { if (!y) return x; return gcd(y, x % y); } inline int lcm(int x, int y) { return (x * y) / gcd(y, x % y); } int main() { int n, x; vector<int> v; cin >> n; for (int i = 1; i <= n; ++i) cin >> x, v.push_back(x); for (int i = 0; i < n; ++i) { v[i + 1] = lcm(v[i], v[i + 1]); } cout << v[n - 1] << endl; return 0; }
#include <iostream> #include <vector> using namespace std; inline int gcd(int x, int y) { if (!y) return x; return gcd(y, x % y); } inline int lcm(int x, int y) { return (x * y) / gcd(y, x % y); } int main() { int n, x; vector<int> v; cin >> n; for (int i = 1; i <= n; ++i) cin >> x, v.push_back(x); for (int i = 0; i < n - 1; ++i) { v[i + 1] = lcm(v[i], v[i + 1]); } cout << v[n - 1] << endl; return 0; }
replace
15
16
15
16
-8
p02469
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; int gcd(int m, int n) { if (n == 0) return m; return gcd(n, m % n); } int lcm(int m, int n) { return m * n / gcd(m, n); } int main() { int n, A[n - 1]; cin >> n; for (int i = 0; i <= n - 1; i++) cin >> A[i]; int ans = A[0]; for (int i = 1; i <= n - 1; i++) ans = lcm(ans, A[i]); cout << ans << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; int gcd(int m, int n) { if (n == 0) return m; return gcd(n, m % n); } int lcm(int m, int n) { return m * n / gcd(m, n); } int main() { int n, A[10]; cin >> n; for (int i = 0; i <= n - 1; i++) cin >> A[i]; int ans = A[0]; for (int i = 1; i <= n - 1; i++) ans = lcm(ans, A[i]); cout << ans << endl; return 0; }
replace
13
14
13
14
-11
p02469
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; // < "D:\D_Download\Visual Studio // 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual // Studio 2015\Projects\programing_contest_c++\Debug\b.txt" #include <algorithm> #include <map> #include <string> #include <vector> using namespace std; long long int powint(long long int a, int b) { if (b == 0) return 1; if (b == 1) return a; else { long long int ans = 1; long long int c = powint(a, b / 2); ans *= c * c; ans *= (b % 2) ? a : 1; return ans; } } long long int gcd(long long int l, long long int r) { if (l > r) return gcd(r, l); else { if (r % l) { return gcd(l, r % l); } else { return l; } } } map<long long int, int> soinnsuu(long long int a) { map<long long int, int> ans; for (long long i = 2; i * i <= a; ++i) { while (a % i == 0) { ans[i]++; a /= i; } } if (a != 1) ans[a]++; return ans; } vector<int> primes; void hurui(const int amax) { static bool flag = false; if (flag) return; vector<int> sos; sos = vector<int>(amax + 1, true); sos[0] = false; sos[1] = false; for (int i = 2; i <= amax; ++i) { if (sos[i]) { for (int j = 2 * i; j <= amax; j += i) sos[j] = false; } } for (int i = 0; i <= amax; ++i) { if (sos[i]) { primes.push_back(i); } } flag = true; } bool isso(long long int a) { if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } vector<long long int> divisor(map<long long int, int> s) { vector<long long int> res; int num = 1; for (auto it = s.begin(); it != s.end(); ++it) { num *= (*it).second + 1; } for (int i = 0; i < num; ++i) { int bs = i; long long int p = 1; for (auto it = s.begin(); it != s.end(); ++it) { int a = bs % ((*it).second + 1); p *= powint((*it).first, a); bs /= ((*it).second + 1); } res.push_back(p); } return res; } long long int powmod(long long int a, long long int b, const int mod) { assert(b >= 0); if (b == 0) return 1; if (b == 1) return a; long long int ans = 1; long long int aa = powmod(a, b / 2, mod); ans *= aa * aa; ans %= mod; if (b % 2) ans *= a; ans %= mod; return ans; } int main() { int N; cin >> N; long long int a = 0; while (N--) { long long int b; cin >> b; a = a * b / (gcd(a, b)); } cout << a << endl; return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; // < "D:\D_Download\Visual Studio // 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual // Studio 2015\Projects\programing_contest_c++\Debug\b.txt" #include <algorithm> #include <map> #include <string> #include <vector> using namespace std; long long int powint(long long int a, int b) { if (b == 0) return 1; if (b == 1) return a; else { long long int ans = 1; long long int c = powint(a, b / 2); ans *= c * c; ans *= (b % 2) ? a : 1; return ans; } } long long int gcd(long long int l, long long int r) { if (l > r) return gcd(r, l); else { if (r % l) { return gcd(l, r % l); } else { return l; } } } map<long long int, int> soinnsuu(long long int a) { map<long long int, int> ans; for (long long i = 2; i * i <= a; ++i) { while (a % i == 0) { ans[i]++; a /= i; } } if (a != 1) ans[a]++; return ans; } vector<int> primes; void hurui(const int amax) { static bool flag = false; if (flag) return; vector<int> sos; sos = vector<int>(amax + 1, true); sos[0] = false; sos[1] = false; for (int i = 2; i <= amax; ++i) { if (sos[i]) { for (int j = 2 * i; j <= amax; j += i) sos[j] = false; } } for (int i = 0; i <= amax; ++i) { if (sos[i]) { primes.push_back(i); } } flag = true; } bool isso(long long int a) { if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } vector<long long int> divisor(map<long long int, int> s) { vector<long long int> res; int num = 1; for (auto it = s.begin(); it != s.end(); ++it) { num *= (*it).second + 1; } for (int i = 0; i < num; ++i) { int bs = i; long long int p = 1; for (auto it = s.begin(); it != s.end(); ++it) { int a = bs % ((*it).second + 1); p *= powint((*it).first, a); bs /= ((*it).second + 1); } res.push_back(p); } return res; } long long int powmod(long long int a, long long int b, const int mod) { assert(b >= 0); if (b == 0) return 1; if (b == 1) return a; long long int ans = 1; long long int aa = powmod(a, b / 2, mod); ans *= aa * aa; ans %= mod; if (b % 2) ans *= a; ans %= mod; return ans; } int main() { int N; cin >> N; long long int a = 1; while (N--) { long long int b; cin >> b; a = a * b / (gcd(a, b)); } cout << a << endl; return 0; }
replace
129
130
129
130
-8
p02471
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define mp make_pair typedef long long ll; typedef pair<int, int> pii; int ExtendedGCD(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } else { int g = ExtendedGCD(b, a % b, x, y); tie(x, y) = make_tuple(y, x - a / b * y); return g; } } // int ExtendedGCD(int a,int b,int& x,int& y) //{ // x=1,y=0; // for(int u=0,v=1;b;){ // ll t=a/b; // swap(a-=b*t,b); // swap(x-=u*t,u); // swap(y-=v*t,v); // } // return a; // } int main() { for (int a, b; cin >> a >> b && a | b;) { int x, y; for (int i = 0; i < 10000000; i++) ExtendedGCD(a, b, x, y); cout << x << ' ' << y << endl; ; } }
#include <bits/stdc++.h> using namespace std; #define mp make_pair typedef long long ll; typedef pair<int, int> pii; int ExtendedGCD(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } else { int g = ExtendedGCD(b, a % b, x, y); tie(x, y) = make_tuple(y, x - a / b * y); return g; } } // int ExtendedGCD(int a,int b,int& x,int& y) //{ // x=1,y=0; // for(int u=0,v=1;b;){ // ll t=a/b; // swap(a-=b*t,b); // swap(x-=u*t,u); // swap(y-=v*t,v); // } // return a; // } int main() { for (int a, b; cin >> a >> b && a | b;) { int x, y; for (int i = 0; i < 1000000; i++) ExtendedGCD(a, b, x, y); cout << x << ' ' << y << endl; ; } }
replace
34
35
34
35
TLE
p02471
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; long long d = 0; bool flag = 0; long long gcd(long long a, long long b) { if (a % b == 0) return b; return gcd(b, a % b); } long long judge(long long x, long long y) { return abs(x) + abs(y); } pair<long long, long long> calc(long long a, long long b, long long x, long long y) { if (a * x + b * y == d) return pair<long long, long long>(x, y); if (a * x + b * y == -d) return pair<long long, long long>(-x, -y); flag ^= 1; long long n = a * x + b * y, tmp = 0; if (b % n == 0) return calc(b, a, y, x); tmp = b / n; x *= tmp; y = y * tmp - 1; long long k = (x + y) / (b - a); x -= b * k; y += a * k; return calc(b, a, y, x); } int main() { long long a = 0, b = 0; cin >> a >> b; d = gcd(a, b); pair<long long, long long> ans = calc(a, b, 1, 0); if (flag) { long long tmp = ans.first; ans.first = ans.second; ans.second = tmp; } long long k = (ans.first + ans.second) / (b - a); ans.first -= b * k; ans.second += a * k; while (judge(ans.first - b / d, ans.second + a / d) < judge(ans.first, ans.second)) { ans.first -= b / d; ans.second += a / d; } while (judge(ans.first + b / d, ans.second - a / d) < judge(ans.first, ans.second)) { ans.first += b / d; ans.second -= a / d; } cout << ans.first << " " << ans.second << endl; return 0; }
#include <cmath> #include <iostream> using namespace std; long long d = 0; bool flag = 0; long long gcd(long long a, long long b) { if (a % b == 0) return b; return gcd(b, a % b); } long long judge(long long x, long long y) { return abs(x) + abs(y); } pair<long long, long long> calc(long long a, long long b, long long x, long long y) { if (a * x + b * y == d) return pair<long long, long long>(x, y); if (a * x + b * y == -d) return pair<long long, long long>(-x, -y); flag ^= 1; long long n = a * x + b * y, tmp = 0; if (b % n == 0) return calc(b, a, y, x); tmp = b / n; x *= tmp; y = y * tmp - 1; long long k = (x + y) / (b - a); x -= b * k; y += a * k; return calc(b, a, y, x); } int main() { long long a = 0, b = 0; cin >> a >> b; if (a == b) { cout << "0 1" << endl; return 0; } d = gcd(a, b); pair<long long, long long> ans = calc(a, b, 1, 0); if (flag) { long long tmp = ans.first; ans.first = ans.second; ans.second = tmp; } long long k = (ans.first + ans.second) / (b - a); ans.first -= b * k; ans.second += a * k; while (judge(ans.first - b / d, ans.second + a / d) < judge(ans.first, ans.second)) { ans.first -= b / d; ans.second += a / d; } while (judge(ans.first + b / d, ans.second - a / d) < judge(ans.first, ans.second)) { ans.first += b / d; ans.second -= a / d; } cout << ans.first << " " << ans.second << endl; return 0; }
insert
38
38
38
42
0
p02471
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long using namespace std; ll s1, s2, s, t1, t2, t; void xgcd(ll x, ll y) { if (x % y == 0) return; ll q = x / y; s = s2 - q * s1; t = t2 - q * t1; s2 = s1, t2 = t1, s1 = s, t1 = t; xgcd(y, x % y); } int main() { ll a, b; while (1) { cin >> a >> b; s1 = 0; s2 = 1; t1 = 1; t2 = 0; xgcd(a, b); cout << s1 << " " << t1 << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; ll s1, s2, s, t1, t2, t; void xgcd(ll x, ll y) { if (x % y == 0) return; ll q = x / y; s = s2 - q * s1; t = t2 - q * t1; s2 = s1, t2 = t1, s1 = s, t1 = t; xgcd(y, x % y); } int main() { ll a, b; // while(1) { cin >> a >> b; s1 = 0; s2 = 1; t1 = 1; t2 = 0; xgcd(a, b); cout << s1 << " " << t1 << endl; //} return 0; }
replace
18
27
18
27
TLE
p02472
Python
Runtime Error
a = int(input()) b = int(input()) print(a + b)
print(sum(map(int, input().split())))
replace
0
3
0
1
ValueError: invalid literal for int() with base 10: '5 8'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02472/Python/s921485887.py", line 1, in <module> a = int(input()) ValueError: invalid literal for int() with base 10: '5 8'
p02477
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define maxn 400010 #define ll long long const double pi = acos(-1); typedef complex<double> cp; int n, rev[maxn]; cp omega[maxn], iomega[maxn]; void init(int N) { n = N; for (int i = 0; i < n; i++) { omega[i] = cp(cos(2 * pi / n * i), sin(2 * pi / n * i)); iomega[i] = conj(omega[i]); } int k = log2(n); for (int i = 0; i < n; i++) { int t = 0; for (int j = 0; j < k; j++) if (i & (1 << j)) t |= (1 << (k - j - 1)); rev[i] = t; } } void transform(vector<cp> &a, cp *omega) { for (int i = 0; i < n; i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int len = 2; len <= n; len <<= 1) { int mid = len >> 1; for (int j = 0; j < n; j += len) for (int i = 0; i < mid; i++) { cp t = omega[n / len * i] * a[j + mid + i]; a[j + mid + i] = a[j + i] - t; a[j + i] += t; } } } void fft(vector<cp> &a) { transform(a, omega); } void ifft(vector<cp> &a) { transform(a, iomega); for (int i = 0; i < n; i++) a[i] /= n; } vector<ll> s, a, b; void mul() { int n = 1; int n1 = (int)a.size(), n2 = (int)b.size(); while (n < n1 + n2) n <<= 1; vector<cp> tmp_a(n), tmp_b(n); for (int i = 0; i < n1; i++) tmp_a[i] = cp(a[i], 0); for (int i = 0; i < n2; i++) tmp_b[i] = cp(b[i], 0); init(n); fft(tmp_a); fft(tmp_b); for (int i = 0; i < n; i++) tmp_a[i] *= tmp_b[i]; ifft(tmp_a); s.resize(n); for (int i = 0; i < n; i++) s[i] = round(tmp_a[i].real()); while ((int)s.size() > 1 && s.back() == 0) s.pop_back(); return; } int main() { char c[maxn]; bool flag = 0; scanf("%s", c); int n = strlen(c); if (c[0] == '-') flag ^= 1; for (int i = 0; i < n; i++) { if (c[i] != '-') a.push_back(c[i] - '0'); } reverse(a.begin(), a.end()); scanf("%s", c); if (c[0] == '-') flag ^= 1; n = strlen(c); for (int i = 0; i < n; i++) { if (c[i] != '-') b.push_back(c[i] - '0'); } reverse(b.begin(), b.end()); vector<int> ans; mul(); n = (int)s.size(); int cur = 0; for (int i = 0; i < n; i++) { cur = cur / 10 + s[i]; ans.push_back(cur % 10); } cur /= 10; if (cur) ans.push_back(cur); reverse(ans.begin(), ans.end()); if (!flag || ((int)ans.size() == 1 && ans[0] == 0)) { for (int i = 0; i < (int)ans.size(); i++) printf("%d", ans[i]); puts(""); } else { putchar('-'); for (int i = 0; i < (int)ans.size(); i++) printf("%d", ans[i]); puts(""); } return 0; }
#include <bits/stdc++.h> using namespace std; #define maxn 800010 #define ll long long const double pi = acos(-1); typedef complex<double> cp; int n, rev[maxn]; cp omega[maxn], iomega[maxn]; void init(int N) { n = N; for (int i = 0; i < n; i++) { omega[i] = cp(cos(2 * pi / n * i), sin(2 * pi / n * i)); iomega[i] = conj(omega[i]); } int k = log2(n); for (int i = 0; i < n; i++) { int t = 0; for (int j = 0; j < k; j++) if (i & (1 << j)) t |= (1 << (k - j - 1)); rev[i] = t; } } void transform(vector<cp> &a, cp *omega) { for (int i = 0; i < n; i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int len = 2; len <= n; len <<= 1) { int mid = len >> 1; for (int j = 0; j < n; j += len) for (int i = 0; i < mid; i++) { cp t = omega[n / len * i] * a[j + mid + i]; a[j + mid + i] = a[j + i] - t; a[j + i] += t; } } } void fft(vector<cp> &a) { transform(a, omega); } void ifft(vector<cp> &a) { transform(a, iomega); for (int i = 0; i < n; i++) a[i] /= n; } vector<ll> s, a, b; void mul() { int n = 1; int n1 = (int)a.size(), n2 = (int)b.size(); while (n < n1 + n2) n <<= 1; vector<cp> tmp_a(n), tmp_b(n); for (int i = 0; i < n1; i++) tmp_a[i] = cp(a[i], 0); for (int i = 0; i < n2; i++) tmp_b[i] = cp(b[i], 0); init(n); fft(tmp_a); fft(tmp_b); for (int i = 0; i < n; i++) tmp_a[i] *= tmp_b[i]; ifft(tmp_a); s.resize(n); for (int i = 0; i < n; i++) s[i] = round(tmp_a[i].real()); while ((int)s.size() > 1 && s.back() == 0) s.pop_back(); return; } int main() { char c[maxn]; bool flag = 0; scanf("%s", c); int n = strlen(c); if (c[0] == '-') flag ^= 1; for (int i = 0; i < n; i++) { if (c[i] != '-') a.push_back(c[i] - '0'); } reverse(a.begin(), a.end()); scanf("%s", c); if (c[0] == '-') flag ^= 1; n = strlen(c); for (int i = 0; i < n; i++) { if (c[i] != '-') b.push_back(c[i] - '0'); } reverse(b.begin(), b.end()); vector<int> ans; mul(); n = (int)s.size(); int cur = 0; for (int i = 0; i < n; i++) { cur = cur / 10 + s[i]; ans.push_back(cur % 10); } cur /= 10; if (cur) ans.push_back(cur); reverse(ans.begin(), ans.end()); if (!flag || ((int)ans.size() == 1 && ans[0] == 0)) { for (int i = 0; i < (int)ans.size(); i++) printf("%d", ans[i]); puts(""); } else { putchar('-'); for (int i = 0; i < (int)ans.size(); i++) printf("%d", ans[i]); puts(""); } return 0; }
replace
2
3
2
3
0
p02534
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main(void) { int n; while (n--) { cout << "ACL"; } }
#include <iostream> using namespace std; int main(void) { int n; cin >> n; while (n--) { cout << "ACL"; } }
insert
5
5
5
6
TLE
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int par[100010]; int parent(int i) { if (par[i] == i) return i; else { return par[i] = parent(par[i]); } } int main() { int N, M; cin >> N >> M; for (int i = 0; i < N; i++) par[i] = i; for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; A--; B--; int pa = par[A], pb = par[B]; if (pa != pb) { par[pa] = pb; } } set<int> S; for (int i = 0; i < N; i++) { S.insert(parent(par[i])); } cout << S.size() - 1 << endl; }
#include <bits/stdc++.h> using namespace std; int par[100010]; int parent(int i) { if (par[i] == i) return i; else { return par[i] = parent(par[i]); } } int main() { int N, M; cin >> N >> M; for (int i = 0; i < N; i++) par[i] = i; for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; A--; B--; int pa = parent(A), pb = parent(B); if (pa == pb) continue; else { if (pa > pb) { par[pa] = pb; } else { par[pb] = pa; } } } set<int> S; for (int i = 0; i < N; i++) { S.insert(parent(par[i])); } cout << S.size() - 1 << endl; }
replace
25
28
25
34
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int root(int x, vector<int> &city) { if (city.at(x) == x) { return x; } else { return city.at(x) = root(city.at(x), city); } } int main() { int n, m; cin >> n >> m; vector<int> city(n); vector<int> rank(n); vector<int> unionCity(n); for (int i = 0; i < n; i++) { city.at(i) = i; rank.at(i) = 0; unionCity.at(i) = 0; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; int x = city.at(a - 1); int y = city.at(b - 1); if (rank[x] < rank[y]) { city.at(x) = y; } else { city.at(y) = x; } if (rank.at(x) == rank.at(y)) { rank.at(x)++; } } for (int i = 0; i < n; i++) { int rt = root(i, city); if (unionCity.at(rt) == 0) { unionCity.at(rt)++; } } int sum = 0; for (int i = 0; i < n; i++) { sum += unionCity.at(i); } cout << sum - 1 << endl; }
#include <bits/stdc++.h> using namespace std; int root(int x, vector<int> &city) { if (city.at(x) == x) { return x; } else { return city.at(x) = root(city.at(x), city); } } int main() { int n, m; cin >> n >> m; vector<int> city(n); vector<int> rank(n); vector<int> unionCity(n); for (int i = 0; i < n; i++) { city.at(i) = i; rank.at(i) = 0; unionCity.at(i) = 0; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; int x = city.at(a - 1); int y = city.at(b - 1); int rtx = root(x, city); int rty = root(y, city); if (rtx != rty) { if (rank[rtx] < rank[rty]) { city.at(rtx) = rty; } else { city.at(rty) = rtx; } if (rank.at(rtx) == rank.at(rty)) { rank.at(rtx)++; } } } for (int i = 0; i < n; i++) { int rt = root(i, city); if (unionCity.at(rt) == 0) { unionCity.at(rt)++; } } int sum = 0; for (int i = 0; i < n; i++) { sum += unionCity.at(i); } cout << sum - 1 << endl; }
replace
28
35
28
39
0
p02536
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; struct union_find { vector<int> r; union_find(int n) : r(n, -1) {} int root(int a) { if (r[a] < 0) { return a; } else { return root(r[a]); } } void add(int a, int b) { int ra = root(a); int rb = root(b); if (r[ra] > r[rb]) { swap(ra, rb); } r[ra] += r[rb]; r[rb] = ra; } int sum() { int s = 0; for (int i = 0; i < r.size(); i++) { if (r[i] < 0) { s++; } } return s; } }; int main() { int n, m; cin >> n >> m; union_find u(n); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; a--; b--; u.add(a, b); } cout << u.sum() - 1 << endl; }
#include <bits/stdc++.h> using namespace std; struct union_find { vector<int> r; union_find(int n) : r(n, -1) {} int root(int a) { if (r[a] < 0) { return a; } else { return root(r[a]); } } void add(int a, int b) { int ra = root(a); int rb = root(b); if (r[ra] > r[rb]) { swap(ra, rb); } else if (ra == rb) { return; } r[ra] += r[rb]; r[rb] = ra; } int sum() { int s = 0; for (int i = 0; i < r.size(); i++) { if (r[i] < 0) { s++; } } return s; } }; int main() { int n, m; cin >> n >> m; union_find u(n); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; a--; b--; u.add(a, b); } cout << u.sum() - 1 << endl; }
insert
18
18
18
20
TLE
p02536
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int P(int i, vector<int> &p) { if (p[i] == i) return i; else return p[i] = P(p[i], p); } void C(int a, int b, vector<int> &p) { p[P(a, p)] = P(b, p); } int main() { int N, M; cin >> N >> M; vector<int> p(N); int i; for (i = 0; i < N; i++) p[i] = i; int a, b; for (i = 0; i < M; i++) { cin >> a >> b; C(a, b, p); } vector<bool> f(N, false); int cnt = 0; for (i = 0; i < N; i++) { if (!f[P(i, p)]) { f[p[i]] = true; cnt++; } } cout << cnt - 1 << endl; }
#include <iostream> #include <vector> using namespace std; int P(int i, vector<int> &p) { if (p[i] == i) return i; else return p[i] = P(p[i], p); } void C(int a, int b, vector<int> &p) { p[P(a, p)] = P(b, p); } int main() { int N, M; cin >> N >> M; vector<int> p(N); int i; for (i = 0; i < N; i++) p[i] = i; int a, b; for (i = 0; i < M; i++) { cin >> a >> b; C(a - 1, b - 1, p); } vector<bool> f(N, false); int cnt = 0; for (i = 0; i < N; i++) { if (!f[P(i, p)]) { f[p[i]] = true; cnt++; } } cout << cnt - 1 << endl; }
replace
24
25
24
25
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using iii = pair<ii, int>; #define vt vector #define pq priority_queue #define pb push_back #define mp make_pair #define fi first #define se second const int MOD = 1e9 + 7; const int INF = 2e9; const int N = 1e5 + 5; int n, m, a, b, ans; int par[N]; bool vis[N]; void init() { for (int i = 1; i <= n; i++) par[i] = i; } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } void join(int x, int y) { par[root(x)] = y; } void solve() { int T = 1; // cin >> T; for (int tc = 1; tc <= T; tc++) { cin >> n >> m; init(); while (m--) { cin >> a >> b; join(a, b); } for (int i = 1; i <= n; i++) { if (!vis[root(i)]) { vis[root(i)] = true; ans++; } } cout << ans - 1 << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using iii = pair<ii, int>; #define vt vector #define pq priority_queue #define pb push_back #define mp make_pair #define fi first #define se second const int MOD = 1e9 + 7; const int INF = 2e9; const int N = 1e5 + 5; int n, m, a, b, ans; int par[N]; bool vis[N]; void init() { for (int i = 1; i <= n; i++) par[i] = i; } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } void join(int x, int y) { par[root(x)] = root(y); } void solve() { int T = 1; // cin >> T; for (int tc = 1; tc <= T; tc++) { cin >> n >> m; init(); while (m--) { cin >> a >> b; join(a, b); } for (int i = 1; i <= n; i++) { if (!vis[root(i)]) { vis[root(i)] = true; ans++; } } cout << ans - 1 << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
replace
26
27
26
27
0
p02536
C++
Runtime Error
#include <iostream> #include <stdlib.h> #include <string> #include <vector> using namespace std; struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } }; int main() { int N, M, count = 0; cin >> N >> M; UnionFind tree(N); for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; tree.unite(A, B); } for (int i = 0; i < N; i++) { if (tree.root(i) == i) count++; } cout << count - 1 << endl; return 0; }
#include <iostream> #include <stdlib.h> #include <string> #include <vector> using namespace std; struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } }; int main() { int N, M, count = 0; cin >> N >> M; UnionFind tree(N); for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; A--; B--; tree.unite(A, B); } for (int i = 0; i < N; i++) { if (tree.root(i) == i) count++; } cout << count - 1 << endl; return 0; }
insert
39
39
39
41
0
p02536
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; class UnionFind { public: explicit UnionFind(int N) : root(N, -1), size(N, 1) {} int getRoot(int u) { return root[u] == -1 ? u : root[u] = getRoot(root[u]); } int getSize(int u) { return size[getRoot(u)]; } bool same(int a, int b) { return getRoot(a) == getRoot(b); } bool merge(int a, int b) { int u = getRoot(a); int v = getRoot(b); if (u == v) return false; root[u] = v; size[v] += size[u]; return true; } private: vector<int> root; vector<int> size; }; int main() { int N, M; cin >> N >> M; UnionFind uf(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; uf.merge(a, b); } int res = -1; for (int i = 0; i < N; i++) if (uf.getRoot(i) == i) ++res; cout << res << endl; }
#include <iostream> #include <vector> using namespace std; class UnionFind { public: explicit UnionFind(int N) : root(N, -1), size(N, 1) {} int getRoot(int u) { return root[u] == -1 ? u : root[u] = getRoot(root[u]); } int getSize(int u) { return size[getRoot(u)]; } bool same(int a, int b) { return getRoot(a) == getRoot(b); } bool merge(int a, int b) { int u = getRoot(a); int v = getRoot(b); if (u == v) return false; root[u] = v; size[v] += size[u]; return true; } private: vector<int> root; vector<int> size; }; int main() { int N, M; cin >> N >> M; UnionFind uf(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; --a; --b; uf.merge(a, b); } int res = -1; for (int i = 0; i < N; i++) if (uf.getRoot(i) == i) ++res; cout << res << endl; }
insert
33
33
33
35
0
p02536
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 int groups; UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; groups = N; } 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につける groups--; } 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 t(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; t.unite(a, b); } cout << t.groups - 1 << endl; }
#include <iostream> #include <vector> using namespace std; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 int groups; UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; groups = N; } 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につける groups--; } 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 t(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a--; b--; t.unite(a, b); } cout << t.groups - 1 << endl; }
insert
45
45
45
47
0
p02536
C++
Runtime Error
// #include "bits/stdc++.h" #include <bits/stdc++.h> using namespace std; // using namespace atcoder; using ll = long long; template <typename T> void print_vec(const vector<T> &v, bool is_reverse = false, ll num = 0) { if (num == 0) num = (ll)v.size(); cout << endl; cout << "i= "; for (ll i = 0; i < num; i++) cout << i << " "; cout << endl; cout << " "; if (is_reverse) for (ll i = num - 1; i >= 0; i--) { cout << v[i]; if (i != 0) cout << " "; } else for (ll i = 0; i < num; i++) { cout << v[i]; if (i != num - 1) cout << " "; } cout << endl; } // template <typename T> // void print_pairvec(const vector<T> &v, ll num=0){ // if(num == 0) num = (ll)v.size(); // cout << endl; for(ll i=0; i<num; i++){ cout << v[i].first << " " << // v[i].second << endl;} // } template <typename T> void print_pairvec(const T &_pair, ll num = 0) { cout << endl; for (pair<ll, int> x : _pair) { cout << x.first << " " << x.second << endl; } } template <typename T> void print_vec2(const vector<vector<T>> &v) { cout << endl; cout << " "; for (ll i = 0; i < v[0].size(); i++) cout << i << " "; cout << endl; for (ll i = 0; i < v.size(); i++) { cout << "i=" << i << ": "; for (ll j = 0; j < v[i].size(); j++) { if (v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << " "; else cout << "\x1B[31m" << v[i][j] << " "; // https://stackoverrun.com/ja/q/12618775 } cout << "\x1B[0m" << endl; } } // UnionFind // coding: https://youtu.be/TdR816rqc3s?t=726 // comment: https://youtu.be/TdR816rqc3s?t=6822 struct UnionFind { vector<int> d; UnionFind(int n = 0) : d(n, -1) {} int find(int x) { if (d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return -d[find(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<ll> a(m), b(m); for (int i = 0; i < m; i++) cin >> a[i] >> b[i]; UnionFind uf(n); for (int i = 0; i < m; i++) { uf.unite(a[i], b[i]); } // vector<bool> is_root(n, false); // for(int i=0; i<n; i++){ // ll x = uf.find(i); // if(x == -1) is_root[i] = true; // else is_root[x] = true; // } // ll cnt = 0; // for(int i=0; i<n; i++) if(is_root[i]) cnt++; ll cnt = 0; for (int i = 0; i < n; i++) if (uf.d[i] < 0) cnt++; ll ans = cnt - 1; cout << ans << endl; return 0; // cout << fixed;//小数点を確実に10桁で表示するためのもの // cout << setprecision(10); }
// #include "bits/stdc++.h" #include <bits/stdc++.h> using namespace std; // using namespace atcoder; using ll = long long; template <typename T> void print_vec(const vector<T> &v, bool is_reverse = false, ll num = 0) { if (num == 0) num = (ll)v.size(); cout << endl; cout << "i= "; for (ll i = 0; i < num; i++) cout << i << " "; cout << endl; cout << " "; if (is_reverse) for (ll i = num - 1; i >= 0; i--) { cout << v[i]; if (i != 0) cout << " "; } else for (ll i = 0; i < num; i++) { cout << v[i]; if (i != num - 1) cout << " "; } cout << endl; } // template <typename T> // void print_pairvec(const vector<T> &v, ll num=0){ // if(num == 0) num = (ll)v.size(); // cout << endl; for(ll i=0; i<num; i++){ cout << v[i].first << " " << // v[i].second << endl;} // } template <typename T> void print_pairvec(const T &_pair, ll num = 0) { cout << endl; for (pair<ll, int> x : _pair) { cout << x.first << " " << x.second << endl; } } template <typename T> void print_vec2(const vector<vector<T>> &v) { cout << endl; cout << " "; for (ll i = 0; i < v[0].size(); i++) cout << i << " "; cout << endl; for (ll i = 0; i < v.size(); i++) { cout << "i=" << i << ": "; for (ll j = 0; j < v[i].size(); j++) { if (v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << " "; else cout << "\x1B[31m" << v[i][j] << " "; // https://stackoverrun.com/ja/q/12618775 } cout << "\x1B[0m" << endl; } } // UnionFind // coding: https://youtu.be/TdR816rqc3s?t=726 // comment: https://youtu.be/TdR816rqc3s?t=6822 struct UnionFind { vector<int> d; UnionFind(int n = 0) : d(n, -1) {} int find(int x) { if (d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return -d[find(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<ll> a(m), b(m); for (int i = 0; i < m; i++) cin >> a[i] >> b[i]; UnionFind uf(n); for (int i = 0; i < m; i++) { uf.unite(a[i] - 1, b[i] - 1); } // vector<bool> is_root(n, false); // for(int i=0; i<n; i++){ // ll x = uf.find(i); // if(x == -1) is_root[i] = true; // else is_root[x] = true; // } // ll cnt = 0; // for(int i=0; i<n; i++) if(is_root[i]) cnt++; ll cnt = 0; for (int i = 0; i < n; i++) if (uf.d[i] < 0) cnt++; ll ans = cnt - 1; cout << ans << endl; return 0; // cout << fixed;//小数点を確実に10桁で表示するためのもの // cout << setprecision(10); }
replace
98
99
98
99
0
p02536
C++
Runtime Error
#include <algorithm> #include <iostream> #include <math.h> #include <numeric> #include <queue> #include <stdio.h> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; struct UnionFind { vector<int> parent; int N; UnionFind(int n) : N(n) { // メンバイニシャライザを実行している for (int i = 0; i < N; i++) { parent[i] = i; } } int getParent(int x) { return parent[x]; } int root(int x) { if (parent[x] == x) { return x; } else { parent[x] = root(parent[x]); // 親の付け替え(ならし) return parent[x]; } } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { parent[ry] = rx; } } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(int argc, char *argv[]) { int N, M; scanf("%d %d", &N, &M); vector<int> A(M); vector<int> B(M); UnionFind tree(N); for (int i = 0; i < M; i++) { scanf("%d %d", &A[i], &B[i]); if (A[i] > B[i]) { int tmp; tmp = B[i]; B[i] = A[i]; A[i] = tmp; } tree.unite(A[i] - 1, B[i] - 1); } int count = 0; for (int i = 0; i < N; i++) { if (tree.getParent(i) == i) { count++; } } printf("%d\n", count - 1); }
#include <algorithm> #include <iostream> #include <math.h> #include <numeric> #include <queue> #include <stdio.h> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; struct UnionFind { vector<int> parent; int N; UnionFind(int n) : parent(n), N(n) { // メンバイニシャライザを実行している for (int i = 0; i < n; i++) { parent[i] = i; } } int getParent(int x) { return parent[x]; } int root(int x) { if (parent[x] == x) { return x; } else { parent[x] = root(parent[x]); // 親の付け替え(ならし) return parent[x]; } } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { parent[ry] = rx; } } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(int argc, char *argv[]) { int N, M; scanf("%d %d", &N, &M); vector<int> A(M); vector<int> B(M); UnionFind tree(N); for (int i = 0; i < M; i++) { scanf("%d %d", &A[i], &B[i]); if (A[i] > B[i]) { int tmp; tmp = B[i]; B[i] = A[i]; A[i] = tmp; } tree.unite(A[i] - 1, B[i] - 1); } int count = 0; for (int i = 0; i < N; i++) { if (tree.getParent(i) == i) { count++; } } printf("%d\n", count - 1); }
replace
15
17
15
17
-11
p02536
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <math.h> #include <numeric> #include <queue> #include <stdio.h> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; struct UnionFind { vector<int> parent; UnionFind(int N) : parent(N) { for (int i = 0; i < N; i++) { parent[i] = i; } } int getParent(int x) { return parent[x]; } int root(int x) { if (parent[x] == x) { return x; } else { return root(parent[x]); } } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { parent[ry] = rx; } } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(int argc, char *argv[]) { int N, M; scanf("%d %d", &N, &M); vector<int> A(M); vector<int> B(M); UnionFind tree(N); for (int i = 0; i < M; i++) { scanf("%d %d", &A[i], &B[i]); if (A[i] > B[i]) { int tmp; tmp = B[i]; B[i] = A[i]; A[i] = tmp; } tree.unite(A[i] - 1, B[i] - 1); } int count = 0; for (int i = 0; i < N; i++) { if (tree.getParent(i) == i) { count++; } } printf("%d\n", count - 1); }
#include <algorithm> #include <iostream> #include <math.h> #include <numeric> #include <queue> #include <stdio.h> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; struct UnionFind { vector<int> parent; UnionFind(int N) : parent(N) { for (int i = 0; i < N; i++) { parent[i] = i; } } int getParent(int x) { return parent[x]; } int root(int x) { if (parent[x] == x) { return x; } else { parent[x] = root(parent[x]); return parent[x]; } } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { parent[ry] = rx; } } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(int argc, char *argv[]) { int N, M; scanf("%d %d", &N, &M); vector<int> A(M); vector<int> B(M); UnionFind tree(N); for (int i = 0; i < M; i++) { scanf("%d %d", &A[i], &B[i]); if (A[i] > B[i]) { int tmp; tmp = B[i]; B[i] = A[i]; A[i] = tmp; } tree.unite(A[i] - 1, B[i] - 1); } int count = 0; for (int i = 0; i < N; i++) { if (tree.getParent(i) == i) { count++; } } printf("%d\n", count - 1); }
replace
25
26
25
27
TLE
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; class DisjointSet { public: vector<int> rank, p; DisjointSet() {} DisjointSet(int size) { rank.resize(size, 0); p.resize(size, 0); for (int i = 0; i < size; i++) { makeSet(i); } } void makeSet(int x) { p[x] = x; rank[x] = 0; } void unite(int x, int y) { link(findSet(x), findSet(y)); } void link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; } else { p[x] = y; if (rank[x] == rank[y]) { rank[y]++; } } } int findSet(int x) { if (x != p[x]) { p[x] = findSet(p[x]); } return p[x]; } }; int main() { int N, M, A, B; cin >> N >> M; DisjointSet ds = DisjointSet(N); for (int i = 0; i < M; i++) { cin >> A >> B; ds.unite(A, B); } set<int> check; for (int i = 0; i < N; i++) { check.insert(ds.findSet(i)); } cout << check.size() - 1 << endl; }
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; class DisjointSet { public: vector<int> rank, p; DisjointSet() {} DisjointSet(int size) { rank.resize(size, 0); p.resize(size, 0); for (int i = 0; i < size; i++) { makeSet(i); } } void makeSet(int x) { p[x] = x; rank[x] = 0; } void unite(int x, int y) { link(findSet(x), findSet(y)); } void link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; } else { p[x] = y; if (rank[x] == rank[y]) { rank[y]++; } } } int findSet(int x) { if (x != p[x]) { p[x] = findSet(p[x]); } return p[x]; } }; int main() { int N, M, A, B; cin >> N >> M; DisjointSet ds = DisjointSet(N); for (int i = 0; i < M; i++) { cin >> A >> B; ds.unite(A - 1, B - 1); } set<int> check; for (int i = 0; i < N; i++) { check.insert(ds.findSet(i)); } cout << check.size() - 1 << endl; }
replace
51
52
51
52
0
p02536
C++
Runtime Error
////////////////<------------- Xorcestor_X -------------->//////////////// #include <algorithm> #include <bits/stdc++.h> #include <deque> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> using namespace std; #define endl "\n" #define ll long long int #define c(x) \ ll x; \ cin >> x #define cc(x, y) \ ll x, y; \ cin >> x >> y #define ccc(x, y, z) \ ll x, y, z; \ cin >> x >> y >> z #define db long double #define fast \ cin.tie(NULL); \ cout.tie(NULL); \ ios_base::sync_with_stdio(false) #define inf 1e16 #define mod 1000000007 #define mod2 998244353 #define eps 1e-7 #define PI 3.1415926535897932385 #define pb push_back #define bitc __builtin_popcountll #define mp make_pair #define ff first #define ss second #define all(ar) ar.begin(), ar.end() #define len(x) (ll)(x).size() #define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++) #define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--) #define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define print(arr, n) \ cout << " :: " << #arr << " :: " << endl; \ rep(i, n) cout << arr[i] << " "; \ cout << endl #define print2(arr, n, m) \ cout << " :: " << #arr << " :: " << endl; \ rep(i, n) { \ rep(j, m) cout << arr[i][j] << " "; \ cout << endl; \ } #define dyn1(arr, n) \ ll *arr = new ll[n]; \ rep(i, n) arr[i] = 0; #define dyn2(arr, n, m) \ ll **arr = new ll *[(n)]; \ rep(i, n) arr[i] = new ll[m]; \ rep(i, n) rep(j, m) arr[i][j] = 0 #define carr(arr, n) fr(i, 0, n - 1) cin >> arr[i] #define carr2(arr, n, m) fr(i, 0, n - 1) fr(j, 0, m - 1) cin >> arr[i][j] #define debug(a) cout << " :: " << #a << " == " << a << endl #define debug2(a, b) \ cout << " :: " << #a << " == " << a << " :: " << #b << " == " << b << endl #define watch(x) cerr << (#x) << " is " << (x) << endl #define debugx(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #define time__(d) \ for (long blockTime = 0; \ (blockTime == 0 ? (blockTime = clock()) != 0 : false); \ debugx("%s time : %.4fs", d, \ (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef pair<ll, ll> pii; typedef vector<pii> vii; const ll N = 105; typedef ll mt[N][N]; inline ll maxim(ll a, ll b) { if (a > b) return a; else return b; } inline ll minim(ll a, ll b) { if (a < b) return a; else return b; } inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll reduce(ll a, ll m) { a %= m; a += m; a %= m; return a; } ll Extended_gcd(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } else { ll x1, y1; ll g = Extended_gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return g; } } bool leap(ll y) { if (y % 400 == 0) return true; if ((y % 4 == 0) && (y % 100 != 0)) return true; return false; } ll isprime(ll n) { if (n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } bool prime[2000010]; void SieveOfEratosthenes() { memset(prime, true, sizeof(prime)); prime[1] = false; for (int p = 2; p * p <= 2000001; p++) { if (prime[p] == true) { for (int i = p * p; i <= 2000001; i += p) prime[i] = false; } } } // vector<ll> prime_vec[1000010]; // void modified_SieveOfEratosthenes() // { // //prime[1]=false; // for (int p=2; p*p<=1000001; p++) // { // if (prime_vec[p].size()==0) // { // for (int i=p*2; i<=1000001; i += p) // prime_vec[i].pb(p); // } // } // } ////*** Various Exponentiation Functions ***//// ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a); a = (a * a); b >>= 1; } return res; } // ll result[1000010]; // ll tempo[1000010]; // ll per[1000010]; // // // void permutation_exp(ll n,ll x) // { // fr(o,1,n) // { // result[o]=o; // } // // while(x>0) // { // if(x&1) // { // fr(o,1,n) // { // tempo[o]=result[o]; // } // // fr(o,1,n) // { // result[o]=tempo[per[o]]; // } // } // // fr(o,1,n) // { // tempo[o]=per[o]; // } // // fr(o,1,n) // { // per[o]=tempo[tempo[o]]; // } // x>>=1; // } // } ll len; ll ultimate[110][110]; void matrix_exp(ll arr[100][110], ll n) { // ll len=4; ll res[110][110]; rep(o, len) { rep(p, len) { if (o != p) res[o][p] = 0; else res[o][p] = 1; } } ll temp[110][110]; while (n) { if (n & 1) { rep(x, len) { rep(y, len) { temp[x][y] = 0; } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { fr(z, 0, len - 1) { temp[i][j] += reduce(res[i][z] * arr[z][j], mod); } } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { res[i][j] = reduce(temp[i][j], mod); } } } rep(x, len) { rep(y, len) { temp[x][y] = 0; } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { fr(z, 0, len - 1) { temp[i][j] += reduce(arr[i][z] * arr[z][j], mod); temp[i][j] %= mod; } } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { arr[i][j] = reduce(temp[i][j], mod); } } n /= 2; } fr(i, 0, len - 1) { fr(j, 0, len - 1) { ultimate[i][j] = reduce(res[i][j], mod); } } } // // ll modpow(ll a, ll b,ll x) // { // ll res = 1; // while (b > 0) // { // if (b & 1) res = (res * a)%x; // a = (a * a)%x; // b >>= 1; // } // return res; // } // // ////*** End of Various Exponentiation Functions ***//// // // int modInverse(int a, int m) // { // int m0 = m; // int y = 0, x = 1; // if (m == 1) // return 0; // while (a > 1) // { // // q is quotient // int q = a / m; // int t = m; // // m is remainder now, process same as // // Euclid's algo // m = a % m, a = t; // t = y; // // Update y and x // y = x - q * y; // x = t; // } // // Make x positive // if (x < 0) // x += m0; // return x; // } // // ll int_sqrt(ll h) // { // ll g=(ll)sqrt(h); // g++; // if(g*g==h) return g; // g--; // if(g*g==h) return g; // g--; // if(g*g==h) return g; // return -1; // } // // // ll lpf[1000001] = {}; // // void precompute() // { // fr(i,2,1000000) // { // if(!lpf[i]) // { // for(ll j=i;j<=1000000;j+=i) // { // if(!lpf[j]) // lpf[j]=i; // } // } // } // } // // ll fact[2000010]; // // void compute_fact() // { // fact[0]=1; // fr(o,1,2000004) // { // fact[o]=(fact[o-1]*o)%mod; // } // } // // ll ncr_mod(ll n,ll r) // { // if(r==0||n==r) return 1; // else return // (((fact[n]*modInverse(fact[r],mod))%mod)*modInverse(fact[n-r],mod))%mod; // } // // // ll f(ll x) // { // ll total=0; // return total; // } // // ll ternary_search(ll l, ll r) // { //set the error limit here // while (r - l > 10) // { // ll m1 = l + (r - l) / 3; // ll m2 = r - (r - l) / 3; // ll f1 = f(m1); //evaluates the function at m1 // ll f2 = f(m2); //evaluates the function at m2 // if (f1 > f2) // l = m1; // else // r = m2; // } // ll mino=f(l); // ll mino_x=l; // // fr(o,l,r) // { // if(f(o)<mino) // { // mino_x=o; // mino=f(o); // } // } // return f(mino_x); // } // // // // ll tree[2000010]; // // void build(ll* a,ll v,ll tl,ll tr) // // { // // if(tl==tr) // // { // // tree[v]=((a[tl]==x)?1:0); // // } // // else // // { // // ll tm=(tl+tr)/2; // // build(a,v*2,tl,tm); // // build(a,v*2+1,tm+1,tr); // // tree[v]=tree[v*2]+tree[v*2+1]; // // } // // } // // // // ll query(ll v,ll tl,ll tr,ll l,ll r) // // { // // if(l>r) return 0; // // if(l==tl&&r==tr) return tree[v]; // // ll tm=(tl+tr)/2; // // return // query(v*2,tl,tm,l,minim(r,tm))+query(v*2+1,tm+1,tr,maxim(l,tm+1),r); // // } // // // // // // void update(ll v,ll tl,ll tr,ll pos,ll new_val) // // { // // if(tl==tr) // // { // // tree[v]=((arr[pos]==x)?1:0); // // } // // else // // { // // ll tm=(tl+tr)/2; // // if(pos<=tm) update(v*2,tl,tm,pos,new_val); // // else update(v*2+1,tm+1,tr,pos,new_val); // // tree[v]=tree[v*2]+tree[v*2+1]; // // } // // } // // // // // // ll find_kth(ll v,ll tl,ll tr,ll k) // // { // // if(k>tree[v]) return -1; // // if(tl==tr) return tl; // // ll tm=(tl+tr)/2; // // if(tree[v*2]>=k) return find_kth(v*2,tl,tm,k); // // else return find_kth(v*2+1,tm+1,tr,k-tree[v*2]); // // } // // // bool opt_prime(ll n) // { // if(n<2000000) return prime[n]; // else return isprime(n); // } // // // ll far[1000010]; // ll farsum[1000010]; // // ll n,k; // ll arr[1000010]; // // ll func(ll gap) // { // ll count=0; // ll pre=-inf; // rep(o,n) // { // if(arr[o]-pre>=gap) // { // pre=arr[o]; // count++; // } // } // // if(count>=k) return true; // else return false; // } // // ll bs(ll low,ll high,ll target) // { // while(low+4<high) // { // ll mid=(low+high)/2; // //increasing function with inclusion // if(func(mid)) // { // low=mid; // } // else // { // high=mid-1; // } // } // // frr(o,high,low) // { // if(func(o)) return o; // } // // return -1; // } // // // // // ll lpfx(ll x) // { // if(x%2==0) return 2; // else // { // for(int z=3;z*z<=x;z+=2) // { // if(x%z==0) return z; // } // } // return x; // } // // // ll fz(ll n) // { // ll o=1; // while(o<=n) // { // o*=2; // } // return o; // } ll vis[100010] = {0}; vector<ll> adjx[100010]; ll dfs(ll node) { vis[node] = 1; rep(o, adjx[node].size()) { if (!vis[adjx[node][o]]) { dfs(adjx[node][o]); } } } int main() { fast; // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("input.txt", "r", stdin); // // for writing output to output.txt // freopen("output.txt", "w", stdout); // #endif cc(n, m); rep(o, m) { cc(a, b); adjx[a].pb(b); adjx[b].pb(a); } ll count = 0; fr(o, 1, n) { if (!vis[o]) { vis[o] = 1; count++; dfs(o); } } cout << count - 1 << endl; return 0; }
////////////////<------------- Xorcestor_X -------------->//////////////// #include <algorithm> #include <bits/stdc++.h> #include <deque> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> using namespace std; #define endl "\n" #define ll long long int #define c(x) \ ll x; \ cin >> x #define cc(x, y) \ ll x, y; \ cin >> x >> y #define ccc(x, y, z) \ ll x, y, z; \ cin >> x >> y >> z #define db long double #define fast \ cin.tie(NULL); \ cout.tie(NULL); \ ios_base::sync_with_stdio(false) #define inf 1e16 #define mod 1000000007 #define mod2 998244353 #define eps 1e-7 #define PI 3.1415926535897932385 #define pb push_back #define bitc __builtin_popcountll #define mp make_pair #define ff first #define ss second #define all(ar) ar.begin(), ar.end() #define len(x) (ll)(x).size() #define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++) #define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--) #define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define print(arr, n) \ cout << " :: " << #arr << " :: " << endl; \ rep(i, n) cout << arr[i] << " "; \ cout << endl #define print2(arr, n, m) \ cout << " :: " << #arr << " :: " << endl; \ rep(i, n) { \ rep(j, m) cout << arr[i][j] << " "; \ cout << endl; \ } #define dyn1(arr, n) \ ll *arr = new ll[n]; \ rep(i, n) arr[i] = 0; #define dyn2(arr, n, m) \ ll **arr = new ll *[(n)]; \ rep(i, n) arr[i] = new ll[m]; \ rep(i, n) rep(j, m) arr[i][j] = 0 #define carr(arr, n) fr(i, 0, n - 1) cin >> arr[i] #define carr2(arr, n, m) fr(i, 0, n - 1) fr(j, 0, m - 1) cin >> arr[i][j] #define debug(a) cout << " :: " << #a << " == " << a << endl #define debug2(a, b) \ cout << " :: " << #a << " == " << a << " :: " << #b << " == " << b << endl #define watch(x) cerr << (#x) << " is " << (x) << endl #define debugx(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #define time__(d) \ for (long blockTime = 0; \ (blockTime == 0 ? (blockTime = clock()) != 0 : false); \ debugx("%s time : %.4fs", d, \ (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef pair<ll, ll> pii; typedef vector<pii> vii; const ll N = 105; typedef ll mt[N][N]; inline ll maxim(ll a, ll b) { if (a > b) return a; else return b; } inline ll minim(ll a, ll b) { if (a < b) return a; else return b; } inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll reduce(ll a, ll m) { a %= m; a += m; a %= m; return a; } ll Extended_gcd(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } else { ll x1, y1; ll g = Extended_gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return g; } } bool leap(ll y) { if (y % 400 == 0) return true; if ((y % 4 == 0) && (y % 100 != 0)) return true; return false; } ll isprime(ll n) { if (n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } bool prime[2000010]; void SieveOfEratosthenes() { memset(prime, true, sizeof(prime)); prime[1] = false; for (int p = 2; p * p <= 2000001; p++) { if (prime[p] == true) { for (int i = p * p; i <= 2000001; i += p) prime[i] = false; } } } // vector<ll> prime_vec[1000010]; // void modified_SieveOfEratosthenes() // { // //prime[1]=false; // for (int p=2; p*p<=1000001; p++) // { // if (prime_vec[p].size()==0) // { // for (int i=p*2; i<=1000001; i += p) // prime_vec[i].pb(p); // } // } // } ////*** Various Exponentiation Functions ***//// ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a); a = (a * a); b >>= 1; } return res; } // ll result[1000010]; // ll tempo[1000010]; // ll per[1000010]; // // // void permutation_exp(ll n,ll x) // { // fr(o,1,n) // { // result[o]=o; // } // // while(x>0) // { // if(x&1) // { // fr(o,1,n) // { // tempo[o]=result[o]; // } // // fr(o,1,n) // { // result[o]=tempo[per[o]]; // } // } // // fr(o,1,n) // { // tempo[o]=per[o]; // } // // fr(o,1,n) // { // per[o]=tempo[tempo[o]]; // } // x>>=1; // } // } ll len; ll ultimate[110][110]; void matrix_exp(ll arr[100][110], ll n) { // ll len=4; ll res[110][110]; rep(o, len) { rep(p, len) { if (o != p) res[o][p] = 0; else res[o][p] = 1; } } ll temp[110][110]; while (n) { if (n & 1) { rep(x, len) { rep(y, len) { temp[x][y] = 0; } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { fr(z, 0, len - 1) { temp[i][j] += reduce(res[i][z] * arr[z][j], mod); } } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { res[i][j] = reduce(temp[i][j], mod); } } } rep(x, len) { rep(y, len) { temp[x][y] = 0; } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { fr(z, 0, len - 1) { temp[i][j] += reduce(arr[i][z] * arr[z][j], mod); temp[i][j] %= mod; } } } fr(i, 0, len - 1) { fr(j, 0, len - 1) { arr[i][j] = reduce(temp[i][j], mod); } } n /= 2; } fr(i, 0, len - 1) { fr(j, 0, len - 1) { ultimate[i][j] = reduce(res[i][j], mod); } } } // // ll modpow(ll a, ll b,ll x) // { // ll res = 1; // while (b > 0) // { // if (b & 1) res = (res * a)%x; // a = (a * a)%x; // b >>= 1; // } // return res; // } // // ////*** End of Various Exponentiation Functions ***//// // // int modInverse(int a, int m) // { // int m0 = m; // int y = 0, x = 1; // if (m == 1) // return 0; // while (a > 1) // { // // q is quotient // int q = a / m; // int t = m; // // m is remainder now, process same as // // Euclid's algo // m = a % m, a = t; // t = y; // // Update y and x // y = x - q * y; // x = t; // } // // Make x positive // if (x < 0) // x += m0; // return x; // } // // ll int_sqrt(ll h) // { // ll g=(ll)sqrt(h); // g++; // if(g*g==h) return g; // g--; // if(g*g==h) return g; // g--; // if(g*g==h) return g; // return -1; // } // // // ll lpf[1000001] = {}; // // void precompute() // { // fr(i,2,1000000) // { // if(!lpf[i]) // { // for(ll j=i;j<=1000000;j+=i) // { // if(!lpf[j]) // lpf[j]=i; // } // } // } // } // // ll fact[2000010]; // // void compute_fact() // { // fact[0]=1; // fr(o,1,2000004) // { // fact[o]=(fact[o-1]*o)%mod; // } // } // // ll ncr_mod(ll n,ll r) // { // if(r==0||n==r) return 1; // else return // (((fact[n]*modInverse(fact[r],mod))%mod)*modInverse(fact[n-r],mod))%mod; // } // // // ll f(ll x) // { // ll total=0; // return total; // } // // ll ternary_search(ll l, ll r) // { //set the error limit here // while (r - l > 10) // { // ll m1 = l + (r - l) / 3; // ll m2 = r - (r - l) / 3; // ll f1 = f(m1); //evaluates the function at m1 // ll f2 = f(m2); //evaluates the function at m2 // if (f1 > f2) // l = m1; // else // r = m2; // } // ll mino=f(l); // ll mino_x=l; // // fr(o,l,r) // { // if(f(o)<mino) // { // mino_x=o; // mino=f(o); // } // } // return f(mino_x); // } // // // // ll tree[2000010]; // // void build(ll* a,ll v,ll tl,ll tr) // // { // // if(tl==tr) // // { // // tree[v]=((a[tl]==x)?1:0); // // } // // else // // { // // ll tm=(tl+tr)/2; // // build(a,v*2,tl,tm); // // build(a,v*2+1,tm+1,tr); // // tree[v]=tree[v*2]+tree[v*2+1]; // // } // // } // // // // ll query(ll v,ll tl,ll tr,ll l,ll r) // // { // // if(l>r) return 0; // // if(l==tl&&r==tr) return tree[v]; // // ll tm=(tl+tr)/2; // // return // query(v*2,tl,tm,l,minim(r,tm))+query(v*2+1,tm+1,tr,maxim(l,tm+1),r); // // } // // // // // // void update(ll v,ll tl,ll tr,ll pos,ll new_val) // // { // // if(tl==tr) // // { // // tree[v]=((arr[pos]==x)?1:0); // // } // // else // // { // // ll tm=(tl+tr)/2; // // if(pos<=tm) update(v*2,tl,tm,pos,new_val); // // else update(v*2+1,tm+1,tr,pos,new_val); // // tree[v]=tree[v*2]+tree[v*2+1]; // // } // // } // // // // // // ll find_kth(ll v,ll tl,ll tr,ll k) // // { // // if(k>tree[v]) return -1; // // if(tl==tr) return tl; // // ll tm=(tl+tr)/2; // // if(tree[v*2]>=k) return find_kth(v*2,tl,tm,k); // // else return find_kth(v*2+1,tm+1,tr,k-tree[v*2]); // // } // // // bool opt_prime(ll n) // { // if(n<2000000) return prime[n]; // else return isprime(n); // } // // // ll far[1000010]; // ll farsum[1000010]; // // ll n,k; // ll arr[1000010]; // // ll func(ll gap) // { // ll count=0; // ll pre=-inf; // rep(o,n) // { // if(arr[o]-pre>=gap) // { // pre=arr[o]; // count++; // } // } // // if(count>=k) return true; // else return false; // } // // ll bs(ll low,ll high,ll target) // { // while(low+4<high) // { // ll mid=(low+high)/2; // //increasing function with inclusion // if(func(mid)) // { // low=mid; // } // else // { // high=mid-1; // } // } // // frr(o,high,low) // { // if(func(o)) return o; // } // // return -1; // } // // // // // ll lpfx(ll x) // { // if(x%2==0) return 2; // else // { // for(int z=3;z*z<=x;z+=2) // { // if(x%z==0) return z; // } // } // return x; // } // // // ll fz(ll n) // { // ll o=1; // while(o<=n) // { // o*=2; // } // return o; // } ll vis[100010] = {0}; vector<ll> adjx[100010]; void dfs(ll node) { vis[node] = 1; rep(o, adjx[node].size()) { if (!vis[adjx[node][o]]) { dfs(adjx[node][o]); } } } int main() { fast; // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("input.txt", "r", stdin); // // for writing output to output.txt // freopen("output.txt", "w", stdout); // #endif cc(n, m); rep(o, m) { cc(a, b); adjx[a].pb(b); adjx[b].pb(a); } ll count = 0; fr(o, 1, n) { if (!vis[o]) { vis[o] = 1; count++; dfs(o); } } cout << count - 1 << endl; return 0; }
replace
540
541
540
541
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define endl "\n" const int N = 1e9 + 7; #define int long long #define double long double #define pb push_back #define vi vector<int> #define rep(i, n) for (int i = 1; i <= n; i++) vi ar[100001]; int vis[100000]; void dfs(int node) { vis[node] = 1; for (int child : ar[node]) { if (!vis[child]) dfs(child); } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS; int n, m, a, b; cin >> n >> m; while (m--) { cin >> a >> b; ar[a].pb(b), ar[b].pb(a); } int cc = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { cc++; dfs(i); } } cout << cc - 1; return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define endl "\n" const int N = 1e9 + 7; #define int long long #define double long double #define pb push_back #define vi vector<int> #define rep(i, n) for (int i = 1; i <= n; i++) vi ar[100001]; int vis[100001]; void dfs(int node) { vis[node] = 1; for (int child : ar[node]) { if (!vis[child]) dfs(child); } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS; int n, m, a, b; cin >> n >> m; while (m--) { cin >> a >> b; ar[a].pb(b), ar[b].pb(a); } int cc = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { cc++; dfs(i); } } cout << cc - 1; return 0; }
replace
15
16
15
16
-11
p02536
C++
Runtime Error
#include "algorithm" #include "bitset" #include "cassert" #include "climits" #include "cmath" #include "cstdio" #include "ctime" #include "functional" #include "iomanip" #include "iostream" #include "list" #include "map" #include "numeric" #include "queue" #include "random" #include "set" #include "stack" #include "string" #include "unordered_map" #include "unordered_set" using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; #define rep(begin, i, end) for (ll i = begin; i < (ll)(end); i++) #define all(v) v.begin(), v.end() const int MOD = 1000000007; 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); rep(0, i, M) { int a, b; cin >> a >> b; tree.unite(a, b); } map<int, int> m; rep(0, i, N) { m[tree.root(i)]++; } ll ans = m.size() - 1; cout << ans << endl; }
#include "algorithm" #include "bitset" #include "cassert" #include "climits" #include "cmath" #include "cstdio" #include "ctime" #include "functional" #include "iomanip" #include "iostream" #include "list" #include "map" #include "numeric" #include "queue" #include "random" #include "set" #include "stack" #include "string" #include "unordered_map" #include "unordered_set" using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; #define rep(begin, i, end) for (ll i = begin; i < (ll)(end); i++) #define all(v) v.begin(), v.end() const int MOD = 1000000007; 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); rep(0, i, M) { int a, b; cin >> a >> b; a--; b--; tree.unite(a, b); } map<int, int> m; rep(0, i, N) { m[tree.root(i)]++; } ll ans = m.size() - 1; cout << ans << endl; }
insert
68
68
68
70
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(void) { long long N, M; cin >> N >> M; vector<long long> A(M); vector<long long> B(M); for (long long i = 0; i < M; i++) { cin >> A[i] >> B[i]; } UnionFind tree(N); for (long long i = 0; i < M; i++) { tree.unite(A[i], B[i]); } vector<long long> count(N, 0); long long ans = 0; for (long long i = 0; i < N; i++) { if (count[tree.root(i)] == 1) { } else { count[tree.root(i)] = 1; ans++; } } cout << ans - 1 << endl; }
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(void) { long long N, M; cin >> N >> M; vector<long long> A(M); vector<long long> B(M); for (long long i = 0; i < M; i++) { cin >> A[i] >> B[i]; } UnionFind tree(N); for (long long i = 0; i < M; i++) { tree.unite(A[i] - 1, B[i] - 1); } vector<long long> count(N, 0); long long ans = 0; for (long long i = 0; i < N; i++) { if (count[tree.root(i)] == 1) { } else { count[tree.root(i)] = 1; ans++; } } cout << ans - 1 << endl; }
replace
42
43
42
43
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef unsigned long long ull; const long long MOD = 1e9 + 7; const long long LINF = 1e18; const int INF = 0x3f3f3f3f; const int MAXN = 200050; int fa[MAXN]; int find(int x) { if (fa[x] == x) return x; else return fa[x] = find(fa[x]); } void solve(int T) { int n, m, u, v; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { fa[i] = i; } for (int i = 1; i <= m; i++) { scanf("%d %d", &u, &v); fa[find(u)] = v; } int ans = 0; for (int i = 1; i <= n; i++) { if (fa[i] == i) ans++; } printf("%d", ans - 1); } signed main() { int t = 1; // ios::sync_with_stdio(false); // cin.tie(0); // scanf("%d",&t); for (int i = 1; i <= t; i++) { solve(i); } }
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef unsigned long long ull; const long long MOD = 1e9 + 7; const long long LINF = 1e18; const int INF = 0x3f3f3f3f; const int MAXN = 200050; int fa[MAXN]; int find(int x) { if (fa[x] == x) return x; else return fa[x] = find(fa[x]); } void solve(int T) { int n, m, u, v; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { fa[i] = i; } for (int i = 1; i <= m; i++) { scanf("%d %d", &u, &v); fa[find(u)] = find(v); } int ans = 0; for (int i = 1; i <= n; i++) { if (fa[i] == i) ans++; } printf("%d", ans - 1); } signed main() { int t = 1; // ios::sync_with_stdio(false); // cin.tie(0); // scanf("%d",&t); for (int i = 1; i <= t; i++) { solve(i); } }
replace
27
28
27
28
0
p02536
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int father(vector<int> &f, int x) { return (f[x] == x) ? x : (father(f, f[x])); } int merge(vector<int> &f, int x, int y) { x = father(f, x); y = father(f, y); if (x == y) return 0; f[x] = y; return 1; } int main() { int n; int m; cin >> n >> m; vector<int> f(n + 1, 0); for (int i = 1; i <= n; i++) f[i] = i; int ans = n - 1; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (merge(f, x, y)) ans--; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int father(vector<int> &f, int x) { return (f[x] == x) ? x : (f[x] = father(f, f[x])); } int merge(vector<int> &f, int x, int y) { x = father(f, x); y = father(f, y); if (x == y) return 0; f[x] = y; return 1; } int main() { int n; int m; cin >> n >> m; vector<int> f(n + 1, 0); for (int i = 1; i <= n; i++) f[i] = i; int ans = n - 1; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (merge(f, x, y)) ans--; } cout << ans << endl; }
replace
3
4
3
4
TLE
p02536
C++
Runtime Error
#include <cstdio> #include <iostream> #include <vector> #define maxn 1000 using namespace std; vector<int> G[maxn]; bool vis[maxn] = {false}; int m, n; void dfs(int v) { vis[v] = true; for (int i = 0; i < G[v].size(); i++) { if (vis[G[v][i]] == false) { dfs(G[v][i]); } } } int main() { scanf("%d %d", &m, &n); for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); G[a].push_back(b); G[b].push_back(a); } int block = 0; for (int i = 1; i <= m; i++) { if (vis[i] == false) { dfs(i); block++; } } printf("%d", block - 1); return 0; }
#include <cstdio> #include <iostream> #include <vector> #define maxn 200000 using namespace std; vector<int> G[maxn]; bool vis[maxn] = {false}; int m, n; void dfs(int v) { vis[v] = true; for (int i = 0; i < G[v].size(); i++) { if (vis[G[v][i]] == false) { dfs(G[v][i]); } } } int main() { scanf("%d %d", &m, &n); for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); G[a].push_back(b); G[b].push_back(a); } int block = 0; for (int i = 1; i <= m; i++) { if (vis[i] == false) { dfs(i); block++; } } printf("%d", block - 1); return 0; }
replace
3
4
3
4
0
p02536
C++
Runtime Error
#line 1 "main.cpp" #define _CRT_SECURE_NO_WARNINGS #pragma GCC target("avx") #pragma GCC optimize("O3") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (lint)(n); i++) #define REP(i, n) for (int i = 1; i <= (lint)(n); i++) #define all(V) V.begin(), V.end() #define stackReplaceBegin(size) \ void *p = malloc(size); \ esp_new = (lint)p + size - 1; \ void *stack_extend_memory_ = malloc(size); \ void *stack_extend_origin_memory_; \ char *stack_extend_dummy_memory_ = (char *)alloca( \ (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \ *stack_extend_dummy_memory_ = 0; \ asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp" \ : "=b"(stack_extend_origin_memory_) \ : "a"((char *)stack_extend_memory_ + (size)-1024)); typedef long long lint; typedef unsigned long long ulint; typedef std::pair<int, int> P; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; lint esp_new; template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } inline lint gcd(lint a, lint b) { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; } bool isprime(lint n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } template <typename T> T mypow(T a, unsigned int b) { T res(1), now(a); while (b) { if (b & 1) res *= now; b >>= 1; now *= now; } return res; } lint modpow(lint a, unsigned int b, unsigned int mod) { lint res(1), now(a % mod); while (b) { if (b & 1) { res *= now; res %= mod; } b >>= 1; now *= now; now %= mod; } return res; } template <typename T> void printArray(std::vector<T> &vec) { rep(i, (int)vec.size() - 1) std::cout << vec[i] << " "; if (!vec.empty()) std::cout << vec.back() << std::endl; } template <typename T> void printArray(T l, T r) { T rprev = r; rprev--; for (T i = l; i != rprev; i++) { std::cout << *i << " "; } std::cout << *rprev << std::endl; } int n, m; std::vector<int> vec[30010]; bool used[30010]; void dfs(int node) { used[node] = true; for (int i : vec[node]) { if (!used[i]) dfs(i); } } int main() { std::cin >> n >> m; rep(i, m) { int a, b; std::cin >> a >> b; vec[a].emplace_back(b); vec[b].emplace_back(a); } int ans = 0; REP(i, n) { if (!used[i]) { dfs(i); ans++; } } std::cout << ans - 1 << std::endl; return 0; }
#line 1 "main.cpp" #define _CRT_SECURE_NO_WARNINGS #pragma GCC target("avx") #pragma GCC optimize("O3") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (lint)(n); i++) #define REP(i, n) for (int i = 1; i <= (lint)(n); i++) #define all(V) V.begin(), V.end() #define stackReplaceBegin(size) \ void *p = malloc(size); \ esp_new = (lint)p + size - 1; \ void *stack_extend_memory_ = malloc(size); \ void *stack_extend_origin_memory_; \ char *stack_extend_dummy_memory_ = (char *)alloca( \ (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16); \ *stack_extend_dummy_memory_ = 0; \ asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp" \ : "=b"(stack_extend_origin_memory_) \ : "a"((char *)stack_extend_memory_ + (size)-1024)); typedef long long lint; typedef unsigned long long ulint; typedef std::pair<int, int> P; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; lint esp_new; template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } inline lint gcd(lint a, lint b) { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; } bool isprime(lint n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } template <typename T> T mypow(T a, unsigned int b) { T res(1), now(a); while (b) { if (b & 1) res *= now; b >>= 1; now *= now; } return res; } lint modpow(lint a, unsigned int b, unsigned int mod) { lint res(1), now(a % mod); while (b) { if (b & 1) { res *= now; res %= mod; } b >>= 1; now *= now; now %= mod; } return res; } template <typename T> void printArray(std::vector<T> &vec) { rep(i, (int)vec.size() - 1) std::cout << vec[i] << " "; if (!vec.empty()) std::cout << vec.back() << std::endl; } template <typename T> void printArray(T l, T r) { T rprev = r; rprev--; for (T i = l; i != rprev; i++) { std::cout << *i << " "; } std::cout << *rprev << std::endl; } int n, m; std::vector<int> vec[100010]; bool used[100010]; void dfs(int node) { used[node] = true; for (int i : vec[node]) { if (!used[i]) dfs(i); } } int main() { std::cin >> n >> m; rep(i, m) { int a, b; std::cin >> a >> b; vec[a].emplace_back(b); vec[b].emplace_back(a); } int ans = 0; REP(i, n) { if (!used[i]) { dfs(i); ans++; } } std::cout << ans - 1 << std::endl; return 0; }
replace
125
127
125
127
0
p02536
C++
Runtime Error
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; #define fi first #define se second #define all(v) (v).begin(), (v).end() const ll inf = (1e18); // const ll mod=998244353; const ll mod = 1000000007; const vector<int> dy = {-1, 0, 1, 0}, dx = {0, -1, 0, 1}; ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } ll LCM(ll c, ll d) { return c / GCD(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } __init; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void debag(const vector<T> &a) { cerr << "debag :"; for (auto v : a) cerr << v << " "; cerr << "\n"; } template <class T> void print(const vector<T> &a) { for (auto v : a) cout << v << " "; cout << "\n"; } class UF { public: vector<int> par, num; int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); } explicit UF(int N) : par(N), num(N, 1) { iota(all(par), 0); } void unite(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (num[u] < num[v]) swap(u, v); num[u] += num[v]; par[v] = u; } bool same(int u, int v) { return find(u) == find(v); } bool ispar(int v) { return v = find(v); } int size(int v) { return num[find(v)]; } }; int main() { int n, m; cin >> n >> m; UF uf(n); int a, b, ans = 0; for (int i = 0; i < m; i++) { cin >> a >> b; uf.unite(a, b); } for (int i = 0; i < n; i++) { if (uf.find(i) != i) continue; ans++; } cout << ans - 1 << "\n"; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; #define fi first #define se second #define all(v) (v).begin(), (v).end() const ll inf = (1e18); // const ll mod=998244353; const ll mod = 1000000007; const vector<int> dy = {-1, 0, 1, 0}, dx = {0, -1, 0, 1}; ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } ll LCM(ll c, ll d) { return c / GCD(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } __init; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void debag(const vector<T> &a) { cerr << "debag :"; for (auto v : a) cerr << v << " "; cerr << "\n"; } template <class T> void print(const vector<T> &a) { for (auto v : a) cout << v << " "; cout << "\n"; } class UF { public: vector<int> par, num; int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); } explicit UF(int N) : par(N), num(N, 1) { iota(all(par), 0); } void unite(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (num[u] < num[v]) swap(u, v); num[u] += num[v]; par[v] = u; } bool same(int u, int v) { return find(u) == find(v); } bool ispar(int v) { return v = find(v); } int size(int v) { return num[find(v)]; } }; int main() { int n, m; cin >> n >> m; UF uf(n); int a, b, ans = 0; for (int i = 0; i < m; i++) { cin >> a >> b; a--; b--; uf.unite(a, b); } for (int i = 0; i < n; i++) { if (uf.find(i) != i) continue; ans++; } cout << ans - 1 << "\n"; }
insert
72
72
72
74
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (ll i = (a); i < (b); i++) #define REP(i, n) rep(i, 0, n) struct UnionFind { vector<int> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return (false); if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if (data[k] < 0) return (k); return (data[k] = find(data[k])); } int size(int k) { return (-data[find(k)]); } int cnt() { int cnt = 0; for (auto it : data) if (it < 0) cnt++; return cnt; } }; void solve() { int n, m; cin >> n >> m; UnionFind uf(n); REP(i, m) { int a, b; cin >> a >> b; uf.unite(a, b); } cout << uf.cnt() - 1 << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (ll i = (a); i < (b); i++) #define REP(i, n) rep(i, 0, n) struct UnionFind { vector<int> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return (false); if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if (data[k] < 0) return (k); return (data[k] = find(data[k])); } int size(int k) { return (-data[find(k)]); } int cnt() { int cnt = 0; for (auto it : data) if (it < 0) cnt++; return cnt; } }; void solve() { int n, m; cin >> n >> m; UnionFind uf(n); REP(i, m) { int a, b; cin >> a >> b; a--; b--; uf.unite(a, b); } cout << uf.cnt() - 1 << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
insert
46
46
46
48
0
p02536
C++
Runtime Error
#include <iostream> using namespace std; int dsu[100010]; int fnd(int x) { return (x == dsu[x]) ? x : dsu[x] = fnd(dsu[x]); } int merge(int u, int v) { dsu[fnd(u)] = fnd(v); } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { dsu[i] = i; } int x, y, cnt = 0; for (int i = 1; i <= m; i++) { cin >> x >> y; merge(x, y); } for (int i = 1; i < n; i++) { if (fnd(i) != fnd(i + 1)) { merge(i, i + 1); cnt++; } } cout << cnt; return 0; }
#include <iostream> using namespace std; int dsu[100010]; int fnd(int x) { return (x == dsu[x]) ? x : dsu[x] = fnd(dsu[x]); } void merge(int u, int v) { dsu[fnd(u)] = fnd(v); } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { dsu[i] = i; } int x, y, cnt = 0; for (int i = 1; i <= m; i++) { cin >> x >> y; merge(x, y); } for (int i = 1; i < n; i++) { if (fnd(i) != fnd(i + 1)) { merge(i, i + 1); cnt++; } } cout << cnt; return 0; }
replace
4
5
4
5
0
p02536
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; vector<vector<int>> michi(n, vector<int>(0)); vector<int> checked(n, 0); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; michi.at(a).push_back(b); michi.at(b).push_back(a); } long long ans = 0; int y = 1; int l = 0; while (y != 0) { queue<int> q; q.push(l); checked.at(l) = 1; while (q.size()) { int x = q.front(); q.pop(); for (int i = 0; i < (int)michi.at(x).size(); i++) { int u = michi.at(x).at(i); if (checked.at(u) == 1) { continue; } checked.at(u) = 1; q.push(u); } } y = 0; for (int i = 0; i < n; i++) { if (checked.at(i) == 0) { l = i; y = 1; // cout << l << endl; ans++; break; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; vector<vector<int>> michi(n, vector<int>(0)); vector<int> checked(n, 0); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; michi.at(a).push_back(b); michi.at(b).push_back(a); } long long ans = 0; int y = 1; int l = 0; while (y != 0) { queue<int> q; q.push(l); checked.at(l) = 1; while (q.size()) { int x = q.front(); q.pop(); for (int i = 0; i < (int)michi.at(x).size(); i++) { int u = michi.at(x).at(i); if (checked.at(u) == 1) { continue; } checked.at(u) = 1; q.push(u); } } y = 0; for (int i = l; i < n; i++) { if (checked.at(i) == 0) { l = i; y = 1; // cout << l << endl; ans++; break; } } } cout << ans << endl; }
replace
37
38
37
38
TLE
p02536
C++
Runtime Error
#include <cstdio> #include <vector> using namespace std; int a[10005]; vector<int> g[10005]; bool vis[10005]; void dfs(int x) { vis[x] = true; for (int i : g[x]) { if (!vis[i]) { dfs(i); } } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); } int cnt = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { cnt++; dfs(i); } } printf("%d\n", cnt - 1); return 0; }
#include <cstdio> #include <vector> using namespace std; const int N = 1e5 + 5; int a[N]; vector<int> g[N]; bool vis[N]; void dfs(int x) { vis[x] = true; for (int i : g[x]) { if (!vis[i]) { dfs(i); } } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); } int cnt = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { cnt++; dfs(i); } } printf("%d\n", cnt - 1); return 0; }
replace
5
8
5
9
0
p02536
C++
Runtime Error
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <typename M, typename N> istream &operator>>(istream &is, pair<M, N> &p) { is >> p.first >> p.second; return is; } template <typename M> istream &operator>>(istream &is, vector<M> &v) { for (auto &it : v) is >> it; return is; } template <typename M> istream &operator>>(istream &is, vector<vector<M>> &v) { for (auto &it : v) is >> it; return is; } template <typename M, typename N> ostream &operator<<(ostream &os, const pair<M, N> &p) { os << p.first << ' ' << p.second << '\n'; return os; } template <typename M> ostream &operator<<(ostream &os, const vector<M> &v) { for (auto it : v) os << it << ' '; cout << '\n'; return os; } template <typename M> ostream &operator<<(ostream &os, const vector<vector<M>> &v) { for (auto it : v) os << it; return os; } #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define mn(x) *min_element((x).begin(), (x).end()) #define mx(x) *max_element((x).begin(), (x).end()) #define acc(x) accumulate((x).begin(), (x).end(), 0ll) #define eb emplace_back #define el '\n' typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<pll> vpll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<bool> vb; typedef vector<string> vs; const ld pi = acos(-1); const ll inf = (ll)1e9 + 10; // const int mod=1000000007; const int mod = 998244353; int n, m, k; const int N = 2e5 + 10; vvi adj(N); vb mark(N); void dfs(int u, int p) { mark[u] = true; for (auto it : adj[u]) { if (it != p) { dfs(it, u); } } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0, u, v; i < m; i++) { cin >> u >> v; adj[--u].pb(--v); adj[v].pb(u); } int ans = -1; for (int i = 0; i < n; i++) { if (!mark[i]) { ans++; dfs(i, i); } } cout << ans << el; return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <typename M, typename N> istream &operator>>(istream &is, pair<M, N> &p) { is >> p.first >> p.second; return is; } template <typename M> istream &operator>>(istream &is, vector<M> &v) { for (auto &it : v) is >> it; return is; } template <typename M> istream &operator>>(istream &is, vector<vector<M>> &v) { for (auto &it : v) is >> it; return is; } template <typename M, typename N> ostream &operator<<(ostream &os, const pair<M, N> &p) { os << p.first << ' ' << p.second << '\n'; return os; } template <typename M> ostream &operator<<(ostream &os, const vector<M> &v) { for (auto it : v) os << it << ' '; cout << '\n'; return os; } template <typename M> ostream &operator<<(ostream &os, const vector<vector<M>> &v) { for (auto it : v) os << it; return os; } #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define mn(x) *min_element((x).begin(), (x).end()) #define mx(x) *max_element((x).begin(), (x).end()) #define acc(x) accumulate((x).begin(), (x).end(), 0ll) #define eb emplace_back #define el '\n' typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<pll> vpll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<bool> vb; typedef vector<string> vs; const ld pi = acos(-1); const ll inf = (ll)1e9 + 10; // const int mod=1000000007; const int mod = 998244353; int n, m, k; const int N = 2e5 + 10; vvi adj(N); vb mark(N); void dfs(int u, int p) { mark[u] = true; for (auto it : adj[u]) { if (it != p && !mark[it]) { dfs(it, u); } } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0, u, v; i < m; i++) { cin >> u >> v; adj[--u].pb(--v); adj[v].pb(u); } int ans = -1; for (int i = 0; i < n; i++) { if (!mark[i]) { ans++; dfs(i, i); } } cout << ans << el; return 0; }
replace
97
98
97
98
0
p02536
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <string> #include <vector> using namespace std; using ll = long long; 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につける } }; int main() { int N, M; cin >> N >> M; UnionFind tree(N); for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; tree.unite(x, y); // xの木とyの木を併合する } map<int, int> mp; for (int i = 0; i < N; i++) { int a = tree.root(i); mp[a]++; } int ans = 0; for (int i = 0; i < N; i++) { if (mp[i] >= 1) ans++; } cout << ans - 1 << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <string> #include <vector> using namespace std; using ll = long long; 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につける } }; int main() { int N, M; cin >> N >> M; UnionFind tree(N); for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--; y--; tree.unite(x, y); // xの木とyの木を併合する } map<int, int> mp; for (int i = 0; i < N; i++) { int a = tree.root(i); mp[a]++; } int ans = 0; for (int i = 0; i < N; i++) { if (mp[i] >= 1) ans++; } cout << ans - 1 << endl; return 0; }
insert
41
41
41
43
0
p02536
C++
Runtime Error
//@formatter:off #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); ++i) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define rep2(i, s, n) for (int i = int(s); i < int(n); ++i) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define eb emplace_back #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vector<ll>> #define vd vector<double> #define vvd vector<vector<double>> #define vs vector<string> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> #define vp vector<P> #define vvp vector<vector<P>> using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) { return is >> p.first >> p.second; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << '{' << p.first << "," << p.second << '}'; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) { is >> t; } return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '['; rep(i, v.size()) os << v[i] << (i == int(v.size() - 1) ? "" : ","); return os << ']'; } void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; } void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on class unionfind { int n; vector<int> par, rank; public: unionfind(int n) : n(n), par(n, -1), rank(n, 0) {} int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); }; bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; }; bool connected() { rep(i, n - 1) if (!same(i, i + 1)) return false; return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; unionfind uf(n); rep(i, m) { int a, b; cin >> a >> b; uf.merge(a, b); } int ans = -1; rep(i, n) { if (uf.root(i) == i) ans++; } cout << ans << endl; }
//@formatter:off #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); ++i) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define rep2(i, s, n) for (int i = int(s); i < int(n); ++i) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define eb emplace_back #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vector<ll>> #define vd vector<double> #define vvd vector<vector<double>> #define vs vector<string> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> #define vp vector<P> #define vvp vector<vector<P>> using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) { return is >> p.first >> p.second; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << '{' << p.first << "," << p.second << '}'; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) { is >> t; } return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '['; rep(i, v.size()) os << v[i] << (i == int(v.size() - 1) ? "" : ","); return os << ']'; } void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; } void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on class unionfind { int n; vector<int> par, rank; public: unionfind(int n) : n(n), par(n, -1), rank(n, 0) {} int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); }; bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; }; bool connected() { rep(i, n - 1) if (!same(i, i + 1)) return false; return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; unionfind uf(n); rep(i, m) { int a, b; cin >> a >> b; a--; b--; uf.merge(a, b); } int ans = -1; rep(i, n) { if (uf.root(i) == i) ans++; } cout << ans << endl; }
insert
112
112
112
114
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; vector<vector<int>> adj; vector<bool> vis; void dfs(int v, int p) { vis[v] = 1; for (auto x : adj[v]) { if (x != p) dfs(x, v); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; adj = vector<vector<int>>(n); vis = vector<bool>(n, false); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } int ans = 0; for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i, -1); ans++; } } cout << ans - 1; }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> adj; vector<bool> vis; void dfs(int v, int p) { vis[v] = 1; for (auto x : adj[v]) { if (x != p && !vis[x]) dfs(x, v); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; adj = vector<vector<int>>(n); vis = vector<bool>(n, false); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } int ans = 0; for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i, -1); ans++; } } cout << ans - 1; }
replace
10
11
10
11
0
p02536
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; int n, m, fa[1000001], x, y, ans; int father(int a) { if (fa[a] != a) fa[a] = father(a); return fa[a]; } int main() { scanf("%d%d", &n, &m); ans = n - 1; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); x = father(x); y = father(y); if (x != y) { fa[x] = y; ans--; } } printf("%d\n", ans); }
#include <cstdio> #include <iostream> using namespace std; int n, m, fa[1000001], x, y, ans; int father(int a) { if (fa[a] != a) fa[a] = father(fa[a]); return fa[a]; } int main() { scanf("%d%d", &n, &m); ans = n - 1; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); x = father(x); y = father(y); if (x != y) { fa[x] = y; ans--; } } printf("%d\n", ans); }
replace
6
7
6
7
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define fbo find_by_order #define ook order_of_key #define fi first #define se second #define long long long typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<int> vi; typedef vector<ii> vii; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; mt19937_64 // rng(std::chrono::system_clock::now().time_since_epoch().count()); // /mnt/c/Users/vince/Desktop/POST-IOI/ int par[100003]; int parent(int u) { return par[u] = (u == par[u]) ? u : parent(par[u]); } int gab(int u, int v) { par[parent(u)] = parent(v); } int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.in", "r", stdin); int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) par[i] = i; while (m--) { int u, v; scanf("%d %d", &u, &v); if (parent(u) == parent(v)) continue; gab(u, v); } set<int> st; for (int i = 1; i <= n; i++) st.insert(parent(i)); printf("%lu\n", st.size() - 1); }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define fbo find_by_order #define ook order_of_key #define fi first #define se second #define long long long typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<int> vi; typedef vector<ii> vii; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; mt19937_64 // rng(std::chrono::system_clock::now().time_since_epoch().count()); // /mnt/c/Users/vince/Desktop/POST-IOI/ int par[100003]; int parent(int u) { return par[u] = (u == par[u]) ? u : parent(par[u]); } void gab(int u, int v) { par[parent(u)] = parent(v); } int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.in", "r", stdin); int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) par[i] = i; while (m--) { int u, v; scanf("%d %d", &u, &v); if (parent(u) == parent(v)) continue; gab(u, v); } set<int> st; for (int i = 1; i <= n; i++) st.insert(parent(i)); printf("%lu\n", st.size() - 1); }
replace
24
25
24
25
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll mod = 1e9 + 7; struct UnionFind { vector<int> Parent; UnionFind(int n) { Parent = vector<int>(n, -1); } int root(int a) { if (Parent[a] < 0) return a; return root(Parent[a]); } int size(int a) { return -Parent[root(a)]; } bool connect(int a, int b) { if (root(a) == root(b)) return false; if (size(a) < size(b)) swap(a, b); Parent[root(a)] -= size(b); Parent[root(b)] = root(a); return true; } }; int main() { int n, m; cin >> n >> m; UnionFind field(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; field.connect(a, b); } int cnt = 0; for (int i = 0; i < n; ++i) { if (field.Parent[i] < 0) { cnt++; } } if (cnt == 0) cout << 0 << endl; else cout << cnt - 1 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll mod = 1e9 + 7; struct UnionFind { vector<int> Parent; UnionFind(int n) { Parent = vector<int>(n, -1); } int root(int a) { if (Parent[a] < 0) return a; return root(Parent[a]); } int size(int a) { return -Parent[root(a)]; } bool connect(int a, int b) { if (root(a) == root(b)) return false; if (size(a) < size(b)) swap(a, b); Parent[root(a)] -= size(b); Parent[root(b)] = root(a); return true; } }; int main() { int n, m; cin >> n >> m; UnionFind field(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--; b--; field.connect(a, b); } int cnt = 0; for (int i = 0; i < n; ++i) { if (field.Parent[i] < 0) { cnt++; } } if (cnt == 0) cout << 0 << endl; else cout << cnt - 1 << endl; }
insert
35
35
35
37
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, m, cnt, father[N]; int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) father[i] = i; for (int i = 1, x, y; i <= m; i++) { scanf("%d%d", &x, &y); father[find(x)] = y; } for (int i = 1; i <= n; i++) if (find(i) == i) cnt++; printf("%d\n", cnt - 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, m, cnt, father[N]; int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) father[i] = i; for (int i = 1, x, y; i <= m; i++) { scanf("%d%d", &x, &y); father[find(x)] = find(y); } for (int i = 1; i <= n; i++) if (find(i) == i) cnt++; printf("%d\n", cnt - 1); return 0; }
replace
14
15
14
15
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; vector<int> cities[100000]; bool visited[100000]; void dfs(int i) { visited[i] = true; for (int x : cities[i]) { if (not(visited[x])) { dfs(x); } } } int main() { int n, m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; cities[a].push_back(b); cities[b].push_back(a); } int ans = 0; for (int i = 1; i <= n; i++) { if (not(visited[i])) { ans++; dfs(i); } } cout << ans - 1; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; vector<int> cities[100005]; bool visited[100005]; void dfs(int i) { visited[i] = true; for (int x : cities[i]) { if (not(visited[x])) { dfs(x); } } } int main() { int n, m; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; cities[a].push_back(b); cities[b].push_back(a); } int ans = 0; for (int i = 1; i <= n; i++) { if (not(visited[i])) { ans++; dfs(i); } } cout << ans - 1; }
replace
5
7
5
7
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int cities, roads, a, b, group; vector<int> adj[10100]; bool visited[100100]; void dfs(int c) { if (!visited[c]) { visited[c] = true; for (int i = 0; i < adj[c].size(); i++) { int d = adj[c][i]; dfs(d); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> cities >> roads; for (int i = 1; i <= roads; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= cities; i++) { if (!visited[i]) group++; dfs(i); } cout << group - 1 << endl; }
#include <bits/stdc++.h> using namespace std; int cities, roads, a, b, group; vector<int> adj[100100]; bool visited[100100]; void dfs(int c) { if (!visited[c]) { visited[c] = true; for (int i = 0; i < adj[c].size(); i++) { int d = adj[c][i]; dfs(d); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> cities >> roads; for (int i = 1; i <= roads; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= cities; i++) { if (!visited[i]) group++; dfs(i); } cout << group - 1 << endl; }
replace
4
5
4
5
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, m, a, b, blocks; vector<int> toc; vector<vector<int>> rd; bool used[100009]; void DFS(int i) { used[i] = 1; for (int j = 0; j < toc[i]; j++) { DFS(rd[i][j]); } } int main() { cin >> n >> m; rd.resize(n); toc.resize(n); for (int i = 0; i < m; i++) { cin >> a >> b; rd[a - 1].push_back(b - 1); rd[b - 1].push_back(a - 1); toc[a - 1]++; toc[b - 1]++; } for (int i = 0; i < n; i++) { if (used[i]) continue; blocks++; DFS(i); } cout << blocks - 1; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a, b, blocks; vector<int> toc; vector<vector<int>> rd; bool used[100009]; void DFS(int i) { used[i] = 1; for (int j = 0; j < toc[i]; j++) { if (used[rd[i][j]]) continue; DFS(rd[i][j]); } } int main() { cin >> n >> m; rd.resize(n); toc.resize(n); for (int i = 0; i < m; i++) { cin >> a >> b; rd[a - 1].push_back(b - 1); rd[b - 1].push_back(a - 1); toc[a - 1]++; toc[b - 1]++; } for (int i = 0; i < n; i++) { if (used[i]) continue; blocks++; DFS(i); } cout << blocks - 1; return 0; }
insert
9
9
9
11
-11
p02536
C++
Runtime Error
#include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> vi(n, 0); vector<vector<int>> v(n, vector<int>(n, -1)); map<int, vector<int>> mp; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; mp[x].push_back(y); mp[y].push_back(x); } int cnt1 = 1; for (int i = 0; i < n; i++) { if (vi[i] != 0) continue; cnt1++; queue<int> q; q.push(i + 1); while (!q.empty()) { int size = q.size(); for (int j = 0; j < size; j++) { int el = q.front(); q.pop(); vi[el - 1] = cnt1; for (int ii = 0; ii < mp[el].size(); ii++) { if (vi[mp[el][ii] - 1] == 0) q.push(mp[el][ii]); } } } } set<int> st; for (auto el : vi) st.insert(el); cout << st.size() - 1 << "\n"; return 0; }
#include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> vi(n, 0); map<int, vector<int>> mp; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; mp[x].push_back(y); mp[y].push_back(x); } int cnt1 = 1; for (int i = 0; i < n; i++) { if (vi[i] != 0) continue; cnt1++; queue<int> q; q.push(i + 1); while (!q.empty()) { int size = q.size(); for (int j = 0; j < size; j++) { int el = q.front(); q.pop(); vi[el - 1] = cnt1; for (int ii = 0; ii < mp[el].size(); ii++) { if (vi[mp[el][ii] - 1] == 0) q.push(mp[el][ii]); } } } } set<int> st; for (auto el : vi) st.insert(el); cout << st.size() - 1 << "\n"; return 0; }
delete
11
12
11
11
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; vector<int> rec; int find(int u) { return u == rec[u] ? u : rec[u] = find(rec[u]); } int main() { int n, m; cin >> n >> m; rec.resize(n); for (int i = 0; i < n; ++i) { rec[i] = i; } int c = n - 1; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; int _u = find(u), _v = find(v); if (_u != _v) { rec[_v] = _u; --c; } } cout << c << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> rec; int find(int u) { return u == rec[u] ? u : rec[u] = find(rec[u]); } int main() { int n, m; cin >> n >> m; rec.resize(n); for (int i = 0; i < n; ++i) { rec[i] = i; } int c = n - 1; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u; --v; int _u = find(u), _v = find(v); if (_u != _v) { rec[_v] = _u; --c; } } cout << c << endl; return 0; }
insert
15
15
15
17
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < n; i++) class UnionFind { vector<int> par; vector<int> siz; public: UnionFind(int n) : par(n), siz(n, 1) { for (int i = 0; i < n; i++) { par[i] = i; } } int root(int x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool same(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) { return 0; } if (siz[x] == siz[y]) { swap(x, y); } siz[x] += siz[y]; par[y] = x; return 1; } int size(int x) { return siz[root(x)]; } }; int main() { int n, q, a, b; cin >> n >> q; int ans = n - 1; UnionFind uf(n); rep(i, q) { cin >> a >> b; if (uf.unite(a, b)) { ans--; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < n; i++) class UnionFind { vector<int> par; vector<int> siz; public: UnionFind(int n) : par(n), siz(n, 1) { for (int i = 0; i < n; i++) { par[i] = i; } } int root(int x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool same(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) { return 0; } if (siz[x] == siz[y]) { swap(x, y); } siz[x] += siz[y]; par[y] = x; return 1; } int size(int x) { return siz[root(x)]; } }; int main() { int n, q, a, b; cin >> n >> q; int ans = n - 1; UnionFind uf(n); rep(i, q) { cin >> a >> b; a--; b--; if (uf.unite(a, b)) { ans--; } } cout << ans << endl; }
replace
45
46
45
47
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, x, n) for (int i = x; i < (int)n; i++) #define rep(i, n) REP(i, 0, n) #define sp(p) cout << setprecision(16) << fixed << p << endl; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define SORT(a) sort(all(a)) #define RSORT(a) sort(rall(a)) #define UNIQ(a) unique(all(a)) #define UNIQNUM(a) UNIQ(a) - a.begin() #define UNIQIT(a) a.erase(UNIQ(a), a.end()); #define VOUT(v, i) \ rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? "\n" : " "); #define vout(v) VOUT(v, z); #define vdbg(v, i) \ cout << #v << ": "; \ for (int i = 0; i < (int)v.size(); i++) { \ cout << v[i] << " "; \ } \ cout << "\n"; #define vmin(v) *min_element(all(v)) #define vmax(v) *max_element(all(v)) #define vsum(v) accumulate(all(v), 0LL) #define MOUT(m, r, c) \ rep(i, r) { \ rep(j, c) { cout << m[i][j] << " "; } \ cout << endl; \ } #define mout(m) MOUT(m, m.size(), m[0].size()) #define debg(a) cout << #a << ": " << a << endl; #define out(a) debg(a); #define aout(a) \ for (auto &e : a) \ cout << e << " "; #define show(a) \ for (cont & y : a) { \ for (cont & x : y) { \ cout << x << " "; \ } \ cout << endl; \ } #define digit(a) to_string(a).length(); // template<class T>inline int out(const T &t){ print(t); putchar('\n'); return // 0; } template<class T>inline T gcd(T a,T b){if(b==0)return a; // return(gcd(b,a%b));} template<class T>inline T lcm(T a,T b){return // a/gcd(a,b)*b;} bool is_palindrome(string s) { return s == string(s.rbegin(), s.rend()); } #define popcount __builtin_popcount typedef long long ll; typedef long double ld; typedef pair<ll, ll> P; typedef vector<ll> V; typedef vector<vector<ll>> VV; // const long long MOD=1000000007; const long long INF = 1e18; #define EPS (1e-7) #define PI (acos(-1)) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } // Implement (union by size) + (path compression) // Reference: // Zvi Galil and Giuseppe F. Italiano, // Data structures and algorithms for disjoint set union problems struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; int main() { long long N; scanf("%lld", &N); long long M; scanf("%lld", &M); std::vector<long long> A(M); std::vector<long long> B(M); for (int i = 0; i < M; i++) { scanf("%lld", &A[i]); scanf("%lld", &B[i]); } dsu d(N); for (long long i = 0; i < M; i++) { d.merge(A[i], B[i]); } vector<vector<int>> v = d.groups(); // out(v.size()); cout << v.size() - 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, x, n) for (int i = x; i < (int)n; i++) #define rep(i, n) REP(i, 0, n) #define sp(p) cout << setprecision(16) << fixed << p << endl; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define SORT(a) sort(all(a)) #define RSORT(a) sort(rall(a)) #define UNIQ(a) unique(all(a)) #define UNIQNUM(a) UNIQ(a) - a.begin() #define UNIQIT(a) a.erase(UNIQ(a), a.end()); #define VOUT(v, i) \ rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? "\n" : " "); #define vout(v) VOUT(v, z); #define vdbg(v, i) \ cout << #v << ": "; \ for (int i = 0; i < (int)v.size(); i++) { \ cout << v[i] << " "; \ } \ cout << "\n"; #define vmin(v) *min_element(all(v)) #define vmax(v) *max_element(all(v)) #define vsum(v) accumulate(all(v), 0LL) #define MOUT(m, r, c) \ rep(i, r) { \ rep(j, c) { cout << m[i][j] << " "; } \ cout << endl; \ } #define mout(m) MOUT(m, m.size(), m[0].size()) #define debg(a) cout << #a << ": " << a << endl; #define out(a) debg(a); #define aout(a) \ for (auto &e : a) \ cout << e << " "; #define show(a) \ for (cont & y : a) { \ for (cont & x : y) { \ cout << x << " "; \ } \ cout << endl; \ } #define digit(a) to_string(a).length(); // template<class T>inline int out(const T &t){ print(t); putchar('\n'); return // 0; } template<class T>inline T gcd(T a,T b){if(b==0)return a; // return(gcd(b,a%b));} template<class T>inline T lcm(T a,T b){return // a/gcd(a,b)*b;} bool is_palindrome(string s) { return s == string(s.rbegin(), s.rend()); } #define popcount __builtin_popcount typedef long long ll; typedef long double ld; typedef pair<ll, ll> P; typedef vector<ll> V; typedef vector<vector<ll>> VV; // const long long MOD=1000000007; const long long INF = 1e18; #define EPS (1e-7) #define PI (acos(-1)) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } // Implement (union by size) + (path compression) // Reference: // Zvi Galil and Giuseppe F. Italiano, // Data structures and algorithms for disjoint set union problems struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; int main() { long long N; scanf("%lld", &N); long long M; scanf("%lld", &M); std::vector<long long> A(M); std::vector<long long> B(M); for (int i = 0; i < M; i++) { scanf("%lld", &A[i]); scanf("%lld", &B[i]); } dsu d(N); for (long long i = 0; i < M; i++) { d.merge(A[i] - 1, B[i] - 1); } vector<vector<int>> v = d.groups(); // out(v.size()); cout << v.size() - 1 << endl; return 0; }
replace
153
154
153
154
0
p02536
C++
Runtime Error
// In the name of god #include <bits/stdc++.h> #define F first #define S second #define pb push_back #define all(x) x.begin(), x.end() #define use_file \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); using namespace std; using ll = long long; const ll N = 1e5, inf = 4e18, mod = 1e9 + 7; ll n, m, ans; vector<ll> adj[N]; bool mark[N]; void dfs(ll u) { mark[u] = 1; for (int j : adj[u]) { if (!mark[j]) dfs(j); } } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { ll u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } for (int i = 1; i <= n; i++) { if (!mark[i]) { dfs(i); ans++; } } cout << ans - 1; cout << endl; }
// In the name of god #include <bits/stdc++.h> #define F first #define S second #define pb push_back #define all(x) x.begin(), x.end() #define use_file \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); using namespace std; using ll = long long; const ll N = 1e5 + 5, inf = 4e18, mod = 1e9 + 7; ll n, m, ans; vector<ll> adj[N]; bool mark[N]; void dfs(ll u) { mark[u] = 1; for (int j : adj[u]) { if (!mark[j]) dfs(j); } } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { ll u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } for (int i = 1; i <= n; i++) { if (!mark[i]) { dfs(i); ans++; } } cout << ans - 1; cout << endl; }
replace
11
12
11
12
0
p02536
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; void dfs(int node, vector<vector<int>> arr, char c[]) { if (c[node] == 'g') { return; } c[node] = 'g'; for (int i = 0; i < arr[node].size(); i++) { if (c[arr[node][i]] == 'w') { dfs(arr[node][i], arr, c); } } } int main() { /* #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); //for writing output to output.txt freopen("output.txt", "w", stdout); #endif */ int n, m; cin >> n >> m; vector<vector<int>> arr(n + 1); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; arr[a].push_back(b); arr[b].push_back(a); } char *c = new char[n + 1]; for (int i = 0; i <= n; i++) { c[i] = 'w'; } int ans = 0; for (int i = 1; i <= n; i++) { if (c[i] == 'w') { dfs(i, arr, c); if (i == 1) { continue; } ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void dfs(int node, vector<vector<int>> &arr, char c[]) { if (c[node] == 'g') { return; } c[node] = 'g'; for (int i = 0; i < arr[node].size(); i++) { if (c[arr[node][i]] == 'w') { dfs(arr[node][i], arr, c); } } } int main() { /* #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); //for writing output to output.txt freopen("output.txt", "w", stdout); #endif */ int n, m; cin >> n >> m; vector<vector<int>> arr(n + 1); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; arr[a].push_back(b); arr[b].push_back(a); } char *c = new char[n + 1]; for (int i = 0; i <= n; i++) { c[i] = 'w'; } int ans = 0; for (int i = 1; i <= n; i++) { if (c[i] == 'w') { dfs(i, arr, c); if (i == 1) { continue; } ans++; } } cout << ans << endl; return 0; }
replace
3
4
3
4
TLE
p02536
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define vv(T) vector<vector<T>> #define coa \ cout << ans << endl; \ return 0; using namespace std; using ll = long long; template <typename T> using vec = vector<T>; template <typename T> using vvec = vector<vector<T>>; using qint = queue<int>; using sint = stack<int>; using pii = pair<int, int>; class UnionFind { vector<int> parent; // UnionFind木の実装 public: UnionFind(int N) : parent(N, -1) {} // コンストラクタ int root(int x) { // 根を求める。 if (parent[x] < 0) return x; return parent[x] = root(parent[x]); } void unite(int x, int y) { // xとyの木を結合する。 int rx = root(x); int ry = root(y); if (rx == ry) return; if (-parent[rx] > -parent[ry]) { // 小さい方の木を大きい方につなげる。 parent[rx] += parent[ry]; parent[ry] = rx; } else { parent[ry] += parent[rx]; parent[rx] = ry; } return; } bool isSame(int x, int y) { // 同じ木にあるか? return root(x) == root(y); } int size(int x) { // xの木に属する要素はいくつか? return -parent[root(x)]; } }; int main() { int n, m; cin >> n >> m; UnionFind uf(n); rep(i, m) { int a, b; cin >> a >> b; uf.unite(a, b); } int ans = -1; rep(i, n) { int ri = uf.root(i); if (ri == i) ans++; } coa; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define vv(T) vector<vector<T>> #define coa \ cout << ans << endl; \ return 0; using namespace std; using ll = long long; template <typename T> using vec = vector<T>; template <typename T> using vvec = vector<vector<T>>; using qint = queue<int>; using sint = stack<int>; using pii = pair<int, int>; class UnionFind { vector<int> parent; // UnionFind木の実装 public: UnionFind(int N) : parent(N, -1) {} // コンストラクタ int root(int x) { // 根を求める。 if (parent[x] < 0) return x; return parent[x] = root(parent[x]); } void unite(int x, int y) { // xとyの木を結合する。 int rx = root(x); int ry = root(y); if (rx == ry) return; if (-parent[rx] > -parent[ry]) { // 小さい方の木を大きい方につなげる。 parent[rx] += parent[ry]; parent[ry] = rx; } else { parent[ry] += parent[rx]; parent[rx] = ry; } return; } bool isSame(int x, int y) { // 同じ木にあるか? return root(x) == root(y); } int size(int x) { // xの木に属する要素はいくつか? return -parent[root(x)]; } }; int main() { int n, m; cin >> n >> m; UnionFind uf(n); rep(i, m) { int a, b; cin >> a >> b; a--; b--; uf.unite(a, b); } int ans = -1; rep(i, n) { int ri = uf.root(i); if (ri == i) ans++; } coa; }
insert
60
60
60
62
0
p02536
C++
Runtime Error
/* Author: Ritik Patel */ //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& STL DEBUGGER //&&&&&&&&&&&&&&&&&&&&&&&&&&& // #define _GLIBCXX_DEBUG // Iterator safety; out-of-bounds access for // Containers, etc. #pragma GCC optimize "trapv" // abort() on (signed) integer // overflow. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& LIBRARIES &&&&&&&&&&&&&&&&&&&&&&&&&&& #include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update template<typename T, typename V = __gnu_pbds::null_type> using ordered_set = __gnu_pbds::tree<T, V, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; */ // find_by_order()->returns an iterator to the k-th largest element(0-based // indexing) order_of_key()->Number of items that are strictly smaller than our // item //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DEFINES &&&&&&&&&&&&&&&&&&&&&&&&&&& #define int long long int // #define ll long long int #define all(i) i.begin(), i.end() #define sz(a) (int)a.size() // #define ld long double // const ld PI = 3.141592; const int dx4[4] = {0, 1, 0, -1}; const int dy4[4] = {-1, 0, 1, 0}; const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DEBUG &&&&&&&&&&&&&&&&&&&&&&&&&&& #define XOX vector<string> vec_splitter(string s) { for (char &c : s) c = c == ',' ? ' ' : c; stringstream ss; ss << s; vector<string> res; for (string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if (idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #ifdef XOX #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) #else #define debug(...) 42 #endif //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& CODE &&&&&&&&&&&&&&&&&&&&&&&&&&& int ans = 0; const int MAXN = 1e4 + 5; vector<bool> vis(MAXN, false); vector<bool> considered(MAXN, false); vector<int> g[MAXN]; void dfs(int u) { vis[u] = true; for (auto &v : g[u]) { if (!vis[v]) { dfs(v); } } } void solve() { int N, M; cin >> N >> M; for (int i = 0; i < M; ++i) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); considered[x] = true; considered[y] = true; } // ans is no of cycles int add = 0; for (int i = 1; i <= N; ++i) { if (!considered[i]) { ++add; continue; } if (!vis[i]) { dfs(i); ++ans; } } cout << ans + add - 1 << '\n'; } int32_t main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; // cin >> T; for (int i = 1; i <= T; ++i) { // cout << "Case #" << i << ": "; solve(); } return 0; } /* Sample inp */
/* Author: Ritik Patel */ //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& STL DEBUGGER //&&&&&&&&&&&&&&&&&&&&&&&&&&& // #define _GLIBCXX_DEBUG // Iterator safety; out-of-bounds access for // Containers, etc. #pragma GCC optimize "trapv" // abort() on (signed) integer // overflow. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& LIBRARIES &&&&&&&&&&&&&&&&&&&&&&&&&&& #include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update template<typename T, typename V = __gnu_pbds::null_type> using ordered_set = __gnu_pbds::tree<T, V, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; */ // find_by_order()->returns an iterator to the k-th largest element(0-based // indexing) order_of_key()->Number of items that are strictly smaller than our // item //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DEFINES &&&&&&&&&&&&&&&&&&&&&&&&&&& #define int long long int // #define ll long long int #define all(i) i.begin(), i.end() #define sz(a) (int)a.size() // #define ld long double // const ld PI = 3.141592; const int dx4[4] = {0, 1, 0, -1}; const int dy4[4] = {-1, 0, 1, 0}; const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DEBUG &&&&&&&&&&&&&&&&&&&&&&&&&&& #define XOX vector<string> vec_splitter(string s) { for (char &c : s) c = c == ',' ? ' ' : c; stringstream ss; ss << s; vector<string> res; for (string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if (idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #ifdef XOX #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) #else #define debug(...) 42 #endif //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& CODE &&&&&&&&&&&&&&&&&&&&&&&&&&& int ans = 0; const int MAXN = 1e5 + 5; vector<bool> vis(MAXN, false); vector<bool> considered(MAXN, false); vector<int> g[MAXN]; void dfs(int u) { vis[u] = true; for (auto &v : g[u]) { if (!vis[v]) { dfs(v); } } } void solve() { int N, M; cin >> N >> M; for (int i = 0; i < M; ++i) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); considered[x] = true; considered[y] = true; } // ans is no of cycles int add = 0; for (int i = 1; i <= N; ++i) { if (!considered[i]) { ++add; continue; } if (!vis[i]) { dfs(i); ++ans; } } cout << ans + add - 1 << '\n'; } int32_t main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; // cin >> T; for (int i = 1; i <= T; ++i) { // cout << "Case #" << i << ": "; solve(); } return 0; } /* Sample inp */
replace
77
78
77
78
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // using Graph = vector<vector<int>>; struct UnionFind { vector<int> par; vector<int> rank; int num; UnionFind(int N) { num = N; rank.resize(N); par.resize(N); for (int i = 0; i < N; i++) { rank[i] = 0; par[i] = i; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] > rank[y]) { swap(x, y); } if (rank[x] == rank[y]) rank[y]++; par[x] = y; num--; } bool same(int x, int y) { return find(x) == find(y); } int count() const { return num; } }; int main() { int n, m; cin >> n >> m; UnionFind uf(n); int ans = n - 1; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; uf.unite(a, b); } cout << uf.count() - 1 << endl; }
#include <bits/stdc++.h> using namespace std; // using Graph = vector<vector<int>>; struct UnionFind { vector<int> par; vector<int> rank; int num; UnionFind(int N) { num = N; rank.resize(N); par.resize(N); for (int i = 0; i < N; i++) { rank[i] = 0; par[i] = i; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] > rank[y]) { swap(x, y); } if (rank[x] == rank[y]) rank[y]++; par[x] = y; num--; } bool same(int x, int y) { return find(x) == find(y); } int count() const { return num; } }; int main() { int n, m; cin >> n >> m; UnionFind uf(n); int ans = n - 1; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; uf.unite(a, b); } cout << uf.count() - 1 << endl; }
insert
48
48
48
50
0
p02536
C++
Time Limit Exceeded
#include <iostream> #include <map> #include <string> using namespace std; int par[100000]; // 添え字として0~99999を許容する int root(int i) { if (par[i] == i) { return i; } return root(par[i]); } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { par[i] = i; } for (int i = 0; i < m; ++i) { int k1, k2; cin >> k1 >> k2; k1--; k2--; par[root(k1)] = root(k2); } int cnt = 0; for (int i = 0; i < n; ++i) { if (root(i) == i) { cnt++; } } cout << (cnt - 1); }
#include <iostream> #include <map> #include <string> using namespace std; int par[100000]; // 添え字として0~99999を許容する int root(int i) { if (par[i] == i) { return i; } return par[i] = root(par[i]); } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { par[i] = i; } for (int i = 0; i < m; ++i) { int k1, k2; cin >> k1 >> k2; k1--; k2--; par[root(k1)] = root(k2); } int cnt = 0; for (int i = 0; i < n; ++i) { if (root(i) == i) { cnt++; } } cout << (cnt - 1); }
replace
11
12
11
12
TLE
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define G getline #define SZ size() #define C clear() #define B begin() #define F front() #define T top() #define E end() #define EM empty() #define V vector #define Q queue #define DQ deque #define PQ priority_queue #define ST stack #define FI first #define SE second #define PI acos(-1) #define PS push #define PP pop() #define PSF push_front #define PSB push_back #define PPF pop_front() #define PPB pop_back() #define LL long long #define ULL unsigned long long #define PII pair<int, int> #define PSI pair<string, int> #define PIS pair<int, string> #define PLI pair<LL, int> #define PLL pair<LL, LL> #define MII map<int, int> #define MSI map<string, int> #define MIS map<int, string> #define MLI map<LL, int> #define FAST() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) // int dx[]={-1,1,0,0}; // int dy[]={0,0,-1,1}; // int dx[]={-1,0,1,-1,1,-1,0,1}; // int dy[]={1,1,1,0,0,-1,-1,-1}; const int N = 1e5 + 5; vector<int> adj[N]; bool visited[N]; void DFS(int cur, int par) { visited[cur] = 1; for (int x : adj[cur]) { if (x ^ par) DFS(x, cur); } } int main() { FAST(); int i, m, n, x, y, ans = 0; cin >> n >> m; while (m--) { cin >> x >> y; adj[x].emplace_back(y); adj[y].emplace_back(x); } for (i = 1; i <= n; i++) { if (!visited[i]) { ++ans; DFS(i, i); } } cout << ans - 1; return 0; }
#include <bits/stdc++.h> using namespace std; #define G getline #define SZ size() #define C clear() #define B begin() #define F front() #define T top() #define E end() #define EM empty() #define V vector #define Q queue #define DQ deque #define PQ priority_queue #define ST stack #define FI first #define SE second #define PI acos(-1) #define PS push #define PP pop() #define PSF push_front #define PSB push_back #define PPF pop_front() #define PPB pop_back() #define LL long long #define ULL unsigned long long #define PII pair<int, int> #define PSI pair<string, int> #define PIS pair<int, string> #define PLI pair<LL, int> #define PLL pair<LL, LL> #define MII map<int, int> #define MSI map<string, int> #define MIS map<int, string> #define MLI map<LL, int> #define FAST() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) // int dx[]={-1,1,0,0}; // int dy[]={0,0,-1,1}; // int dx[]={-1,0,1,-1,1,-1,0,1}; // int dy[]={1,1,1,0,0,-1,-1,-1}; const int N = 1e5 + 5; vector<int> adj[N]; bool visited[N]; void DFS(int cur, int par) { if (visited[cur]) return; visited[cur] = 1; for (int x : adj[cur]) { if (x ^ par) DFS(x, cur); } } int main() { FAST(); int i, m, n, x, y, ans = 0; cin >> n >> m; while (m--) { cin >> x >> y; adj[x].emplace_back(y); adj[y].emplace_back(x); } for (i = 1; i <= n; i++) { if (!visited[i]) { ++ans; DFS(i, i); } } cout << ans - 1; return 0; }
insert
52
52
52
54
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long vector<int> graph[1000005]; int color[1000005]; int dfs(int v) { color[v] = 1; for (auto i : graph[v]) { if (color[i] == 0) dfs(i); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } dfs(1); int ans = 0; for (int i = 1; i <= n; i++) { if (color[i] == 0) { ans++; dfs(i); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long vector<int> graph[1000005]; int color[1000005]; int dfs(int v) { color[v] = 1; for (auto i : graph[v]) { if (color[i] == 0) dfs(i); } return 0; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } dfs(1); int ans = 0; for (int i = 1; i <= n; i++) { if (color[i] == 0) { ans++; dfs(i); } } cout << ans << endl; }
insert
13
13
13
14
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = unsigned long long int; vector<int> adj[30002]; bool v[30002]; int tam; void dfs(int i) { if (v[i]) return; v[i] = true; tam++; for (int j = 0; j < adj[i].size(); j++) { dfs(adj[i][j]); } } int main() { int n, m; cin >> n >> m; set<int> node; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); node.insert(a); node.insert(b); } tam = 0; int grafos = 0; for (int i = 1; i <= n; i++) { if (!v[i] && node.count(i)) { dfs(i); grafos++; } } // cout<<grafos<<" "<<tam<<endl; int ans = 0; ans += (grafos - 1); ans += (n - tam); cout << ans; }
#include <bits/stdc++.h> using namespace std; using ll = unsigned long long int; vector<int> adj[100002]; bool v[100002]; int tam; void dfs(int i) { if (v[i]) return; v[i] = true; tam++; for (int j = 0; j < adj[i].size(); j++) { dfs(adj[i][j]); } } int main() { int n, m; cin >> n >> m; set<int> node; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); node.insert(a); node.insert(b); } tam = 0; int grafos = 0; for (int i = 1; i <= n; i++) { if (!v[i] && node.count(i)) { dfs(i); grafos++; } } // cout<<grafos<<" "<<tam<<endl; int ans = 0; ans += (grafos - 1); ans += (n - tam); cout << ans; }
replace
3
5
3
5
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 998244353; int modP(int a, int b) { if (a == 1) return 1; if (b == 0) return 1; if (b % 2 == 0) { a *= a; a = a % mod; return modP(a, b / 2) % mod; } return a * modP(a, b - 1) % mod; } int add(int a, int b) { return (a + b) % mod; } int inv(int a, int b) { return a * modP(b, mod - 2) % mod; } int fact[(int)1e5]; int fac(int n) { if (n <= 1) return fact[n] = 1; if (fact[n] != 0) return fact[n]; return fact[n] = n * fac(n - 1); } int Cnk(int n, int k) { return fac(n) / fac(k) / fac(n - k); } vector<int> g[(int)2e5]; void dfs(int s, vector<bool> &vis, int p) { vis[s] = true; for (int i : g[s]) { if (i == p) continue; dfs(i, vis, s); } } signed main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } int cnt = 0; vector<bool> vis(n, false); for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i, vis, -1); cnt++; } } cout << cnt - 1 << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 998244353; int modP(int a, int b) { if (a == 1) return 1; if (b == 0) return 1; if (b % 2 == 0) { a *= a; a = a % mod; return modP(a, b / 2) % mod; } return a * modP(a, b - 1) % mod; } int add(int a, int b) { return (a + b) % mod; } int inv(int a, int b) { return a * modP(b, mod - 2) % mod; } int fact[(int)1e5]; int fac(int n) { if (n <= 1) return fact[n] = 1; if (fact[n] != 0) return fact[n]; return fact[n] = n * fac(n - 1); } int Cnk(int n, int k) { return fac(n) / fac(k) / fac(n - k); } vector<int> g[(int)2e5]; void dfs(int s, vector<bool> &vis, int p) { vis[s] = true; for (int i : g[s]) { if (i == p || vis[i]) continue; dfs(i, vis, s); } } signed main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } int cnt = 0; vector<bool> vis(n, false); for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i, vis, -1); cnt++; } } cout << cnt - 1 << endl; }
replace
32
33
32
33
0
p02536
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> using namespace std; const int maxn = 4e6 + 10; vector<int> G[maxn]; bool vis[maxn]; void dfs(int u, int fa) { vis[u] = true; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (v != fa) { dfs(v, u); } } } void solved() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } int ans = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i, -1), ans++; } cout << ans - 1 << endl; } int main() { solved(); return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> using namespace std; const int maxn = 4e6 + 10; vector<int> G[maxn]; bool vis[maxn]; void dfs(int u, int fa) { vis[u] = true; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (v != fa && !vis[v]) { dfs(v, u); } } } void solved() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } int ans = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i, -1), ans++; } cout << ans - 1 << endl; } int main() { solved(); return 0; }
replace
16
17
16
17
127
/tmp/f0821b5e-faa7-4bee-af42-ac6b824f4e76.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p02536
C++
Runtime Error
#include <bits/stdc++.h> #include <map> #include <set> #include <unordered_map> #include <vector> #define lli long long int #define loop(i, x, n) for (int i = x; i < n; i++) #define loopr(i, x, n) for (int i = n - 1; i >= x; i--) #define st set<lli> #define v vector<lli> #define um unordered_map<lli, lli> #define vpl vector<pair<lli, lli>> #define llu long long unsigned #define ms muliset<int, greater<int>> #define ums unordered_map<string, lli> #define pb push_back #define time \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define test \ lli t; \ cin >> t; \ while (t--) using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } v adj[100001]; lli vist[10001]; void dfs(lli node) { vist[node] = 1; for (lli child : adj[node]) if (!vist[child]) dfs(child); } int main() { time lli n, m; cin >> n >> m; loop(i, 0, m) { lli x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } lli c = -1; loop(i, 1, n + 1) { if (!vist[i]) { dfs(i); c++; } } cout << c << endl; return 0; }
#include <bits/stdc++.h> #include <map> #include <set> #include <unordered_map> #include <vector> #define lli long long int #define loop(i, x, n) for (int i = x; i < n; i++) #define loopr(i, x, n) for (int i = n - 1; i >= x; i--) #define st set<lli> #define v vector<lli> #define um unordered_map<lli, lli> #define vpl vector<pair<lli, lli>> #define llu long long unsigned #define ms muliset<int, greater<int>> #define ums unordered_map<string, lli> #define pb push_back #define time \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define test \ lli t; \ cin >> t; \ while (t--) using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } v adj[100001]; lli vist[100001]; void dfs(lli node) { vist[node] = 1; for (lli child : adj[node]) if (!vist[child]) dfs(child); } int main() { time lli n, m; cin >> n >> m; loop(i, 0, m) { lli x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } lli c = -1; loop(i, 1, n + 1) { if (!vist[i]) { dfs(i); c++; } } cout << c << endl; return 0; }
replace
31
32
31
32
0
p02536
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using ll = long long; struct UnionFind { // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの // そうでなければ親のid vector<int> r; UnionFind(int N) { r = vector<int>(N, -1); } int root(int x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } int size(int x) { return -r[root(x)]; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); UnionFind UF(n); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; UF.unite(a[i], b[i]); } vector<int> root(n); for (int i = 0; i < n; i++) { root[UF.root(i)]++; } int ans = 0; for (int i = 0; i < n; i++) { if (root[i] != 0) { ans++; } } cout << ans - 1 << endl; return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; struct UnionFind { // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの // そうでなければ親のid vector<int> r; UnionFind(int N) { r = vector<int>(N, -1); } int root(int x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } int size(int x) { return -r[root(x)]; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); UnionFind UF(n); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; UF.unite(a[i], b[i]); } vector<int> root(n); for (int i = 0; i < n; i++) { root[UF.root(i)]++; } int ans = 0; for (int i = 0; i < n; i++) { if (root[i] != 0) { ans++; } } cout << ans - 1 << endl; return 0; }
insert
39
39
39
41
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int mxn = 2e5 + 5; class DisjointSet { public: vector<int> parent; DisjointSet(int n) : parent(n) { for (int i = 0; i < n; i++) { parent[i] = i; } } void join(int a, int b) { parent[find(b)] = find(a); } int find(int a) { return a == parent[a] ? a : parent[a] = find(parent[a]); } bool check(int a, int b) { return find(a) == find(b); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, n, x, t1, t2; cin >> n >> x; DisjointSet dsu(n); for (i = 0; i < x; i++) { cin >> t1 >> t2; dsu.join(t1, t2); } int ans = 0; int t = dsu.find(0); for (i = 0; i < n; i++) { int temp = dsu.find(i); if (temp != t) { ans++; dsu.join(0, i); } } cout << ans; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int mxn = 2e5 + 5; class DisjointSet { public: vector<int> parent; DisjointSet(int n) : parent(n) { for (int i = 0; i < n; i++) { parent[i] = i; } } void join(int a, int b) { parent[find(b)] = find(a); } int find(int a) { return a == parent[a] ? a : parent[a] = find(parent[a]); } bool check(int a, int b) { return find(a) == find(b); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, n, x, t1, t2; cin >> n >> x; DisjointSet dsu(n); for (i = 0; i < x; i++) { cin >> t1 >> t2; t1--; t2--; dsu.join(t1, t2); } int ans = 0; int t = dsu.find(0); for (i = 0; i < n; i++) { int temp = dsu.find(i); if (temp != t) { ans++; dsu.join(0, i); } } cout << ans; return 0; }
insert
30
30
30
32
0
p02536
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <cstdio> #include <tuple> #define MAX 1000007 #define MP make_pair #define ii long long #define dd double #define MAX_SIZE 1e7 #define mod 1000000007 #define gc getchar_unlocked #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define ll long long #define si(x) scanf("%d", &x) #define sl(x) scanf("%lld", &x) #define ss(s) scanf("%s", s) #define pi(x) printf("%d\n", x) #define pl(x) printf("%lld\n", x) #define ps(s) printf("%s\n", s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define REP(i, b, n) for (long long i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) #define sz(a) int((a).size()) #define forn(i, n) \ for (int i = 0; i < int(n); ++i) \ ..> || #define w(x) \ int x; \ cin >> x; \ while (x--) #define inp(tmp) \ ii tmp; \ cin >> tmp #define DBG 0 #define print if (DBG) using namespace std; typedef pair<ii, ii> pii; typedef pair<ll, ll> pl; typedef vector<ii> vi; typedef vector<ll> vl; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, vi> Node; ii n, m; vi par; ii find_set(ii v) { if (v == par[v]) return v; return find_set(par[v]); } void con(ii a, ii b) { a = find_set(a); b = find_set(b); if (a != b) par[b] = a; } int main() { cin >> n >> m; par.pb(-1); REP(i, 1, n + 1) { par.pb(i); } rep(i, m) { inp(x); inp(y); con(x, y); } ii comp = 0; REP(i, 1, n + 1) { if (par[i] == i) { print cout << i << " is main "; comp++; } } cout << comp - 1; cin >> n; } /* 3 1 1 2 */
#include <bits/stdc++.h> #include <cstdio> #include <tuple> #define MAX 1000007 #define MP make_pair #define ii long long #define dd double #define MAX_SIZE 1e7 #define mod 1000000007 #define gc getchar_unlocked #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define ll long long #define si(x) scanf("%d", &x) #define sl(x) scanf("%lld", &x) #define ss(s) scanf("%s", s) #define pi(x) printf("%d\n", x) #define pl(x) printf("%lld\n", x) #define ps(s) printf("%s\n", s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define REP(i, b, n) for (long long i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) #define sz(a) int((a).size()) #define forn(i, n) \ for (int i = 0; i < int(n); ++i) \ ..> || #define w(x) \ int x; \ cin >> x; \ while (x--) #define inp(tmp) \ ii tmp; \ cin >> tmp #define DBG 0 #define print if (DBG) using namespace std; typedef pair<ii, ii> pii; typedef pair<ll, ll> pl; typedef vector<ii> vi; typedef vector<ll> vl; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, vi> Node; ii n, m; vi par; ii find_set(ii v) { if (v == par[v]) return v; return par[v] = find_set(par[v]); } void con(ii a, ii b) { a = find_set(a); b = find_set(b); if (a != b) par[b] = a; } int main() { cin >> n >> m; par.pb(-1); REP(i, 1, n + 1) { par.pb(i); } rep(i, m) { inp(x); inp(y); con(x, y); } ii comp = 0; REP(i, 1, n + 1) { if (par[i] == i) { print cout << i << " is main "; comp++; } } cout << comp - 1; cin >> n; } /* 3 1 1 2 */
replace
61
62
61
62
TLE
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define local #ifdef local template <class T> void _E(T x) { cerr << x; } void _E(double x) { cerr << fixed << setprecision(6) << x; } void _E(string s) { cerr << "\"" << s << "\""; } template <class A, class B> void _E(pair<A, B> x) { cerr << '('; _E(x.first); cerr << ", "; _E(x.second); cerr << ")"; } template <class T> void _E(vector<T> x) { cerr << "["; for (auto it = x.begin(); it != x.end(); ++it) { if (it != x.begin()) cerr << ", "; _E(*it); } cerr << "]"; } void ERR() {} template <class A, class... B> void ERR(A x, B... y) { _E(x); cerr << (sizeof...(y) ? ", " : " "); ERR(y...); } #define debug(x...) \ do { \ cerr << "{ " #x " } -> { "; \ ERR(x); \ cerr << "}" << endl; \ } while (false) #else #define debug(...) 114514.1919810 #endif #define _rep(n, a, b) for (int n = (a); n <= (b); ++n) #define _rev(n, a, b) for (int n = (a); n >= (b); --n) #define _for(n, a, b) for (int n = (a); n < (b); ++n) #define _rof(n, a, b) for (int n = (a); n > (b); --n) #define oo 0x3f3f3f3f3f3f3f3f #define ll long long #define db long double #define eps 1e-3 #define bin(x) cerr << bitset<10>(x) << endl #define what_is(x) cerr << #x << " is " << x << endl #define met(a, b) memset(a, b, sizeof(a)) #define all(x) x.begin(), x.end() #define pii pair<ll, ll> #define pdd pair<db, db> #define endl "\n" const ll mod = 998244353; const ll maxn = 1e5 + 10; const db pi = acos(-1.0); ll qpow(ll a, ll b) { ll ret = 1; for (; b; a = a * a % mod, b >>= 1) { if (b & 1) { ret = ret * a % mod; } } return ret; } vector<ll> f(maxn), invf(maxn); ll inv(ll a) { return qpow(a, mod - 2); } void prework() { f[0] = 1; _rep(i, 1, maxn - 1) { f[i] = f[i - 1] * i % mod; } invf[maxn - 1] = qpow(f[maxn - 1], mod - 2); for (ll i = maxn - 2; i >= 0; i--) { invf[i] = invf[i + 1] * (i + 1) % mod; } } ll C(ll n, ll m) { if (n > m || m < 0) return 0; if (n == 0 || m == n) return 1; ll res = (f[m] * invf[m - n] % mod * invf[n]) % mod; return res; } int par[maxn]; int find(int a) { return a == par[a] ? a : par[a] = find(a); } void merge(int a, int b) { par[find(a)] = find(b); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; _rep(i, 1, n) { par[i] = i; } _rep(i, 1, m) { int u, v; cin >> u >> v; merge(u, v); } int cnt = 0; _rep(i, 1, n) { if (par[i] == i) { cnt++; } } cout << cnt - 1 << endl; }
#include <bits/stdc++.h> using namespace std; #define local #ifdef local template <class T> void _E(T x) { cerr << x; } void _E(double x) { cerr << fixed << setprecision(6) << x; } void _E(string s) { cerr << "\"" << s << "\""; } template <class A, class B> void _E(pair<A, B> x) { cerr << '('; _E(x.first); cerr << ", "; _E(x.second); cerr << ")"; } template <class T> void _E(vector<T> x) { cerr << "["; for (auto it = x.begin(); it != x.end(); ++it) { if (it != x.begin()) cerr << ", "; _E(*it); } cerr << "]"; } void ERR() {} template <class A, class... B> void ERR(A x, B... y) { _E(x); cerr << (sizeof...(y) ? ", " : " "); ERR(y...); } #define debug(x...) \ do { \ cerr << "{ " #x " } -> { "; \ ERR(x); \ cerr << "}" << endl; \ } while (false) #else #define debug(...) 114514.1919810 #endif #define _rep(n, a, b) for (int n = (a); n <= (b); ++n) #define _rev(n, a, b) for (int n = (a); n >= (b); --n) #define _for(n, a, b) for (int n = (a); n < (b); ++n) #define _rof(n, a, b) for (int n = (a); n > (b); --n) #define oo 0x3f3f3f3f3f3f3f3f #define ll long long #define db long double #define eps 1e-3 #define bin(x) cerr << bitset<10>(x) << endl #define what_is(x) cerr << #x << " is " << x << endl #define met(a, b) memset(a, b, sizeof(a)) #define all(x) x.begin(), x.end() #define pii pair<ll, ll> #define pdd pair<db, db> #define endl "\n" const ll mod = 998244353; const ll maxn = 1e5 + 10; const db pi = acos(-1.0); ll qpow(ll a, ll b) { ll ret = 1; for (; b; a = a * a % mod, b >>= 1) { if (b & 1) { ret = ret * a % mod; } } return ret; } vector<ll> f(maxn), invf(maxn); ll inv(ll a) { return qpow(a, mod - 2); } void prework() { f[0] = 1; _rep(i, 1, maxn - 1) { f[i] = f[i - 1] * i % mod; } invf[maxn - 1] = qpow(f[maxn - 1], mod - 2); for (ll i = maxn - 2; i >= 0; i--) { invf[i] = invf[i + 1] * (i + 1) % mod; } } ll C(ll n, ll m) { if (n > m || m < 0) return 0; if (n == 0 || m == n) return 1; ll res = (f[m] * invf[m - n] % mod * invf[n]) % mod; return res; } int par[maxn]; int find(int a) { return a == par[a] ? a : par[a] = find(par[a]); } void merge(int a, int b) { par[find(a)] = find(b); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; _rep(i, 1, n) { par[i] = i; } _rep(i, 1, m) { int u, v; cin >> u >> v; merge(u, v); } int cnt = 0; _rep(i, 1, n) { if (par[i] == i) { cnt++; } } cout << cnt - 1 << endl; }
replace
100
101
100
101
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lli long long int #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define test \ lli t; \ cin >> t; \ while (t--) #define vll vector<lli> #define mll map<lli, lli> #define vvll vector<vector<lli>> #define vpll vector<pair<lli, lli>> #define vvpll vector<vector<pair<lli, lli>>> #define mpll map<pair<lli, lli>, lli> #define pll pair<lli, lli> #define sll stack<lli> #define qll queqe<lli> #define pb push_back #define bs binary_search #define lb lower_bound #define ub upper_bound #define ff first #define ss second #define mod 1000000007 #define ma 1000000000000000000 #define mi -1000000000000000000 lli vis[1001] = {0}; vll adj[1001]; queue<lli> q; void explore(lli n) { while (!q.empty()) { lli s = q.front(); q.pop(); for (auto i : adj[s]) { if (vis[i] == 0) { vis[i] = 1; q.push(i); } } } } int main() { fastio lli n, m, i, j, u, v, d = 0, a; cin >> n >> m; for (i = 0; i < m; i++) { cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } for (i = 1; i <= n; i++) { if (vis[i] == 0) { vis[i] = 1; q.push(i); explore(i); d++; } } cout << d - 1; return 0; }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define test \ lli t; \ cin >> t; \ while (t--) #define vll vector<lli> #define mll map<lli, lli> #define vvll vector<vector<lli>> #define vpll vector<pair<lli, lli>> #define vvpll vector<vector<pair<lli, lli>>> #define mpll map<pair<lli, lli>, lli> #define pll pair<lli, lli> #define sll stack<lli> #define qll queqe<lli> #define pb push_back #define bs binary_search #define lb lower_bound #define ub upper_bound #define ff first #define ss second #define mod 1000000007 #define ma 1000000000000000000 #define mi -1000000000000000000 lli vis[100001] = {0}; vll adj[100001]; queue<lli> q; void explore(lli n) { while (!q.empty()) { lli s = q.front(); q.pop(); for (auto i : adj[s]) { if (vis[i] == 0) { vis[i] = 1; q.push(i); } } } } int main() { fastio lli n, m, i, j, u, v, d = 0, a; cin >> n >> m; for (i = 0; i < m; i++) { cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } for (i = 1; i <= n; i++) { if (vis[i] == 0) { vis[i] = 1; q.push(i); explore(i); d++; } } cout << d - 1; return 0; }
replace
28
30
28
30
0
p02536
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <numeric> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) 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); rep(i, m) { int a, b; cin >> a >> b; tree.unite(a, b); } int cnt = 0; rep(i, n) { if (tree.par[i] == i) cnt++; } cout << cnt - 1 << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <numeric> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) 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); rep(i, m) { int a, b; cin >> a >> b; tree.unite(a - 1, b - 1); } int cnt = 0; rep(i, n) { if (tree.par[i] == i) cnt++; } cout << cnt - 1 << endl; }
replace
46
47
46
47
0
p02536
C++
Runtime Error
#include "stdio.h" #include <vector> using namespace std; vector<int> gra[10001]; int v[10002]; int DFS(int s) { v[s] = 1; for (int i = 0; i < gra[s].size(); i++) { if (v[gra[s][i]] == 0) DFS(gra[s][i]); } return 0; } int main() { int N, M; scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) { v[i] = 0; } int v1, v2; for (int i = 0; i < M; i++) { scanf("%d%d", &v1, &v2); gra[v1].push_back(v2); gra[v2].push_back(v1); } int res = 0; for (int i = 1; i <= N; i++) if (v[i] == 0) { DFS(i); res++; } printf("%d", res - 1); }
#include "stdio.h" #include <vector> using namespace std; vector<int> gra[100010]; int v[100020]; int DFS(int s) { v[s] = 1; for (int i = 0; i < gra[s].size(); i++) { if (v[gra[s][i]] == 0) DFS(gra[s][i]); } return 0; } int main() { int N, M; scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) { v[i] = 0; } int v1, v2; for (int i = 0; i < M; i++) { scanf("%d%d", &v1, &v2); gra[v1].push_back(v2); gra[v2].push_back(v1); } int res = 0; for (int i = 1; i <= N; i++) if (v[i] == 0) { DFS(i); res++; } printf("%d", res - 1); }
replace
4
6
4
6
0
p02536
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <algorithm> //next_permutation #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define dec(n) cout << fixed << setprecision(n); #define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define small "abcdefghijklmnopqrstuvwxyz" using namespace std; using ll = long long; using P = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vl>; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } const ll MOD = 1000000007; const ll MAX = 2000001; ll mod(ll a) { return a % MOD; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } struct UnionFind { // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの // そうでなければ親のid vector<int> r; UnionFind(int N) { r = vector<int>(N, -1); } int root(int x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } int size(int x) { return -r[root(x)]; } }; int main() { ll n, m; cin >> n >> m; UnionFind UF(n); rep(i, m) { ll x, y; cin >> x >> y; UF.unite(x, y); } set<ll> parent; rep(i, n) { parent.insert(UF.root(i)); } cout << parent.size() - 1 << endl; }
#define _GLIBCXX_DEBUG #include <algorithm> //next_permutation #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define dec(n) cout << fixed << setprecision(n); #define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define small "abcdefghijklmnopqrstuvwxyz" using namespace std; using ll = long long; using P = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vl>; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } const ll MOD = 1000000007; const ll MAX = 2000001; ll mod(ll a) { return a % MOD; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } struct UnionFind { // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの // そうでなければ親のid vector<int> r; UnionFind(int N) { r = vector<int>(N, -1); } int root(int x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } int size(int x) { return -r[root(x)]; } }; int main() { ll n, m; cin >> n >> m; UnionFind UF(n); rep(i, m) { ll x, y; cin >> x >> y; x--; y--; UF.unite(x, y); } set<ll> parent; rep(i, n) { parent.insert(UF.root(i)); } cout << parent.size() - 1 << endl; }
insert
63
63
63
65
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #include <cstdlib> #include <numeric> #define ll long long int #define all(x) x.begin(), x.end() #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define v vector<long long int> #define vv vector<int> #define gcd(m, n) __gcd(m, n) #define vp vector<pair<long long int, long long int>> #define pb push_back #define mp make_pair #define pp pop_back #define iter vector<int>::iterator #define pa pair<long long int, long long int> #define f(i, n) for (ll i = 0; i < n; i++) #define loop(i, a, n) for (ll i = a; i < n; i += 1) #define fe(d, e) for (auto d : e) #define mod 1000000007 #define F first #define S second #define sett set<long long int> #define um unordered_map<ll, ll> #define N 100005LL using namespace std; vector<vector<int>> graph; vector<bool> visited; void dfs(int node) { visited[node] = true; for (auto u : graph[node]) if (!visited[u]) dfs(u); } int solve1() { int n, m; cin >> n >> m; graph.resize(n + 1); visited.resize(n + 1, false); f(i, m) { ll x, y; cin >> x >> y; graph[x].pb(y); graph[y].pb(x); } ll ans = 0; loop(i, 1, n + 1) { if (!visited[i]) { ans += 1; dfs(i); } } cout << ans - 1 << "\n"; } int main() { fastio; ll t = 1; // cin>>t; while (t--) solve1(); }
#include <bits/stdc++.h> #include <cstdlib> #include <numeric> #define ll long long int #define all(x) x.begin(), x.end() #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define v vector<long long int> #define vv vector<int> #define gcd(m, n) __gcd(m, n) #define vp vector<pair<long long int, long long int>> #define pb push_back #define mp make_pair #define pp pop_back #define iter vector<int>::iterator #define pa pair<long long int, long long int> #define f(i, n) for (ll i = 0; i < n; i++) #define loop(i, a, n) for (ll i = a; i < n; i += 1) #define fe(d, e) for (auto d : e) #define mod 1000000007 #define F first #define S second #define sett set<long long int> #define um unordered_map<ll, ll> #define N 100005LL using namespace std; vector<vector<int>> graph; vector<bool> visited; void dfs(int node) { visited[node] = true; for (auto u : graph[node]) if (!visited[u]) dfs(u); } void solve1() { int n, m; cin >> n >> m; graph.resize(n + 1); visited.resize(n + 1, false); f(i, m) { ll x, y; cin >> x >> y; graph[x].pb(y); graph[y].pb(x); } ll ans = 0; loop(i, 1, n + 1) { if (!visited[i]) { ans += 1; dfs(i); } } cout << ans - 1 << "\n"; } int main() { fastio; ll t = 1; // cin>>t; while (t--) solve1(); }
replace
41
42
41
42
0
p02536
C++
Runtime Error
// luogu-judger-enable-o2 #include <bits/stdc++.h> using namespace std; int n, m, k; int x, y; int p[5000]; int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } map<int, bool> mp; int main() { cin >> n >> m; for (int i = 0; i < n; ++i) p[i] = i; for (int i = 0; i < m; ++i) { cin >> x >> y; p[find(x - 1)] = find(y - 1); } int sum = 0; for (int i = 0; i < n; ++i) { if (!mp[find(i)]) { sum++; mp[find(i)] = true; } } cout << sum - 1 << endl; return 0; }
// luogu-judger-enable-o2 #include <bits/stdc++.h> using namespace std; int n, m, k; int x, y; int p[100001]; int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } map<int, bool> mp; int main() { cin >> n >> m; for (int i = 0; i < n; ++i) p[i] = i; for (int i = 0; i < m; ++i) { cin >> x >> y; p[find(x - 1)] = find(y - 1); } int sum = 0; for (int i = 0; i < n; ++i) { if (!mp[find(i)]) { sum++; mp[find(i)] = true; } } cout << sum - 1 << endl; return 0; }
replace
5
6
5
6
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long #define X first #define Y second #define all(x) x.begin(), x.end() #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define sim template <class c #define ris return *this #define dor > debug &operator<< #define eni(x) \ sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \ c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c *x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair<b, c> d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c &) { ris; } #endif } ; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int MOD = 998244353; const int MX = (int)1e5 + 10; void add_self(int &x, int y, int md = MOD) { x += y; if (x >= md) x -= md; } void sub_self(int &x, int y, int md = MOD) { x -= y; if (x < 0) x += md; } void mul_self(int &x, int y, int md = MOD) { x = x * 1LL * y % md; } inline int mul(int x, int y, int md = MOD) { return x * 1LL * y % md; } inline int add(int x, int y, int md = MOD) { x += y; if (x >= md) x -= md; return x; } inline int sub(int x, int y, int md = MOD) { x -= y; if (x < 0) x += md; return x; } const int dx[] = {0, 0, -1, 1}; const int dy[] = {1, -1, 0, 0}; vector<int> read(int n, int k) { vector<int> vec(n); for (int i = 0; i < k; ++i) cin >> vec[i]; int a, b, c, d; cin >> a >> b >> c >> d; for (int i = k; i < n; ++i) vec[i] = (a * 1ll * vec[i - 2] + b * 1ll * vec[i - 1] + c) % d; return vec; } vector<int> g[MX], vis(MX); void dfs(int x, int par = -1) { vis[x] = true; for (auto &y : g[x]) { if (y == par) continue; dfs(y, x); } } void solve() { int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; --x, --y; g[x].push_back(y); g[y].push_back(x); } int comp = 0; for (int i = 0; i < n; ++i) { if (!vis[i]) { dfs(i); comp++; } } cout << comp - 1 << "\n"; return; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); int t; t = 1; // cin >> t; // pre(); for (int i = 1; i <= t; ++i) { // cout << "Case #" << i << ": "; solve(); // cout << '\n'; // cout << endl; } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long #define X first #define Y second #define all(x) x.begin(), x.end() #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define sim template <class c #define ris return *this #define dor > debug &operator<< #define eni(x) \ sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \ c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c *x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair<b, c> d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c &) { ris; } #endif } ; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int MOD = 998244353; const int MX = (int)1e5 + 10; void add_self(int &x, int y, int md = MOD) { x += y; if (x >= md) x -= md; } void sub_self(int &x, int y, int md = MOD) { x -= y; if (x < 0) x += md; } void mul_self(int &x, int y, int md = MOD) { x = x * 1LL * y % md; } inline int mul(int x, int y, int md = MOD) { return x * 1LL * y % md; } inline int add(int x, int y, int md = MOD) { x += y; if (x >= md) x -= md; return x; } inline int sub(int x, int y, int md = MOD) { x -= y; if (x < 0) x += md; return x; } const int dx[] = {0, 0, -1, 1}; const int dy[] = {1, -1, 0, 0}; vector<int> read(int n, int k) { vector<int> vec(n); for (int i = 0; i < k; ++i) cin >> vec[i]; int a, b, c, d; cin >> a >> b >> c >> d; for (int i = k; i < n; ++i) vec[i] = (a * 1ll * vec[i - 2] + b * 1ll * vec[i - 1] + c) % d; return vec; } vector<int> g[MX], vis(MX); void dfs(int x, int par = -1) { vis[x] = true; for (auto &y : g[x]) { if (y == par) continue; if (!vis[y]) dfs(y, x); } } void solve() { int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; --x, --y; g[x].push_back(y); g[y].push_back(x); } int comp = 0; for (int i = 0; i < n; ++i) { if (!vis[i]) { dfs(i); comp++; } } cout << comp - 1 << "\n"; return; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); int t; t = 1; // cin >> t; // pre(); for (int i = 1; i <= t; ++i) { // cout << "Case #" << i << ": "; solve(); // cout << '\n'; // cout << endl; } return 0; }
replace
99
100
99
101
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; /*---------------------DEBUGGING--------------------------------------------*/ void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif /*-------------------------------------------------------------------------------------*/ // #define mp make_pair #define pb push_back #define ll long long #define pii pair<int, int> #define pcc pair<char, char> #define F first #define S second #define int long long #define pi 3.141592653589793238462643383279502 #define M 1000000007 // 998244353 #define rep(i, a, n) for (int i = a; i < n; i++) #define INF 100000000000000000 #define N 200005 #define vi vector<int> #define all(v) v.begin(), v.end() #define endl "\n" // #define ordered_set tree<int, null_type,less<int>, // rb_tree_tag,tree_order_statistics_node_update> vector<int> adj[N]; int vis[N]; int dfs(int x) { vis[x] = 1; for (auto y : adj[x]) { if (!vis[y]) dfs(y); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; rep(i, 0, m) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } int cc = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { dfs(i); cc++; } } cout << (cc - 1) << endl; }
#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; /*---------------------DEBUGGING--------------------------------------------*/ void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif /*-------------------------------------------------------------------------------------*/ // #define mp make_pair #define pb push_back #define ll long long #define pii pair<int, int> #define pcc pair<char, char> #define F first #define S second #define int long long #define pi 3.141592653589793238462643383279502 #define M 1000000007 // 998244353 #define rep(i, a, n) for (int i = a; i < n; i++) #define INF 100000000000000000 #define N 200005 #define vi vector<int> #define all(v) v.begin(), v.end() #define endl "\n" // #define ordered_set tree<int, null_type,less<int>, // rb_tree_tag,tree_order_statistics_node_update> vector<int> adj[N]; int vis[N]; void dfs(int x) { vis[x] = 1; for (auto y : adj[x]) { if (!vis[y]) dfs(y); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; rep(i, 0, m) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } int cc = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { dfs(i); cc++; } } cout << (cc - 1) << endl; }
replace
72
73
72
73
0
p02536
C++
Runtime Error
#include <iostream> using namespace std; #define ll long long #include <set> #include <vector> // vector<ll>adj[100001]; ll parent[100001]; ll tao[100001]; ll find(ll i) { if (parent[i] == i) return i; else return parent[i] = find(parent[i]); } ll union_set(ll a, ll b) { ll x = find(a); ll y = find(b); if (tao[x] < tao[y]) swap(x, y); parent[y] = x; if (tao[x] == tao[y]) tao[x]++; } set<ll> s; int main() { ll n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { parent[i] = i; tao[i] = 1; } while (m--) { ll l, r; cin >> l >> r; union_set(l, r); } for (int i = 1; i <= n; i++) s.insert(find(i)); cout << s.size() - 1 << endl; return 0; }
#include <iostream> using namespace std; #define ll long long #include <set> #include <vector> // vector<ll>adj[100001]; ll parent[100001]; ll tao[100001]; ll find(ll i) { if (parent[i] == i) return i; else return parent[i] = find(parent[i]); } void union_set(ll a, ll b) { ll x = find(a); ll y = find(b); if (tao[x] < tao[y]) swap(x, y); parent[y] = x; if (tao[x] == tao[y]) tao[x]++; } set<ll> s; int main() { ll n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { parent[i] = i; tao[i] = 1; } while (m--) { ll l, r; cin >> l >> r; union_set(l, r); } for (int i = 1; i <= n; i++) s.insert(find(i)); cout << s.size() - 1 << endl; return 0; }
replace
14
15
14
15
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define pii pair<int, int> #define int long long int #define vec vector<int> #define inf 1e18 using namespace std; vector<int> adj[100001]; int vis[100001]; int dfs(int node) { vis[node] = 1; for (int ch : adj[node]) { if (!vis[ch]) dfs(ch); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt = 1; // cin>>tt; while (tt--) { int n, m, i; cin >> n >> m; for (i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } int c = 0; for (i = 1; i <= n; i++) { if (!vis[i]) dfs(i), c++; } cout << c - 1; } }
#include <bits/stdc++.h> #define pb push_back #define pii pair<int, int> #define int long long int #define vec vector<int> #define inf 1e18 using namespace std; vector<int> adj[100001]; int vis[100001]; void dfs(int node) { vis[node] = 1; for (int ch : adj[node]) { if (!vis[ch]) dfs(ch); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt = 1; // cin>>tt; while (tt--) { int n, m, i; cin >> n >> m; for (i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } int c = 0; for (i = 1; i <= n; i++) { if (!vis[i]) dfs(i), c++; } cout << c - 1; } }
replace
9
10
9
10
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> const int mmax = 1e5; using namespace std; long long n, m, ans; bool vis[mmax]; vector<int> adj[mmax]; void dfs(int u) { vis[u] = 1; for (int v : adj[u]) if (!vis[v]) dfs(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("test.inp", "r", stdin); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) if (!vis[i]) { dfs(i); ans++; } cout << --ans; return 0; }
#include <bits/stdc++.h> const int mmax = 1e6; using namespace std; long long n, m, ans; bool vis[mmax]; vector<int> adj[mmax]; void dfs(int u) { vis[u] = 1; for (int v : adj[u]) if (!vis[v]) dfs(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("test.inp", "r", stdin); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) if (!vis[i]) { dfs(i); ans++; } cout << --ans; return 0; }
replace
1
2
1
2
0
p02536
C++
Runtime Error
/* -> Written by <- ----------- |K_A_Z_A_M_A| |___________| | _ | | (^_^) | | /( | )\ | |____|_|____| H O A N G */ #define Task "" #define F first #define S second #define push_back pb #define bit(x, i) (x >> i) & 1 #define inf 1e9 + 7 #define INF 1e18 + 7 #define all(x) x.begin(), x.end() #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; const int MOD = 998244353; const double eps = 1e-6; const int maxn = 1e5 + 5; int n, m; vector<int> a[maxn]; int par[maxn]; void dfs(int u) { for (auto v : a[u]) if (v != par[u]) { par[v] = u; dfs(v); } } void Solve() { cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; a[u].pb(v); a[v].pb(u); } int cnt = 0; for (int i = 1; i <= n; i++) if (par[i] == 0) { cnt++; dfs(i); } cout << cnt - 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); if (fopen(Task ".inp", "r")) { freopen(Task ".inp", "r", stdin); freopen(Task ".out", "w", stdout); } int test_case = 1; // cin >> test_case; while (test_case--) Solve(); return 0; }
/* -> Written by <- ----------- |K_A_Z_A_M_A| |___________| | _ | | (^_^) | | /( | )\ | |____|_|____| H O A N G */ #define Task "" #define F first #define S second #define push_back pb #define bit(x, i) (x >> i) & 1 #define inf 1e9 + 7 #define INF 1e18 + 7 #define all(x) x.begin(), x.end() #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; const int MOD = 998244353; const double eps = 1e-6; const int maxn = 1e5 + 5; int n, m; vector<int> a[maxn]; int par[maxn]; void dfs(int u) { for (auto v : a[u]) if (v != par[u]) { if (par[v]) continue; par[v] = u; dfs(v); } } void Solve() { cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; a[u].pb(v); a[v].pb(u); } int cnt = 0; for (int i = 1; i <= n; i++) if (par[i] == 0) { cnt++; dfs(i); } cout << cnt - 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); if (fopen(Task ".inp", "r")) { freopen(Task ".inp", "r", stdin); freopen(Task ".out", "w", stdout); } int test_case = 1; // cin >> test_case; while (test_case--) Solve(); return 0; }
insert
50
50
50
52
0
p02536
C++
Runtime Error
//_nebula_ #include <bits/stdc++.h> using namespace std; #define int long long #define double long double int t; const int M = 1e9 + 7; const int inf = INT64_MAX; int max(int a, int b, int c) { return max(a, max(b, c)); } int min(int a, int b, int c) { return min(a, min(b, c)); } int fspow(int x, int y) { int ans; if (y == 0) return 1; ans = fspow(x, y / 2); if (y % 2 == 0) return ((ans * ans) % M); return (x * ((ans * ans) % M)) % M; } int gcd(int x, int y) { if (x < y) swap(x, y); if (y == 0) return x; return gcd(x % y, y); } int p2(int x) { int ans = 0; while (x > 1) { ans++; x /= 2; } return ans; } vector<int> grp[10005]; bool vis[100005]; void dfs(int s) { if (vis[s]) return; vis[s] = 1; for (auto i : grp[s]) dfs(i); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); t = 1; // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // cin>>t; // int tmp=t; while (t--) { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; grp[u].push_back(v); grp[v].push_back(u); } int ans = 0; for (int i = 1; i <= n; i++) { if (vis[i]) continue; ans++; dfs(i); } cout << ans - 1 << "\n"; } }
//_nebula_ #include <bits/stdc++.h> using namespace std; #define int long long #define double long double int t; const int M = 1e9 + 7; const int inf = INT64_MAX; int max(int a, int b, int c) { return max(a, max(b, c)); } int min(int a, int b, int c) { return min(a, min(b, c)); } int fspow(int x, int y) { int ans; if (y == 0) return 1; ans = fspow(x, y / 2); if (y % 2 == 0) return ((ans * ans) % M); return (x * ((ans * ans) % M)) % M; } int gcd(int x, int y) { if (x < y) swap(x, y); if (y == 0) return x; return gcd(x % y, y); } int p2(int x) { int ans = 0; while (x > 1) { ans++; x /= 2; } return ans; } vector<int> grp[100005]; bool vis[100005]; void dfs(int s) { if (vis[s]) return; vis[s] = 1; for (auto i : grp[s]) dfs(i); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); t = 1; // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // cin>>t; // int tmp=t; while (t--) { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; grp[u].push_back(v); grp[v].push_back(u); } int ans = 0; for (int i = 1; i <= n; i++) { if (vis[i]) continue; ans++; dfs(i); } cout << ans - 1 << "\n"; } }
replace
34
35
34
35
0
p02536
C++
Runtime Error
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools #include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const i64 MOD = 1e9 + 7; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T &x, T y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T &x, T y) { if (x < y) { x = y; return true; } return false; } // doc: // https://shibh308.github.io/library/library/lib/classes/unionfind.cpp.html struct UnionFind { vector<int> par; int count; UnionFind(int n) : par(n, -1), count(0) {} int Find(int x) { return par[x] < 0 ? x : Find(par[x]); } int Size(int x) { return par[x] < 0 ? -par[x] : Size(par[x]); } bool Unite(int x, int y) { x = Find(x); y = Find(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return ++count; } }; void solve(long long n, long long m, std::vector<long long> a, std::vector<long long> b) { UnionFind uf(n); for (int i = 0; i < n; ++i) uf.Unite(a[i] - 1, b[i] - 1); cout << n - uf.count - 1 << endl; } signed main() { long long N; scanf("%lld", &N); long long M; scanf("%lld", &M); std::vector<long long> A(M); std::vector<long long> B(M); for (int i = 0; i < M; i++) { scanf("%lld", &A[i]); scanf("%lld", &B[i]); } solve(N, M, std::move(A), std::move(B)); return 0; }
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools #include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const i64 MOD = 1e9 + 7; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T &x, T y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T &x, T y) { if (x < y) { x = y; return true; } return false; } // doc: // https://shibh308.github.io/library/library/lib/classes/unionfind.cpp.html struct UnionFind { vector<int> par; int count; UnionFind(int n) : par(n, -1), count(0) {} int Find(int x) { return par[x] < 0 ? x : Find(par[x]); } int Size(int x) { return par[x] < 0 ? -par[x] : Size(par[x]); } bool Unite(int x, int y) { x = Find(x); y = Find(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return ++count; } }; void solve(long long n, long long m, std::vector<long long> a, std::vector<long long> b) { UnionFind uf(n); for (int i = 0; i < m; ++i) uf.Unite(a[i] - 1, b[i] - 1); cout << n - uf.count - 1 << endl; } signed main() { long long N; scanf("%lld", &N); long long M; scanf("%lld", &M); std::vector<long long> A(M); std::vector<long long> B(M); for (int i = 0; i < M; i++) { scanf("%lld", &A[i]); scanf("%lld", &B[i]); } solve(N, M, std::move(A), std::move(B)); return 0; }
replace
52
53
52
53
0
p02536
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stdio.h> #include <string> #include <tuple> #include <vector> #define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで #define RREP(i, n) for (int i = n; i >= 1; i--) using namespace std; using ll = long long; using P = pair<int, int>; using graph = vector<vector<int>>; const ll mod = pow(10, 9) + 7; const int INF = 1e9; const ll LINF = 1e18; const double pi = 3.14159265358979323846; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { int N, M; cin >> N >> M; graph r(N + 1); rep(i, M) { int A, B; cin >> A >> B; r[A].push_back(B); r[B].push_back(A); } vector<bool> rooted(N + 1, false); int ans = 0; REP(i, N) { if (rooted[i] == false) { rooted[i] = true; queue<int> q; for (int &e : r[i]) { q.push(e); } while (!q.empty()) { int a = q.front(); rooted[a] = true; q.pop(); for (int &e : r[a]) { q.push(e); } } ans++; } } cout << ans - 1 << endl; }
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stdio.h> #include <string> #include <tuple> #include <vector> #define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで #define RREP(i, n) for (int i = n; i >= 1; i--) using namespace std; using ll = long long; using P = pair<int, int>; using graph = vector<vector<int>>; const ll mod = pow(10, 9) + 7; const int INF = 1e9; const ll LINF = 1e18; const double pi = 3.14159265358979323846; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { int N, M; cin >> N >> M; graph r(N + 1); rep(i, M) { int A, B; cin >> A >> B; r[A].push_back(B); r[B].push_back(A); } vector<bool> rooted(N + 1, false); int ans = 0; REP(i, N) { if (rooted[i] == false) { rooted[i] = true; queue<int> q; for (int &e : r[i]) { q.push(e); } while (!q.empty()) { int a = q.front(); rooted[a] = true; q.pop(); for (int &e : r[a]) { if (rooted[e] == false) q.push(e); } } ans++; } } cout << ans - 1 << endl; }
replace
66
67
66
68
TLE
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define P pair<int, int> #define mkp make_pair struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; int main() { int N, M; cin >> N >> M; dsu d(N); rep(i, M) { int a, b; cin >> a >> b; d.merge(a, b); } set<int> s; rep(i, N) { s.insert(d.leader(i)); } cout << s.size() - 1 << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define P pair<int, int> #define mkp make_pair struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; int main() { int N, M; cin >> N >> M; dsu d(N); rep(i, M) { int a, b; cin >> a >> b; a--; b--; d.merge(a, b); } set<int> s; rep(i, N) { s.insert(d.leader(i)); } cout << s.size() - 1 << endl; }
insert
78
78
78
80
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair using ll = long long; struct UnionFind { vector<int> d; // n要素で初期化 UnionFind(int n) : d(n, -1) {} // 木の根を決める int root(int x) { if (d[x] < 0) return x; return d[x] = root(d[x]); } // xとy属する集合を併合 bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } // xとyが同じ集合に属するか否か bool same(int x, int y) { return root(x) == root(y); } // xの属する集合のサイズ int size(int x) { return -d[root(x)]; } }; int main() { int n, m; cin >> n >> m; UnionFind uf(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; uf.unite(a, b); } int ans = 0; for (int i = 0; i < n; i++) { if (!uf.same(i, i + 1)) { uf.unite(i, i + 1); ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair using ll = long long; struct UnionFind { vector<int> d; // n要素で初期化 UnionFind(int n) : d(n, -1) {} // 木の根を決める int root(int x) { if (d[x] < 0) return x; return d[x] = root(d[x]); } // xとy属する集合を併合 bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } // xとyが同じ集合に属するか否か bool same(int x, int y) { return root(x) == root(y); } // xの属する集合のサイズ int size(int x) { return -d[root(x)]; } }; int main() { int n, m; cin >> n >> m; UnionFind uf(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; uf.unite(a, b); } int ans = 0; for (int i = 0; i < n - 1; i++) { if (!uf.same(i, i + 1)) { uf.unite(i, i + 1); ans++; } } cout << ans << endl; return 0; }
replace
49
50
49
50
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++) #define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい #ifdef _DEBUG #define PRE_COMMAND \ std::cin.rdbuf(in.rdbuf()); \ std::cout << fixed << setprecision(15); #else #define PRE_COMMAND cout << fixed << setprecision(15); #endif const double EPS = 1e-10, PI = acos(-1.0); template <class T> auto MAX(T &seq) { return *max_element(seq.begin(), seq.end()); } template <class T> auto MIN(T &seq) { return *min_element(seq.begin(), seq.end()); } template <class T> auto SUM(T &seq) { T temp{0}; auto &temp2 = temp[0]; return accumulate(seq.begin(), seq.end(), temp2); } template <class T> void SORT(T &seq) { sort(seq.begin(), seq.end()); } template <class T, class S> void SORT(T &seq, S &sort_order) { sort(seq.begin(), seq.end(), sort_order); } template <class T> void SORTR(vector<T> &seq) { sort(seq.begin(), seq.end(), greater<T>()); } template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) { long long n = n_0 % mod_0, k = k_0, mod = mod_0, now = 1; while (k) { if (k & 1) now = (now * n) % mod; n = (n * n) % mod; k >>= 1; } return now; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } int main() { PRE_COMMAND int n, m, a, b; cin >> n >> m; struct unionfind { // size:要素数,tree:unionfind木 int n; vector<int> tree_leader, tree_size; unionfind(int temp) : n(temp) { tree_leader.assign(n, 0); tree_size.assign(n, 1); for (int i = 0; i < n; i++) { tree_leader[i] = i; } } // 結合 void merge(int index1, int index2) { int l1 = leader(index1), l2 = leader(index2); if (l1 != l2) { if (tree_size[l1] <= tree_size[l2]) { tree_leader[l1] = l2; tree_size[l2] = tree_size[l1] + tree_size[l2]; } else { tree_leader[l2] = l1; tree_size[l1] = tree_size[l1] + tree_size[l2]; } } } // 同じか判定 bool same(int index1, int index2) { return leader(index1) == leader(index2); } // leaderを探す int leader(int index) { vector<int> temp_list(0); int temp = tree_leader[index]; while (index != temp) { temp_list.push_back(index); index = temp; temp = tree_leader[index]; } for (int i : temp_list) { tree_leader[i] = index; } return index; } // 連結成分のサイズ int size(int index) { return tree_size[leader(index)]; } // 「一つの連結成分の頂点番号のリスト」のリスト vector<vector<int>> groups() { int cnt = -1; vector<int> id_list(n, -1); vector<vector<int>> ret; for (int i = 0; i < n; i++) { int l = leader(i); if (id_list[l] == -1) { id_list[l] = ++cnt; ret.push_back({i}); } else { ret[id_list[l]].push_back(i); } } return ret; } // 連結成分の個数 int component() { return (int)groups().size(); } }; unionfind uf(n); REP(i, m) { cin >> a >> b; uf.merge(a, b); } cout << uf.component() - 1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++) #define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい #ifdef _DEBUG #define PRE_COMMAND \ std::cin.rdbuf(in.rdbuf()); \ std::cout << fixed << setprecision(15); #else #define PRE_COMMAND cout << fixed << setprecision(15); #endif const double EPS = 1e-10, PI = acos(-1.0); template <class T> auto MAX(T &seq) { return *max_element(seq.begin(), seq.end()); } template <class T> auto MIN(T &seq) { return *min_element(seq.begin(), seq.end()); } template <class T> auto SUM(T &seq) { T temp{0}; auto &temp2 = temp[0]; return accumulate(seq.begin(), seq.end(), temp2); } template <class T> void SORT(T &seq) { sort(seq.begin(), seq.end()); } template <class T, class S> void SORT(T &seq, S &sort_order) { sort(seq.begin(), seq.end(), sort_order); } template <class T> void SORTR(vector<T> &seq) { sort(seq.begin(), seq.end(), greater<T>()); } template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) { long long n = n_0 % mod_0, k = k_0, mod = mod_0, now = 1; while (k) { if (k & 1) now = (now * n) % mod; n = (n * n) % mod; k >>= 1; } return now; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } int main() { PRE_COMMAND int n, m, a, b; cin >> n >> m; struct unionfind { // size:要素数,tree:unionfind木 int n; vector<int> tree_leader, tree_size; unionfind(int temp) : n(temp) { tree_leader.assign(n, 0); tree_size.assign(n, 1); for (int i = 0; i < n; i++) { tree_leader[i] = i; } } // 結合 void merge(int index1, int index2) { int l1 = leader(index1), l2 = leader(index2); if (l1 != l2) { if (tree_size[l1] <= tree_size[l2]) { tree_leader[l1] = l2; tree_size[l2] = tree_size[l1] + tree_size[l2]; } else { tree_leader[l2] = l1; tree_size[l1] = tree_size[l1] + tree_size[l2]; } } } // 同じか判定 bool same(int index1, int index2) { return leader(index1) == leader(index2); } // leaderを探す int leader(int index) { vector<int> temp_list(0); int temp = tree_leader[index]; while (index != temp) { temp_list.push_back(index); index = temp; temp = tree_leader[index]; } for (int i : temp_list) { tree_leader[i] = index; } return index; } // 連結成分のサイズ int size(int index) { return tree_size[leader(index)]; } // 「一つの連結成分の頂点番号のリスト」のリスト vector<vector<int>> groups() { int cnt = -1; vector<int> id_list(n, -1); vector<vector<int>> ret; for (int i = 0; i < n; i++) { int l = leader(i); if (id_list[l] == -1) { id_list[l] = ++cnt; ret.push_back({i}); } else { ret[id_list[l]].push_back(i); } } return ret; } // 連結成分の個数 int component() { return (int)groups().size(); } }; unionfind uf(n); REP(i, m) { cin >> a >> b; uf.merge(a - 1, b - 1); } cout << uf.component() - 1 << endl; }
replace
119
120
119
120
0
p02536
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; using Graph = vector<vector<ll>>; #define REP(i, n) for (ll i = 0; i < ll(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i < ll(b); i++) #define FORD(i, a, b) for (ll i = a; i >= b; i--) #define ALL(x) x.begin(), x.end() #define SIZE(x) ll(x.size()) #define INF 1000000000000 // 10^12 #define MOD 1000000007 // 10^9+7:合同式の法 int main() { ll N, M; cin >> N >> M; Graph G(N); for (ll i = 0; i < M; ++i) { ll a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } // BFS のためのデータ構造 vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化 // queue<ll> que; ll total = 0; // BFS 開始 (キューが空になるまで探索を行う) REP(i, N) { if (dist[i] == -1) { dist[i] = total; // cout << dist[i] << endl; total++; queue<ll> que; que.push(i); while (!que.empty()) { ll v = que.front(); // キューから先頭頂点を取り出す que.pop(); // v から辿れる頂点をすべて調べる for (ll nv : G[v]) { if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない // 新たな白色頂点 nv について距離情報を更新してキューに追加する dist[nv] = dist[v]; que.push(nv); } } } } cout << total - 1 << endl; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; using Graph = vector<vector<ll>>; #define REP(i, n) for (ll i = 0; i < ll(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i < ll(b); i++) #define FORD(i, a, b) for (ll i = a; i >= b; i--) #define ALL(x) x.begin(), x.end() #define SIZE(x) ll(x.size()) #define INF 1000000000000 // 10^12 #define MOD 1000000007 // 10^9+7:合同式の法 int main() { ll N, M; cin >> N >> M; Graph G(N); for (ll i = 0; i < M; ++i) { ll a, b; cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } // BFS のためのデータ構造 vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化 // queue<ll> que; ll total = 0; // BFS 開始 (キューが空になるまで探索を行う) REP(i, N) { if (dist[i] == -1) { dist[i] = total; // cout << dist[i] << endl; total++; queue<ll> que; que.push(i); while (!que.empty()) { ll v = que.front(); // キューから先頭頂点を取り出す que.pop(); // v から辿れる頂点をすべて調べる for (ll nv : G[v]) { if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない // 新たな白色頂点 nv について距離情報を更新してキューに追加する dist[nv] = dist[v]; que.push(nv); } } } } cout << total - 1 << endl; return 0; }
replace
26
28
26
28
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // Graph class represents a undirected graph // using adjacency list representation class Graph { // No. of vertices int V; // Pointer to an array containing adjacency lists list<int> *adj; // A function used by DFS void DFSUtil(int v, bool visited[]); public: // Constructor Graph(int V); void addEdge(int v, int w); int NumberOfconnectedComponents(); }; // Function to return the number of // connected components in an undirected graph int Graph::NumberOfconnectedComponents() { // Mark all the vertices as not visited bool *visited = new bool[V]; // To store the number of connected components int count = 0; for (int v = 0; v < V; v++) visited[v] = false; for (int v = 0; v < V; v++) { if (visited[v] == false) { DFSUtil(v, visited); count += 1; } } return count; } void Graph::DFSUtil(int v, bool visited[]) { // Mark the current node as visited visited[v] = true; // Recur for all the vertices // adjacent to this vertex list<int>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) if (!visited[*i]) DFSUtil(*i, visited); } Graph::Graph(int V) { this->V = V; adj = new list<int>[V]; } // Add an undirected edge void Graph::addEdge(int v, int w) { adj[v].push_back(w); adj[w].push_back(v); } // Driver code int main() { int n, m; cin >> n >> m; Graph g(n); while (m--) { int a, b; cin >> a >> b; g.addEdge(a, b); } cout << g.NumberOfconnectedComponents() - 1; return 0; }
#include <bits/stdc++.h> using namespace std; // Graph class represents a undirected graph // using adjacency list representation class Graph { // No. of vertices int V; // Pointer to an array containing adjacency lists list<int> *adj; // A function used by DFS void DFSUtil(int v, bool visited[]); public: // Constructor Graph(int V); void addEdge(int v, int w); int NumberOfconnectedComponents(); }; // Function to return the number of // connected components in an undirected graph int Graph::NumberOfconnectedComponents() { // Mark all the vertices as not visited bool *visited = new bool[V]; // To store the number of connected components int count = 0; for (int v = 0; v < V; v++) visited[v] = false; for (int v = 0; v < V; v++) { if (visited[v] == false) { DFSUtil(v, visited); count += 1; } } return count; } void Graph::DFSUtil(int v, bool visited[]) { // Mark the current node as visited visited[v] = true; // Recur for all the vertices // adjacent to this vertex list<int>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) if (!visited[*i]) DFSUtil(*i, visited); } Graph::Graph(int V) { this->V = V; adj = new list<int>[V]; } // Add an undirected edge void Graph::addEdge(int v, int w) { adj[v].push_back(w); adj[w].push_back(v); } // Driver code int main() { int n, m; cin >> n >> m; Graph g(n); while (m--) { int a, b; cin >> a >> b; a--; b--; g.addEdge(a, b); } cout << g.NumberOfconnectedComponents() - 1; return 0; }
replace
80
81
80
82
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update /* * coder :: ATUL_PANDEY_2608 * >>> INDIA <<< */ using namespace std; using namespace __gnu_pbds; // #define part .. #define pb(a) push_back(a) #define all(a) a.begin(), a.end() #define mod 1000000007 // #define maxx 200006 #define ll long long #define quick ios_base::sync_with_stdio(NULL), cin.tie(0); #define listll vector<long long> #define listi vector<int> #define pii pair<int, int> #define pll pair<long long, long long> #define minheap priority_queue<long long, vector<long long>, greater<long long>> #define rep(i, a, b) for (int i = a; i < b; i++) ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll power(ll a, ll n, ll m = 1000000007) { ll ans = 1; while (n) { if (n & 1) { ans *= a; ans %= m; } a *= a; a %= m; n /= 2; } return ans; } vector<int> prm; vector<bool> vsp; void prime(int nn = 100000) { vsp = vector<bool>(nn + 1, 0); int size_prime = nn; for (ll i = 2; i * i < size_prime; i++) { if (vsp[i] == 1) continue; for (ll j = i * i; j < size_prime; j += i) { vsp[j] = 1; } } for (int i = 2; i < size_prime; i++) if (vsp[i] == 0) prm.pb(i); } template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // end of #define // define globals ... const int maxx = 2e5 + 26; // write function from here ... // never forget to recheck your predefined function in template .. // already defined :: prime , power , gcd : ( ll ).. vector<int> adj[maxx]; vector<int> vis; void dfs(int u, int par) { vis[u] = 1; if (adj[u].size() == 0) return; for (auto it : adj[u]) { if (it != par) { dfs((int)it, u); } } }; int Main() { ll n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].pb(b); adj[b].pb(a); } vis = vector<int>(maxx, 0); int cnt = 0; for (int i = 0; i < n; i++) { if (vis[i] == 0) { dfs(i, -1); cnt++; } } cout << cnt - 1 << endl; return 0; } int main() { quick; int t = 1; // cin>>t; while (t--) Main(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update /* * coder :: ATUL_PANDEY_2608 * >>> INDIA <<< */ using namespace std; using namespace __gnu_pbds; // #define part .. #define pb(a) push_back(a) #define all(a) a.begin(), a.end() #define mod 1000000007 // #define maxx 200006 #define ll long long #define quick ios_base::sync_with_stdio(NULL), cin.tie(0); #define listll vector<long long> #define listi vector<int> #define pii pair<int, int> #define pll pair<long long, long long> #define minheap priority_queue<long long, vector<long long>, greater<long long>> #define rep(i, a, b) for (int i = a; i < b; i++) ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll power(ll a, ll n, ll m = 1000000007) { ll ans = 1; while (n) { if (n & 1) { ans *= a; ans %= m; } a *= a; a %= m; n /= 2; } return ans; } vector<int> prm; vector<bool> vsp; void prime(int nn = 100000) { vsp = vector<bool>(nn + 1, 0); int size_prime = nn; for (ll i = 2; i * i < size_prime; i++) { if (vsp[i] == 1) continue; for (ll j = i * i; j < size_prime; j += i) { vsp[j] = 1; } } for (int i = 2; i < size_prime; i++) if (vsp[i] == 0) prm.pb(i); } template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // end of #define // define globals ... const int maxx = 2e5 + 26; // write function from here ... // never forget to recheck your predefined function in template .. // already defined :: prime , power , gcd : ( ll ).. vector<int> adj[maxx]; vector<int> vis; void dfs(int u, int par) { vis[u] = 1; if (adj[u].size() == 0) return; for (auto it : adj[u]) { if (it != par && vis[it] == 0) { dfs((int)it, u); } } }; int Main() { ll n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].pb(b); adj[b].pb(a); } vis = vector<int>(maxx, 0); int cnt = 0; for (int i = 0; i < n; i++) { if (vis[i] == 0) { dfs(i, -1); cnt++; } } cout << cnt - 1 << endl; return 0; } int main() { quick; int t = 1; // cin>>t; while (t--) Main(); return 0; }
replace
86
87
86
87
0
p02536
C++
Runtime Error
#include <bits/stdc++.h> #ifdef DEBUG #define PRINT(x) \ cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = " \ << (x) << endl; #define PRINTA(a, first, last) \ cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "[" \ << (first) << ", " << (last) << ")" << endl; \ for (int _i = (first); _i < (last); ++_i) { \ cout << #a << "[" << _i << "] = " << (a)[_i] << endl; \ } #define PRINTI(a, i) \ cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "[" \ << #i << "] = " << #a << "[" << (i) << "] = " << (a)[i] << endl; #define dprintf(...) printf(__VA_ARGS__) #else #define PRINT(x) #define PRINTA(a, first, last) #define PRINTI(a, i) #define dprintf(...) #endif #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define MOD 1000000007 // #define MOD 998244353 using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; template <class T, class U> void amax(T &x, U y) { if (x < y) x = y; } template <class T, class U> void amin(T &x, U y) { if (x > y) x = y; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { os << "["; for (int i = 0; i < v.size(); ++i) { if (i) { if (i % 5 == 0) { os << ",\n "; } else { os << ", "; } } os << v[i]; } os << "]"; return os; } template <class T> using vec2 = vector<vector<T>>; template <class T> using vec3 = vector<vector<vector<T>>>; template <class T> vec2<T> vec2_init(int n0, int n1, T x = 0) { return vec2<T>(n0, vector<T>(n1, x)); } template <class T> vec3<T> vec3_init(int n0, int n1, int n2, T x = 0) { return vec3<T>(n0, vec2_init(n1, n2, x)); } /* * U P * - x * ^ * | * LEFT -y <--+--> +y RIGHT * | * v * + x * D O W N */ const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; enum { DOWN, RIGHT, UP, LEFT, }; constexpr double inf = numeric_limits<double>::infinity(); class disjoint_set { private: // if vertex i is a root, r[i] = -(size of the component which has i), // eles r[i] = the root of i. std::vector<int> r; public: const size_t n_vertices; size_t n_components; disjoint_set(size_t n_vertices) : n_vertices(n_vertices), n_components(n_vertices), r(n_vertices, -1) {} size_t root(size_t x) { if (r[x] < 0) { return x; } else { return r[x] = root(r[x]); } } void merge(size_t x, size_t y) { x = root(x); y = root(y); if (x == y) { return; } if (r[x] < r[y]) { r[x] += r[y]; r[y] = x; } else { r[y] += r[x]; r[x] = y; } --n_components; } bool is_same(size_t x, size_t y) { return root(x) == root(y); } size_t component_size(size_t x) { return -r[root(x)]; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; disjoint_set ds(N); REP(_, M) { int A, B; cin >> A >> B; ds.merge(A, B); } int result = ds.n_components - 1; cout << result << endl; return 0; }
#include <bits/stdc++.h> #ifdef DEBUG #define PRINT(x) \ cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = " \ << (x) << endl; #define PRINTA(a, first, last) \ cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "[" \ << (first) << ", " << (last) << ")" << endl; \ for (int _i = (first); _i < (last); ++_i) { \ cout << #a << "[" << _i << "] = " << (a)[_i] << endl; \ } #define PRINTI(a, i) \ cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "[" \ << #i << "] = " << #a << "[" << (i) << "] = " << (a)[i] << endl; #define dprintf(...) printf(__VA_ARGS__) #else #define PRINT(x) #define PRINTA(a, first, last) #define PRINTI(a, i) #define dprintf(...) #endif #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define MOD 1000000007 // #define MOD 998244353 using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; template <class T, class U> void amax(T &x, U y) { if (x < y) x = y; } template <class T, class U> void amin(T &x, U y) { if (x > y) x = y; } template <class T, class U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { os << "["; for (int i = 0; i < v.size(); ++i) { if (i) { if (i % 5 == 0) { os << ",\n "; } else { os << ", "; } } os << v[i]; } os << "]"; return os; } template <class T> using vec2 = vector<vector<T>>; template <class T> using vec3 = vector<vector<vector<T>>>; template <class T> vec2<T> vec2_init(int n0, int n1, T x = 0) { return vec2<T>(n0, vector<T>(n1, x)); } template <class T> vec3<T> vec3_init(int n0, int n1, int n2, T x = 0) { return vec3<T>(n0, vec2_init(n1, n2, x)); } /* * U P * - x * ^ * | * LEFT -y <--+--> +y RIGHT * | * v * + x * D O W N */ const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; enum { DOWN, RIGHT, UP, LEFT, }; constexpr double inf = numeric_limits<double>::infinity(); class disjoint_set { private: // if vertex i is a root, r[i] = -(size of the component which has i), // eles r[i] = the root of i. std::vector<int> r; public: const size_t n_vertices; size_t n_components; disjoint_set(size_t n_vertices) : n_vertices(n_vertices), n_components(n_vertices), r(n_vertices, -1) {} size_t root(size_t x) { if (r[x] < 0) { return x; } else { return r[x] = root(r[x]); } } void merge(size_t x, size_t y) { x = root(x); y = root(y); if (x == y) { return; } if (r[x] < r[y]) { r[x] += r[y]; r[y] = x; } else { r[y] += r[x]; r[x] = y; } --n_components; } bool is_same(size_t x, size_t y) { return root(x) == root(y); } size_t component_size(size_t x) { return -r[root(x)]; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; disjoint_set ds(N); REP(_, M) { int A, B; cin >> A >> B; --A; --B; ds.merge(A, B); } int result = ds.n_components - 1; cout << result << endl; return 0; }
insert
160
160
160
162
0