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
p03108
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int INF = 1 << 30; const int MOD = 1e9 + 7; const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; template <class Type> class UnionFind { public: vector<Type> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(Type x, Type 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); } Type find(Type k) { if (data[k] < 0) return (k); return (data[k] = find(data[k])); } Type size(Type k) { return (-data[find(k)]); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<ll> a(n), b(n); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } ll cnt = (n * n - n) / 2; UnionFind<ll> uf(n + 1); vector<ll> ans; for (int i = m - 1; i >= 0; i--) { ans.push_back(cnt); if (uf.find(a[i]) == uf.find(b[i])) continue; cnt -= uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int INF = 1 << 30; const int MOD = 1e9 + 7; const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; template <class Type> class UnionFind { public: vector<Type> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(Type x, Type 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); } Type find(Type k) { if (data[k] < 0) return (k); return (data[k] = find(data[k])); } Type size(Type k) { return (-data[find(k)]); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<ll> a(m), b(m); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } ll cnt = (n * n - n) / 2; UnionFind<ll> uf(n + 1); vector<ll> ans; for (int i = m - 1; i >= 0; i--) { ans.push_back(cnt); if (uf.find(a[i]) == uf.find(b[i])) continue; cnt -= uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; return 0; }
replace
44
45
44
45
0
p03108
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <list> #include <math.h> #include <numeric> #include <stack> #include <string> #include <vector> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<ULL> VULL; class MYCP { public: static const LL MOD_CONST_ATCODER = 1000 * 1000 * 1000 + 7; static LL DebugFlag; // 数値を区切って文字列にする static string MakeString_LongLong(vector<long long> const &numbers, string const &str) { if (numbers.size() == 0) return ""; string result = "" + to_string(numbers[0]); for (long long i = 1; i < numbers.size(); i++) { result += str; result += to_string(numbers[i]); } return result; } // 空白で区切る為のオーバーロード static string MakeString_LongLong(vector<long long> const &numbers) { if (numbers.size() == 0) return ""; string result = "" + to_string(numbers[0]); for (long long i = 1; i < numbers.size(); i++) { result += " "; result += to_string(numbers[i]); } return result; } // 文字列の配列を改行を挟んでまとめる static string MakeString_VectorString(vector<string> const &str) { string result = ""; for (long long i = 0; i < str.size(); i++) { result += str[i] + "\n"; } return result; } // 文字列を必要な個数だけ読み取る static vector<string> MyReadLineSplit(LL n) { vector<string> str(n); for (long long i = 0; i < n; i++) { std::cin >> str[i]; } return str; } // 数値を必要な個数だけ読み取る static vector<long long> ReadInts(long long number) { vector<long long> a(number); for (int i = 0; i < number; i++) { std::cin >> a[i]; } return a; } // 渡された自然数が素数ならtureを返す static bool PrimeCheck_Int(long long number) { if (number < 2) return false; for (ULL i = 2; i * i <= number; i++) { if (number % i == 0) return false; } return true; } // 渡された数値以下の素数表を作る static vector<long long> MakePrimeList(long long n) { vector<long long> list; LL i, j, p; bool flag; for (i = 2; i <= n; i++) { flag = true; for (j = 0; j < list.size(); j++) { if (!(list[j] * list[j] <= i)) break; if (i % list[j] == 0) { flag = false; break; } } if (flag) list.push_back(i); } return list; } // 文字列の分割 static vector<string> split(string const &str, char sep) { vector<std::string> v; // 分割結果を格納するベクター auto first = str.begin(); // テキストの最初を指すイテレータ while (first != str.end()) { // テキストが残っている間ループ auto last = first; // 分割文字列末尾へのイテレータ while (last != str.end() && *last != sep) // 末尾 or セパレータ文字まで進める last++; v.push_back(string(first, last)); // 分割文字を出力 if (last != str.end()) last++; first = last; // 次の処理のためにイテレータを設定 } return v; } // 合計を求める static LL Sum(vector<LL> a) { LL i, sum = 0; for (i = 0; i < a.size(); i++) { sum += a[i]; } return sum; } // 小文字ならtrueを返す static bool Komoji(char a) { if (a >= 'a' && a <= 'z') return true; return false; } // 大文字ならtrueを返す static bool Oomoji(char a) { if (a >= 'A' && a <= 'Z') return true; return false; } // 切り上げの整数値割り算 static LL KiriageWarizan(LL a, LL b) { LL result = a / b; if (a % b > 0) result++; return result; } // 最大公約数 static LL GreatestCommonFactor(LL a, LL b) { LL temp; if (a < b) { temp = b; b = a; a = temp; } while (true) { temp = a % b; a = b; b = temp; if (b == 0) break; } return a; } // 最小公倍数 static LL LeastCommonMultiple(LL a, LL b) { return (a / GreatestCommonFactor(a, b)) * b; } // 素因数分解 static vector<VLL> PrimeFactorization(LL n) { VLL p_list, s_list; LL i, j, k, count; for (i = 2; n > 1; i++) { if (i * i > n) { p_list.push_back(n); s_list.push_back(1); break; } if (n % i == 0) { count = 0; while (n % i == 0) { n /= i; count++; } p_list.push_back(i); s_list.push_back(count); } } vector<VLL> result; result.push_back(p_list); result.push_back(s_list); return result; } // 組み合わせ nCr static LL Combination(LL n, LL r) { r = min(r, n - r); VLL p(n + 1, 0); LL i, j, k, a, b, c; for (i = 1; i <= r; i++) { auto temp = MYCP::PrimeFactorization(i); for (j = 0; j < temp[0].size(); j++) { p[temp[0][j]] -= temp[1][j]; } a = i + n - r; temp = MYCP::PrimeFactorization(a); for (j = 0; j < temp[0].size(); j++) { p[temp[0][j]] += temp[1][j]; } } LL result = 1; for (i = 0; i < p.size(); i++) { if (p[i] > 0) { for (j = 0; j < p[i]; j++) { result *= i; result %= MYCP::MOD_CONST_ATCODER; } } } return result; } // 符号 static LL sign(LL x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } // 円周率 static double PI() { return (double)3.1415926535898; } // 指定した桁でdoubleを出す static void CoutDoubleKeta(double a, LL keta) { cout << setprecision(keta) << a << flush; } // 数値の出力 static void CoutVectorLL(VLL list) { LL i, j, k, n; n = list.size(); for (i = 0; i < n - 1; i++) { cout << list[i] << " "; } cout << list[n - 1] << flush; } // デバッグ用出力 static LL DebugPrintf(string output) { if (MYCP::DebugFlag != 0) { std::cout << output << endl; } return MYCP::DebugFlag; } // デバッグ用入力 static LL DebugCin() { LL a; if (MYCP::DebugFlag != 0) { cin >> a; } return a; } }; LL MYCP::DebugFlag = 0; // 累積和を求めるクラス class Syakutori { private: vector<LL> list; public: void MakeArray(vector<LL> data) { LL i; list = data; list.push_back(0); list[0] = 0; for (i = 1; i < list.size(); i++) { list[i] = list[i - 1] + data[i - 1]; } } LL Sum(LL start, LL end) { if (end < start) { std::cout << "startがendより大きいです"; return 0; } if (start < 0 || end >= list.size()) { std::cout << "範囲が異常"; return 0; } return list[end] - list[start]; } }; // n進数を管理するクラス class N_Number { public: N_Number(LL n, LL keta) { this->N_Shinsuu = n; VLL temp(keta, 0); this->numbers = temp; } // 数を足す void plus(LL a) { if (a < 0) { a *= (-1); this->minus(a); return; } this->numbers[0] += a; LL size = this->numbers.size(); for (LL i = 0; i < size; i++) { if (i + 1 < size) { this->numbers[i + 1] += this->numbers[i] / this->N_Shinsuu; } this->numbers[i] %= this->N_Shinsuu; } } // 全ての桁が同じ数字になっていればその数字を返す。それ以外の場合は -1 を返す LL check() { LL a = this->numbers[0]; for (LL i = 0; i < this->numbers.size(); i++) { if (this->numbers[i] != a) return -1; } return a; } LL getNumber(LL keta) { return this->numbers[keta]; } LL getKeta() { return this->numbers.size(); } LL getShinsuu() { return this->N_Shinsuu; } void setNumber(LL keta, LL number) { if (0 <= number && number < this->getShinsuu()) { if (0 <= keta && keta < this->getKeta()) { this->numbers[keta] = number; return; } } cout << "er" << endl; } void setAllNumbers(LL number) { LL size = this->getKeta(), i; for (i = 0; i < size; i++) { this->setNumber(i, number); } } string to_string_KetaSoroe() { string s = ""; for (LL i = this->getKeta() - 1; i >= 0; i--) { s += to_string(this->getNumber(i)); } return s; } private: void minus(LL a) { LL i, j, k, zettaiti = abs(a); k = MYCP::KiriageWarizan(zettaiti, this->N_Shinsuu); j = k * (this->N_Shinsuu - 1); for (i = 0; i < this->getKeta(); i++) { this->numbers[i] += j; } this->numbers[0] += k - a; this->plus(0); } VLL numbers; LL N_Shinsuu; }; // UnionFind class Union_Find { private: VLL tree; VLL count; LL root(LL a) { if (this->tree[a] == a) return a; return this->tree[a] = this->root(this->tree[a]); } public: Union_Find(LL n) { VLL set(n); this->tree = set; this->count = set; for (LL i = 0; i < n; i++) { this->tree[i] = i; this->count[i] = 1; } } void unite(LL a, LL b) { LL x, y; if (!this->Check(a, b)) { x = this->getCount(a) + getCount(b); y = this->root(a); this->count[y] = x; y = this->root(b); this->count[y] = x; } x = this->root(a); y = this->root(b); this->tree[x] = y; } bool Check(LL a, LL b) { return this->root(a) == this->root(b); } LL getCount(LL index) { LL temp = this->root(index); return this->count[temp]; } }; // ここからメイン int main(void) { MYCP::DebugFlag = 0; LL i, j, k, n, m; cin >> n >> m; VLL a, b(m); a = b; for (i = 0; i < m; i++) { cin >> a[i] >> b[i]; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); VLL ans(m); LL huben = n * (n - 1) / 2; Union_Find uf(n); for (i = 0; i < m; i++) { ans[i] = huben; if (!uf.Check(a[i], b[i])) { k = uf.getCount(a[i]) * uf.getCount(b[i]); huben -= k; } uf.unite(a[i], b[i]); } reverse(ans.begin(), ans.end()); for (i = 0; i < m; i++) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <list> #include <math.h> #include <numeric> #include <stack> #include <string> #include <vector> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<ULL> VULL; class MYCP { public: static const LL MOD_CONST_ATCODER = 1000 * 1000 * 1000 + 7; static LL DebugFlag; // 数値を区切って文字列にする static string MakeString_LongLong(vector<long long> const &numbers, string const &str) { if (numbers.size() == 0) return ""; string result = "" + to_string(numbers[0]); for (long long i = 1; i < numbers.size(); i++) { result += str; result += to_string(numbers[i]); } return result; } // 空白で区切る為のオーバーロード static string MakeString_LongLong(vector<long long> const &numbers) { if (numbers.size() == 0) return ""; string result = "" + to_string(numbers[0]); for (long long i = 1; i < numbers.size(); i++) { result += " "; result += to_string(numbers[i]); } return result; } // 文字列の配列を改行を挟んでまとめる static string MakeString_VectorString(vector<string> const &str) { string result = ""; for (long long i = 0; i < str.size(); i++) { result += str[i] + "\n"; } return result; } // 文字列を必要な個数だけ読み取る static vector<string> MyReadLineSplit(LL n) { vector<string> str(n); for (long long i = 0; i < n; i++) { std::cin >> str[i]; } return str; } // 数値を必要な個数だけ読み取る static vector<long long> ReadInts(long long number) { vector<long long> a(number); for (int i = 0; i < number; i++) { std::cin >> a[i]; } return a; } // 渡された自然数が素数ならtureを返す static bool PrimeCheck_Int(long long number) { if (number < 2) return false; for (ULL i = 2; i * i <= number; i++) { if (number % i == 0) return false; } return true; } // 渡された数値以下の素数表を作る static vector<long long> MakePrimeList(long long n) { vector<long long> list; LL i, j, p; bool flag; for (i = 2; i <= n; i++) { flag = true; for (j = 0; j < list.size(); j++) { if (!(list[j] * list[j] <= i)) break; if (i % list[j] == 0) { flag = false; break; } } if (flag) list.push_back(i); } return list; } // 文字列の分割 static vector<string> split(string const &str, char sep) { vector<std::string> v; // 分割結果を格納するベクター auto first = str.begin(); // テキストの最初を指すイテレータ while (first != str.end()) { // テキストが残っている間ループ auto last = first; // 分割文字列末尾へのイテレータ while (last != str.end() && *last != sep) // 末尾 or セパレータ文字まで進める last++; v.push_back(string(first, last)); // 分割文字を出力 if (last != str.end()) last++; first = last; // 次の処理のためにイテレータを設定 } return v; } // 合計を求める static LL Sum(vector<LL> a) { LL i, sum = 0; for (i = 0; i < a.size(); i++) { sum += a[i]; } return sum; } // 小文字ならtrueを返す static bool Komoji(char a) { if (a >= 'a' && a <= 'z') return true; return false; } // 大文字ならtrueを返す static bool Oomoji(char a) { if (a >= 'A' && a <= 'Z') return true; return false; } // 切り上げの整数値割り算 static LL KiriageWarizan(LL a, LL b) { LL result = a / b; if (a % b > 0) result++; return result; } // 最大公約数 static LL GreatestCommonFactor(LL a, LL b) { LL temp; if (a < b) { temp = b; b = a; a = temp; } while (true) { temp = a % b; a = b; b = temp; if (b == 0) break; } return a; } // 最小公倍数 static LL LeastCommonMultiple(LL a, LL b) { return (a / GreatestCommonFactor(a, b)) * b; } // 素因数分解 static vector<VLL> PrimeFactorization(LL n) { VLL p_list, s_list; LL i, j, k, count; for (i = 2; n > 1; i++) { if (i * i > n) { p_list.push_back(n); s_list.push_back(1); break; } if (n % i == 0) { count = 0; while (n % i == 0) { n /= i; count++; } p_list.push_back(i); s_list.push_back(count); } } vector<VLL> result; result.push_back(p_list); result.push_back(s_list); return result; } // 組み合わせ nCr static LL Combination(LL n, LL r) { r = min(r, n - r); VLL p(n + 1, 0); LL i, j, k, a, b, c; for (i = 1; i <= r; i++) { auto temp = MYCP::PrimeFactorization(i); for (j = 0; j < temp[0].size(); j++) { p[temp[0][j]] -= temp[1][j]; } a = i + n - r; temp = MYCP::PrimeFactorization(a); for (j = 0; j < temp[0].size(); j++) { p[temp[0][j]] += temp[1][j]; } } LL result = 1; for (i = 0; i < p.size(); i++) { if (p[i] > 0) { for (j = 0; j < p[i]; j++) { result *= i; result %= MYCP::MOD_CONST_ATCODER; } } } return result; } // 符号 static LL sign(LL x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } // 円周率 static double PI() { return (double)3.1415926535898; } // 指定した桁でdoubleを出す static void CoutDoubleKeta(double a, LL keta) { cout << setprecision(keta) << a << flush; } // 数値の出力 static void CoutVectorLL(VLL list) { LL i, j, k, n; n = list.size(); for (i = 0; i < n - 1; i++) { cout << list[i] << " "; } cout << list[n - 1] << flush; } // デバッグ用出力 static LL DebugPrintf(string output) { if (MYCP::DebugFlag != 0) { std::cout << output << endl; } return MYCP::DebugFlag; } // デバッグ用入力 static LL DebugCin() { LL a; if (MYCP::DebugFlag != 0) { cin >> a; } return a; } }; LL MYCP::DebugFlag = 0; // 累積和を求めるクラス class Syakutori { private: vector<LL> list; public: void MakeArray(vector<LL> data) { LL i; list = data; list.push_back(0); list[0] = 0; for (i = 1; i < list.size(); i++) { list[i] = list[i - 1] + data[i - 1]; } } LL Sum(LL start, LL end) { if (end < start) { std::cout << "startがendより大きいです"; return 0; } if (start < 0 || end >= list.size()) { std::cout << "範囲が異常"; return 0; } return list[end] - list[start]; } }; // n進数を管理するクラス class N_Number { public: N_Number(LL n, LL keta) { this->N_Shinsuu = n; VLL temp(keta, 0); this->numbers = temp; } // 数を足す void plus(LL a) { if (a < 0) { a *= (-1); this->minus(a); return; } this->numbers[0] += a; LL size = this->numbers.size(); for (LL i = 0; i < size; i++) { if (i + 1 < size) { this->numbers[i + 1] += this->numbers[i] / this->N_Shinsuu; } this->numbers[i] %= this->N_Shinsuu; } } // 全ての桁が同じ数字になっていればその数字を返す。それ以外の場合は -1 を返す LL check() { LL a = this->numbers[0]; for (LL i = 0; i < this->numbers.size(); i++) { if (this->numbers[i] != a) return -1; } return a; } LL getNumber(LL keta) { return this->numbers[keta]; } LL getKeta() { return this->numbers.size(); } LL getShinsuu() { return this->N_Shinsuu; } void setNumber(LL keta, LL number) { if (0 <= number && number < this->getShinsuu()) { if (0 <= keta && keta < this->getKeta()) { this->numbers[keta] = number; return; } } cout << "er" << endl; } void setAllNumbers(LL number) { LL size = this->getKeta(), i; for (i = 0; i < size; i++) { this->setNumber(i, number); } } string to_string_KetaSoroe() { string s = ""; for (LL i = this->getKeta() - 1; i >= 0; i--) { s += to_string(this->getNumber(i)); } return s; } private: void minus(LL a) { LL i, j, k, zettaiti = abs(a); k = MYCP::KiriageWarizan(zettaiti, this->N_Shinsuu); j = k * (this->N_Shinsuu - 1); for (i = 0; i < this->getKeta(); i++) { this->numbers[i] += j; } this->numbers[0] += k - a; this->plus(0); } VLL numbers; LL N_Shinsuu; }; // UnionFind class Union_Find { private: VLL tree; VLL count; LL root(LL a) { if (this->tree[a] == a) return a; return this->tree[a] = this->root(this->tree[a]); } public: Union_Find(LL n) { VLL set(n); this->tree = set; this->count = set; for (LL i = 0; i < n; i++) { this->tree[i] = i; this->count[i] = 1; } } void unite(LL a, LL b) { LL x, y; if (!this->Check(a, b)) { x = this->getCount(a) + getCount(b); y = this->root(a); this->count[y] = x; y = this->root(b); this->count[y] = x; } x = this->root(a); y = this->root(b); this->tree[x] = y; } bool Check(LL a, LL b) { return this->root(a) == this->root(b); } LL getCount(LL index) { LL temp = this->root(index); return this->count[temp]; } }; // ここからメイン int main(void) { MYCP::DebugFlag = 0; LL i, j, k, n, m; cin >> n >> m; VLL a, b(m); a = b; for (i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); VLL ans(m); LL huben = n * (n - 1) / 2; Union_Find uf(n); for (i = 0; i < m; i++) { ans[i] = huben; if (!uf.Check(a[i], b[i])) { k = uf.getCount(a[i]) * uf.getCount(b[i]); huben -= k; } uf.unite(a[i], b[i]); } reverse(ans.begin(), ans.end()); for (i = 0; i < m; i++) { cout << ans[i] << endl; } return 0; }
insert
503
503
503
505
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define RANGE(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; struct union_find { vector<ll> par; vector<ll> size; union_find(ll n) : par(n), size(n) { RANGE(i, n) { par[i] = i; size[i] = 1; } } void union_tree(ll i, ll j) { ll root_i = root(i); ll root_j = root(j); par[root_i] = root_j; size[root_j] = size[root_i] + size[root_j]; } ll root(ll i) { if (par[i] == i) return i; else root(par[i]); } bool same(ll i, ll j) { return root(i) == root(j); } ll get_size(ll i) { return size[root(i)]; } }; signed main() { ll N, M; cin >> N >> M; ll A[M]; ll B[M]; RANGE(i, M) { ll x, y; cin >> x >> y; A[i] = x - 1; B[i] = y - 1; } ll unconf = (N - 1) * N / 2; stack<ll> result; union_find tree(N); for (ll i = M - 1; i > -1; i--) { result.push(unconf); if (!tree.same(A[i], B[i])) { ll a_size = tree.get_size(A[i]); ll b_size = tree.get_size(B[i]); unconf -= a_size * b_size; tree.union_tree(A[i], B[i]); } } while (!result.empty()) { cout << result.top() << endl; result.pop(); } }
#include <bits/stdc++.h> #define RANGE(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; struct union_find { vector<ll> par; vector<ll> size; union_find(ll n) : par(n), size(n) { RANGE(i, n) { par[i] = i; size[i] = 1; } } void union_tree(ll i, ll j) { ll root_i = root(i); ll root_j = root(j); par[root_i] = root_j; size[root_j] = size[root_i] + size[root_j]; } ll root(ll i) { ll now = i; vector<ll> on_route; while (par[now] != now) { on_route.push_back(now); now = par[now]; } for (ll node : on_route) par[node] = now; return now; } bool same(ll i, ll j) { return root(i) == root(j); } ll get_size(ll i) { return size[root(i)]; } }; signed main() { ll N, M; cin >> N >> M; ll A[M]; ll B[M]; RANGE(i, M) { ll x, y; cin >> x >> y; A[i] = x - 1; B[i] = y - 1; } ll unconf = (N - 1) * N / 2; stack<ll> result; union_find tree(N); for (ll i = M - 1; i > -1; i--) { result.push(unconf); if (!tree.same(A[i], B[i])) { ll a_size = tree.get_size(A[i]); ll b_size = tree.get_size(B[i]); unconf -= a_size * b_size; tree.union_tree(A[i], B[i]); } } while (!result.empty()) { cout << result.top() << endl; result.pop(); } }
replace
25
29
25
34
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define mod 1000000007 #define For(i, n) for (int i = 0; i < n; i++) #define Fre(i, a, b) for (int i = a; i < b; i++) #define sf(n) scanf("%d", &n) #define sff(a, b) scanf("%d %d", &a, &b) #define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c) #define pfn(n) printf("%d\n", n) #define pfs(n) printf("%d ", n) #define ff first #define ss second #define mem(a, b) memset(a, b, sizeof(a)) #define READ freopen("in.txt", "r", stdin) #define WRITE freopen("out.txt", "w", stdout) #define sz size() #define number_of_digit(n) __builtin_ffs(n); #define ll long long #define T int #define ld long double inline void print(T n) { if (n == 0) { putchar('0'); putchar('\n'); } else if (n == -1) { putchar('-'); putchar('1'); putchar('\n'); } else { char buf[11]; buf[10] = '\n'; int i = 9; while (n) { buf[i--] = n % 10 + '0'; n /= 10; } while (buf[i] != '\n') putchar(buf[++i]); } } int read() { int cc = getc(stdin); for (; cc < '0' || cc > '9';) cc = getc(stdin); int ret = 0; for (; cc >= '0' && cc <= '9';) { ret = ret * 10 + cc - '0'; cc = getc(stdin); } return ret; } ll power(ll num, ll g) { if (g == 0) return 1; if (g % 2 == 1) return (num * power((num * num), g / 2)); return power((num * num), g / 2); } ll a[100005]; ll find(ll index) { if (a[index] == index) return index; return a[index] = find(a[index]); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; pair<ll, ll> pa[m]; For(i, m) { cin >> pa[i].ff >> pa[i].ss; } For(i, n + 1) a[i] = i; ll size[n + 1]; For(i, n + 1) size[i] = 1; ll ans = n; ans *= n - 1; ans /= 2; stack<ll> st; for (ll i = m - 1; i >= 0; i--) { st.push(ans); ll u = find(pa[i].ff); ll v = find(pa[i].ss); if (u == v) { // st.push(ans); } else { a[u] = a[v]; ans -= size[v] * size[u]; size[v] += size[u]; // st.push(ans); } } while (!st.empty()) { cout << st.top() << endl; st.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define mod 1000000007 #define For(i, n) for (int i = 0; i < n; i++) #define Fre(i, a, b) for (int i = a; i < b; i++) #define sf(n) scanf("%d", &n) #define sff(a, b) scanf("%d %d", &a, &b) #define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c) #define pfn(n) printf("%d\n", n) #define pfs(n) printf("%d ", n) #define ff first #define ss second #define mem(a, b) memset(a, b, sizeof(a)) #define READ freopen("in.txt", "r", stdin) #define WRITE freopen("out.txt", "w", stdout) #define sz size() #define number_of_digit(n) __builtin_ffs(n); #define ll long long #define T int #define ld long double inline void print(T n) { if (n == 0) { putchar('0'); putchar('\n'); } else if (n == -1) { putchar('-'); putchar('1'); putchar('\n'); } else { char buf[11]; buf[10] = '\n'; int i = 9; while (n) { buf[i--] = n % 10 + '0'; n /= 10; } while (buf[i] != '\n') putchar(buf[++i]); } } int read() { int cc = getc(stdin); for (; cc < '0' || cc > '9';) cc = getc(stdin); int ret = 0; for (; cc >= '0' && cc <= '9';) { ret = ret * 10 + cc - '0'; cc = getc(stdin); } return ret; } ll power(ll num, ll g) { if (g == 0) return 1; if (g % 2 == 1) return (num * power((num * num), g / 2)); return power((num * num), g / 2); } ll a[100005]; ll find(ll index) { if (a[index] == index) return index; return a[index] = find(a[index]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; pair<ll, ll> pa[m]; For(i, m) { cin >> pa[i].ff >> pa[i].ss; } For(i, n + 1) a[i] = i; ll size[n + 1]; For(i, n + 1) size[i] = 1; ll ans = n; ans *= n - 1; ans /= 2; stack<ll> st; for (ll i = m - 1; i >= 0; i--) { st.push(ans); ll u = find(pa[i].ff); ll v = find(pa[i].ss); if (u == v) { // st.push(ans); } else { a[u] = a[v]; ans -= size[v] * size[u]; size[v] += size[u]; // st.push(ans); } } while (!st.empty()) { cout << st.top() << endl; st.pop(); } return 0; }
replace
68
72
68
69
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr long long MOD = 1000000007; constexpr long long INF = 1LL << 60; const long double PI = acosl(-1.0); constexpr long double EPS = 1e-11; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // issame:同じか判定 // unite:併合 // findroot:根の検索 // treesize:その木の要素数 struct UnionFind { vector<long long> size, par; UnionFind(long long n) { size.resize(n, 0); par.resize(n, 0); for (long long i = 0; i < n; i++) { maketree(i); } } void maketree(long long x) { par[x] = x; size[x] = 1; } long long findroot(long long x) { if (x != par[x]) par[x] = findroot(par[x]); return par[x]; } bool issame(long long x, long long y) { return findroot(x) == findroot(y); } bool unite(long long x, long long y) { x = findroot(x); y = findroot(y); if (x == y) return false; // already united if (size[x] > size[y]) { par[y] = x; size[x] += size[y]; } else { par[x] = y; size[y] += size[x]; } return true; } long long treesize(long long x) { return size[findroot(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<ll> a(m), b(m); for (ll i = 0; i < m; i++) { cin >> a[i] >> b[i]; } vector<ll> ans(m, 0); UnionFind uf(n); ans[m - 1] = n * (n - 1) / 2; for (ll i = m - 2; i >= 0; i--) { if (uf.issame(a[i + 1], b[i + 1])) ans[i] = ans[i + 1]; else { ans[i] = ans[i + 1] - uf.treesize(a[i + 1]) * uf.treesize(b[i + 1]); } uf.unite(a[i + 1], b[i + 1]); } for (ll i = 0; i < m; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr long long MOD = 1000000007; constexpr long long INF = 1LL << 60; const long double PI = acosl(-1.0); constexpr long double EPS = 1e-11; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // issame:同じか判定 // unite:併合 // findroot:根の検索 // treesize:その木の要素数 struct UnionFind { vector<long long> size, par; UnionFind(long long n) { size.resize(n, 0); par.resize(n, 0); for (long long i = 0; i < n; i++) { maketree(i); } } void maketree(long long x) { par[x] = x; size[x] = 1; } long long findroot(long long x) { if (x != par[x]) par[x] = findroot(par[x]); return par[x]; } bool issame(long long x, long long y) { return findroot(x) == findroot(y); } bool unite(long long x, long long y) { x = findroot(x); y = findroot(y); if (x == y) return false; // already united if (size[x] > size[y]) { par[y] = x; size[x] += size[y]; } else { par[x] = y; size[y] += size[x]; } return true; } long long treesize(long long x) { return size[findroot(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<ll> a(m), b(m); for (ll i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } vector<ll> ans(m, 0); UnionFind uf(n); ans[m - 1] = n * (n - 1) / 2; for (ll i = m - 2; i >= 0; i--) { if (uf.issame(a[i + 1], b[i + 1])) ans[i] = ans[i + 1]; else { ans[i] = ans[i + 1] - uf.treesize(a[i + 1]) * uf.treesize(b[i + 1]); } uf.unite(a[i + 1], b[i + 1]); } for (ll i = 0; i < m; i++) { cout << ans[i] << endl; } }
insert
67
67
67
69
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define ve vector 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; ve<P> e(m); ve<ll> ans(m); rep(i, m) { int a, b; cin >> a >> b; a--; b--; e[i] = P(a, b); } reverse(e.begin(), e.end()); ans[0] = (n - 1) * n / 2; UnionFind uf(n); rep(i, m) { if (uf.same(e[i].first, e[i].second)) ans[i + 1] = ans[i]; else ans[i + 1] = ans[i] - uf.size(e[i].first) * uf.size(e[i].second); uf.unite(e[i].first, e[i].second); } reverse(ans.begin(), ans.end()); rep(i, m) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define ve vector 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; ve<P> e(m); ve<ll> ans(m); rep(i, m) { int a, b; cin >> a >> b; a--; b--; e[i] = P(a, b); } reverse(e.begin(), e.end()); ans[0] = (n - 1) * n / 2; UnionFind uf(n); rep(i, m - 1) { if (uf.same(e[i].first, e[i].second)) ans[i + 1] = ans[i]; else ans[i + 1] = ans[i] - uf.size(e[i].first) * uf.size(e[i].second); uf.unite(e[i].first, e[i].second); } reverse(ans.begin(), ans.end()); rep(i, m) cout << ans[i] << endl; }
replace
45
46
45
46
-6
free(): invalid pointer
p03108
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} int find(int x) { if (par[x] < 0) return x; return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (par[x] > par[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; } return true; } bool same(int x, int y) { return find(x) == find(y); } ll size(int x) { return -par[find(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<pint> p(m); rep(i, m) { int x, y; cin >> x >> y; --x; --y; p[m - i - 1] = {x, y}; } UnionFind uf(n); vector<ll> ans(m); ans[0] = n * (n - 1) / 2; rep(i, m) { ans[i + 1] = ans[i] - uf.size(p[i].first) * uf.size(p[i].second); uf.unite(p[i].first, p[i].second); } rep(i, m) cout << ans[m - i - 1] << endl; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) typedef long long ll; typedef pair<int, int> pint; typedef pair<ll, int> pli; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} int find(int x) { if (par[x] < 0) return x; return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (par[x] > par[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; } return true; } bool same(int x, int y) { return find(x) == find(y); } ll size(int x) { return -par[find(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<pint> p(m); rep(i, m) { int x, y; cin >> x >> y; --x; --y; p[m - i - 1] = {x, y}; } UnionFind uf(n); vector<ll> ans(m); ans[0] = n * (n - 1) / 2; rep(i, m - 1) { if (!uf.same(p[i].first, p[i].second)) ans[i + 1] = ans[i] - uf.size(p[i].first) * uf.size(p[i].second); else ans[i + 1] = ans[i]; uf.unite(p[i].first, p[i].second); } rep(i, m) cout << ans[m - i - 1] << endl; return 0; }
replace
66
68
66
71
-6
malloc(): corrupted top size
p03108
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; // find_by_order gives kth-largest element // order_of_key gives the number of items in the set that are strictly smaller!! // OT.find_by_order(k-1) // typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define db(x) cerr << #x << "=" << x << endl #define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define db3(x, y, z) \ cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << endl #define db4(x, y, z, w) \ cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << "," << #w << "=" << w << endl #define ll long long #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define mp make_pair #define X first #define Y second #define sz(x) (int)((x).size()) #define pii pair<int, int> #define MOD (ll)(998244353) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repA(i, a, n) for (int i = a; i <= (n); ++i) #define repD(i, a, n) for (int i = a; i >= (n); --i) #define trav(a, x) for (auto &a : x) typedef vector<int> vi; #define inf (ll)(1e18) #define double long double #define int long long ////////////////////// const int N = 15; int rnk[N], parent[N], total[N]; int find(int x) { if (parent[x] < 0) return x; return parent[x] = find(parent[x]); } int merge(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; if (rnk[y] > rnk[x]) swap(x, y); parent[y] = x; rnk[x] = rnk[x] == rnk[y] ? rnk[x] + 1 : rnk[x]; int sv = total[x]; total[x] += total[y]; return sv * total[y]; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; rep(i, N) { parent[i] = -1; total[i] = 1; rnk[i] = 1; } vector<pii> bridge(m); rep(i, m) cin >> bridge[i].X >> bridge[i].Y; reverse(all(bridge)); // int prev = 0; vector<int> store(m); rep(i, m) { store[i] = merge(bridge[i].X, bridge[i].Y); } reverse(all(store)); cout << store[0] << '\n'; repA(i, 1, m - 1) { store[i] += store[i - 1]; cout << store[i] << '\n'; } }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; // find_by_order gives kth-largest element // order_of_key gives the number of items in the set that are strictly smaller!! // OT.find_by_order(k-1) // typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define db(x) cerr << #x << "=" << x << endl #define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define db3(x, y, z) \ cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << endl #define db4(x, y, z, w) \ cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << "," << #w << "=" << w << endl #define ll long long #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define mp make_pair #define X first #define Y second #define sz(x) (int)((x).size()) #define pii pair<int, int> #define MOD (ll)(998244353) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repA(i, a, n) for (int i = a; i <= (n); ++i) #define repD(i, a, n) for (int i = a; i >= (n); --i) #define trav(a, x) for (auto &a : x) typedef vector<int> vi; #define inf (ll)(1e18) #define double long double #define int long long ////////////////////// const int N = 100005; int rnk[N], parent[N], total[N]; int find(int x) { if (parent[x] < 0) return x; return parent[x] = find(parent[x]); } int merge(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; if (rnk[y] > rnk[x]) swap(x, y); parent[y] = x; rnk[x] = rnk[x] == rnk[y] ? rnk[x] + 1 : rnk[x]; int sv = total[x]; total[x] += total[y]; return sv * total[y]; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; rep(i, N) { parent[i] = -1; total[i] = 1; rnk[i] = 1; } vector<pii> bridge(m); rep(i, m) cin >> bridge[i].X >> bridge[i].Y; reverse(all(bridge)); // int prev = 0; vector<int> store(m); rep(i, m) { store[i] = merge(bridge[i].X, bridge[i].Y); } reverse(all(store)); cout << store[0] << '\n'; repA(i, 1, m - 1) { store[i] += store[i - 1]; cout << store[i] << '\n'; } }
replace
44
45
44
45
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } #define CIN(T, x) \ T x; \ cin >> x; #define CINS(T, x, N) \ vector<T> x(N); \ for (int i = 0; i < (N); i++) { \ cin >> x[i]; \ } #define SHOW(x) \ for (int i = 0; i < x.size(); i++) { \ cout << x[i] << " "; \ } \ cout << "\n"; #define SHOW2(x) \ for (int j = 0; j < x.size(); j++) { \ SHOW(x[j]); \ } \ cout << "\n"; int N, M; // int N_MAX = 1e5+5; int N_MAX = 30; vector<int> parent(N_MAX); vector<ll> treeSize(N_MAX, 1); int root(int x) { if (x == parent[x]) return x; 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) return; if (treeSize[rx] >= treeSize[ry]) { parent[ry] = rx; treeSize[rx] += treeSize[ry]; treeSize[ry] = 0; } else { parent[rx] = ry; treeSize[ry] += treeSize[rx]; treeSize[rx] = 0; } } bool same(int x, int y) { return root(x) == root(y); } int main() { cin >> N >> M; vector<int> A(M); vector<int> B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> ans(M); ans[M - 1] = N * (N - 1) / 2; for (int i = 0; i < N; i++) { parent[i] = i; } for (int i = M - 1; i > 0; i--) { if (same(A[i], B[i])) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - treeSize[parent[A[i]]] * treeSize[parent[B[i]]]; unite(A[i], B[i]); } } for (int i = 0; i < M; i++) { cout << ans[i] << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } #define CIN(T, x) \ T x; \ cin >> x; #define CINS(T, x, N) \ vector<T> x(N); \ for (int i = 0; i < (N); i++) { \ cin >> x[i]; \ } #define SHOW(x) \ for (int i = 0; i < x.size(); i++) { \ cout << x[i] << " "; \ } \ cout << "\n"; #define SHOW2(x) \ for (int j = 0; j < x.size(); j++) { \ SHOW(x[j]); \ } \ cout << "\n"; ll N, M; int N_MAX = 1e5 + 5; // int N_MAX = 30; vector<int> parent(N_MAX); vector<ll> treeSize(N_MAX, 1); int root(int x) { if (x == parent[x]) return x; 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) return; if (treeSize[rx] >= treeSize[ry]) { parent[ry] = rx; treeSize[rx] += treeSize[ry]; treeSize[ry] = 0; } else { parent[rx] = ry; treeSize[ry] += treeSize[rx]; treeSize[rx] = 0; } } bool same(int x, int y) { return root(x) == root(y); } int main() { cin >> N >> M; vector<int> A(M); vector<int> B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> ans(M); ans[M - 1] = N * (N - 1) / 2; for (int i = 0; i < N; i++) { parent[i] = i; } for (int i = M - 1; i > 0; i--) { if (same(A[i], B[i])) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - treeSize[parent[A[i]]] * treeSize[parent[B[i]]]; unite(A[i], B[i]); } } for (int i = 0; i < M; i++) { cout << ans[i] << "\n"; } return 0; }
replace
25
28
25
28
0
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int N_MAX = 100001; typedef pair<int, int> P; int main() { long long n, m; cin >> n >> m; P b[N_MAX]; int c[N_MAX], nc[N_MAX], pc[N_MAX], pi[N_MAX]; long long res[N_MAX]; int i1, i2, s1, s2, p, pt; long long c1, c2; for (int i = 0; i < m; i++) cin >> b[i].first >> b[i].second; for (int i = 1; i <= n; i++) { c[i] = i; nc[i] = 1; pc[i] = i; pi[i] = 0; } res[m - 1] = n * (n - 1) / 2; for (int i = m - 1; i > 0; i--) { i1 = b[i].first; i2 = b[i].second; if (c[i1] == c[i2]) res[i - 1] = res[i]; else { s1 = c[i1]; s2 = c[i2]; res[i - 1] = res[i] - nc[s1] * nc[s2]; pt = pc[s1]; p = pc[s2]; pc[s1] = p; nc[s1] += nc[s2]; pc[s2] = 0; nc[s2] = 0; while (true) { c[p] = s1; if (pi[p] == 0) break; p = pi[p]; } pi[p] = pt; } } for (int i = 0; i < m; i++) cout << res[i] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int N_MAX = 100001; typedef pair<int, int> P; int main() { long long n, m; cin >> n >> m; P b[N_MAX]; int c[N_MAX], nc[N_MAX], pc[N_MAX], pi[N_MAX]; long long res[N_MAX]; int i1, i2, s1, s2, p, pt; long long c1, c2; for (int i = 0; i < m; i++) cin >> b[i].first >> b[i].second; for (int i = 1; i <= n; i++) { c[i] = i; nc[i] = 1; pc[i] = i; pi[i] = 0; } res[m - 1] = n * (n - 1) / 2; for (int i = m - 1; i > 0; i--) { i1 = b[i].first; i2 = b[i].second; if (c[i1] == c[i2]) res[i - 1] = res[i]; else { s1 = c[i1]; s2 = c[i2]; if (nc[s1] < nc[s2]) { s1 = c[i2]; s2 = c[i1]; } res[i - 1] = res[i] - nc[s1] * nc[s2]; pt = pc[s1]; p = pc[s2]; pc[s1] = p; nc[s1] += nc[s2]; pc[s2] = 0; nc[s2] = 0; while (true) { c[p] = s1; if (pi[p] == 0) break; p = pi[p]; } pi[p] = pt; } } for (int i = 0; i < m; i++) cout << res[i] << endl; return 0; }
insert
31
31
31
35
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long n, m; long long f[10001], yh[10001]; long long bb(long long p) { if (f[p] == p) return p; return f[p] = bb(f[p]); } int main() { cin >> n >> m; long long i, x[m + 1], y[m + 1], ans[m], j, k, s, z = n; for (i = 1; i <= n; i++) { f[i] = i; yh[i] = 1; } for (i = 1; i <= m; i++) cin >> x[i] >> y[i]; long long jk = ans[m - 1] = n * (n - 1) / 2; for (i = m; i > 0; i--) { long long zux = bb(x[i]), zuy = bb(y[i]); if (zux != zuy) { f[zux] = zuy; jk -= yh[zux] * yh[zuy]; yh[zuy] += yh[zux]; } ans[i - 2] = jk; } for (i = 0; i < m; i++) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m; long long f[100010], yh[100010]; long long bb(long long p) { if (f[p] == p) return p; return f[p] = bb(f[p]); } int main() { cin >> n >> m; long long i, x[m + 1], y[m + 1], ans[m], j, k, s, z = n; for (i = 1; i <= n; i++) { f[i] = i; yh[i] = 1; } for (i = 1; i <= m; i++) cin >> x[i] >> y[i]; long long jk = ans[m - 1] = n * (n - 1) / 2; for (i = m; i > 0; i--) { long long zux = bb(x[i]), zuy = bb(y[i]); if (zux != zuy) { f[zux] = zuy; jk -= yh[zux] * yh[zuy]; yh[zuy] += yh[zux]; } ans[i - 2] = jk; } for (i = 0; i < m; i++) cout << ans[i] << endl; return 0; }
replace
3
4
3
4
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; const ll MOD = 1000000007; class UnionFind { public: vector<int> par; vector<int> rank; UnionFind(int n) { init(n); } void init(int n) { par.resize(n); for (int i = 0; i < n; i++) { par[i] = i; } rank.assign(n, 0); } 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]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> size(N, 1); UnionFind uf(N); vector<ll> ans(N); for (int i = M - 1; i >= 0; i--) { if (uf.same(A[i], B[i])) { ans[i] = 0; } else { ll a_size = size[uf.find(A[i])], b_size = size[uf.find(B[i])]; ans[i] = a_size * b_size; uf.unite(A[i], B[i]); size[uf.find(A[i])] = a_size + b_size; } } ll cumsum = 0; for (int i = 0; i < M; i++) { cumsum += ans[i]; cout << cumsum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; const ll MOD = 1000000007; class UnionFind { public: vector<int> par; vector<int> rank; UnionFind(int n) { init(n); } void init(int n) { par.resize(n); for (int i = 0; i < n; i++) { par[i] = i; } rank.assign(n, 0); } 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]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> size(N, 1); UnionFind uf(N); vector<ll> ans(M); for (int i = M - 1; i >= 0; i--) { if (uf.same(A[i], B[i])) { ans[i] = 0; } else { ll a_size = size[uf.find(A[i])], b_size = size[uf.find(B[i])]; ans[i] = a_size * b_size; uf.unite(A[i], B[i]); size[uf.find(A[i])] = a_size + b_size; } } ll cumsum = 0; for (int i = 0; i < M; i++) { cumsum += ans[i]; cout << cumsum << endl; } return 0; }
replace
56
57
56
57
0
p03108
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define REP(i, n) for (ll i = 0; i < (ll)n; i++) #define INF 1000000000000000 using namespace std; typedef long long ll; typedef double db; typedef string str; struct UnionFind { vector<ll> par; vector<ll> rank; vector<ll> sz; UnionFind(ll n) { init(n); } void init(ll n) { par.resize(n), rank.resize(n), sz.resize(n); REP(i, n) { par[i] = i, rank[i] = 0; sz[i] = 1; } } ll find(ll x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(ll x, ll y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; if (rank[x] == rank[y]) rank[x]++; } } bool same(ll x, ll y) { return find(x) == find(y); } ll size(ll x) { return sz[find(x)]; } }; int main() { ll n, m; cin >> n >> m; UnionFind uf(n); int a[m], b[m]; REP(i, m) cin >> a[i] >> b[i]; reverse(a, a + m); reverse(b, b + m); ll ans[m]; ans[0] = n * (n - 1) / 2; REP(i, m - 1) { if (uf.same(a[i], b[i])) { ans[i + 1] = ans[i]; } else { ans[i + 1] = ans[i] - uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } reverse(ans, ans + m); REP(i, m) cout << ans[i] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define REP(i, n) for (ll i = 0; i < (ll)n; i++) #define INF 1000000000000000 using namespace std; typedef long long ll; typedef double db; typedef string str; struct UnionFind { vector<ll> par; vector<ll> rank; vector<ll> sz; UnionFind(ll n) { init(n); } void init(ll n) { par.resize(n), rank.resize(n), sz.resize(n); REP(i, n) { par[i] = i, rank[i] = 0; sz[i] = 1; } } ll find(ll x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(ll x, ll y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; if (rank[x] == rank[y]) rank[x]++; } } bool same(ll x, ll y) { return find(x) == find(y); } ll size(ll x) { return sz[find(x)]; } }; int main() { ll n, m; cin >> n >> m; UnionFind uf(n); int a[m], b[m]; REP(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } reverse(a, a + m); reverse(b, b + m); ll ans[m]; ans[0] = n * (n - 1) / 2; REP(i, m - 1) { if (uf.same(a[i], b[i])) { ans[i + 1] = ans[i]; } else { ans[i + 1] = ans[i] - uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } reverse(ans, ans + m); REP(i, m) cout << ans[i] << endl; return 0; }
replace
64
65
64
69
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define inf LLONG_MAX #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; struct UnionFind { vll par; vll count; UnionFind(ll n) { par.resize(n), count.resize(n); rep(i, n) par[i] = i, count[i] = 1; } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } void unite(ll x, ll y) { ll rx = root(x); ll ry = root(y); if (rx == ry) return; if (rx > ry) { count[rx] += count[ry]; par[ry] = rx; } else { count[ry] += count[rx]; par[rx] = ry; } } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return count[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vll A(M), B(M); rep(i, M) { cin >> A[i] >> B[i]; } UnionFind tree(N); vll ans(M); ll res = N * (N - 1) / 2; for (ll i = M - 1; i >= 0; i--) { ll a, b; a = A[i], b = B[i]; ans[i] = res; if (tree.same(a, b) == false) { // この橋が異なるグループをつなぐようなら ll asize = tree.size(a), bsize = tree.size(b); res -= asize * bsize; } tree.unite(a, b); } rep(i, M) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define inf LLONG_MAX #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; struct UnionFind { vll par; vll count; UnionFind(ll n) { par.resize(n), count.resize(n); rep(i, n) par[i] = i, count[i] = 1; } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } void unite(ll x, ll y) { ll rx = root(x); ll ry = root(y); if (rx == ry) return; if (rx > ry) { count[rx] += count[ry]; par[ry] = rx; } else { count[ry] += count[rx]; par[rx] = ry; } } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return count[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vll A(M), B(M); rep(i, M) { cin >> A[i] >> B[i]; A[i]--, B[i]--; } UnionFind tree(N); vll ans(M); ll res = N * (N - 1) / 2; for (ll i = M - 1; i >= 0; i--) { ll a, b; a = A[i], b = B[i]; ans[i] = res; if (tree.same(a, b) == false) { // この橋が異なるグループをつなぐようなら ll asize = tree.size(a), bsize = tree.size(b); res -= asize * bsize; } tree.unite(a, b); } rep(i, M) { cout << ans[i] << endl; } return 0; }
replace
50
51
50
54
0
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; struct node { int p; int v; int s; }; int get_pid(vector<node> &vec, int i) { while (i != vec[i].p) i = vec[i].p; return i; } int main() { int n, m; cin >> n >> m; vector<node> vec(n + 1); for (int i = 1; i <= n; ++i) vec[i] = {i, i, 1}; vector<pair<int, int>> vec2(m); for (auto &v : vec2) { int a, b; cin >> a >> b; v = make_pair(a, b); } reverse(vec2.begin(), vec2.end()); vector<int> ans; for (auto &v : vec2) { int a = v.first; int b = v.second; int ap = get_pid(vec, a); int bp = get_pid(vec, b); if (ap == bp) { ans.push_back(0); } else { int s = vec[ap].s + vec[bp].s; ans.push_back(vec[ap].s * vec[bp].s); vec[bp].p = ap; vec[ap].s = s; } } ll vsum = 0; for (auto itr = ans.rbegin(); itr != ans.rend(); ++itr) { vsum += *itr; cout << vsum << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; struct node { int p; int v; int s; }; int get_pid(vector<node> &vec, int i) { int p = i; while (p != vec[p].p) p = vec[p].p; int c = i; while (c != vec[c].p) { int n = vec[c].p; vec[c].p = p; c = n; } return p; } int main() { int n, m; cin >> n >> m; vector<node> vec(n + 1); for (int i = 1; i <= n; ++i) vec[i] = {i, i, 1}; vector<pair<int, int>> vec2(m); for (auto &v : vec2) { int a, b; cin >> a >> b; v = make_pair(a, b); } reverse(vec2.begin(), vec2.end()); vector<int> ans; for (auto &v : vec2) { int a = v.first; int b = v.second; int ap = get_pid(vec, a); int bp = get_pid(vec, b); if (ap == bp) { ans.push_back(0); } else { int s = vec[ap].s + vec[bp].s; ans.push_back(vec[ap].s * vec[bp].s); vec[bp].p = ap; vec[ap].s = s; } } ll vsum = 0; for (auto itr = ans.rbegin(); itr != ans.rend(); ++itr) { vsum += *itr; cout << vsum << endl; } }
replace
14
17
14
24
TLE
p03108
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; typedef long long ll; int an = 0; const ll INF = 1e18; const ll MOD = 1e9 + 7; typedef pair<int, int> P; const int xx[4] = {1, -1, 0, 0}, yy[4] = {0, 0, 1, -1}; struct tp { int x, y; ll cs; }; const int wd = 1000001; struct unf { vector<int> v, s; unf(int n) { v.resize(n); s.resize(n); for (int i = 0; i < n; i++) v[i] = i, s[i] = 1; } int root(int x) { if (v[x] == x) return x; return v[x] = root(v[x]); } void unite(int x, int y) { int p = root(x), q = root(y); if (p == q) return; v[p] = q, s[q] = s[p] + s[q]; } bool same(int x, int y) { return root(x) == root(y); } int sz(int k) { return s[root(k)]; } void print() { for (int x : v) cout << x << " "; cout << endl; } void print2() { for (int x : s) cout << x << " "; cout << endl; } }; int main() { int n, m; cin >> n >> m; unf u(n); ll a[n], b[n], an[n]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } for (int i = m - 1; i >= 0; i--) { if (u.same(a[i], b[i])) an[i] = 0; else { // cout<<u.sz(a[i])<<" "<<u.sz(b[i])<<endl; an[i] = u.sz(a[i]) * u.sz(b[i]); u.unite(a[i], b[i]); } // u.print(); // cout<<"a"; u.print2(); } for (int i = 1; i < m; i++) { an[i] += an[i - 1]; } for (int i = 0; i < m; i++) { cout << an[i] << endl; } }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; typedef long long ll; int an = 0; const ll INF = 1e18; const ll MOD = 1e9 + 7; typedef pair<int, int> P; const int xx[4] = {1, -1, 0, 0}, yy[4] = {0, 0, 1, -1}; struct tp { int x, y; ll cs; }; const int wd = 1000001; struct unf { vector<int> v, s; unf(int n) { v.resize(n); s.resize(n); for (int i = 0; i < n; i++) v[i] = i, s[i] = 1; } int root(int x) { if (v[x] == x) return x; return v[x] = root(v[x]); } void unite(int x, int y) { int p = root(x), q = root(y); if (p == q) return; v[p] = q, s[q] = s[p] + s[q]; } bool same(int x, int y) { return root(x) == root(y); } int sz(int k) { return s[root(k)]; } void print() { for (int x : v) cout << x << " "; cout << endl; } void print2() { for (int x : s) cout << x << " "; cout << endl; } }; int main() { int n, m; cin >> n >> m; unf u(n); ll a[m], b[m], an[m]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } for (int i = m - 1; i >= 0; i--) { if (u.same(a[i], b[i])) an[i] = 0; else { // cout<<u.sz(a[i])<<" "<<u.sz(b[i])<<endl; an[i] = u.sz(a[i]) * u.sz(b[i]); u.unite(a[i], b[i]); } // u.print(); // cout<<"a"; u.print2(); } for (int i = 1; i < m; i++) { an[i] += an[i - 1]; } for (int i = 0; i < m; i++) { cout << an[i] << endl; } }
replace
54
55
54
55
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M + 1); vector<int> Tree(N, -1); vector<int> Treesize(N, 1); long long fuben = (long long)N * (N - 1) / 2; ans[M] = fuben; for (int i = M - 1; i > 0; i--) { int Ap = A[i], Bp = B[i]; while (Tree[Ap] != -1) Ap = Tree[Ap]; while (Tree[Bp] != -1) Bp = Tree[Bp]; if (Ap != Bp) { fuben -= (long long)Treesize[Ap] * Treesize[Bp]; Tree[Bp] = Ap; Treesize[Ap] += Treesize[Bp]; } ans[i] = fuben; } for (int i = 1; i <= M; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M + 1); vector<int> Tree(N, -1); vector<int> Treesize(N, 1); long long fuben = (long long)N * (N - 1) / 2; ans[M] = fuben; for (int i = M - 1; i > 0; i--) { int Ap = A[i], Bp = B[i]; while (Tree[Ap] != -1) Ap = Tree[Ap]; while (Tree[Bp] != -1) Bp = Tree[Bp]; if (Ap != Bp) { fuben -= (long long)Treesize[Ap] * Treesize[Bp]; if (Treesize[Ap] < Treesize[Bp]) swap(Ap, Bp); Tree[Bp] = Ap; Treesize[Ap] += Treesize[Bp]; } ans[i] = fuben; } for (int i = 1; i <= M; i++) { cout << ans[i] << endl; } }
insert
29
29
29
31
TLE
p03108
C++
Runtime Error
#include <algorithm> #include <cstdint> #include <iostream> #include <vector> using namespace std; struct UnionFind { std::vector<std::size_t> par; std::vector<std::size_t> _size; UnionFind(std::size_t N) : par(N), _size(N, 1) { for (std::size_t i = 0; i < N; i++) { par[i] = i; } } std::size_t Root(std::size_t x) { if (par[x] == x) { return x; } return par[x] = Root(par[x]); } void Unite(std::size_t x, std::size_t y) { auto rx = Root(x); auto ry = Root(y); if (rx != ry) { if (_size[rx] < _size[ry]) { par[rx] = ry; _size[ry] += _size[rx]; } else { par[ry] = rx; _size[rx] += _size[ry]; } } } size_t Size(std::size_t x) { return _size[Root(x)]; } bool Same(std::size_t x, std::size_t y) { auto rx = Root(x); auto ry = Root(y); return rx == ry; } }; int main() { int64_t n, m; cin >> n >> m; vector<int64_t> as(m), bs(m); for (int i = 0; i < m; i++) { cin >> as[i] >> bs[i]; } vector<int64_t> ans_list; UnionFind uf(n); int64_t ans = n * (n - 1) / 2; ans_list.push_back(ans); for (int i = m - 1; i > 0; i--) { const int64_t sa = uf.Size(as[i]); const int64_t sb = uf.Size(bs[i]); if (!uf.Same(as[i], bs[i])) { ans -= uf.Size(as[i]) * uf.Size(bs[i]); uf.Unite(as[i], bs[i]); } ans_list.push_back(ans); } reverse(ans_list.begin(), ans_list.end()); for (auto v : ans_list) { cout << v << endl; } }
#include <algorithm> #include <cstdint> #include <iostream> #include <vector> using namespace std; struct UnionFind { std::vector<std::size_t> par; std::vector<std::size_t> _size; UnionFind(std::size_t N) : par(N), _size(N, 1) { for (std::size_t i = 0; i < N; i++) { par[i] = i; } } std::size_t Root(std::size_t x) { if (par[x] == x) { return x; } return par[x] = Root(par[x]); } void Unite(std::size_t x, std::size_t y) { auto rx = Root(x); auto ry = Root(y); if (rx != ry) { if (_size[rx] < _size[ry]) { par[rx] = ry; _size[ry] += _size[rx]; } else { par[ry] = rx; _size[rx] += _size[ry]; } } } size_t Size(std::size_t x) { return _size[Root(x)]; } bool Same(std::size_t x, std::size_t y) { auto rx = Root(x); auto ry = Root(y); return rx == ry; } }; int main() { int64_t n, m; cin >> n >> m; vector<int64_t> as(m), bs(m); for (int i = 0; i < m; i++) { cin >> as[i] >> bs[i]; as[i]--; bs[i]--; } vector<int64_t> ans_list; UnionFind uf(n); int64_t ans = n * (n - 1) / 2; ans_list.push_back(ans); for (int i = m - 1; i > 0; i--) { const int64_t sa = uf.Size(as[i]); const int64_t sb = uf.Size(bs[i]); if (!uf.Same(as[i], bs[i])) { ans -= uf.Size(as[i]) * uf.Size(bs[i]); uf.Unite(as[i], bs[i]); } ans_list.push_back(ans); } reverse(ans_list.begin(), ans_list.end()); for (auto v : ans_list) { cout << v << endl; } }
insert
53
53
53
55
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } const ll INF = 1e15; const ll MOD = 1e9 + 7; const ll LINF = 1e18; class UnionFind { private: vector<ll> Parent; public: UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll root(ll A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } ll size(ll A) { return -Parent[root(A)]; } bool connect(ll A, ll B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } bool isSame(ll A, ll B) { return root(A) == root(B); } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; UnionFind uf(n); vll ans(m); ans[0] = n * (n - 1) / 2; vpll ab(m); rep(i, m) cin >> ab[i].first >> ab[i].second; reverse(all(ab)); rep(i, m - 1) { ll a = ab[i].first, b = ab[i].second; if (!uf.isSame(a, b)) { ans[i + 1] = ans[i] - uf.size(a) * uf.size(b); uf.connect(a, b); } else { ans[i + 1] = ans[i]; } } reverse(all(ans)); rep(i, m) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } const ll INF = 1e15; const ll MOD = 1e9 + 7; const ll LINF = 1e18; class UnionFind { private: vector<ll> Parent; public: UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll root(ll A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } ll size(ll A) { return -Parent[root(A)]; } bool connect(ll A, ll B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } bool isSame(ll A, ll B) { return root(A) == root(B); } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; UnionFind uf(n); vll ans(m); ans[0] = n * (n - 1) / 2; vpll ab(m); rep(i, m) cin >> ab[i].first >> ab[i].second, ab[i].first--, ab[i].second--; reverse(all(ab)); rep(i, m - 1) { ll a = ab[i].first, b = ab[i].second; if (!uf.isSame(a, b)) { ans[i + 1] = ans[i] - uf.size(a) * uf.size(b); uf.connect(a, b); } else { ans[i + 1] = ans[i]; } } reverse(all(ans)); rep(i, m) { cout << ans[i] << endl; } }
replace
96
97
96
97
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i) #define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) int(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } const ll INF = 1e15; const int MOD = 1e9 + 7; const ll LINF = 1e18; class UnionFind { private: vector<ll> Parent; public: UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll root(ll A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } ll size(ll A) { return -Parent[root(A)]; } bool connect(ll A, ll B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } bool isSame(ll A, ll B) { return root(A) == root(B); } }; ll n, m; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vpll p(m); rep(i, m) { ll a, b; cin >> a >> b; a--, b--; p[i] = {a, b}; } reverse(all(p)); UnionFind uf(n); vll ans(n); ans[0] = n * (n - 1) / 2; rep(i, m - 1) { ll a = p[i].first, b = p[i].second; if (uf.isSame(a, b)) { ans[i + 1] = ans[i]; } else { ans[i + 1] = ans[i] - uf.size(a) * uf.size(b); uf.connect(a, b); } } rrep(i, m) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i) #define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) int(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } const ll INF = 1e15; const int MOD = 1e9 + 7; const ll LINF = 1e18; class UnionFind { private: vector<ll> Parent; public: UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll root(ll A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } ll size(ll A) { return -Parent[root(A)]; } bool connect(ll A, ll B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } bool isSame(ll A, ll B) { return root(A) == root(B); } }; ll n, m; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vpll p(m); rep(i, m) { ll a, b; cin >> a >> b; a--, b--; p[i] = {a, b}; } reverse(all(p)); UnionFind uf(n); vll ans(m); ans[0] = n * (n - 1) / 2; rep(i, m - 1) { ll a = p[i].first, b = p[i].second; if (uf.isSame(a, b)) { ans[i + 1] = ans[i]; } else { ans[i + 1] = ans[i] - uf.size(a) * uf.size(b); uf.connect(a, b); } } rrep(i, m) cout << ans[i] << endl; }
replace
100
101
100
101
0
p03108
C++
Runtime Error
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <utility> #include <vector> using namespace std; #define pi pair<int, int> #define pl pair<long long, long long> #define chmax(a, b) (a < b ? a = b : 0) #define chmin(a, b) (a > b ? a = b : 0) #define en cout << endl // セミコロンつけろ // #define MM 1000000000 // #define MOD MM+7 const int MM = 1e9; const int MOD = MM + 7; const long double PI = acos(-1); const long long INF = 1e15; int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1}; // 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122 template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; } template <typename T> T LCM(T x, T y) { T gc = GCD(x, y); return x * y / gc; } struct UnionFind { vector<int> par, rank, cnt; UnionFind(int n) : par(n), rank(n, 0), cnt(n, 1) { for (int i = 0; i < n; ++i) par[i] = i; } int find(int x) { if (x == par[x]) return x; 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); par[y] = x; cnt[x] += cnt[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return cnt[find(x)]; } }; int main() { long long N, M; cin >> N >> M; vector<long long> a(M), b(M); for (int i = 0; i < M; i++) { cin >> a[i] >> b[i]; } UnionFind uf(N); long long num = N * (N - 1) / 2; vector<long long> ans(M); for (int i = M - 1; i >= 0; i--) { ans[i] = num; if (!uf.same(a[i], b[i])) { num -= (long long)uf.size(a[i]) * (long long)uf.size(b[i]); uf.unite(a[i], b[i]); } } for (auto i : ans) { cout << i << endl; } }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <utility> #include <vector> using namespace std; #define pi pair<int, int> #define pl pair<long long, long long> #define chmax(a, b) (a < b ? a = b : 0) #define chmin(a, b) (a > b ? a = b : 0) #define en cout << endl // セミコロンつけろ // #define MM 1000000000 // #define MOD MM+7 const int MM = 1e9; const int MOD = MM + 7; const long double PI = acos(-1); const long long INF = 1e15; int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1}; // 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122 template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; } template <typename T> T LCM(T x, T y) { T gc = GCD(x, y); return x * y / gc; } struct UnionFind { vector<int> par, rank, cnt; UnionFind(int n) : par(n), rank(n, 0), cnt(n, 1) { for (int i = 0; i < n; ++i) par[i] = i; } int find(int x) { if (x == par[x]) return x; 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); par[y] = x; cnt[x] += cnt[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return cnt[find(x)]; } }; int main() { long long N, M; cin >> N >> M; vector<long long> a(M), b(M); for (int i = 0; i < M; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } UnionFind uf(N); long long num = N * (N - 1) / 2; vector<long long> ans(M); for (int i = M - 1; i >= 0; i--) { ans[i] = num; if (!uf.same(a[i], b[i])) { num -= (long long)uf.size(a[i]) * (long long)uf.size(b[i]); uf.unite(a[i], b[i]); } } for (auto i : ans) { cout << i << endl; } }
insert
79
79
79
80
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define _GLIBCXX_DEBUG using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vd = vector<double>; using vvi = vector<vi>; using vvll = vector<vll>; using vvd = vector<vd>; using vvvi = vector<vvi>; using vvvll = vector<vvll>; using vvvd = vector<vvd>; const double pi = 3.141592653589793; class UnionFind { // まとめる 判定 サイズを知る public: // Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。 vector<ll> Parent; // 初期化 UnionFind(ll N) { Parent = vector<ll>(N, -1); } // Aのrootを調べる int root(ll A) { if (Parent[A] < 0) return A; // マイナスならそれはroot return Parent[A] = root(Parent[A]); } // rootの値をプラスに戻して返す(サイズ) int size(ll A) { return -Parent[root(A)]; } // くっつける関数 bool connect(ll A, ll B) { // AとBのroot同士をくっつける A = root(A); // ここのAは、"rootの場所"の意味 B = root(B); if (A == B) return false; // 既にくっついている if (size(A) < size(B)) swap(A, B); // 大きい方にくっつけるために中身交換 Parent[A] += Parent[B]; // 中身更新 Parent[B] = A; return true; } }; int main() { ll n, m; cin >> n >> m; ll now = (n * (n - 1)) / 2; vll ans; ans.push_back(now); UnionFind tree(n); vector<pair<ll, ll>> pp; rep(i, m) { ll x, y; cin >> x >> y; pp.push_back(make_pair(x, y)); } for (int i = m - 1; i >= 0; i--) { ll x, y; x = pp[i].first; y = pp[i].second; ll xs = tree.size(x); ll ys = tree.size(y); if (!tree.connect(x, y)) { ans.push_back(now); } else { now = now - xs * ys; ans.push_back(now); // cout<<111<<endl; } // cout<<now<<endl; } for (int i = m - 1; i >= 0; i--) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define _GLIBCXX_DEBUG using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vd = vector<double>; using vvi = vector<vi>; using vvll = vector<vll>; using vvd = vector<vd>; using vvvi = vector<vvi>; using vvvll = vector<vvll>; using vvvd = vector<vvd>; const double pi = 3.141592653589793; class UnionFind { // まとめる 判定 サイズを知る public: // Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。 vector<ll> Parent; // 初期化 UnionFind(ll N) { Parent = vector<ll>(N, -1); } // Aのrootを調べる int root(ll A) { if (Parent[A] < 0) return A; // マイナスならそれはroot return Parent[A] = root(Parent[A]); } // rootの値をプラスに戻して返す(サイズ) int size(ll A) { return -Parent[root(A)]; } // くっつける関数 bool connect(ll A, ll B) { // AとBのroot同士をくっつける A = root(A); // ここのAは、"rootの場所"の意味 B = root(B); if (A == B) return false; // 既にくっついている if (size(A) < size(B)) swap(A, B); // 大きい方にくっつけるために中身交換 Parent[A] += Parent[B]; // 中身更新 Parent[B] = A; return true; } }; int main() { ll n, m; cin >> n >> m; ll now = (n * (n - 1)) / 2; vll ans; ans.push_back(now); UnionFind tree(n); vector<pair<ll, ll>> pp; rep(i, m) { ll x, y; cin >> x >> y; pp.push_back(make_pair(x, y)); } for (int i = m - 1; i >= 0; i--) { ll x, y; x = pp[i].first - 1; y = pp[i].second - 1; ll xs = tree.size(x); ll ys = tree.size(y); if (!tree.connect(x, y)) { ans.push_back(now); } else { now = now - xs * ys; ans.push_back(now); // cout<<111<<endl; } // cout<<now<<endl; } for (int i = m - 1; i >= 0; i--) { cout << ans[i] << endl; } return 0; }
replace
73
75
73
75
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; long long inf = LLONG_MAX; vector<long long> id; long long find(long long i) { if (id[i] <= 0) return i; return id[i] = find(id[i]); } long long unio(long long i, long long j) { long long u = find(i); long long k = find(j); if (find(i) == find(j)) return 0; if (id[u] > id[k]) swap(u, k); id[u] += id[k]; id[k] = u; return id[u]; } ll fact(ll n) { ll res = 1; for (ll i = 2; i <= n; i++) res = res * i; return res; } ll nCr(ll n, ll r) { return fact(n) / (fact(r) * fact(n - r)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, m; cin >> n >> m; id.resize(n + 1, -1); vector<pair<ll, ll>> arr(m); for (int z = 0; z < m; z++) { ll a, b; cin >> a >> b; arr[z] = make_pair(a, b); } vector<ll> answer(m + 1); answer[m] = nCr(n, 2); for (int z = m - 1; z > 0; z--) { ll a = id[find(arr[z].first)]; ll b = id[find(arr[z].second)]; ll c = unio(arr[z].first, arr[z].second); if (c == 0) { answer[z] = answer[z + 1]; } else { answer[z] = answer[z + 1] + nCr((-a), 2) + nCr((-b), 2) - nCr((-c), 2); } } for (int z = 1; z <= m; z++) { cout << answer[z] << endl; } cin >> n; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; long long inf = LLONG_MAX; vector<long long> id; long long find(long long i) { if (id[i] <= 0) return i; return id[i] = find(id[i]); } long long unio(long long i, long long j) { long long u = find(i); long long k = find(j); if (find(i) == find(j)) return 0; if (id[u] > id[k]) swap(u, k); id[u] += id[k]; id[k] = u; return id[u]; } ll nCr(ll n, ll r) { return n * (n - 1) / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, m; cin >> n >> m; id.resize(n + 1, -1); vector<pair<ll, ll>> arr(m); for (int z = 0; z < m; z++) { ll a, b; cin >> a >> b; arr[z] = make_pair(a, b); } vector<ll> answer(m + 1); answer[m] = nCr(n, 2); for (int z = m - 1; z > 0; z--) { ll a = id[find(arr[z].first)]; ll b = id[find(arr[z].second)]; ll c = unio(arr[z].first, arr[z].second); if (c == 0) { answer[z] = answer[z + 1]; } else { answer[z] = answer[z + 1] + nCr((-a), 2) + nCr((-b), 2) - nCr((-c), 2); } } for (int z = 1; z <= m; z++) { cout << answer[z] << endl; } cin >> n; }
replace
25
33
25
26
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; typedef long long ll; 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; } 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() { int n, m; cin >> n >> m; vector<ll> ans; vector<int> a(m), b(m); rep(i, m) cin >> a[i] >> b[i]; reverse(a.rbegin(), a.rend()); reverse(b.rbegin(), b.rend()); ll tmp = (ll)n * (ll)(n - 1) / 2; ans.push_back(tmp); UnionFind uf(n); rep(i, m) { if (!uf.same(a[i], b[i])) { int sa = uf.size(a[i]); int sb = uf.size(b[i]); tmp -= (ll)sa * (ll)sb; ans.push_back(tmp); uf.unite(a[i], b[i]); } else { ans.push_back(tmp); } } reverse(ans.begin(), ans.end()); for (int i = 1; i <= m; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; typedef long long ll; 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; } 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() { int n, m; cin >> n >> m; vector<ll> ans; vector<int> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } reverse(a.rbegin(), a.rend()); reverse(b.rbegin(), b.rend()); ll tmp = (ll)n * (ll)(n - 1) / 2; ans.push_back(tmp); UnionFind uf(n); rep(i, m) { if (!uf.same(a[i], b[i])) { int sa = uf.size(a[i]); int sb = uf.size(b[i]); tmp -= (ll)sa * (ll)sb; ans.push_back(tmp); uf.unite(a[i], b[i]); } else { ans.push_back(tmp); } } reverse(ans.begin(), ans.end()); for (int i = 1; i <= m; i++) { cout << ans[i] << endl; } }
replace
48
49
48
53
0
p03108
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <iomanip> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, n) FOR(i, 0, n) #define FOR(i, a, b) for (long long i = (a), i##Len_ = (b); i < i##Len_; i++) typedef long ll; // abc120 // i番目の橋をとったときに、いくつの島が行き来できなくなるか // 強固さ、みたいなのを保持しておく? // 1-2 が切れても 1-3 3-1があればよい // unionfind struct UnionFind { vector<ll> par; // new UnionFind(ll n) : par(n, -1) {} // 初期化 void init(ll n) { par.assign(n, -1); } ll root(ll x) { if (par[x] < 0) return x; else return par[x] = root( par[x]); // 親の番号を返す(親が子になっていたら困るので取り込まれてたら代入も行う) } // 同じグループに属するか bool issame(ll x, ll y) { // rootが同じであるか return root(x) == root(y); } // 併合 bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // 多い方に足し合わせる par[x] += par[y]; // 従属(ここで0以上となる) par[y] = x; return true; } ll size(ll x) { return -par[root(x)]; } }; int main() { std::ifstream in("resource/input.txt"); std::cin.rdbuf(in.rdbuf()); ll N, M; cin >> N >> M; // 橋の左をA 右をB vector<ll> A(M), B(M); REP(i, M) { cin >> A[i] >> B[i]; // 0-based --A[i]; --B[i]; } UnionFind uf(N); // 橋の連結 // はじめ、どの 2つの島についてもいくつかの橋を渡って互いに行き来できます。 // はじめの連結数 N * (N-1) / 2 nC2 ll cur = N * (N - 1) / 2; vector<ll> res; for (ll i = 0; i < M; ++i) { res.push_back(cur); // 後ろから調べる ll a = A[M - 1 - i]; ll b = B[M - 1 - i]; // 同じグループに属するか if (uf.issame(a, b)) continue; // サイズ取得 ll sa = uf.size(a); ll sb = uf.size(b); // 最初に外すのは、最後の橋→必ず(1*1)減る // 連結成分sa と 連結成分sb がここで初めて併合された cur -= sa * sb; uf.merge(a, b); } // 後ろからとってたので reverse(res.begin(), res.end()); for (auto r : res) { cout << r << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <iomanip> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, n) FOR(i, 0, n) #define FOR(i, a, b) for (long long i = (a), i##Len_ = (b); i < i##Len_; i++) typedef long ll; // abc120 // i番目の橋をとったときに、いくつの島が行き来できなくなるか // 強固さ、みたいなのを保持しておく? // 1-2 が切れても 1-3 3-1があればよい // unionfind struct UnionFind { vector<ll> par; // new UnionFind(ll n) : par(n, -1) {} // 初期化 void init(ll n) { par.assign(n, -1); } ll root(ll x) { if (par[x] < 0) return x; else return par[x] = root( par[x]); // 親の番号を返す(親が子になっていたら困るので取り込まれてたら代入も行う) } // 同じグループに属するか bool issame(ll x, ll y) { // rootが同じであるか return root(x) == root(y); } // 併合 bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // 多い方に足し合わせる par[x] += par[y]; // 従属(ここで0以上となる) par[y] = x; return true; } ll size(ll x) { return -par[root(x)]; } }; int main() { ll N, M; cin >> N >> M; // 橋の左をA 右をB vector<ll> A(M), B(M); REP(i, M) { cin >> A[i] >> B[i]; // 0-based --A[i]; --B[i]; } UnionFind uf(N); // 橋の連結 // はじめ、どの 2つの島についてもいくつかの橋を渡って互いに行き来できます。 // はじめの連結数 N * (N-1) / 2 nC2 ll cur = N * (N - 1) / 2; vector<ll> res; for (ll i = 0; i < M; ++i) { res.push_back(cur); // 後ろから調べる ll a = A[M - 1 - i]; ll b = B[M - 1 - i]; // 同じグループに属するか if (uf.issame(a, b)) continue; // サイズ取得 ll sa = uf.size(a); ll sb = uf.size(b); // 最初に外すのは、最後の橋→必ず(1*1)減る // 連結成分sa と 連結成分sb がここで初めて併合された cur -= sa * sb; uf.merge(a, b); } // 後ろからとってたので reverse(res.begin(), res.end()); for (auto r : res) { cout << r << endl; } return 0; }
delete
66
69
66
66
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; 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; } const long long INF = 1LL << 60; long long dp[100100]; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int X) { if (Parent[X] < 0) return X; return Parent[X] = root(Parent[X]); } int size(int X) { return -Parent[root(X)]; } bool unite(int A, int B) { A = root(A); B = root(B); if (A == B) return false; if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; UnionFind Uni(N); for (int i = M - 1; i >= 0; i--) { if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] = ans[i] - (long long)Uni.size(A[i]) * Uni.size(B[i]); Uni.unite(A[i], B[i]); } else ans[i - 1] = ans[i]; } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; 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; } const long long INF = 1LL << 60; long long dp[100100]; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int X) { if (Parent[X] < 0) return X; return Parent[X] = root(Parent[X]); } int size(int X) { return -Parent[root(X)]; } bool unite(int A, int B) { A = root(A); B = root(B); if (A == B) return false; if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; UnionFind Uni(N); for (int i = M - 1; i >= 1; i--) { if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] = ans[i] - (long long)Uni.size(A[i]) * Uni.size(B[i]); Uni.unite(A[i], B[i]); } else ans[i - 1] = ans[i]; } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; }
replace
67
68
67
68
-6
free(): invalid pointer
p03108
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define REP(i, from, to) for (long long int i = (from); i < (to); i++) #define REV(v) for (auto itr = v.rbegin(); itr != v.rend(); itr++) #define ll long long #define ALL(a) (a).begin(), (a).end() #define SORT(v) sort(ALL(v)); #define LAST(v) v[v.size() - 1] #define ZERO(a) memset(a, 0, sizeof(a)); #define MINUS(a) memset(a, -1, sizeof(a)); #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define pi pair<int, int> #define MP make_pair #define PB push_back #define EB emplace_back #define IIN(a) \ int a; \ cin >> a; #define LIN(a) \ ll a; \ cin >> a; #define SIN(a) \ string a; \ cin >> a; #define VL(v, n) vector<ll> v(n); #define Read_VL(v, n) \ vector<ll> v(n); \ REP(i, 0, n) cin >> v[i]; #define sumV(v, n) \ Vl(acm, n) REP(i, 0, n) acm[i] = v[i] + (i == 0 ? 0 : acm[i - 1]); #define EPS (1e-10) #define INF 1000000000000LL void YN(bool b) { cout << (b ? "YES" : "NO") << "\n"; } void Yn(bool b) { cout << (b ? "Yes" : "No") << "\n"; } void yn(bool b) { cout << (b ? "yes" : "no") << "\n"; } template <class S> S max(vector<S> &a) { return *max_element(all(a)); } template <class S> S min(vector<S> &a) { return *min_element(all(a)); } template <class T> void puta(T &&t) { cout << t << "\n"; } template <class H, class... T> void puta(H &&h, T &&...t) { cout << h << ' '; puta(t...); } template <typename ELEMENT_TYPE> struct _setfillw { ELEMENT_TYPE c; std::streamsize w; _setfillw(ELEMENT_TYPE c, std::streamsize w) : w(w), c(c) {} }; template <typename ELEMENT_TYPE> inline _setfillw<ELEMENT_TYPE> setfillw(ELEMENT_TYPE c, std::streamsize w) { return _setfillw<ELEMENT_TYPE>(c, w); } template <typename ELEMENT_TYPE> inline std::ostream &operator<<(std::ostream &os, const _setfillw<ELEMENT_TYPE> &manip) { return os << std::setw(manip.w) << std::setfill(manip.c); } template <class T = ll> struct Graph { int n; vector<vector<tuple<ll, T>>> edge; Graph(int N = 1) : n(N) { edge.resize(n); } void add(ll f, ll t, T c, bool d = false) { edge[f].emplace_back(t, c); if (!d) edge[t].emplace_back(f, c); } void view() { REP(i, 0, n) for (auto &e : edge[i]) puta(i, "=>", get<0>(e), ", cost :", get<1>(e)); } }; bool length_cmp(string &a, string &b) { return a.size() < b.size(); } ll StoLL(string s) { ll num = 0; REP(i, 0, s.size()) { num = num * 10 + (s[i] ^ 48); } return num; } ll CtoLL(char c) { if ('a' <= c && c <= 'z') { return (ll)(c ^ 96); } else if ('A' <= c && c <= 'Z') { return (ll)(c ^ 64); } return 0; } ll MOD = 1000000007; ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; #include <array> #include <numeric> struct UF { vector<ll> par, sz; UF() {} UF(ll n) : par(n), sz(n, 1) { iota(par.begin(), par.end(), 0); } ll find(ll x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; par[y] = x; } bool same(ll x, ll y) { return find(x) == find(y); } }; signed main() { LIN(n) LIN(m) UF uf(n); vector<ll> ans(n), a(n), b(n); REP(i, 0, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } ans[m - 1] = n * (n - 1) / 2; for (ll i = m - 1; i >= 1; i--) { if (uf.same(a[i], b[i])) { ans[i - 1] = ans[i]; } else { ll p = uf.sz[uf.find(a[i])]; ll q = uf.sz[uf.find(b[i])]; uf.unite(a[i], b[i]); ans[i - 1] = ans[i] - p * q; } } REP(i, 0, ans.size()) { puta(ans[i]); } return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define REP(i, from, to) for (long long int i = (from); i < (to); i++) #define REV(v) for (auto itr = v.rbegin(); itr != v.rend(); itr++) #define ll long long #define ALL(a) (a).begin(), (a).end() #define SORT(v) sort(ALL(v)); #define LAST(v) v[v.size() - 1] #define ZERO(a) memset(a, 0, sizeof(a)); #define MINUS(a) memset(a, -1, sizeof(a)); #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define pi pair<int, int> #define MP make_pair #define PB push_back #define EB emplace_back #define IIN(a) \ int a; \ cin >> a; #define LIN(a) \ ll a; \ cin >> a; #define SIN(a) \ string a; \ cin >> a; #define VL(v, n) vector<ll> v(n); #define Read_VL(v, n) \ vector<ll> v(n); \ REP(i, 0, n) cin >> v[i]; #define sumV(v, n) \ Vl(acm, n) REP(i, 0, n) acm[i] = v[i] + (i == 0 ? 0 : acm[i - 1]); #define EPS (1e-10) #define INF 1000000000000LL void YN(bool b) { cout << (b ? "YES" : "NO") << "\n"; } void Yn(bool b) { cout << (b ? "Yes" : "No") << "\n"; } void yn(bool b) { cout << (b ? "yes" : "no") << "\n"; } template <class S> S max(vector<S> &a) { return *max_element(all(a)); } template <class S> S min(vector<S> &a) { return *min_element(all(a)); } template <class T> void puta(T &&t) { cout << t << "\n"; } template <class H, class... T> void puta(H &&h, T &&...t) { cout << h << ' '; puta(t...); } template <typename ELEMENT_TYPE> struct _setfillw { ELEMENT_TYPE c; std::streamsize w; _setfillw(ELEMENT_TYPE c, std::streamsize w) : w(w), c(c) {} }; template <typename ELEMENT_TYPE> inline _setfillw<ELEMENT_TYPE> setfillw(ELEMENT_TYPE c, std::streamsize w) { return _setfillw<ELEMENT_TYPE>(c, w); } template <typename ELEMENT_TYPE> inline std::ostream &operator<<(std::ostream &os, const _setfillw<ELEMENT_TYPE> &manip) { return os << std::setw(manip.w) << std::setfill(manip.c); } template <class T = ll> struct Graph { int n; vector<vector<tuple<ll, T>>> edge; Graph(int N = 1) : n(N) { edge.resize(n); } void add(ll f, ll t, T c, bool d = false) { edge[f].emplace_back(t, c); if (!d) edge[t].emplace_back(f, c); } void view() { REP(i, 0, n) for (auto &e : edge[i]) puta(i, "=>", get<0>(e), ", cost :", get<1>(e)); } }; bool length_cmp(string &a, string &b) { return a.size() < b.size(); } ll StoLL(string s) { ll num = 0; REP(i, 0, s.size()) { num = num * 10 + (s[i] ^ 48); } return num; } ll CtoLL(char c) { if ('a' <= c && c <= 'z') { return (ll)(c ^ 96); } else if ('A' <= c && c <= 'Z') { return (ll)(c ^ 64); } return 0; } ll MOD = 1000000007; ll dx[4] = {1, 0, -1, 0}; ll dy[4] = {0, 1, 0, -1}; #include <array> #include <numeric> struct UF { vector<ll> par, sz; UF() {} UF(ll n) : par(n), sz(n, 1) { iota(par.begin(), par.end(), 0); } ll find(ll x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; par[y] = x; } bool same(ll x, ll y) { return find(x) == find(y); } }; signed main() { LIN(n) LIN(m) UF uf(n); vector<ll> ans(m), a(m), b(m); REP(i, 0, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } ans[m - 1] = n * (n - 1) / 2; for (ll i = m - 1; i >= 1; i--) { if (uf.same(a[i], b[i])) { ans[i - 1] = ans[i]; } else { ll p = uf.sz[uf.find(a[i])]; ll q = uf.sz[uf.find(b[i])]; uf.unite(a[i], b[i]); ans[i - 1] = ans[i] - p * q; } } REP(i, 0, ans.size()) { puta(ans[i]); } return 0; }
replace
150
151
150
151
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> class union_find { public: using tp = std::vector<size_t>::size_type; // 取り扱う型. explicit union_find(const tp n) : parent(n), rank(n, 0), cache(), element(n, 1) { for (tp i = 0; i < n; i++) { parent[i] = i; } cache.reserve(n); } tp root(tp n) { // 番号nの親を求める. ついでに経路圧縮を行う. while (parent[n] != n) { // 根になるまで親を探索. ついでにキャッシュ変数に追加. cache.push_back(n); n = parent[n]; } for (const auto i : cache) { // キャッシュ変数に追加された子の親を経路圧縮. parent[i] = n; } cache.clear(); return n; } bool same(tp x, tp y) { // xとyが同じグループに属しているか計算. return root(x) == root(y); } tp unite(const tp x, const tp y) { // xとyを同じグループにする. const auto x_root = root(x); const auto y_root = root(y); if (x_root == y_root) { // 根が同じなら元から同じグループ. return 0; } const auto decrease = element[x_root] * element[y_root]; if (rank[x_root] < rank[y_root]) { // rankの大きい方の根を統合後の根にする. parent[x_root] = y_root; element[y_root] += element[x_root]; } else if (rank[y_root] < rank[x_root]) { // rankの大きい方の根を統合後の根にする. parent[y_root] = x_root; element[x_root] += element[y_root]; } else { // rankが同じなら, どっちかを根にして根になったほうのランクを上げる. parent[x_root] = y_root; rank[x_root]++; element[y_root] += element[x_root]; } return decrease; } private: std::vector<tp> parent; // 親の番号を格納. 自分が根なら parent[i] == i になる. std::vector<tp> rank; // 自分の下に何段分の子があるかを格納. std::vector<tp> cache; // root計算のときに経路圧縮を行うためのキャッシュ変数. std::vector<tp> element; // グループの数. }; int main() { uint64_t n, m; std::cin >> n >> m; std::vector<std::pair<uint64_t, uint64_t>> pair(m); for (auto &i : pair) { uint64_t a, b; std::cin >> a >> b; i = {a, b}; } std::vector<uint64_t> cost(m); cost.back() = n * (n - 1) / 2; union_find uf(n); for (uint64_t i = 0; i < m - 1; i++) { const auto index = m - 1 - i; const auto dec = uf.unite(pair[index].first, pair[index].second); cost[index - 1] = cost[index] - dec; } for (const auto i : cost) { std::cout << i << std::endl; } return 0; }
#include <bits/stdc++.h> class union_find { public: using tp = std::vector<size_t>::size_type; // 取り扱う型. explicit union_find(const tp n) : parent(n), rank(n, 0), cache(), element(n, 1) { for (tp i = 0; i < n; i++) { parent[i] = i; } cache.reserve(n); } tp root(tp n) { // 番号nの親を求める. ついでに経路圧縮を行う. while (parent[n] != n) { // 根になるまで親を探索. ついでにキャッシュ変数に追加. cache.push_back(n); n = parent[n]; } for (const auto i : cache) { // キャッシュ変数に追加された子の親を経路圧縮. parent[i] = n; } cache.clear(); return n; } bool same(tp x, tp y) { // xとyが同じグループに属しているか計算. return root(x) == root(y); } tp unite(const tp x, const tp y) { // xとyを同じグループにする. const auto x_root = root(x); const auto y_root = root(y); if (x_root == y_root) { // 根が同じなら元から同じグループ. return 0; } const auto decrease = element[x_root] * element[y_root]; if (rank[x_root] < rank[y_root]) { // rankの大きい方の根を統合後の根にする. parent[x_root] = y_root; element[y_root] += element[x_root]; } else if (rank[y_root] < rank[x_root]) { // rankの大きい方の根を統合後の根にする. parent[y_root] = x_root; element[x_root] += element[y_root]; } else { // rankが同じなら, どっちかを根にして根になったほうのランクを上げる. parent[x_root] = y_root; rank[x_root]++; element[y_root] += element[x_root]; } return decrease; } private: std::vector<tp> parent; // 親の番号を格納. 自分が根なら parent[i] == i になる. std::vector<tp> rank; // 自分の下に何段分の子があるかを格納. std::vector<tp> cache; // root計算のときに経路圧縮を行うためのキャッシュ変数. std::vector<tp> element; // グループの数. }; int main() { uint64_t n, m; std::cin >> n >> m; std::vector<std::pair<uint64_t, uint64_t>> pair(m); for (auto &i : pair) { uint64_t a, b; std::cin >> a >> b; i = {a, b}; } std::vector<uint64_t> cost(m); cost.back() = n * (n - 1) / 2; union_find uf(n); for (uint64_t i = 0; i < m - 1; i++) { const auto index = m - 1 - i; const auto dec = uf.unite(pair[index].first - 1, pair[index].second - 1); cost[index - 1] = cost[index] - dec; } for (const auto i : cost) { std::cout << i << std::endl; } return 0; }
replace
92
93
92
93
0
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> #define ll long long #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (ll i = a; i >= b; i--) #define vec vector<int> #define vecll vector<ll> using namespace std; int in() { int x; scanf("%d", &x); return x; } ll lin() { ll x; scanf("%lld", &x); return x; } struct UnionFind { vec par, size; UnionFind(int n) : par(n), size(n) { rep(i, n) { par[i] = i; size[i] = 1; } } int root(int x) { if (par[x] == x) return x; return root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; size[ry] += size[rx]; } bool same(int x, int y) { return root(x) == root(y); } }; int main() { ll n = lin(); int m = in(); UnionFind tree(n); vecll a(m), b(m), ans(m); ans[m - 1] = 0; rep(i, m) { a[i] = in() - 1; b[i] = in() - 1; } rep3(i, m - 2, 0) { if (!tree.same(a[i + 1], b[i + 1])) { ans[i] = ans[i + 1] + tree.size[tree.root(a[i + 1])] * tree.size[tree.root(b[i + 1])]; tree.unite(a[i + 1], b[i + 1]); } else ans[i] = ans[i + 1]; } ll k = n * (n - 1) / 2; rep(i, m) cout << k - ans[i] << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> #define ll long long #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (ll i = a; i >= b; i--) #define vec vector<int> #define vecll vector<ll> using namespace std; int in() { int x; scanf("%d", &x); return x; } ll lin() { ll x; scanf("%lld", &x); return x; } struct UnionFind { vec par, size; UnionFind(int n) : par(n), size(n) { rep(i, n) { par[i] = i; size[i] = 1; } } int root(int x) { if (par[x] == x) return x; return root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; if (size[rx] < size[ry]) { par[rx] = ry; size[ry] += size[rx]; } else { par[ry] = rx; size[rx] += size[ry]; } } bool same(int x, int y) { return root(x) == root(y); } }; int main() { ll n = lin(); int m = in(); UnionFind tree(n); vecll a(m), b(m), ans(m); ans[m - 1] = 0; rep(i, m) { a[i] = in() - 1; b[i] = in() - 1; } rep3(i, m - 2, 0) { if (!tree.same(a[i + 1], b[i + 1])) { ans[i] = ans[i + 1] + tree.size[tree.root(a[i + 1])] * tree.size[tree.root(b[i + 1])]; tree.unite(a[i + 1], b[i + 1]); } else ans[i] = ans[i + 1]; } ll k = n * (n - 1) / 2; rep(i, m) cout << k - ans[i] << endl; }
replace
51
53
51
58
TLE
p03108
C++
Runtime Error
#include <iostream> #include <utility> #include <vector> using namespace std; struct unionfind { vector<int> p, sz; unionfind(int n) { p = vector<int>(n, -1); sz = vector<int>(n, 1); } int root(int x) { if (p[x] == -1) { return x; } else { p[x] = root(p[x]); return p[x]; } } bool same(int x, int y) { return root(x) == root(y); } pair<int, int> unite(int x, int y) { x = root(x); y = root(y); pair<int, int> ans = make_pair(sz[x], sz[y]); p[y] = x; sz[x] += sz[y]; return ans; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; unionfind G(N); for (int i = M - 2; i >= 0; i--) { if (!G.same(A[i + 1], B[i + 1])) { pair<int, int> sz = G.unite(A[i + 1], B[i + 1]); ans[i] = ans[i + 1] - (long long)sz.first * sz.second; } else { ans[i] = ans[i + 1]; } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } }
#include <iostream> #include <utility> #include <vector> using namespace std; struct unionfind { vector<int> p, sz; unionfind(int n) { p = vector<int>(n, -1); sz = vector<int>(n, 1); } int root(int x) { if (p[x] == -1) { return x; } else { p[x] = root(p[x]); return p[x]; } } bool same(int x, int y) { return root(x) == root(y); } pair<int, int> unite(int x, int y) { x = root(x); y = root(y); pair<int, int> ans = make_pair(sz[x], sz[y]); p[y] = x; sz[x] += sz[y]; return ans; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; unionfind G(N); for (int i = M - 2; i >= 0; i--) { if (!G.same(A[i + 1], B[i + 1])) { pair<int, int> sz = G.unite(A[i + 1], B[i + 1]); ans[i] = ans[i + 1] - (long long)sz.first * sz.second; } else { ans[i] = ans[i + 1]; } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } }
insert
34
34
34
36
0
p03108
C++
Runtime Error
// includes #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> // macros #define ll long long int #define pb emplace_back #define mk make_pair #define pq priority_queue #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using namespace std; // types typedef pair<int, int> P; typedef pair<ll, int> Pl; typedef pair<ll, ll> Pll; typedef pair<double, double> Pd; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; const int mod = 1e9 + 7; // solve 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; } typedef struct UnionFind_ { vector<int> par; vector<int> rank_; vector<int> size_; UnionFind_(int n) : rank_(n, 0), size_(n, 1) { for (int i = 0; i < n; i++) par.push_back(i); } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y) { if (find(x) == find(y)) return true; else return false; } bool unite(int x, int y) { int xp = find(x); int yp = find(y); if (xp == yp) return false; if (rank_[xp] > rank_[yp]) { par[yp] = xp; size_[xp] += size_[yp]; } else if (rank_[xp] < rank_[yp]) { par[xp] = yp; size_[yp] += size_[xp]; } else { par[yp] = xp; rank_[xp]++; size_[xp]++; } return true; } } UnionFind; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<P> edge(m); rep(i, m) cin >> edge[i].first >> edge[i].second, edge[i].first--, edge[i].second--; vector<ll> ans(m); ll curr = (ll)n * (ll)(n - 1) / 2; UnionFind uf(n); for (int i = m - 1; i >= 0; i--) { ans[i] = curr; int a = edge[i].first, b = edge[i].second; if (!uf.same(a, b)) { curr -= (ll)uf.size_[uf.find(a)] * (ll)uf.size_[uf.find(b)]; uf.unite(a, b); } } assert(curr == 0); for (int i = 0; i < m; i++) { cout << ans[i] << endl; } return 0; }
// includes #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> // macros #define ll long long int #define pb emplace_back #define mk make_pair #define pq priority_queue #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using namespace std; // types typedef pair<int, int> P; typedef pair<ll, int> Pl; typedef pair<ll, ll> Pll; typedef pair<double, double> Pd; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; const int mod = 1e9 + 7; // solve 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; } typedef struct UnionFind_ { vector<int> par; vector<int> rank_; vector<int> size_; UnionFind_(int n) : rank_(n, 0), size_(n, 1) { for (int i = 0; i < n; i++) par.push_back(i); } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y) { if (find(x) == find(y)) return true; else return false; } bool unite(int x, int y) { int xp = find(x); int yp = find(y); if (xp == yp) return false; if (rank_[xp] > rank_[yp]) { par[yp] = xp; size_[xp] += size_[yp]; } else if (rank_[xp] < rank_[yp]) { par[xp] = yp; size_[yp] += size_[xp]; } else { par[yp] = xp; rank_[xp]++; size_[xp] += size_[yp]; } return true; } } UnionFind; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<P> edge(m); rep(i, m) cin >> edge[i].first >> edge[i].second, edge[i].first--, edge[i].second--; vector<ll> ans(m); ll curr = (ll)n * (ll)(n - 1) / 2; UnionFind uf(n); for (int i = m - 1; i >= 0; i--) { ans[i] = curr; int a = edge[i].first, b = edge[i].second; if (!uf.same(a, b)) { curr -= (ll)uf.size_[uf.find(a)] * (ll)uf.size_[uf.find(b)]; uf.unite(a, b); } } assert(curr == 0); for (int i = 0; i < m; i++) { cout << ans[i] << endl; } return 0; }
replace
98
99
98
99
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (signed i = 0; i < n; ++i) #define repi(n) rep(i, n) #define int long long #define str string #define vint vector<int> #define pint pair<int, int> #define pb(a) push_back(a) #define all(v) v.begin(), v.end() #define yn(b) cout << ((b) ? "Yes" : "No") << endl #define YN(b) cout << ((b) ? "YES" : "NO") << endl #define call(a) \ for (auto t : a) \ cout << t << " "; \ cout << endl #define ENDL printf("\n"); #define debg(a) cout << #a << ":" << a << endl; #define SORT(a) sort(all(a)); #define INF 1LL << 60 #define inf INF #define out(i) cout << i << endl; int min(int a, int b) { if (a > b) return b; return a; } int max(int a, int b) { if (a > b) return a; return b; } class unionFind { public: vector<int> parent; unionFind(int n) { parent = vector<int>(n, -1); } int root(int a) { if (parent[a] < 0) return a; return (parent[a] = root(parent[a])); } int size(int a) { return (-parent[root(a)]); } bool connect(int a, int b) { a = root(a); b = root(b); if (a == b) { return false; } if (size(a) < size(b)) swap(a, b); parent[a] += parent[b]; parent[b] = a; return true; } }; signed main() { int n, m; cin >> n >> m; vint a(n), b(n); rep(i, m) cin >> a[i] >> b[i]; rep(i, m) a[i]--, b[i]--; vint ans(m); ans[m - 1] = n * (n - 1) / 2; unionFind uni(n); for (int i = m - 1; i > 0; --i) { ans[i - 1] = ans[i]; if (uni.root(a[i]) != uni.root(b[i])) { ans[i - 1] -= uni.size(a[i]) * uni.size(b[i]); uni.connect(a[i], b[i]); } } for (auto i : ans) cout << i << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (signed i = 0; i < n; ++i) #define repi(n) rep(i, n) #define int long long #define str string #define vint vector<int> #define pint pair<int, int> #define pb(a) push_back(a) #define all(v) v.begin(), v.end() #define yn(b) cout << ((b) ? "Yes" : "No") << endl #define YN(b) cout << ((b) ? "YES" : "NO") << endl #define call(a) \ for (auto t : a) \ cout << t << " "; \ cout << endl #define ENDL printf("\n"); #define debg(a) cout << #a << ":" << a << endl; #define SORT(a) sort(all(a)); #define INF 1LL << 60 #define inf INF #define out(i) cout << i << endl; int min(int a, int b) { if (a > b) return b; return a; } int max(int a, int b) { if (a > b) return a; return b; } class unionFind { public: vector<int> parent; unionFind(int n) { parent = vector<int>(n, -1); } int root(int a) { if (parent[a] < 0) return a; return (parent[a] = root(parent[a])); } int size(int a) { return (-parent[root(a)]); } bool connect(int a, int b) { a = root(a); b = root(b); if (a == b) { return false; } if (size(a) < size(b)) swap(a, b); parent[a] += parent[b]; parent[b] = a; return true; } }; signed main() { int n, m; cin >> n >> m; vint a(m), b(m); rep(i, m) cin >> a[i] >> b[i]; rep(i, m) a[i]--, b[i]--; vint ans(m); ans[m - 1] = n * (n - 1) / 2; unionFind uni(n); for (int i = m - 1; i > 0; --i) { ans[i - 1] = ans[i]; if (uni.root(a[i]) != uni.root(b[i])) { ans[i - 1] -= uni.size(a[i]) * uni.size(b[i]); uni.connect(a[i], b[i]); } } for (auto i : ans) cout << i << endl; return 0; }
replace
64
65
64
65
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; struct UnionFind { vector<ll> par; vector<ll> rank; vector<ll> sz; UnionFind(ll n) : par(n), rank(n), sz(n) { for (ll i = 0; i < n; i++) { par[i] = i; rank[i] = 0; sz[i] = 1; } } ll root(ll x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } void unite(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; if (rank[x] == rank[y]) rank[x]++; } } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { x = root(x); return sz[x]; } }; int main() { ll n, m; cin >> n >> m; vector<int> a(m); vector<int> b(m); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind tree(n); vector<int> ans(n); ll con = n * (n - 1) / 2; for (int i = m - 1; i >= 0; i--) { ans[i] = con; if (tree.same(a[i], b[i])) continue; ll asize = tree.size(a[i]); ll bsize = tree.size(b[i]); tree.unite(a[i], b[i]); ll size = tree.size(a[i]); con = con - size * (size - 1) / 2 + asize * (asize - 1) / 2 + bsize * (bsize - 1) / 2; } for (int i = 0; i < m; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct UnionFind { vector<ll> par; vector<ll> rank; vector<ll> sz; UnionFind(ll n) : par(n), rank(n), sz(n) { for (ll i = 0; i < n; i++) { par[i] = i; rank[i] = 0; sz[i] = 1; } } ll root(ll x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } void unite(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; if (rank[x] == rank[y]) rank[x]++; } } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { x = root(x); return sz[x]; } }; int main() { ll n, m; cin >> n >> m; vector<int> a(m); vector<int> b(m); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind tree(n); vector<ll> ans(m); ll con = n * (n - 1) / 2; for (int i = m - 1; i >= 0; i--) { ans[i] = con; if (tree.same(a[i], b[i])) continue; ll asize = tree.size(a[i]); ll bsize = tree.size(b[i]); tree.unite(a[i], b[i]); ll size = tree.size(a[i]); con = con - size * (size - 1) / 2 + asize * (asize - 1) / 2 + bsize * (bsize - 1) / 2; } for (int i = 0; i < m; i++) { cout << ans[i] << endl; } }
replace
53
54
53
54
0
p03108
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; struct UnionFind { vector<int> par; vector<long> sizes; UnionFind(int N) : par(N), sizes(N) { for (int i = 0; i < N; ++i) { par[i] = i; sizes[i] = 1; } } int root(int x) { return par[x] == x ? x : root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { par[rx] = ry; sizes[ry] += sizes[rx]; } } long size(int x) { return sizes[root(x)]; } bool same(int x, int y) { return root(x) == root(y); } }; int main() { int a[100010], b[100010], m, x, y; long n, c[100010]; cin >> n >> m; for (int i = 0; i < m; ++i) cin >> a[i] >> b[i]; UnionFind tree(n); c[m] = n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { x = a[i] - 1; y = b[i] - 1; if (tree.same(x, y)) { c[i] = c[i + 1]; } else { c[i] = c[i + 1] - tree.size(x) * tree.size(y); tree.unite(x, y); } } for (int i = 1; i <= m; ++i) { cout << c[i] - c[0] << endl; } }
#include <iostream> #include <vector> using namespace std; struct UnionFind { vector<int> par; vector<long> sizes; UnionFind(int N) : par(N), sizes(N) { for (int i = 0; i < N; ++i) { par[i] = i; sizes[i] = 1; } } int root(int x) { return par[x] == x ? x : root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { int sx = sizes[rx]; int sy = sizes[ry]; if (sx < sy) { par[rx] = ry; sizes[ry] += sx; } else { par[ry] = rx; sizes[rx] += sy; } } } long size(int x) { return sizes[root(x)]; } bool same(int x, int y) { return root(x) == root(y); } }; int main() { int a[100010], b[100010], m, x, y; long n, c[100010]; cin >> n >> m; for (int i = 0; i < m; ++i) cin >> a[i] >> b[i]; UnionFind tree(n); c[m] = n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { x = a[i] - 1; y = b[i] - 1; if (tree.same(x, y)) { c[i] = c[i + 1]; } else { c[i] = c[i + 1] - tree.size(x) * tree.size(y); tree.unite(x, y); } } for (int i = 1; i <= m; ++i) { cout << c[i] - c[0] << endl; } }
replace
18
20
18
27
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, m, n) for (int i = m; i < n; i++) #define repr(i, m, n) for (int i = m; i >= n; i--) #define db(x) cout << #x << "=" << x << endl template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define y0 y3487465 // y1 j0 j1 #define INF2 4000000000000000037 #define INF 1000000007 #define PI (acos(-1)) #define MOD 1000000007 #define EPS 1e(-9); using namespace std; typedef long long ll; struct UnionFind { vector<int> par; // 親ノード rootで取得すること vector<int> rank; vector<ll> nodes; UnionFind(int n) { par.resize(n); rank.resize(n); nodes.resize(n); rep(i, n) { par[i] = i, rank[i] = 0; nodes[i] = 1; } } int root(int x) { // ノードの根を返す if (par[x] == x) return x; // 親が自分なら自分を返す else { int r = root(par[x]); // 根まで遡りながら経路圧縮 return par[x] = r; } } bool issame(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[y] = x; // ランクの小さい方に接続 return true; } ll query(int x, int y) { if (issame(x, y)) return 0; ll sum = nodes[root(x)] + nodes[root(y)]; ll res = nodes[root(x)] * nodes[root(y)]; merge(x, y); nodes[root(x)] = sum; return res; } }; void Main() { int N, M; cin >> N >> M; int A[N], B[N]; UnionFind uf(N); rep(i, M) { cin >> A[i] >> B[i]; --A[i]; --B[i]; } ll ans[M + 1]; ll sum = 0; repr(i, M - 1, 0) { ans[i] = uf.query(A[i], B[i]); } rep(i, M) { cout << ans[i] << "\n"; ans[i + 1] += ans[i]; } } //----------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, m, n) for (int i = m; i < n; i++) #define repr(i, m, n) for (int i = m; i >= n; i--) #define db(x) cout << #x << "=" << x << endl template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define y0 y3487465 // y1 j0 j1 #define INF2 4000000000000000037 #define INF 1000000007 #define PI (acos(-1)) #define MOD 1000000007 #define EPS 1e(-9); using namespace std; typedef long long ll; struct UnionFind { vector<int> par; // 親ノード rootで取得すること vector<int> rank; vector<ll> nodes; UnionFind(int n) { par.resize(n); rank.resize(n); nodes.resize(n); rep(i, n) { par[i] = i, rank[i] = 0; nodes[i] = 1; } } int root(int x) { // ノードの根を返す if (par[x] == x) return x; // 親が自分なら自分を返す else { int r = root(par[x]); // 根まで遡りながら経路圧縮 return par[x] = r; } } bool issame(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[y] = x; // ランクの小さい方に接続 return true; } ll query(int x, int y) { if (issame(x, y)) return 0; ll sum = nodes[root(x)] + nodes[root(y)]; ll res = nodes[root(x)] * nodes[root(y)]; merge(x, y); nodes[root(x)] = sum; return res; } }; void Main() { int N, M; cin >> N >> M; int A[M], B[M]; UnionFind uf(N); rep(i, M) { cin >> A[i] >> B[i]; --A[i]; --B[i]; } ll ans[M + 1]; ll sum = 0; repr(i, M - 1, 0) { ans[i] = uf.query(A[i], B[i]); } rep(i, M) { cout << ans[i] << "\n"; ans[i + 1] += ans[i]; } } //----------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------
replace
82
83
82
83
0
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <functional> #include <iterator> #include <map> #include <vector> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<ll>; using vvll = vector<vll>; struct v_t { int p; vi c; }; using vvt = vector<v_t>; void vmove(vvt &v, int a, int b) { for (int mbc : v[b].c) { v[mbc].p = a; } copy(v[b].c.begin(), v[b].c.end(), back_inserter(v[a].c)); v[b].c.clear(); } ll uni(vvt &v, ll ans, int n, int a, int b) { if (v[a].p == v[b].p) return ans; ll an = v[v[a].p].c.size(); ll bn = v[v[b].p].c.size(); if (v[a].c.size() < v[b].c.size()) swap(a, b); vmove(v, v[a].p, v[b].p); return ans - an * (n - an) - bn * (n - bn) + (an + bn) * (n - (an + bn)); } int main(void) { int n, m; while (scanf("%d%d", &n, &m) == 2) { vi a(m), b(m); vector<v_t> v(n + 1); vll ans(m + 1); for (int i = 1; i <= n; i++) { v[i].p = i; v[i].c.push_back(i); } for (int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); } ans[m] = (ll)n * (n - 1); for (int i = m - 1; i >= 0; i--) { ans[i] = ans[i + 1]; ans[i] = uni(v, ans[i + 1], n, a[i], b[i]); } for (int i = 1; i <= m; i++) { printf("%lld\n", ans[i] / 2); } } return 0; }
#include <algorithm> #include <cstdio> #include <functional> #include <iterator> #include <map> #include <vector> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<ll>; using vvll = vector<vll>; struct v_t { int p; vi c; }; using vvt = vector<v_t>; void vmove(vvt &v, int a, int b) { for (int mbc : v[b].c) { v[mbc].p = a; } copy(v[b].c.begin(), v[b].c.end(), back_inserter(v[a].c)); v[b].c.clear(); } ll uni(vvt &v, ll ans, int n, int a, int b) { if (v[a].p == v[b].p) return ans; ll an = v[v[a].p].c.size(); ll bn = v[v[b].p].c.size(); if (v[v[a].p].c.size() < v[v[b].p].c.size()) swap(a, b); vmove(v, v[a].p, v[b].p); return ans - an * (n - an) - bn * (n - bn) + (an + bn) * (n - (an + bn)); } int main(void) { int n, m; while (scanf("%d%d", &n, &m) == 2) { vi a(m), b(m); vector<v_t> v(n + 1); vll ans(m + 1); for (int i = 1; i <= n; i++) { v[i].p = i; v[i].c.push_back(i); } for (int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); } ans[m] = (ll)n * (n - 1); for (int i = m - 1; i >= 0; i--) { ans[i] = ans[i + 1]; ans[i] = uni(v, ans[i + 1], n, a[i], b[i]); } for (int i = 1; i <= m; i++) { printf("%lld\n", ans[i] / 2); } } return 0; }
replace
38
39
38
39
TLE
p03108
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; class UnionFind { public: vector<int> parent; UnionFind(int n) { parent = vector<int>(n, -1); } int root(int a) { if (parent[a] < 0) return a; return parent[a] = root(parent[a]); } int size(int a) { return -parent[root(a)]; } int connect(int a, int b) { a = root(a); b = root(b); if (a == b) { return false; } if (size(a) < size(b)) swap(a, b); parent[a] += parent[b]; parent[b] = a; return true; } }; int main() { int n, m; cin >> n >> m; int a[m], b[m]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; } UnionFind uf(n); long long ans[m]; ans[m - 1] = (long long)n * (n - 1) / 2; for (int i = m - 1; i >= 1; i--) { ans[i - 1] = ans[i]; if (uf.root(a[i]) != uf.root(b[i])) { ans[i - 1] = ans[i] - (long long)uf.size(a[i]) * uf.size(b[i]); uf.connect(a[i], b[i]); } } for (int i = 0; i < m; i++) cout << ans[i] << endl; return 0; };
#include <iostream> #include <vector> using namespace std; class UnionFind { public: vector<int> parent; UnionFind(int n) { parent = vector<int>(n, -1); } int root(int a) { if (parent[a] < 0) return a; return parent[a] = root(parent[a]); } int size(int a) { return -parent[root(a)]; } int connect(int a, int b) { a = root(a); b = root(b); if (a == b) { return false; } if (size(a) < size(b)) swap(a, b); parent[a] += parent[b]; parent[b] = a; return true; } }; int main() { int n, m; cin >> n >> m; int a[m], b[m]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind uf(n); long long ans[m]; ans[m - 1] = (long long)n * (n - 1) / 2; for (int i = m - 1; i >= 1; i--) { ans[i - 1] = ans[i]; if (uf.root(a[i]) != uf.root(b[i])) { ans[i - 1] = ans[i] - (long long)uf.size(a[i]) * uf.size(b[i]); uf.connect(a[i], b[i]); } } for (int i = 0; i < m; i++) cout << ans[i] << endl; return 0; };
insert
35
35
35
37
0
p03108
C++
Runtime Error
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef int itn; const ll LINF = 1e18; const int INF = 1e8; // マクロ定義 #define vvint(vec, n, m, l) \ vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化 #define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l)); #define vint vector<int> #define pint pair<int, int> #define rep(i, a) for (int i = 0; i < (a); i++) #define all(x) (x).begin(), (x).end() #define debug system("pause") // デバッグ用 class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool isSame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<ll> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; } UnionFind tree(n); vector<ll> ans(m); ans[m - 1] = n * (n - 1); ans[m - 1] /= 2; for (int i = m - 2; i >= 0; i--) { if (!tree.isSame(a[i + 1], b[i + 1])) { ans[i] = ans[i + 1] - tree.size(a[i + 1]) * tree.size(b[i + 1]); tree.merge(a[i + 1], b[i + 1]); } else { ans[i] = ans[i + 1]; } } rep(i, m) { cout << ans[i] << endl; } }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef int itn; const ll LINF = 1e18; const int INF = 1e8; // マクロ定義 #define vvint(vec, n, m, l) \ vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化 #define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l)); #define vint vector<int> #define pint pair<int, int> #define rep(i, a) for (int i = 0; i < (a); i++) #define all(x) (x).begin(), (x).end() #define debug system("pause") // デバッグ用 class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool isSame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector<ll> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; } UnionFind tree(n); vector<ll> ans(m); ans[m - 1] = n * (n - 1); ans[m - 1] /= 2; for (int i = m - 2; i >= 0; i--) { if (!tree.isSame(a[i + 1] - 1, b[i + 1] - 1)) { ans[i] = ans[i + 1] - tree.size(a[i + 1] - 1) * tree.size(b[i + 1] - 1); tree.merge(a[i + 1] - 1, b[i + 1] - 1); } else { ans[i] = ans[i + 1]; } } rep(i, m) { cout << ans[i] << endl; } }
replace
95
98
95
98
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for (int i = m; i < n; ++i) #define rem(i, m, n) for (int i = m; i >= n; --i) typedef long long ll; #define mod 1000000007 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 (b < a) { a = b; return 1; } return 0; } class UnionFind { public: vector<int> uni; UnionFind(int s) : uni(s, -1) {} int root(int a) { if (uni[a] < 0) return a; return uni[a] = root(uni[a]); } bool connect(int a, int b) { a = root(a); b = root(b); if (a == b) return false; if (uni[a] > uni[b]) swap(a, b); uni[a] = uni[a] + uni[b]; uni[b] = a; return true; } bool isConnect(int a, int b) { return root(a) == root(b); } int size(int a) { return -uni[root(a)]; } }; int main() { int N, M; cin >> N >> M; vector<int> A(N), B(N); rep(i, 0, M) { cin >> A[i] >> B[i]; A[i]--, B[i]--; } vector<ll> ans(M); UnionFind uf(N); rem(i, M - 1, 0) { if (uf.isConnect(A[i], B[i])) continue; ans[i] = (ll)uf.size(A[i]) * (ll)uf.size(B[i]); uf.connect(A[i], B[i]); } rep(i, 1, M) ans[i] += ans[i - 1]; rep(i, 0, M) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for (int i = m; i < n; ++i) #define rem(i, m, n) for (int i = m; i >= n; --i) typedef long long ll; #define mod 1000000007 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 (b < a) { a = b; return 1; } return 0; } class UnionFind { public: vector<int> uni; UnionFind(int s) : uni(s, -1) {} int root(int a) { if (uni[a] < 0) return a; return uni[a] = root(uni[a]); } bool connect(int a, int b) { a = root(a); b = root(b); if (a == b) return false; if (uni[a] > uni[b]) swap(a, b); uni[a] = uni[a] + uni[b]; uni[b] = a; return true; } bool isConnect(int a, int b) { return root(a) == root(b); } int size(int a) { return -uni[root(a)]; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); rep(i, 0, M) { cin >> A[i] >> B[i]; A[i]--, B[i]--; } vector<ll> ans(M); UnionFind uf(N); rem(i, M - 1, 0) { if (uf.isConnect(A[i], B[i])) continue; ans[i] = (ll)uf.size(A[i]) * (ll)uf.size(B[i]); uf.connect(A[i], B[i]); } rep(i, 1, M) ans[i] += ans[i - 1]; rep(i, 0, M) cout << ans[i] << endl; return 0; }
replace
52
53
52
53
0
p03108
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define MOD 1000000007 struct UnionFind { vector<int> data; void init(int n) { data.assign(n, -1); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[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) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } int size(int x) { return -data[root(x)]; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; } UnionFind uf; uf.init(n); ll res = 1ll * n * (n - 1) / 2; vector<ll> ans(m); for (int i = m - 1; i >= 0; --i) { ans[i] = res; if (!uf.same(a[i], b[i])) { res -= 1ll * uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } for (int i = 0; i < m; ++i) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define MOD 1000000007 struct UnionFind { vector<int> data; void init(int n) { data.assign(n, -1); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[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) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } int size(int x) { return -data[root(x)]; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } UnionFind uf; uf.init(n); ll res = 1ll * n * (n - 1) / 2; vector<ll> ans(m); for (int i = m - 1; i >= 0; --i) { ans[i] = res; if (!uf.same(a[i], b[i])) { res -= 1ll * uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } for (int i = 0; i < m; ++i) { cout << ans[i] << endl; } return 0; }
insert
43
43
43
44
0
p03108
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define MOD 1000000007 #define REP(i, N) for (int i = 0; i < N; ++i) #define REP1(i, N) for (int i = 1; i <= N; ++i) #define RREP(i, N) for (int i = N - 1; i >= 0; --i) #define ALL(a) a.begin(), a.end() struct UnionFind { vector<int> par; vector<int> sz; UnionFind(int n) { init(n); } void init(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; ++i) { par[i] = i; sz[i] = 1; } } 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 (sz[x] < sz[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; int main() { int n, m; cin >> n >> m; int a[n], b[n]; for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } UnionFind uf(n); vector<ll> ans; ll res = 1ll * n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { ans.push_back(res); if (!uf.same(a[i], b[i])) { res -= 1ll * uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } reverse(ans.begin(), ans.end()); for (int i = 0; i < m; ++i) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define MOD 1000000007 #define REP(i, N) for (int i = 0; i < N; ++i) #define REP1(i, N) for (int i = 1; i <= N; ++i) #define RREP(i, N) for (int i = N - 1; i >= 0; --i) #define ALL(a) a.begin(), a.end() struct UnionFind { vector<int> par; vector<int> sz; UnionFind(int n) { init(n); } void init(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; ++i) { par[i] = i; sz[i] = 1; } } 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 (sz[x] < sz[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; int main() { int n, m; cin >> n >> m; int a[m], b[m]; for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } UnionFind uf(n); vector<ll> ans; ll res = 1ll * n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { ans.push_back(res); if (!uf.same(a[i], b[i])) { res -= 1ll * uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } reverse(ans.begin(), ans.end()); for (int i = 0; i < m; ++i) { cout << ans[i] << endl; } return 0; }
replace
59
60
59
60
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rep2(x, fr, to) for (int x = (fr); x < (to); x++) #define rep(x, to) for (int x = 0; x < (to); x++) #define repr(x, fr, to) for (int x = (fr); x >= (to); x--) #define all(c) c.begin(), c.end() #define sz(v) (int)v.size() using namespace std; typedef long long ll; typedef vector<int> VI; typedef pair<int, int> pii; typedef vector<ll> VL; const int MD = (int)1e9 + 7; void dbg() { cerr << "\n"; } template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) { cerr << fst << ": "; dbg(rst...); } template <class T, class T2> void amax(T &a, T2 b) { if (a < b) a = b; } struct UF { vector<int> p; UF() {} UF(int n) : p(n, -1) {} int find(int a) { return p[a] < 0 ? a : p[a] = find(p[a]); } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (-p[a] < -p[b]) swap(a, b); p[a] += p[b]; p[b] = a; return true; } bool root(int a) { return p[a] < 0; } bool same(int a, int b) { return find(a) == find(b); } int size(int a) { return -p[find(a)]; } }; int main() { // cin.tie(0); ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<pii> ab(n); rep(i, m) { int a, b; cin >> a >> b; ab[i] = pii(a - 1, b - 1); } ll ans = (ll)n * (n - 1) / 2; VL res(m); UF uf(n); repr(i, m - 1, 0) { res[i] = ans; int a, b; tie(a, b) = ab[i]; if (!uf.same(a, b)) { ll bsa = uf.size(a), bsb = uf.size(b); ans -= bsa * bsb; uf.unite(a, b); } } for (auto x : res) cout << x << "\n"; return 0; }
#include <bits/stdc++.h> #define rep2(x, fr, to) for (int x = (fr); x < (to); x++) #define rep(x, to) for (int x = 0; x < (to); x++) #define repr(x, fr, to) for (int x = (fr); x >= (to); x--) #define all(c) c.begin(), c.end() #define sz(v) (int)v.size() using namespace std; typedef long long ll; typedef vector<int> VI; typedef pair<int, int> pii; typedef vector<ll> VL; const int MD = (int)1e9 + 7; void dbg() { cerr << "\n"; } template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) { cerr << fst << ": "; dbg(rst...); } template <class T, class T2> void amax(T &a, T2 b) { if (a < b) a = b; } struct UF { vector<int> p; UF() {} UF(int n) : p(n, -1) {} int find(int a) { return p[a] < 0 ? a : p[a] = find(p[a]); } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (-p[a] < -p[b]) swap(a, b); p[a] += p[b]; p[b] = a; return true; } bool root(int a) { return p[a] < 0; } bool same(int a, int b) { return find(a) == find(b); } int size(int a) { return -p[find(a)]; } }; int main() { // cin.tie(0); ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<pii> ab(m); rep(i, m) { int a, b; cin >> a >> b; ab[i] = pii(a - 1, b - 1); } ll ans = (ll)n * (n - 1) / 2; VL res(m); UF uf(n); repr(i, m - 1, 0) { res[i] = ans; int a, b; tie(a, b) = ab[i]; if (!uf.same(a, b)) { ll bsa = uf.size(a), bsb = uf.size(b); ans -= bsa * bsb; uf.unite(a, b); } } for (auto x : res) cout << x << "\n"; return 0; }
replace
49
50
49
50
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define PI arccos(-1) #define rep(i, N) for (int i = 0; i < (N); i++) #define REP(i, a, N) for (int i = a; i < (N); i++) #define rrep(i, N, k) \ for (int i = (N); i >= (k); i--) \ ; #define all(x) (x).begin(), (x).end() #define vi vector<int> #define pi pair<int, int> #define pl pair<ll, ll> template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &elemnt : v) is >> elemnt; return is; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } const int MOD = 1e9 + 7; const int INF = numeric_limits<int>::max() - (int)1e8; const ll INFLL = numeric_limits<ll>::max() - (ll)1e17; class UnionFind { private: vector<int> data; public: explicit UnionFind(size_t data_size) : data(data_size, -1) {} bool same(size_t x, size_t y) { return find(x) == find(y); } bool unite(size_t x, size_t y) { if (same(x, y)) return false; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return true; } int find(size_t index) { if (data[index] < 0) return index; else return (data[index] = find(data[index])); } int size(size_t index) { return (-data[find(index)]); } }; 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]; A[i]--; B[i]--; } auto data = UnionFind(N); ll temp = 0; vector<ll> ans(M, 0); for (int i = M - 1; i >= 0; i--) { ans[i] = N * (N - 1) / 2 - temp; if (data.find(A[i]) != data.find(B[i])) { temp += data.size(A[i]) * data.size(B[i]); data.unite(A[i], B[i]); } } for (auto now : ans) cout << now << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define PI arccos(-1) #define rep(i, N) for (int i = 0; i < (N); i++) #define REP(i, a, N) for (int i = a; i < (N); i++) #define rrep(i, N, k) \ for (int i = (N); i >= (k); i--) \ ; #define all(x) (x).begin(), (x).end() #define vi vector<int> #define pi pair<int, int> #define pl pair<ll, ll> template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &elemnt : v) is >> elemnt; return is; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } const int MOD = 1e9 + 7; const int INF = numeric_limits<int>::max() - (int)1e8; const ll INFLL = numeric_limits<ll>::max() - (ll)1e17; class UnionFind { private: vector<int> data; public: explicit UnionFind(size_t data_size) : data(data_size, -1) {} bool same(size_t x, size_t y) { return find(x) == find(y); } bool unite(size_t x, size_t 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(size_t index) { if (data[index] < 0) return index; else return (data[index] = find(data[index])); } int size(size_t index) { return (-data[find(index)]); } }; 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]; A[i]--; B[i]--; } auto data = UnionFind(N); ll temp = 0; vector<ll> ans(M, 0); for (int i = M - 1; i >= 0; i--) { ans[i] = N * (N - 1) / 2 - temp; if (data.find(A[i]) != data.find(B[i])) { temp += data.size(A[i]) * data.size(B[i]); data.unite(A[i], B[i]); } } for (auto now : ans) cout << now << endl; }
replace
36
37
36
38
0
p03108
C++
Runtime Error
#include <iostream> using namespace std; #ifdef DEBUG #define IFD if (true) #else #define IFD if (false) #endif #include <vector> // 要素個数カウント付き Union Find // 要素の型。int か long あたりを想定 template <typename T_uf_data> class UnionFind { private: std::vector<T_uf_data> par; // 親 (parent) を指す std::vector<T_uf_data> rnk; std::vector<T_uf_data> cnt; // 引数の値が含まれるグループの要素数(ルートノードのみが意味を持つ) // その値を持つルートノードを返す T_uf_data find_root(int x) { if (par[x] == x) { return x; } else { return par[x] = find_root(par[x]); } } public: // コンストラクタの引数で要素数を指定 UnionFind(int n) : par(n), rnk(n), cnt(n) { for (int i = 0; i < n; i++) { par[i] = i; // 親を持たないノードは自分自身を親としておく rnk[i] = 0; cnt[i] = 1; // 初期状態ではすべてのノードの要素数は 1 } } // 二つの要素をグループ化 void Unite(int x, int y) { x = find_root(x); y = find_root(y); if (x == y) return; if (rnk[x] < rnk[y]) { // y が親になる par[x] = y; cnt[y] += cnt[x]; } else { // x が親になる par[y] = x; cnt[x] += cnt[y]; if (rnk[x] == rnk[y]) rnk[x]++; } } // x と y が同じグループかを返す bool isSameGroup(int x, int y) { return find_root(x) == find_root(y); } // x を含むグループの要素数を返す int Size(int x) { return cnt[find_root(x)]; } }; int main() { int N, M, A[100001], B[100001]; long long ans[100001]; cin >> N >> M; UnionFind<int> uf(M + 1); for (int i = 1; i <= M; i++) { cin >> A[i] >> B[i]; } ans[M] = (long long)N * (N - 1) / 2; for (int i = M - 1; i >= 1; i--) { ans[i] = ans[i + 1]; if (!uf.isSameGroup(A[i + 1], B[i + 1])) { ans[i] -= (long long)uf.Size(A[i + 1]) * uf.Size(B[i + 1]); uf.Unite(A[i + 1], B[i + 1]); } } for (int i = 1; i <= M; i++) { cout << ans[i] << endl; } return 0; }
#include <iostream> using namespace std; #ifdef DEBUG #define IFD if (true) #else #define IFD if (false) #endif #include <vector> // 要素個数カウント付き Union Find // 要素の型。int か long あたりを想定 template <typename T_uf_data> class UnionFind { private: std::vector<T_uf_data> par; // 親 (parent) を指す std::vector<T_uf_data> rnk; std::vector<T_uf_data> cnt; // 引数の値が含まれるグループの要素数(ルートノードのみが意味を持つ) // その値を持つルートノードを返す T_uf_data find_root(int x) { if (par[x] == x) { return x; } else { return par[x] = find_root(par[x]); } } public: // コンストラクタの引数で要素数を指定 UnionFind(int n) : par(n), rnk(n), cnt(n) { for (int i = 0; i < n; i++) { par[i] = i; // 親を持たないノードは自分自身を親としておく rnk[i] = 0; cnt[i] = 1; // 初期状態ではすべてのノードの要素数は 1 } } // 二つの要素をグループ化 void Unite(int x, int y) { x = find_root(x); y = find_root(y); if (x == y) return; if (rnk[x] < rnk[y]) { // y が親になる par[x] = y; cnt[y] += cnt[x]; } else { // x が親になる par[y] = x; cnt[x] += cnt[y]; if (rnk[x] == rnk[y]) rnk[x]++; } } // x と y が同じグループかを返す bool isSameGroup(int x, int y) { return find_root(x) == find_root(y); } // x を含むグループの要素数を返す int Size(int x) { return cnt[find_root(x)]; } }; int main() { int N, M, A[100001], B[100001]; long long ans[100001]; cin >> N >> M; UnionFind<int> uf(100001); for (int i = 1; i <= M; i++) { cin >> A[i] >> B[i]; } ans[M] = (long long)N * (N - 1) / 2; for (int i = M - 1; i >= 1; i--) { ans[i] = ans[i + 1]; if (!uf.isSameGroup(A[i + 1], B[i + 1])) { ans[i] -= (long long)uf.Size(A[i + 1]) * uf.Size(B[i + 1]); uf.Unite(A[i + 1], B[i + 1]); } } for (int i = 1; i <= M; i++) { cout << ans[i] << endl; } return 0; }
replace
74
75
74
75
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; struct UnionFind { vector<int> par; // 正の場合、親を表す // 負の場合、根で数値は連結成分のサイズ UnionFind(int n) : par(n, -1) {} int root(int x) { if (par[x] < 0) { return x; } else { return par[x] = root(par[x]); // 経路圧縮 } } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // 小さい方を大きい方にくっつけるため par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -par[root(x)]; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m); vector<int> b(m); for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); vector<ll> ans; ll t = (ll)n * ((ll)n - 1) / 2; ans.push_back(t); UnionFind tree(n); for (int i = 0; i < m - 1; ++i) { ll a_size = (ll)tree.size(a[i]); ll b_size = (ll)tree.size(b[i]); tree.unite(a[i], b[i]); ll size = (ll)tree.size(a[i]); if (t > 0 && size > a_size && size > b_size) { t -= size * (size - 1) / 2 - (a_size) * (a_size - 1) / 2 - (b_size) * (b_size - 1) / 2; } if (t < 0) t = 0; ans.push_back(t); } reverse(ans.begin(), ans.end()); for (ll i = 0; i < ans.size(); ++i) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; struct UnionFind { vector<int> par; // 正の場合、親を表す // 負の場合、根で数値は連結成分のサイズ UnionFind(int n) : par(n, -1) {} int root(int x) { if (par[x] < 0) { return x; } else { return par[x] = root(par[x]); // 経路圧縮 } } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // 小さい方を大きい方にくっつけるため par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -par[root(x)]; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m); vector<int> b(m); for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; --a[i]; --b[i]; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); vector<ll> ans; ll t = (ll)n * ((ll)n - 1) / 2; ans.push_back(t); UnionFind tree(n); for (int i = 0; i < m - 1; ++i) { ll a_size = (ll)tree.size(a[i]); ll b_size = (ll)tree.size(b[i]); tree.unite(a[i], b[i]); ll size = (ll)tree.size(a[i]); if (t > 0 && size > a_size && size > b_size) { t -= size * (size - 1) / 2 - (a_size) * (a_size - 1) / 2 - (b_size) * (b_size - 1) / 2; } if (t < 0) t = 0; ans.push_back(t); } reverse(ans.begin(), ans.end()); for (ll i = 0; i < ans.size(); ++i) { cout << ans[i] << endl; } }
insert
52
52
52
54
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define double long double #define rep(i, a, n) for (int i = (int)(a); (i) < (int)(n); ++(i)) #define repe(i, a, n) for (int i = (int)(a); (i) <= (int)(n); ++(i)) #define repif(i, a, x) for (int i = (int)(a); (x); ++(i)) #define repr(i, a, n) for (int i = ((int)(a)-1); (i) >= (int)(n); --(i)) #define reper(i, a, n) for (int i = (int)(a); (i) >= (int)(n); --(i)) #define SZ(x) ((int)(x).size()) #define ALL(x) begin(x), end(x) #define CEIL(x) ((int)ceil(((double)x))) #define POW(x, y) ((int)pow(x, y)) #define UNIQUE(x) (x).erase(unique(ALL((x))), end(x)) using namespace std; using VI = vector<int>; using VVI = vector<vector<int>>; using PII = pair<int, int>; using TIII = tuple<int, int, int>; using VPII = vector<pair<int, int>>; using VTIII = vector<tuple<int, int, int>>; using Complex = complex<double>; template <typename T> inline bool chmax(T &a, const T &b, bool t = false) { if (a < b or (t and a == b)) { a = b; return true; } return false; } template <typename T> inline bool chmin(T &a, const T &b, bool t = false) { if (a > b or (t and a == b)) { a = b; return true; } return false; } template <typename T> inline bool isin(const T &x, const T &l, const T &r) { return l <= x and x < r; } inline ostream &fcout(int n = 10) { return cout << fixed << setprecision(n); } inline ostream &rcout(int n = 2) { return cout << right << setw(n); } constexpr int64_t INF = numeric_limits<int64_t>::max(); constexpr int32_t INF32 = numeric_limits<int32_t>::max(); constexpr int64_t MOD = 1000000007LL; constexpr double PI = 3.14159265358979323846264338327950288; constexpr double EPS = 1e-10; struct UF { int num; vector<int> r, p, s; UF() = default; UF(int sz) : num(sz), r(sz, 1), p(sz, 0), s(sz, 1) { iota(begin(p), end(p), 0LL); } int find(int x) { return (x == p[x] ? x : p[x] = find(p[x])); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y); r[x] += r[y]; p[y] = x; s[x] += s[y]; --num; } int count() const { return num; } int size(int x) { return s[find(x)]; } }; int N, M; PII AB[100010]; UF uf; int ans[100010]; int32_t main() { cin >> N >> M; uf = UF(N); repr(i, M, 0) cin >> AB[i].first >> AB[i].second; ans[0] = N * (N - 1) / 2; rep(i, 0, M) { auto [a, b] = AB[i]; if (uf.same(a, b)) ans[i + 1] = ans[i]; else { ans[i + 1] = ans[i] - uf.size(a) * uf.size(b); uf.unite(a, b); } } repr(i, M, 0) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> #define int long long #define double long double #define rep(i, a, n) for (int i = (int)(a); (i) < (int)(n); ++(i)) #define repe(i, a, n) for (int i = (int)(a); (i) <= (int)(n); ++(i)) #define repif(i, a, x) for (int i = (int)(a); (x); ++(i)) #define repr(i, a, n) for (int i = ((int)(a)-1); (i) >= (int)(n); --(i)) #define reper(i, a, n) for (int i = (int)(a); (i) >= (int)(n); --(i)) #define SZ(x) ((int)(x).size()) #define ALL(x) begin(x), end(x) #define CEIL(x) ((int)ceil(((double)x))) #define POW(x, y) ((int)pow(x, y)) #define UNIQUE(x) (x).erase(unique(ALL((x))), end(x)) using namespace std; using VI = vector<int>; using VVI = vector<vector<int>>; using PII = pair<int, int>; using TIII = tuple<int, int, int>; using VPII = vector<pair<int, int>>; using VTIII = vector<tuple<int, int, int>>; using Complex = complex<double>; template <typename T> inline bool chmax(T &a, const T &b, bool t = false) { if (a < b or (t and a == b)) { a = b; return true; } return false; } template <typename T> inline bool chmin(T &a, const T &b, bool t = false) { if (a > b or (t and a == b)) { a = b; return true; } return false; } template <typename T> inline bool isin(const T &x, const T &l, const T &r) { return l <= x and x < r; } inline ostream &fcout(int n = 10) { return cout << fixed << setprecision(n); } inline ostream &rcout(int n = 2) { return cout << right << setw(n); } constexpr int64_t INF = numeric_limits<int64_t>::max(); constexpr int32_t INF32 = numeric_limits<int32_t>::max(); constexpr int64_t MOD = 1000000007LL; constexpr double PI = 3.14159265358979323846264338327950288; constexpr double EPS = 1e-10; struct UF { int num; vector<int> r, p, s; UF() = default; UF(int sz) : num(sz), r(sz, 1), p(sz, 0), s(sz, 1) { iota(begin(p), end(p), 0LL); } int find(int x) { return (x == p[x] ? x : p[x] = find(p[x])); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y); r[x] += r[y]; p[y] = x; s[x] += s[y]; --num; } int count() const { return num; } int size(int x) { return s[find(x)]; } }; int N, M; PII AB[100010]; UF uf; int ans[100010]; int32_t main() { cin >> N >> M; uf = UF(N); repr(i, M, 0) cin >> AB[i].first >> AB[i].second; ans[0] = N * (N - 1) / 2; rep(i, 0, M) { auto [a, b] = AB[i]; a--; b--; if (uf.same(a, b)) ans[i + 1] = ans[i]; else { ans[i + 1] = ans[i] - uf.size(a) * uf.size(b); uf.unite(a, b); } } repr(i, M, 0) cout << ans[i] << endl; return 0; }
insert
83
83
83
85
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long struct UnionFind { vector<int> par; vector<int> siz; UnionFind(int n) : par(n), siz(n) { for (int i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } int root(int x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } int size(int x) { if (par[x] == x) return siz[x]; else return siz[x] = size(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { par[ry] = rx; siz[rx] += siz[ry]; } } bool same(int x, int y) { return root(x) == root(y); } }; signed main() { // cout << fixed << setprecision(10) << flush; int n, m; cin >> n >> m; vector<int> a(m), b(m); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; } UnionFind uf(n); vector<int> ans(m); for (int i = m - 1; i >= 0; i--) { int sa = uf.size(a[i]); int sb = uf.size(b[i]); if (uf.same(a[i], b[i])) { ans[i] = 0; } else { ans[i] = sa * sb; } uf.unite(a[i], b[i]); } for (int i = 0; i < m - 1; i++) { ans[i + 1] += ans[i]; } for (int i = 0; i < m; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long struct UnionFind { vector<int> par; vector<int> siz; UnionFind(int n) : par(n), siz(n) { for (int i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } int root(int x) { if (par[x] == x) return x; else return par[x] = root(par[x]); } int size(int x) { if (par[x] == x) return siz[x]; else return siz[x] = size(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx != ry) { par[ry] = rx; siz[rx] += siz[ry]; } } bool same(int x, int y) { return root(x) == root(y); } }; signed main() { // cout << fixed << setprecision(10) << flush; int n, m; cin >> n >> m; vector<int> a(m), b(m); for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind uf(n); vector<int> ans(m); for (int i = m - 1; i >= 0; i--) { int sa = uf.size(a[i]); int sb = uf.size(b[i]); if (uf.same(a[i], b[i])) { ans[i] = 0; } else { ans[i] = sa * sb; } uf.unite(a[i], b[i]); } for (int i = 0; i < m - 1; i++) { ans[i + 1] += ans[i]; } for (int i = 0; i < m; i++) { cout << ans[i] << endl; } return 0; }
insert
50
50
50
52
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; struct union_find { vector<int> par; union_find(int n) : par(n, -1) {} int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (par[rx] > par[ry]) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -par[root(x)]; } }; int main() { ll n, m; cin >> n >> m; union_find tree(n); vector<pair<ll, ll>> bridge(m); for (int i = 0; i < m; i++) { cin >> bridge[i].first >> bridge[i].second; } vector<ll> ans(m, 0); ans[0] = n * (n - 1) / 2; reverse(bridge.begin(), bridge.end()); for (int i = 0; i < m; i++) { ans[i + 1] = ans[i]; ll x = bridge[i].first - 1; ll y = bridge[i].second - 1; ll xs = tree.size(x); ll ys = tree.size(y); if (tree.same(x, y) == false) { ans[i + 1] -= xs * ys; } tree.unite(x, y); } reverse(ans.begin(), ans.end()); for (int i = 0; i < m; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct union_find { vector<int> par; union_find(int n) : par(n, -1) {} int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (par[rx] > par[ry]) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -par[root(x)]; } }; int main() { ll n, m; cin >> n >> m; union_find tree(n); vector<pair<ll, ll>> bridge(m); for (int i = 0; i < m; i++) { cin >> bridge[i].first >> bridge[i].second; } vector<ll> ans(m, 0); ans[0] = n * (n - 1) / 2; reverse(bridge.begin(), bridge.end()); for (int i = 0; i < m - 1; i++) { ans[i + 1] = ans[i]; ll x = bridge[i].first - 1; ll y = bridge[i].second - 1; ll xs = tree.size(x); ll ys = tree.size(y); if (tree.same(x, y) == false) { ans[i + 1] -= xs * ys; } tree.unite(x, y); } reverse(ans.begin(), ans.end()); for (int i = 0; i < m; i++) { cout << ans[i] << endl; } }
replace
44
45
44
45
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; class UnionFind { public: vector<int> par; UnionFind(int N) { par = vector<int>(N, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } int size(int x) { return -par[root(x)]; } int connect(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (size(x) < size(y)) swap(x, y); par[x] += par[y]; par[y] = x; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> a(N), b(N); for (int i = 0; i < M; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind uni(N); vector<long long> ans(M, 0); ans[M - 1] = (long long)N * (N - 1) / 2; for (int i = M - 1; i > 0; i--) { if (uni.root(a[i]) != uni.root(b[i])) { ans[i - 1] = ans[i] - (long long)uni.size(a[i]) * uni.size(b[i]); uni.connect(a[i], b[i]); } else { ans[i - 1] = ans[i]; } } for (int i = 0; i < M; i++) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; class UnionFind { public: vector<int> par; UnionFind(int N) { par = vector<int>(N, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } int size(int x) { return -par[root(x)]; } int connect(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (size(x) < size(y)) swap(x, y); par[x] += par[y]; par[y] = x; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> a(M), b(M); for (int i = 0; i < M; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind uni(N); vector<long long> ans(M, 0); ans[M - 1] = (long long)N * (N - 1) / 2; for (int i = M - 1; i > 0; i--) { if (uni.root(a[i]) != uni.root(b[i])) { ans[i - 1] = ans[i] - (long long)uni.size(a[i]) * uni.size(b[i]); uni.connect(a[i], b[i]); } else { ans[i - 1] = ans[i]; } } for (int i = 0; i < M; i++) cout << ans[i] << endl; }
replace
38
39
38
39
0
p03108
C++
Runtime Error
#include <assert.h> #include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> data; UnionFind(int size) { data.resize(size, -1); } int find(int now) { return (data[now] < 0 ? now : data[now] = find(data[now])); } int size(int now) { return -data[find(now)]; } bool same(int x, int y) { return (find(x) == find(y)); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; data[x] += data[y]; data[y] = x; return; } }; int main() { int N, M; cin >> N >> M; vector<pair<int, int>> edge(M); for (int i = 0; i < M; i++) { cin >> edge[i].first >> edge[i].second; edge[i].first--; edge[i].second--; } vector<long long> ans(M); UnionFind uf(N); for (int i = M; -1 < i; i--) if (i == M) ans[i - 1] = (long long)N * (N - 1) / 2; else { if (uf.same(edge[i].first, edge[i].second)) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - (long long)uf.size(edge[i].first) * uf.size(edge[i].second); uf.unite(edge[i].first, edge[i].second); } } for (long long t : ans) cout << t << endl; return 0; }
#include <assert.h> #include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> data; UnionFind(int size) { data.resize(size, -1); } int find(int now) { return (data[now] < 0 ? now : data[now] = find(data[now])); } int size(int now) { return -data[find(now)]; } bool same(int x, int y) { return (find(x) == find(y)); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; data[x] += data[y]; data[y] = x; return; } }; int main() { int N, M; cin >> N >> M; vector<pair<int, int>> edge(M); for (int i = 0; i < M; i++) { cin >> edge[i].first >> edge[i].second; edge[i].first--; edge[i].second--; } vector<long long> ans(M); UnionFind uf(N); for (int i = M; 0 < i; i--) if (i == M) ans[i - 1] = (long long)N * (N - 1) / 2; else { if (uf.same(edge[i].first, edge[i].second)) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - (long long)uf.size(edge[i].first) * uf.size(edge[i].second); uf.unite(edge[i].first, edge[i].second); } } for (long long t : ans) cout << t << endl; return 0; }
replace
35
36
35
36
-6
free(): invalid pointer
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) typedef long long ll; vector<ll> parent(100010, -1); ll N, M, x, y; ll find(ll x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); } void unite(ll x, ll y) { if (find(x) != find(y)) { if (parent[x] > parent[y]) swap(x, y); parent[x] += parent[y]; parent[y] = x; } } ll size(ll x) { return -parent[find(x)]; } int main() { cin >> N >> M; ll A[100010]; ll B[100010]; vector<ll> ans; REP(i, M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } ll t = (N * (N - 1)) / 2; REP(i, M) { ans.push_back(t); ll x = A[M - i - 1], y = B[M - i - 1]; if (find(x) != find(y)) { t -= size(x) * size(y); unite(x, y); } } reverse(ans.begin(), ans.end()); for (auto &it : ans) { cout << it << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) typedef long long ll; vector<ll> parent(100010, -1); ll N, M, x, y; ll find(ll x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); } void unite(ll x, ll y) { x = find(x), y = find(y); if (x != y) { if (parent[x] > parent[y]) swap(x, y); parent[x] += parent[y]; parent[y] = x; } } ll size(ll x) { return -parent[find(x)]; } int main() { cin >> N >> M; ll A[100010]; ll B[100010]; vector<ll> ans; REP(i, M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } ll t = (N * (N - 1)) / 2; REP(i, M) { ans.push_back(t); ll x = A[M - i - 1], y = B[M - i - 1]; if (find(x) != find(y)) { t -= size(x) * size(y); unite(x, y); } } reverse(ans.begin(), ans.end()); for (auto &it : ans) { cout << it << endl; } return 0; }
replace
10
11
10
12
0
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define INF 100000000 #define ll long long int using namespace std; /* vector <int> dx = {1,0,-1,0}; vector <int> dy = {0,1,0,-1}; */ struct Node { ll parent; ll size; }; vector<Node> UnionFind(100098); int find(Node a); void connect(Node a, Node b); int main() { ll N, M; cin >> N >> M; ll A[M], B[M]; for (int i = 0; i < M; i++) cin >> A[i] >> B[i]; ll ans[100008] = {}; ans[M - 1] = N * (N - 1) / 2; for (int i = 1; i <= N; i++) { UnionFind[i].parent = -i; UnionFind[i].size = 1; } for (int i = M - 2; i >= 0; i--) { int a = A[i + 1]; int b = B[i + 1]; int roota = find(UnionFind[a]); int rootb = find(UnionFind[b]); ll delta = UnionFind[roota].size * UnionFind[rootb].size; bool flag = roota == rootb; connect(UnionFind[a], UnionFind[b]); if (not flag) { ans[i] = ans[i + 1] - delta; continue; } else { ans[i] = ans[i + 1]; //- UnionFind[-UnionFind[a].parent].size * //UnionFind[-UnionFind[b].parent].size; continue; } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; } int find(Node a) { if (a.parent < 0) return -a.parent; int par = find(UnionFind[a.parent]); UnionFind[par].size += a.size; a.size = 0; a.parent = par; return par; } void connect(Node a, Node b) { int apar = find(a); int bpar = find(b); if (apar == bpar) return; UnionFind[bpar].parent = apar; UnionFind[apar].size += UnionFind[bpar].size; UnionFind[bpar].size = 0; return; }
#include <algorithm> #include <bitset> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define INF 100000000 #define ll long long int using namespace std; /* vector <int> dx = {1,0,-1,0}; vector <int> dy = {0,1,0,-1}; */ struct Node { ll parent; ll size; }; vector<Node> UnionFind(100098); int find(Node a); void connect(Node a, Node b); int main() { ll N, M; cin >> N >> M; ll A[M], B[M]; for (int i = 0; i < M; i++) cin >> A[i] >> B[i]; ll ans[100008] = {}; ans[M - 1] = N * (N - 1) / 2; for (int i = 1; i <= N; i++) { UnionFind[i].parent = -i; UnionFind[i].size = 1; } for (int i = M - 2; i >= 0; i--) { int a = A[i + 1]; int b = B[i + 1]; int roota = find(UnionFind[a]); int rootb = find(UnionFind[b]); ll delta = UnionFind[roota].size * UnionFind[rootb].size; bool flag = roota == rootb; connect(UnionFind[a], UnionFind[b]); if (not flag) { ans[i] = ans[i + 1] - delta; continue; } else { ans[i] = ans[i + 1]; //- UnionFind[-UnionFind[a].parent].size * //UnionFind[-UnionFind[b].parent].size; continue; } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; } int find(Node a) { if (a.parent < 0) return -a.parent; int par = find(UnionFind[a.parent]); UnionFind[par].size += a.size; a.size = 0; a.parent = par; return par; } void connect(Node a, Node b) { int apar = find(a); int bpar = find(b); if (apar == bpar) return; if (UnionFind[apar].size >= UnionFind[bpar].size) { UnionFind[bpar].parent = apar; UnionFind[apar].size += UnionFind[bpar].size; UnionFind[bpar].size = 0; } else { UnionFind[apar].parent = bpar; UnionFind[bpar].size += UnionFind[apar].size; UnionFind[apar].size = 0; } return; }
replace
100
103
100
112
TLE
p03108
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; class UnionFind { public: vector<long long> par; vector<long long> size; UnionFind(long long N) : par(N), size(N, 1) { for (long long i = 1; i < N; i++) { par[i] = i; } } long long root(long long x) { while (par[x] != x) { x = par[x]; } return x; } void unite(long long x, long long y) { long long rx = root(x); long long ry = root(y); if (size[ry] >= size[rx]) { size[rx] += size[ry]; par[ry] = rx; } else { size[ry] += size[rx]; par[rx] = ry; } } bool same(long long x, long long y) { long long rx = root(x); long long ry = root(y); return rx == ry; } long long siz(long long x) { return size[root(x)]; } }; int main() { long long N, M; cin >> N >> M; vector<vector<long long>> stock(M, vector<long long>(2, 0)); vector<long long> ans(M, 0); for (long long i = 0; i < M; i++) { long long a, b; cin >> a >> b; stock[i][0] = a - 1; stock[i][1] = b - 1; } UnionFind uf(N); ans[M - 1] = N * (N - 1) / 2; for (long long i = M - 1; i > 0; i--) { long long a = stock[i][0]; long long b = stock[i][1]; if (uf.same(a, b)) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - uf.siz(a) * uf.siz(b); uf.unite(a, b); } } for (auto a : ans) { cout << a << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; class UnionFind { public: vector<long long> par; vector<long long> size; UnionFind(long long N) : par(N), size(N, 1) { for (long long i = 1; i < N; i++) { par[i] = i; } } long long root(long long x) { while (par[x] != x) { x = par[x]; } return x; } void unite(long long x, long long y) { long long rx = root(x); long long ry = root(y); if (size[ry] <= size[rx]) { size[rx] += size[ry]; par[ry] = rx; } else { size[ry] += size[rx]; par[rx] = ry; } } bool same(long long x, long long y) { long long rx = root(x); long long ry = root(y); return rx == ry; } long long siz(long long x) { return size[root(x)]; } }; int main() { long long N, M; cin >> N >> M; vector<vector<long long>> stock(M, vector<long long>(2, 0)); vector<long long> ans(M, 0); for (long long i = 0; i < M; i++) { long long a, b; cin >> a >> b; stock[i][0] = a - 1; stock[i][1] = b - 1; } UnionFind uf(N); ans[M - 1] = N * (N - 1) / 2; for (long long i = M - 1; i > 0; i--) { long long a = stock[i][0]; long long b = stock[i][1]; if (uf.same(a, b)) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - uf.siz(a) * uf.siz(b); uf.unite(a, b); } } for (auto a : ans) { cout << a << endl; } return 0; }
replace
25
26
25
26
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, n) for (auto i = 0; i < n; i++) #define REPR(i, n) for (auto i = n - 1; i >= 0; i--) typedef pair<ll, ll> LP; const ll mod = 1e9 + 7; struct UnionFind { vector<int> data; // dataの各要素について // 負の値:その集合のルートであること示す。また、その絶対値は集合の要素数となっている。 // 正の値:親ノードの番号(dataのインデックス)。root()を呼び出すたびに集合のルートを指すように書きなおされるので木はそんなに深くならない // 初期化 size:最大要素数 UnionFind(int size) : data(size, -1) {} // 集合を併合する // すでに同じ集合だった場合は、falseが返る bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { // 要素数の大きな方へ合併するためのswap if (data[y] < data[x]) swap(x, y); // 要素数を加算する data[x] += data[y]; // yの属する集合のルートをxに変更 data[y] = x; } return x != y; } // 同じ集合かどうか判定 bool same(int x, int y) { return root(x) == root(y); } // 集合の識別番号を返す // 負の値を持つものがその集合のルート // 正の値は同じ集合に属するものを指す(辿ればいずれルートへ着く) int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); } // 集合の要素数を返す int size(int x) { return -data[root(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<LP> AB(m); REP(i, m) { cin >> AB[i].first >> AB[i].second; } UnionFind tree(n); ll ans[m]; ll sum = n * (n - 1) / 2; REPR(i, m) { ans[i] = sum; if (!tree.same(AB[i].first, AB[i].second)) { sum -= tree.size(AB[i].first) * tree.size(AB[i].second); tree.unite(AB[i].first, AB[i].second); } } REP(i, m) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, n) for (auto i = 0; i < n; i++) #define REPR(i, n) for (auto i = n - 1; i >= 0; i--) typedef pair<ll, ll> LP; const ll mod = 1e9 + 7; struct UnionFind { vector<int> data; // dataの各要素について // 負の値:その集合のルートであること示す。また、その絶対値は集合の要素数となっている。 // 正の値:親ノードの番号(dataのインデックス)。root()を呼び出すたびに集合のルートを指すように書きなおされるので木はそんなに深くならない // 初期化 size:最大要素数 UnionFind(int size) : data(size, -1) {} // 集合を併合する // すでに同じ集合だった場合は、falseが返る bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { // 要素数の大きな方へ合併するためのswap if (data[y] < data[x]) swap(x, y); // 要素数を加算する data[x] += data[y]; // yの属する集合のルートをxに変更 data[y] = x; } return x != y; } // 同じ集合かどうか判定 bool same(int x, int y) { return root(x) == root(y); } // 集合の識別番号を返す // 負の値を持つものがその集合のルート // 正の値は同じ集合に属するものを指す(辿ればいずれルートへ着く) int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); } // 集合の要素数を返す int size(int x) { return -data[root(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<LP> AB(m); REP(i, m) { cin >> AB[i].first >> AB[i].second; } UnionFind tree(n); ll ans[m]; ll sum = n * (n - 1) / 2; REPR(i, m) { ans[i] = sum; if (!tree.same(AB[i].first - 1, AB[i].second - 1)) { sum -= tree.size(AB[i].first - 1) * tree.size(AB[i].second - 1); tree.unite(AB[i].first - 1, AB[i].second - 1); } } REP(i, m) { cout << ans[i] << endl; } }
replace
58
61
58
61
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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; } #define all(x) (x).begin(), (x).end() #define pll pair<ll, ll> #define rep(i, n) for (int i = 0; i < n; i++) #define sz(x) ((ll)(x).size()) #define pb push_back #define mp make_pair #define bit(n) (1LL << (n)) #define F first #define S second int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll INF = 1LL << 60; const ll mod = (int)1e9 + 7; class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } void init(ll sz_) { siz.assign(sz_, 1LL); // assign: 再代入 par.resize(sz_); // resize: 再確保 for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; int main() { // ll N; cin >> N; ll N, M; cin >> N >> M; // string S; cin >> S; // ll H,W; cin >> H >> W; vector<pll> pe(M); rep(i, M) { int a, b; cin >> a >> b; pe[i] = mp(a, b); } vector<ll> ans(M); ans[M - 1] = N * (N - 1) / 2; UnionFind tree(N); for (int i = M - 1; i > 0; i--) { int a, b; a = pe[i].F; b = pe[i].S; if (tree.issame(a, b)) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - tree.size(a) * tree.size(b); tree.merge(a, b); } } rep(i, M) { cout << ans[i] << endl; } } /* */
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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; } #define all(x) (x).begin(), (x).end() #define pll pair<ll, ll> #define rep(i, n) for (int i = 0; i < n; i++) #define sz(x) ((ll)(x).size()) #define pb push_back #define mp make_pair #define bit(n) (1LL << (n)) #define F first #define S second int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll INF = 1LL << 60; const ll mod = (int)1e9 + 7; class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } void init(ll sz_) { siz.assign(sz_, 1LL); // assign: 再代入 par.resize(sz_); // resize: 再確保 for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; int main() { // ll N; cin >> N; ll N, M; cin >> N >> M; // string S; cin >> S; // ll H,W; cin >> H >> W; vector<pll> pe(M); rep(i, M) { int a, b; cin >> a >> b; pe[i] = mp(a - 1, b - 1); } vector<ll> ans(M); ans[M - 1] = N * (N - 1) / 2; UnionFind tree(N); for (int i = M - 1; i > 0; i--) { int a, b; a = pe[i].F; b = pe[i].S; if (tree.issame(a, b)) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - tree.size(a) * tree.size(b); tree.merge(a, b); } } rep(i, M) { cout << ans[i] << endl; } } /* */
replace
90
91
90
91
0
p03108
C++
Time Limit Exceeded
// #pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #define String std::string #define fir first #define sec second #define mp std::make_pair #define Pair std::pair<int, int> #define Map std::map<int, int> #define Vector std::vector<int> typedef long long ll; typedef unsigned long long ull; const int N = 100000 + 5; const int M = 3000 + 5; const int MOD = 1e9 + 7; const int inv2 = 500000004; const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1}; template <class T> inline T readT() { T res = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) res = (res << 1) + (res << 3) + ch - '0'; return res * f; } #define read readT<int> int n, m; Pair a[N]; int fa[N], size[N]; ll ans[N]; inline int find(int x) { return x == fa[x] ? fa[x] : find(fa[x]); } inline ll merge(int x, int y) { int fx = find(x), fy = find(y); if (fx == fy) return 0; ll res = (ll)size[fx] * size[fy]; fa[fx] = fy; size[fy] += size[fx]; return res; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) { fa[i] = i; size[i] = 1; } for (int i = 1; i <= m; i++) { a[i].fir = read(); a[i].sec = read(); } for (int i = m; i >= 1; i--) { // printf("%lld\n", merge(a[i].fir, a[i].sec)); ans[i] = merge(a[i].fir, a[i].sec); } for (int i = 1; i <= m; i++) { ans[i] += ans[i - 1]; printf("%lld\n", ans[i]); } return 0; }
// #pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #define String std::string #define fir first #define sec second #define mp std::make_pair #define Pair std::pair<int, int> #define Map std::map<int, int> #define Vector std::vector<int> typedef long long ll; typedef unsigned long long ull; const int N = 100000 + 5; const int M = 3000 + 5; const int MOD = 1e9 + 7; const int inv2 = 500000004; const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1}; const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1}; template <class T> inline T readT() { T res = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) res = (res << 1) + (res << 3) + ch - '0'; return res * f; } #define read readT<int> int n, m; Pair a[N]; int fa[N], size[N]; ll ans[N]; inline int find(int x) { return x == fa[x] ? fa[x] : fa[x] = find(fa[x]); } inline ll merge(int x, int y) { int fx = find(x), fy = find(y); if (fx == fy) return 0; ll res = (ll)size[fx] * size[fy]; fa[fx] = fy; size[fy] += size[fx]; return res; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) { fa[i] = i; size[i] = 1; } for (int i = 1; i <= m; i++) { a[i].fir = read(); a[i].sec = read(); } for (int i = m; i >= 1; i--) { // printf("%lld\n", merge(a[i].fir, a[i].sec)); ans[i] = merge(a[i].fir, a[i].sec); } for (int i = 1; i <= m; i++) { ans[i] += ans[i - 1]; printf("%lld\n", ans[i]); } return 0; }
replace
37
38
37
38
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rp(i, k, n) for (int i = k; i < n; i++) typedef long long ll; typedef double ld; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll INF = 1ll << 60; const ll MOD = 1e9 + 7ll; const double PI = 3.14159265358979323846; // union by size + path having class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<pair<int, int>> path; rp(i, 0, m) { int a, b; cin >> a >> b; path.emplace_back(a, b); } UnionFind UF(n); vector<ll> res(m + 10); res[m - 1] = n * (n - 1) / 2ll; for (int i = m - 1; i >= 1; i--) { int a = path[i].first; int b = path[i].second; // aとbのグループのサイズをもらう ll a_size = UF.size(a); ll b_size = UF.size(b); // aとbをくっつける if (UF.merge(a, b)) { // res更新 ll ab_size = a_size + b_size; res[i - 1] = res[i] - ab_size * (ab_size - 1) / 2ll + a_size * (a_size - 1) / 2ll + b_size * (b_size - 1) / 2ll; } else res[i - 1] = res[i]; // printf("a = %d b = %d a_size = %lld b_size = %lld res = %lld\n", a, b, // a_size, b_size, res[i-1]); } rp(i, 0, m) { cout << res[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rp(i, k, n) for (int i = k; i < n; i++) typedef long long ll; typedef double ld; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll INF = 1ll << 60; const ll MOD = 1e9 + 7ll; const double PI = 3.14159265358979323846; // union by size + path having class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; int main() { ll n, m; cin >> n >> m; vector<pair<int, int>> path; rp(i, 0, m) { int a, b; cin >> a >> b; path.emplace_back(a, b); } UnionFind UF(n + 10); vector<ll> res(m + 10); res[m - 1] = n * (n - 1) / 2ll; for (int i = m - 1; i >= 1; i--) { int a = path[i].first; int b = path[i].second; // aとbのグループのサイズをもらう ll a_size = UF.size(a); ll b_size = UF.size(b); // aとbをくっつける if (UF.merge(a, b)) { // res更新 ll ab_size = a_size + b_size; res[i - 1] = res[i] - ab_size * (ab_size - 1) / 2ll + a_size * (a_size - 1) / 2ll + b_size * (b_size - 1) / 2ll; } else res[i - 1] = res[i]; // printf("a = %d b = %d a_size = %lld b_size = %lld res = %lld\n", a, b, // a_size, b_size, res[i-1]); } rp(i, 0, m) { cout << res[i] << endl; } return 0; }
replace
85
86
85
86
0
p03108
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <tuple> #include <utility> #include <vector> #define DEBUG(x) cout << #x << ": " << x << endl using namespace std; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) return A; else return Parent[A] = root(Parent[A]); } int size(int A) { return -Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) return false; if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = Parent[A]; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; UnionFind Uni(N); for (int i = M - 1; i >= 1; i--) { ans[i - 1] = ans[i]; if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]); Uni.connect(A[i], B[i]); } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <tuple> #include <utility> #include <vector> #define DEBUG(x) cout << #x << ": " << x << endl using namespace std; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) return A; else return Parent[A] = root(Parent[A]); } int size(int A) { return -Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) return false; if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; UnionFind Uni(N); for (int i = M - 1; i >= 1; i--) { ans[i - 1] = ans[i]; if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]); Uni.connect(A[i], B[i]); } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; }
replace
49
50
49
50
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define EPS (1e-10) using namespace std; typedef long long ll; typedef pair<int, int> P; class UnionFind { vector<int> par, sz; public: UnionFind() {} UnionFind(int n) { par = sz = vector<int>(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] > sz[y]) { par[y] = x; sz[x] += sz[y]; } else { par[x] = y; sz[y] += sz[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; int a[200000], b[200000]; int main() { int n, m; cin >> n >> m; rep(i, m) { scanf("%d%d", &a[i], &b[i]); } UnionFind uf(n); ll ans = n * (ll)(n - 1) / 2; vector<ll> v; for (int i = m - 1; i >= 0; i--) { v.push_back(ans); if (uf.same(a[i], b[i])) continue; ans -= uf.size(a[i]) * (ll)uf.size(b[i]); uf.unite(a[i], b[i]); } reverse(v.begin(), v.end()); for (ll i : v) { printf("%lld\n", i); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define EPS (1e-10) using namespace std; typedef long long ll; typedef pair<int, int> P; class UnionFind { vector<int> par, sz; public: UnionFind() {} UnionFind(int n) { par = sz = vector<int>(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] > sz[y]) { par[y] = x; sz[x] += sz[y]; } else { par[x] = y; sz[y] += sz[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; int a[200000], b[200000]; int main() { int n, m; cin >> n >> m; rep(i, m) { scanf("%d%d", &a[i], &b[i]); a[i]--; b[i]--; } UnionFind uf(n); ll ans = n * (ll)(n - 1) / 2; vector<ll> v; for (int i = m - 1; i >= 0; i--) { v.push_back(ans); if (uf.same(a[i], b[i])) continue; ans -= uf.size(a[i]) * (ll)uf.size(b[i]); uf.unite(a[i], b[i]); } reverse(v.begin(), v.end()); for (ll i : v) { printf("%lld\n", i); } }
replace
47
48
47
52
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0; i < (n); i++) #define REP1(i, n) for (int i = 1; i < (n); i++) #define REP2(i, d, n) for (int i = (d); i < (n); i++) #define RREP(i, n) for (int i = (n); i >= 0; i--) #define CLR(a) memset((a), 0, sizeof(a)) #define MCLR(a) memset((a), -1, sizeof(a)) #define RANGE(x, y, maxX, maxY) \ (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY)) typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<LL> VLL; typedef pair<int, int> PII; const int INF = 0x3f3f3f3f; const LL INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1}; struct UnionFind { vector<LL> data; UnionFind(LL size) : data(size, -1) {} bool unionSet(LL x, LL y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(LL x, LL y) { return root(x) == root(y); } LL root(LL x) { return data[x] < 0 ? x : data[x] = root(data[x]); } LL size(LL x) { return -data[root(x)]; } }; void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B) { LL ans = 1; REP1(i, N) ans += i; vector<LL> result; UnionFind uf(N); RREP(i, A.size() - 1) { result.PB(ans); if (!uf.findSet(A[i], B[i])) { LL a_size = uf.size(A[i]); LL b_size = uf.size(B[i]); uf.unionSet(A[i], B[i]); ans -= a_size * b_size; } } RREP(i, result.size() - 1) { cout << result[i] - 1 << endl; } } 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]); } solve(N, M, std::move(A), std::move(B)); return 0; }
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0; i < (n); i++) #define REP1(i, n) for (int i = 1; i < (n); i++) #define REP2(i, d, n) for (int i = (d); i < (n); i++) #define RREP(i, n) for (int i = (n); i >= 0; i--) #define CLR(a) memset((a), 0, sizeof(a)) #define MCLR(a) memset((a), -1, sizeof(a)) #define RANGE(x, y, maxX, maxY) \ (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY)) typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<LL> VLL; typedef pair<int, int> PII; const int INF = 0x3f3f3f3f; const LL INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1}; struct UnionFind { vector<LL> data; UnionFind(LL size) : data(size, -1) {} bool unionSet(LL x, LL y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(LL x, LL y) { return root(x) == root(y); } LL root(LL x) { return data[x] < 0 ? x : data[x] = root(data[x]); } LL size(LL x) { return -data[root(x)]; } }; void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B) { LL ans = 1; REP1(i, N) ans += i; vector<LL> result; REP(i, M) { A[i]--; B[i]--; } UnionFind uf(N); RREP(i, A.size() - 1) { result.PB(ans); if (!uf.findSet(A[i], B[i])) { LL a_size = uf.size(A[i]); LL b_size = uf.size(B[i]); uf.unionSet(A[i], B[i]); ans -= a_size * b_size; } } RREP(i, result.size() - 1) { cout << result[i] - 1 << endl; } } 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]); } solve(N, M, std::move(A), std::move(B)); return 0; }
insert
53
53
53
57
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll par[100005], sum[100005], bridg[100005][2], ans[100005]; ll find(ll x) { return par[x] == x ? x : find(par[x]); } int main() { ll n, m; cin >> n >> m; for (int i = 0; i < n; i++) { par[i] = i; sum[i] = 1; } for (int i = 0; i < m; i++) { scanf("%lld%lld", &bridg[i][0], &bridg[i][1]); } ans[m - 1] = n * (n - 1) / 2; for (int i = m - 1; i > 0; i--) { ll x, y; x = find(bridg[i][0]); y = find(bridg[i][1]); if (x != y) { ans[i - 1] = ans[i] - sum[x] * sum[y]; sum[y] += sum[x]; par[x] = y; } else ans[i - 1] = ans[i]; } for (int i = 0; i < m; i++) printf("%lld\n", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll par[100005], sum[100005], bridg[100005][2], ans[100005]; ll find(ll x) { return par[x] == x ? x : par[x] = find(par[x]); } int main() { ll n, m; cin >> n >> m; for (int i = 0; i < n; i++) { par[i] = i; sum[i] = 1; } for (int i = 0; i < m; i++) { scanf("%lld%lld", &bridg[i][0], &bridg[i][1]); } ans[m - 1] = n * (n - 1) / 2; for (int i = m - 1; i > 0; i--) { ll x, y; x = find(bridg[i][0]); y = find(bridg[i][1]); if (x != y) { ans[i - 1] = ans[i] - sum[x] * sum[y]; sum[y] += sum[x]; par[x] = y; } else ans[i - 1] = ans[i]; } for (int i = 0; i < m; i++) printf("%lld\n", ans[i]); return 0; }
replace
4
5
4
5
TLE
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define f first #define s second #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0) ll p[100002]; ll find(ll x) { return p[x] == x ? x : find(p[x]); } int main() { IOS; ll n, m, i; cin >> n >> m; ll link[m][2], sum[n], ans[m]; for (i = 0; i < n; i++) { p[i] = i; sum[i] = 1; } for (i = 0; i < m; i++) { cin >> link[i][0] >> link[i][1]; } ans[m - 1] = (n * (n - 1) / 2); for (i = m - 1; i >= 0; i--) { ll q, w; q = find(link[i][0]); w = find(link[i][1]); if (q != w) { ans[i - 1] = ans[i] - (sum[q] * sum[w]); sum[w] += sum[q]; p[q] = w; } else ans[i - 1] = ans[i]; } for (auto g : ans) cout << g << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define f first #define s second #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0) ll p[100002]; ll find(ll x) { return p[x] == x ? x : p[x] = find(p[x]); // p[x]=find(p[x]) necessary else TLE as // it takes longer time to find parent } int main() { IOS; ll n, m, i; cin >> n >> m; ll link[m][2], sum[n], ans[m]; for (i = 0; i < n; i++) { p[i] = i; sum[i] = 1; } for (i = 0; i < m; i++) { cin >> link[i][0] >> link[i][1]; } ans[m - 1] = (n * (n - 1) / 2); for (i = m - 1; i >= 0; i--) { ll q, w; q = find(link[i][0]); w = find(link[i][1]); if (q != w) { ans[i - 1] = ans[i] - (sum[q] * sum[w]); sum[w] += sum[q]; p[q] = w; } else ans[i - 1] = ans[i]; } for (auto g : ans) cout << g << endl; return 0; }
replace
11
12
11
16
TLE
p03108
C++
Runtime Error
// だーれだ? // qqqqqqHHMqqqqqHf!(:;jHmmkf``(:;jkqqqqqqqqqmMH#HMqqK_````````````````(kqqqqqqHMMM // qqqqqqHHmqqqqH=` :;;jkqH=?~..;;jqqqqqqqqqmM#HM#MHH!```` // ```````..-``dqWbWkRqHMMM qqqqqqMHmqqqK!``.;;;dqH: // ```.?TUqqHmqqqqmHHHM4Mqkt..```` ..JgHHW@P`.HHkqHHHqM#NN // qqqqqmHMmqHTYWHgHmmgHR...``` :;Jkq9qqqqqgMH#1dHk$...-gW@HY"=!``dH! // dHNmqqqqmM### qqqqqqgMmH[````` // ?dHMMMHHHHHJ<;JkK>XqqqmMH#<+Hb%.WYYTHppf-````.gt`.HHMmmqqqmM### // qqkqqqm@H?S,```` Jpbbpp:```` // :!dK<;JkqqMMY(;dW^````.ppbpf!```.Y<`.HHHMgmqqqmH### qqqqqqqqD`` // ````(ppbppW````` ~.f`:;jkqM#^.;jf!````.Wppbpf````````(HH#HmmqqmH#NN# // qqqqqqqkP``````.fpbpbpf``` ` `.``~:;WHY``(+=``````Jppbpf\```` // ``.MH#HHgqqqmHNNNN qqqqqqqk]```` `,fpbbpf>```` `` ``_:<? ``.?!``` `` // Wpppp%``` ``` dHH##HmmqqmH#### qqqqqqqk]``````,fpbppt``` `` `` `_~````` `` `` // ```WppW=``` ````.HHHHMHmmqqmM#### qqqqqqqk$`` ````4ppfY```` `` `` `- `` `` `` // ``` ``(T=`````` ``.MH##HMMmmqqmM##HH qqqqqqHqP``` ````?7!```` `` `` ``` `` ` // ` `` ````````` ` ````.MHH#HgMmqmqmHHHHH qqqkqqmkR```` ` `` ` `` ` ` ` // ``` ` ``` ` ......_. ```dH#HHgMqqgqqmH#HH NqqqqqHqR``` _ ........_` `` ``` // ``` ````` ``` ............```,HHHHmqqqHqqmMH## MmqqqqHqR``` _........_ `` `` // ``` `` ` `` ` ` ` `__ ` ``,HHHMmmqqMHmmMHH# HMqqqqXkH `````````````` ` // `` ` `` `` `` `` `` ```` ````` ````.HHHMmMHm@HHqmMHH HHHmqqfHk;``` ` ` `` ` // ` `` ``` ` ` ``` `` `` `` ```` `..```.HHHHMMHMg@HHHHHMH HHMmmqMHkP<.`` ``` ` // ` ``` `` ``` ``` ` `` `` `` ` ```` ```.MH##HM#HMg@@@@H@HH // ##HNmqR`jW<<_ ``` ````` `` ` ` ``` `` `` `` `` ```` ````` // .dHH##H#HHHMHH@@H@@@@ HH##NqH..X2:<<-.`` ` `` ``` ``` ` `` `` `` ` `` ` ``` ` // .gMHHH#H#HHH#HM@@H@@H@@H -WHHHNH[ =v._:::<-. ` `` ` `` ```````.`` ````````` //.MHH###H#MHHHHH@H@H@@HH@ ` 7MHHMH.l==. ~:::::<_-.. ````` ``````` `` ...uX; //`,MHHHH#M@@MM@@@@@@H@@@@ // . TMHHbZl=1. _~::::::(<::<:<___-___:<<:::::jX3=. ` ` // `,MMHM@@@@Mg@@@@@@@@@H@ // #include <bits/stdc++.h> #define int long long #define ll long long #define rep(i, a, b) for (signed i = a; i < (b); ++i) #define erep(i, a, b) for (signed i = a; i <= (b); ++i) #define per(i, a, b) for (signed i = (a); i > (b); --i) #define eper(i, a, b) for (signed i = (a); i >= b; --i) #define fore(i, x, a) for (auto &&x : a) #define ITR(i, b, e) for (auto i = (b); i != (e); ++i) #define pb push_back #define mp make_pair #define ALL(x) begin(x), end(x) #define F first #define S second #define debug(x) cout << #x << ": " << (x) << '\n'; const long long INF = 1001001001001001001; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; using namespace std; using Pii = pair<int, int>; using vii = vector<int>; template <class T> using PS_queue = priority_queue<T, vector<T>, greater<T>>; template <class T> using vv = vector<T>; 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 (b < a) { a = b; return 1; } return 0; } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { ITR(i, begin(v), end(v)) is >> *i; return is; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct edge { int from, to, cost; }; int dy[] = {0, 1, -1, 0}; int dx[] = {1, 0, 0, -1}; // cout << fixed; // cout << setprecision(10) << val; template <class Abel> struct UnionFind { vector<int> par; vector<int> rank; vector<Abel> diff_weight; UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(int n = 1, Abel SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 1, diff_weight[i] = SUM_UNITY; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } Abel weight(int x) { root(x); return diff_weight[x]; } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return rank[root(x)]; } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); rank[x] += rank[y]; par[y] = x; return true; } bool merge(int x, int y, Abel w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } Abel diff(int x, int y) { return weight(y) - weight(x); } }; int n, m; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vii a(m), b(m); rep(i, 0, m) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } UnionFind<int> uf(n); vii kotae(m, 0); kotae[m] = n * (n - 1) / 2; eper(i, m - 1, 0) { int s = uf.size(a[i]); int t = uf.size(b[i]); kotae[i] = kotae[i + 1]; if (uf.issame(a[i], b[i])) continue; kotae[i] -= s * t; uf.unite(a[i], b[i]); } erep(i, 1, m) cout << kotae[i] << endl; return 0; }
// だーれだ? // qqqqqqHHMqqqqqHf!(:;jHmmkf``(:;jkqqqqqqqqqmMH#HMqqK_````````````````(kqqqqqqHMMM // qqqqqqHHmqqqqH=` :;;jkqH=?~..;;jqqqqqqqqqmM#HM#MHH!```` // ```````..-``dqWbWkRqHMMM qqqqqqMHmqqqK!``.;;;dqH: // ```.?TUqqHmqqqqmHHHM4Mqkt..```` ..JgHHW@P`.HHkqHHHqM#NN // qqqqqmHMmqHTYWHgHmmgHR...``` :;Jkq9qqqqqgMH#1dHk$...-gW@HY"=!``dH! // dHNmqqqqmM### qqqqqqgMmH[````` // ?dHMMMHHHHHJ<;JkK>XqqqmMH#<+Hb%.WYYTHppf-````.gt`.HHMmmqqqmM### // qqkqqqm@H?S,```` Jpbbpp:```` // :!dK<;JkqqMMY(;dW^````.ppbpf!```.Y<`.HHHMgmqqqmH### qqqqqqqqD`` // ````(ppbppW````` ~.f`:;jkqM#^.;jf!````.Wppbpf````````(HH#HmmqqmH#NN# // qqqqqqqkP``````.fpbpbpf``` ` `.``~:;WHY``(+=``````Jppbpf\```` // ``.MH#HHgqqqmHNNNN qqqqqqqk]```` `,fpbbpf>```` `` ``_:<? ``.?!``` `` // Wpppp%``` ``` dHH##HmmqqmH#### qqqqqqqk]``````,fpbppt``` `` `` `_~````` `` `` // ```WppW=``` ````.HHHHMHmmqqmM#### qqqqqqqk$`` ````4ppfY```` `` `` `- `` `` `` // ``` ``(T=`````` ``.MH##HMMmmqqmM##HH qqqqqqHqP``` ````?7!```` `` `` ``` `` ` // ` `` ````````` ` ````.MHH#HgMmqmqmHHHHH qqqkqqmkR```` ` `` ` `` ` ` ` // ``` ` ``` ` ......_. ```dH#HHgMqqgqqmH#HH NqqqqqHqR``` _ ........_` `` ``` // ``` ````` ``` ............```,HHHHmqqqHqqmMH## MmqqqqHqR``` _........_ `` `` // ``` `` ` `` ` ` ` `__ ` ``,HHHMmmqqMHmmMHH# HMqqqqXkH `````````````` ` // `` ` `` `` `` `` `` ```` ````` ````.HHHMmMHm@HHqmMHH HHHmqqfHk;``` ` ` `` ` // ` `` ``` ` ` ``` `` `` `` ```` `..```.HHHHMMHMg@HHHHHMH HHMmmqMHkP<.`` ``` ` // ` ``` `` ``` ``` ` `` `` `` ` ```` ```.MH##HM#HMg@@@@H@HH // ##HNmqR`jW<<_ ``` ````` `` ` ` ``` `` `` `` `` ```` ````` // .dHH##H#HHHMHH@@H@@@@ HH##NqH..X2:<<-.`` ` `` ``` ``` ` `` `` `` ` `` ` ``` ` // .gMHHH#H#HHH#HM@@H@@H@@H -WHHHNH[ =v._:::<-. ` `` ` `` ```````.`` ````````` //.MHH###H#MHHHHH@H@H@@HH@ ` 7MHHMH.l==. ~:::::<_-.. ````` ``````` `` ...uX; //`,MHHHH#M@@MM@@@@@@H@@@@ // . TMHHbZl=1. _~::::::(<::<:<___-___:<<:::::jX3=. ` ` // `,MMHM@@@@Mg@@@@@@@@@H@ // #include <bits/stdc++.h> #define int long long #define ll long long #define rep(i, a, b) for (signed i = a; i < (b); ++i) #define erep(i, a, b) for (signed i = a; i <= (b); ++i) #define per(i, a, b) for (signed i = (a); i > (b); --i) #define eper(i, a, b) for (signed i = (a); i >= b; --i) #define fore(i, x, a) for (auto &&x : a) #define ITR(i, b, e) for (auto i = (b); i != (e); ++i) #define pb push_back #define mp make_pair #define ALL(x) begin(x), end(x) #define F first #define S second #define debug(x) cout << #x << ": " << (x) << '\n'; const long long INF = 1001001001001001001; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; using namespace std; using Pii = pair<int, int>; using vii = vector<int>; template <class T> using PS_queue = priority_queue<T, vector<T>, greater<T>>; template <class T> using vv = vector<T>; 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 (b < a) { a = b; return 1; } return 0; } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { ITR(i, begin(v), end(v)) is >> *i; return is; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct edge { int from, to, cost; }; int dy[] = {0, 1, -1, 0}; int dx[] = {1, 0, 0, -1}; // cout << fixed; // cout << setprecision(10) << val; template <class Abel> struct UnionFind { vector<int> par; vector<int> rank; vector<Abel> diff_weight; UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(int n = 1, Abel SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 1, diff_weight[i] = SUM_UNITY; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } Abel weight(int x) { root(x); return diff_weight[x]; } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return rank[root(x)]; } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); rank[x] += rank[y]; par[y] = x; return true; } bool merge(int x, int y, Abel w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } Abel diff(int x, int y) { return weight(y) - weight(x); } }; int n, m; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; vii a(m), b(m); rep(i, 0, m) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } UnionFind<int> uf(n); vii kotae(m + 1, 0); kotae[m] = n * (n - 1) / 2; eper(i, m - 1, 0) { int s = uf.size(a[i]); int t = uf.size(b[i]); kotae[i] = kotae[i + 1]; if (uf.issame(a[i], b[i])) continue; kotae[i] -= s * t; uf.unite(a[i], b[i]); } erep(i, 1, m) cout << kotae[i] << endl; return 0; }
replace
169
170
169
170
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long using namespace std; const int _M = 1e5 + 40; ll a[_M], t[_M], ans[_M], k = 0; struct { int x, y; } e[_M]; int fd(int x) { return x == a[x] ? x : fd(a[x]); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) a[i] = i, t[i] = 1; for (int i = 1; i <= m; i++) scanf("%d%d", &e[i].x, &e[i].y); ll an = (ll)n * (ll)(n - 1) / 2; ans[k++] = an; // cout<<an<<endl; for (int i = m; i >= 1; i--) { int ra = fd(e[i].x), rb = fd(e[i].y); if (ra != rb) { an -= ((ll)t[ra] * (ll)t[rb]); t[ra] += t[rb]; a[rb] = ra; } ans[k++] = an; } k -= 2; while (k >= 0) cout << ans[k--] << endl; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int _M = 1e5 + 40; ll a[_M], t[_M], ans[_M], k = 0; struct { int x, y; } e[_M]; int fd(int x) { return x == a[x] ? x : a[x] = fd(a[x]); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) a[i] = i, t[i] = 1; for (int i = 1; i <= m; i++) scanf("%d%d", &e[i].x, &e[i].y); ll an = (ll)n * (ll)(n - 1) / 2; ans[k++] = an; // cout<<an<<endl; for (int i = m; i >= 1; i--) { int ra = fd(e[i].x), rb = fd(e[i].y); if (ra != rb) { an -= ((ll)t[ra] * (ll)t[rb]); t[ra] += t[rb]; a[rb] = ra; } ans[k++] = an; } k -= 2; while (k >= 0) cout << ans[k--] << endl; return 0; }
replace
13
14
13
14
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(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 (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; int main(int argc, char const *argv[]) { int n, m; cin >> n >> m; pair<int, int> ab[m]; for (int i = 0; i < m; ++i) { cin >> ab[i].first >> ab[i].second; } reverse(ab, ab + m); long long now = (long long)n * (n - 1) / 2LL, ans[m]; UnionFind uf(n); for (int i = 0; i < m; ++i) { ans[i] = now; if (uf.issame(ab[i].first, ab[i].second)) { continue; } now -= (long long)uf.size(ab[i].first) * uf.size(ab[i].second); uf.merge(ab[i].first, ab[i].second); } reverse(ans, ans + m); for (auto v : ans) { cout << v << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(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 (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; int main(int argc, char const *argv[]) { int n, m; cin >> n >> m; pair<int, int> ab[m]; for (int i = 0; i < m; ++i) { cin >> ab[i].first >> ab[i].second; --ab[i].first; --ab[i].second; } reverse(ab, ab + m); long long now = (long long)n * (n - 1) / 2LL, ans[m]; UnionFind uf(n); for (int i = 0; i < m; ++i) { ans[i] = now; if (uf.issame(ab[i].first, ab[i].second)) { continue; } now -= (long long)uf.size(ab[i].first) * uf.size(ab[i].second); uf.merge(ab[i].first, ab[i].second); } reverse(ans, ans + m); for (auto v : ans) { cout << v << endl; } return 0; }
insert
39
39
39
41
0
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <string> #include <tuple> #include <vector> #define rep(i, n) for (ll i = 0; i < n; i++) #define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup. using namespace std; typedef long long ll; typedef unsigned long long ull; using Graph = vector<vector<ll>>; // std::cout<<std::fixed<<std::setprecision(10); ll par[100000]; // par[i]... iの親 ll mem[100000]; // iの(自分も含めた) 子の構成島数 ll root(ll x) { // 自身が親と同一の時まで再帰的にrootをたどる if (par[x] == x) return x; else { return root(par[x]); } } void unite(ll x, ll y) { ll rx = root(x); ll ry = root(y); if (rx == ry) return; else { mem[ry] += mem[rx]; par[rx] = ry; return; } } int main() { ull N, M; cin >> N >> M; ull A[M], B[M]; rep(i, M) { ull a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } /* Uion-find というデータ構造を使えば処理時間を短縮できるらしい */ // cf. https://qiita.com/ofutonfuton/items/c17dfd33fc542c222396 // cf. https://atc001.contest.atcoder.jp/tasks/unionfind_a rep(i, N) par[i] = i; // 最初は全て自分が親 rep(i, N) mem[i] = 1; //////////////////// ull ans[M]; // ans ans[M - 1] = N * (N - 1) / 2; // 全ての橋が崩壊した時の不便さ for (ll i = M - 1; i > 0; i--) { ll ra = root(A[i]); ll rb = root(B[i]); if (ra == rb) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - mem[ra] * mem[rb]; // cout << "log:" << ans[i-1] << endl; // cout << "A, B " << A[i] << ' ' << B[i] << endl; // cout << "rA, rB " << root(A[i]) << ' ' << root(B[i]) << endl; // cout << "mrA, mrB " << mem[root(A[i])] << ' ' << mem[root(B[i])] << // endl; unite(rb, ra); } } rep(i, M) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <string> #include <tuple> #include <vector> #define rep(i, n) for (ll i = 0; i < n; i++) #define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup. using namespace std; typedef long long ll; typedef unsigned long long ull; using Graph = vector<vector<ll>>; // std::cout<<std::fixed<<std::setprecision(10); ll par[100000]; // par[i]... iの親 ll mem[100000]; // iの(自分も含めた) 子の構成島数 ll root(ll x) { // 自身が親と同一の時まで再帰的にrootをたどる if (par[x] == x) return x; else { return root(par[x]); } } void unite(ll x, ll y) { ll rx = root(x); ll ry = root(y); if (rx == ry) return; else { if (rx > ry) { ll tmp = rx; rx = ry; ry = tmp; } mem[ry] += mem[rx]; par[rx] = ry; return; } } int main() { ull N, M; cin >> N >> M; ull A[M], B[M]; rep(i, M) { ull a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } /* Uion-find というデータ構造を使えば処理時間を短縮できるらしい */ // cf. https://qiita.com/ofutonfuton/items/c17dfd33fc542c222396 // cf. https://atc001.contest.atcoder.jp/tasks/unionfind_a rep(i, N) par[i] = i; // 最初は全て自分が親 rep(i, N) mem[i] = 1; //////////////////// ull ans[M]; // ans ans[M - 1] = N * (N - 1) / 2; // 全ての橋が崩壊した時の不便さ for (ll i = M - 1; i > 0; i--) { ll ra = root(A[i]); ll rb = root(B[i]); if (ra == rb) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - mem[ra] * mem[rb]; // cout << "log:" << ans[i-1] << endl; // cout << "A, B " << A[i] << ' ' << B[i] << endl; // cout << "rA, rB " << root(A[i]) << ' ' << root(B[i]) << endl; // cout << "mrA, mrB " << mem[root(A[i])] << ' ' << mem[root(B[i])] << // endl; unite(rb, ra); } } rep(i, M) { cout << ans[i] << endl; } return 0; }
insert
38
38
38
43
TLE
p03108
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <string> #include <tuple> #include <vector> #define rep(i, n) for (ll i = 0; i < n; i++) #define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup. using namespace std; typedef long long ll; typedef unsigned long long ull; using Graph = vector<vector<ll>>; // std::cout<<std::fixed<<std::setprecision(10); ll par[100000]; // par[i]... iの親 ll mem[100000]; // iの(自分も含めた) 子の構成島数 ll root(ll x) { // 自身が親と同一の時まで再帰的にrootをたどる if (par[x] == x) return x; else { return root(par[x]); } } void unite(ll x, ll y) { ll rx = root(x); ll ry = root(y); if (rx == ry) return; else { // if(rx>ry) { // ll tmp = rx; // rx = ry; // ry = tmp; // } mem[ry] += mem[rx]; par[rx] = ry; return; } } int main() { ull N, M; cin >> N >> M; ull A[M], B[M]; rep(i, M) { ull a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } /* Uion-find というデータ構造を使えば処理時間を短縮できるらしい */ // cf. https://qiita.com/ofutonfuton/items/c17dfd33fc542c222396 // cf. https://atc001.contest.atcoder.jp/tasks/unionfind_a rep(i, N) par[i] = i; // 最初は全て自分が親 rep(i, N) mem[i] = 1; //////////////////// ull ans[M]; // ans ans[M - 1] = N * (N - 1) / 2; // 全ての橋が崩壊した時の不便さ for (ll i = M - 1; i > 0; i--) { ll ra = root(A[i]); ll rb = root(B[i]); if (ra == rb) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - mem[ra] * mem[rb]; // cout << "log:" << ans[i-1] << endl; // cout << "A, B " << A[i] << ' ' << B[i] << endl; // cout << "rA, rB " << root(A[i]) << ' ' << root(B[i]) << endl; // cout << "mrA, mrB " << mem[root(A[i])] << ' ' << mem[root(B[i])] << // endl; unite(rb, ra); } } rep(i, M) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <string> #include <tuple> #include <vector> #define rep(i, n) for (ll i = 0; i < n; i++) #define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup. using namespace std; typedef long long ll; typedef unsigned long long ull; using Graph = vector<vector<ll>>; // std::cout<<std::fixed<<std::setprecision(10); ll par[100000]; // par[i]... iの親 ll mem[100000]; // iの(自分も含めた) 子の構成島数 ll root(ll x) { // 自身が親と同一の時まで再帰的にrootをたどる if (par[x] == x) return x; else { return root(par[x]); } } void unite(ll x, ll y) { ll rx = root(x); ll ry = root(y); if (rx == ry) return; else { if (rx < ry) { ll tmp = rx; rx = ry; ry = tmp; } mem[ry] += mem[rx]; par[rx] = ry; return; } } int main() { ull N, M; cin >> N >> M; ull A[M], B[M]; rep(i, M) { ull a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } /* Uion-find というデータ構造を使えば処理時間を短縮できるらしい */ // cf. https://qiita.com/ofutonfuton/items/c17dfd33fc542c222396 // cf. https://atc001.contest.atcoder.jp/tasks/unionfind_a rep(i, N) par[i] = i; // 最初は全て自分が親 rep(i, N) mem[i] = 1; //////////////////// ull ans[M]; // ans ans[M - 1] = N * (N - 1) / 2; // 全ての橋が崩壊した時の不便さ for (ll i = M - 1; i > 0; i--) { ll ra = root(A[i]); ll rb = root(B[i]); if (ra == rb) ans[i - 1] = ans[i]; else { ans[i - 1] = ans[i] - mem[ra] * mem[rb]; // cout << "log:" << ans[i-1] << endl; // cout << "A, B " << A[i] << ' ' << B[i] << endl; // cout << "rA, rB " << root(A[i]) << ' ' << root(B[i]) << endl; // cout << "mrA, mrB " << mem[root(A[i])] << ' ' << mem[root(B[i])] << // endl; unite(rb, ra); } } rep(i, M) { cout << ans[i] << endl; } return 0; }
replace
38
43
38
43
TLE
p03108
C++
Runtime Error
#include <iostream> typedef long long ll; using namespace std; ll parent[10001]; ll s[10001]; ll input[10001][2]; ll ans[10001]; ll sum; ll Find(ll x) { if (x == parent[x]) { return x; } else { ll p = Find(parent[x]); parent[x] = p; return p; } } void Union(ll x, ll y) { x = Find(x); y = Find(y); if (x != y) { parent[y] = x; sum += s[x] * (s[x] - 1) / 2; sum += s[y] * (s[y] - 1) / 2; s[x] += s[y]; sum -= s[x] * (s[x] - 1) / 2; } } int main() { ll n, m; scanf("%lld %lld", &n, &m); for (int i = 1; i <= n; i++) { parent[i] = i; s[i] = 1; } for (int i = 0; i < m; i++) scanf("%lld %lld", &input[i][0], &input[i][1]); sum = n * (n - 1) / 2; for (int i = m - 1; i >= 0; i--) { ans[i] = sum; Union(input[i][0], input[i][1]); } for (int i = 0; i < m; i++) printf("%lld\n", ans[i]); return 0; }
#include <iostream> typedef long long ll; using namespace std; ll parent[100001]; ll s[100001]; ll input[100001][2]; ll ans[100001]; ll sum; ll Find(ll x) { if (x == parent[x]) { return x; } else { ll p = Find(parent[x]); parent[x] = p; return p; } } void Union(ll x, ll y) { x = Find(x); y = Find(y); if (x != y) { parent[y] = x; sum += s[x] * (s[x] - 1) / 2; sum += s[y] * (s[y] - 1) / 2; s[x] += s[y]; sum -= s[x] * (s[x] - 1) / 2; } } int main() { ll n, m; scanf("%lld %lld", &n, &m); for (int i = 1; i <= n; i++) { parent[i] = i; s[i] = 1; } for (int i = 0; i < m; i++) scanf("%lld %lld", &input[i][0], &input[i][1]); sum = n * (n - 1) / 2; for (int i = m - 1; i >= 0; i--) { ans[i] = sum; Union(input[i][0], input[i][1]); } for (int i = 0; i < m; i++) printf("%lld\n", ans[i]); return 0; }
replace
3
7
3
7
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; ++i) #define all(v) v.begin(), v.end() #define sz(v) (int(v.size())) typedef unsigned long long ul; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef vector<vector<int>> vii; typedef pair<int, int> pii; typedef vector<pii> vp; int OO = 2e9; const double eps = 1e-9; const int N = 200; int mx = -1e9, mn = 1e9; int arr[N], arr2[N]; struct dsu { int *par, *sz; dsu(int n) { par = new int[n + 1]; sz = new int[n + 1]; lp(i, n + 1) { par[i] = i; sz[i] = 1; } } int get_root(int a) { if (par[a] == a) return a; return get_root(par[a]); } long long Union(int a, int b) { int apar = get_root(a), bpar = get_root(b); if (apar == bpar) return 0; par[apar] = bpar; int tmp = sz[bpar]; sz[bpar] += sz[apar]; return 1LL * tmp * sz[apar]; } }; int main() { int n, m; cin >> n >> m; vector<pii> edges(m); for (int i = 0; i < m; ++i) { cin >> edges[i].first >> edges[i].second; } dsu ds(n); vector<long long> ans(m); long long cnt = 1LL * n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { ans[i] = cnt; cnt -= ds.Union(edges[i].first, edges[i].second); } for (int i = 0; i < m; ++i) { cout << ans[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; ++i) #define all(v) v.begin(), v.end() #define sz(v) (int(v.size())) typedef unsigned long long ul; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef vector<vector<int>> vii; typedef pair<int, int> pii; typedef vector<pii> vp; int OO = 2e9; const double eps = 1e-9; const int N = 200; int mx = -1e9, mn = 1e9; int arr[N], arr2[N]; struct dsu { int *par, *sz; dsu(int n) { par = new int[n + 1]; sz = new int[n + 1]; lp(i, n + 1) { par[i] = i; sz[i] = 1; } } int get_root(int a) { if (par[a] == a) return a; return get_root(par[a]); } long long Union(int a, int b) { int apar = get_root(a), bpar = get_root(b); if (apar == bpar) return 0; int tmp1 = sz[bpar]; int tmp2 = sz[apar]; if (sz[bpar] > sz[apar]) { par[apar] = bpar; sz[bpar] += sz[apar]; } else { par[bpar] = apar; sz[apar] += sz[bpar]; } return 1LL * tmp1 * tmp2; } }; int main() { int n, m; cin >> n >> m; vector<pii> edges(m); for (int i = 0; i < m; ++i) { cin >> edges[i].first >> edges[i].second; } dsu ds(n); vector<long long> ans(m); long long cnt = 1LL * n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { ans[i] = cnt; cnt -= ds.Union(edges[i].first, edges[i].second); } for (int i = 0; i < m; ++i) { cout << ans[i] << '\n'; } return 0; }
replace
40
44
40
50
TLE
p03108
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define lli long long int #define REP(i, n) for (int i = 0; i < n; i++) #define REP2(i, n) for (int i = 1; i <= n; i++) #define DEBUG 0 typedef struct UnionFind { vector<lli> par; // par[i]:iの親番号 par[3] = 2 : 3の親が2 vector<lli> size; // size[i]:iの大きさ 自分のみのときは1,根っこにsizeを持たせる*/ UnionFind(int N) { /*最初はすべてが根であると初期化*/ REP(i, N) { par.push_back(i); size.push_back(1); } } lli root(lli x) { /*データxが属する木の根を再起で得る root(x) = {xの木の根}}*/ if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(lli x, lli y) { /*xとyの併合*/ lli rx = root(x); lli ry = root(y); if (rx == ry) return; par[rx] = ry; /*xとyの根が同じじゃない: xの根をrxをyの根ryに変更する */ lli resize = size[rx] + size[ry]; size[rx] = resize; size[ry] = resize; } bool same(lli x, lli y) { lli rx = root(x); lli ry = root(y); return rx == ry; } lli getSize(lli x) { lli rx = root(x); return size[rx]; } }; int main() { lli n, m; cin >> n >> m; vector<lli> a(m), b(m); UnionFind uf(n); REP(i, m) { lli ta, tb; cin >> ta >> tb; ta--; tb--; a[i] = ta; b[i] = tb; } vector<lli> ans(n, 0); ans[m - 1] = n * (n - 1) / 2; for (lli i = m - 1; i > 0; i--) { if (uf.same(a[i], b[i])) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - uf.getSize(a[i]) * uf.getSize(b[i]); uf.unite(a[i], b[i]); } } REP(i, m) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define lli long long int #define REP(i, n) for (int i = 0; i < n; i++) #define REP2(i, n) for (int i = 1; i <= n; i++) #define DEBUG 0 typedef struct UnionFind { vector<lli> par; // par[i]:iの親番号 par[3] = 2 : 3の親が2 vector<lli> size; // size[i]:iの大きさ 自分のみのときは1,根っこにsizeを持たせる*/ UnionFind(int N) { /*最初はすべてが根であると初期化*/ REP(i, N) { par.push_back(i); size.push_back(1); } } lli root(lli x) { /*データxが属する木の根を再起で得る root(x) = {xの木の根}}*/ if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(lli x, lli y) { /*xとyの併合*/ lli rx = root(x); lli ry = root(y); if (rx == ry) return; par[rx] = ry; /*xとyの根が同じじゃない: xの根をrxをyの根ryに変更する */ lli resize = size[rx] + size[ry]; size[rx] = resize; size[ry] = resize; } bool same(lli x, lli y) { lli rx = root(x); lli ry = root(y); return rx == ry; } lli getSize(lli x) { lli rx = root(x); return size[rx]; } }; int main() { lli n, m; cin >> n >> m; vector<lli> a(m), b(m); UnionFind uf(n); REP(i, m) { lli ta, tb; cin >> ta >> tb; ta--; tb--; a[i] = ta; b[i] = tb; } vector<lli> ans(m, 0); ans[m - 1] = n * (n - 1) / 2; for (lli i = m - 1; i > 0; i--) { if (uf.same(a[i], b[i])) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - uf.getSize(a[i]) * uf.getSize(b[i]); uf.unite(a[i], b[i]); } } REP(i, m) { cout << ans[i] << endl; } return 0; }
replace
72
73
72
73
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; class QuickUnion { public: vector<int> id; vector<long long int> size; void init(long long int s) { id.resize(s); iota(id.begin(), id.end(), 0); vector<long long int> vec(s, 1LL); size = vec; } int root(int i) { while (i != id[i]) i = id[i]; return i; } long long int unite(int p, int q) { int i = root(p); int j = root(q); if (i == j) return 0LL; else { long long int res = size[i] * size[j]; size[j] = size[i] + size[j]; id[i] = j; return res; } } }; int main() { long long int n; int m; cin >> n >> m; vector<pair<int, int>> v(m); for (int i = 0; i < m; i++) { cin >> v[m - i - 1].first >> v[m - i - 1].second; v[m - i - 1].first--; v[m - i - 1].second--; } QuickUnion islands; islands.init(n); vector<long long int> ans(m); ans[0] = n * (n - 1) / 2; for (int i = 0; i < m - 1; i++) { if (ans[i] == 0) break; else ans[i + 1] = ans[i] - islands.unite(v[i].first, v[i].second); } for (int i = 0; i < m; i++) cout << ans[m - 1 - i] << endl; }
#include <bits/stdc++.h> using namespace std; class QuickUnion { public: vector<int> id; vector<long long int> size; void init(long long int s) { id.resize(s); iota(id.begin(), id.end(), 0); vector<long long int> vec(s, 1LL); size = vec; } int root(int i) { while (i != id[i]) i = id[i]; return i; } long long int unite(int p, int q) { int i = root(p); int j = root(q); if (i == j) return 0LL; else { long long int res = size[i] * size[j]; if (size[i] < size[j]) { id[i] = j; size[j] = size[i] + size[j]; } else { id[j] = i; size[i] = size[i] + size[j]; } return res; } } }; int main() { long long int n; int m; cin >> n >> m; vector<pair<int, int>> v(m); for (int i = 0; i < m; i++) { cin >> v[m - i - 1].first >> v[m - i - 1].second; v[m - i - 1].first--; v[m - i - 1].second--; } QuickUnion islands; islands.init(n); vector<long long int> ans(m); ans[0] = n * (n - 1) / 2; for (int i = 0; i < m - 1; i++) { if (ans[i] == 0) break; else ans[i + 1] = ans[i] - islands.unite(v[i].first, v[i].second); } for (int i = 0; i < m; i++) cout << ans[m - 1 - i] << endl; }
replace
25
27
25
32
TLE
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; // しょっぱなやばい、とけなそう感・・・ // 方針たたんな // 最後から見るか。 // 連結数はN(N-1)/2。最後は-1; // 毎回すべての連結確認する、はむりむり // 1[2,3,,,10] 2[3,4,,,10] 3[4,5,,,10]みたいな // 和を足せばいい? // んなことないか。1-2,1-3,2-3で1-2きれても3連結 // 孤立した島の積をとるか // 1-2-3,4-5-6だったら3*3=9がだめ 3+3+3 // 1,2,3,4,5だったら?5*4/2 4+4+4+4+4)/2 // 1-2,3-4,5だったら8 3+3+3+3+4)/2 // はしを1本折るごとに探索してたんじゃ絶対むりむりちん10^5でしょ // DP,どんなふうに・・・。 // 俺のもってる知識じゃまったくあてにならなそうだな。 // 解答みちゃう。 // ミカサがどうしたって? // Union-Findすげえ。sizeを調べるのはどうしたらいいか // 子どものsizeの配列をつねに用意。親に+1しとく // 1-2でS[1]=2 S[2]=1 // 1-2-3 1-2-4 で、S[4]=1 S[3]=1 S[2]=3 S[1]=4みたいな感じか // S[親]+=S[子1]+S[子2]か struct UnionFind { vector<ll> par; vector<ll> siz; UnionFind(ll N) : par(N), siz(N, 1) { for (int i = 0; i < N; i++) { par[i] = i; } } ll root(ll x) { if (par[x] == x) return x; return root(par[x]); } void unite(ll x, ll y) { ll rx = root(x), ry = root(y); if (rx == ry) return; par[ry] = rx; // x<yとしたい siz[rx] += siz[ry]; // 小さい数に集める } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vector<ll> A(M, 0), B(M, 0); for (int i = 0; i < M; i++) { ll a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } UnionFind tree(N); ll all = N * (N - 1) / 2; vector<ll> ans(M, 0); for (int i = M - 1; i >= 0; i--) { ans[i] = all; if (all == 0) break; // 連結成分の大きさってなんだ // all-N1*N2をしたい // いみわかった。 // 1 2-3 4 5 9 10-1*1 // 1 2-3 4-5 8 9-1*1 // 1-2-3 4-5 6 8-1*2 // 1-2-3-4-5 0 6-3*2 if (tree.issame(A[i], B[i]) == false) { all -= tree.size(A[i]) * tree.size(B[i]); } tree.unite(A[i], B[i]); } for (int i = 0; i < M; i++) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // しょっぱなやばい、とけなそう感・・・ // 方針たたんな // 最後から見るか。 // 連結数はN(N-1)/2。最後は-1; // 毎回すべての連結確認する、はむりむり // 1[2,3,,,10] 2[3,4,,,10] 3[4,5,,,10]みたいな // 和を足せばいい? // んなことないか。1-2,1-3,2-3で1-2きれても3連結 // 孤立した島の積をとるか // 1-2-3,4-5-6だったら3*3=9がだめ 3+3+3 // 1,2,3,4,5だったら?5*4/2 4+4+4+4+4)/2 // 1-2,3-4,5だったら8 3+3+3+3+4)/2 // はしを1本折るごとに探索してたんじゃ絶対むりむりちん10^5でしょ // DP,どんなふうに・・・。 // 俺のもってる知識じゃまったくあてにならなそうだな。 // 解答みちゃう。 // ミカサがどうしたって? // Union-Findすげえ。sizeを調べるのはどうしたらいいか // 子どものsizeの配列をつねに用意。親に+1しとく // 1-2でS[1]=2 S[2]=1 // 1-2-3 1-2-4 で、S[4]=1 S[3]=1 S[2]=3 S[1]=4みたいな感じか // S[親]+=S[子1]+S[子2]か struct UnionFind { vector<ll> par; vector<ll> siz; UnionFind(ll N) : par(N), siz(N, 1) { for (int i = 0; i < N; i++) { par[i] = i; } } ll root(ll x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(ll x, ll y) { ll rx = root(x), ry = root(y); if (rx == ry) return; par[ry] = rx; // x<yとしたい siz[rx] += siz[ry]; // 小さい数に集める } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vector<ll> A(M, 0), B(M, 0); for (int i = 0; i < M; i++) { ll a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } UnionFind tree(N); ll all = N * (N - 1) / 2; vector<ll> ans(M, 0); for (int i = M - 1; i >= 0; i--) { ans[i] = all; if (all == 0) break; // 連結成分の大きさってなんだ // all-N1*N2をしたい // いみわかった。 // 1 2-3 4 5 9 10-1*1 // 1 2-3 4-5 8 9-1*1 // 1-2-3 4-5 6 8-1*2 // 1-2-3-4-5 0 6-3*2 if (tree.issame(A[i], B[i]) == false) { all -= tree.size(A[i]) * tree.size(B[i]); } tree.unite(A[i], B[i]); } for (int i = 0; i < M; i++) cout << ans[i] << endl; }
replace
38
39
38
39
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; // しょっぱなやばい、とけなそう感・・・ // 方針たたんな // 最後から見るか。 // 連結数はN(N-1)/2。最後は-1; // 毎回すべての連結確認する、はむりむり // 1[2,3,,,10] 2[3,4,,,10] 3[4,5,,,10]みたいな // 和を足せばいい? // んなことないか。1-2,1-3,2-3で1-2きれても3連結 // 孤立した島の積をとるか // 1-2-3,4-5-6だったら3*3=9がだめ 3+3+3 // 1,2,3,4,5だったら?5*4/2 4+4+4+4+4)/2 // 1-2,3-4,5だったら8 3+3+3+3+4)/2 // はしを1本折るごとに探索してたんじゃ絶対むりむりちん10^5でしょ // DP,どんなふうに・・・。 // 俺のもってる知識じゃまったくあてにならなそうだな。 // 解答みちゃう。 // ミカサがどうしたって? // Union-Findすげえ。sizeを調べるのはどうしたらいいか // 子どものsizeの配列をつねに用意。親に+1しとく // 1-2でS[1]=2 S[2]=1 // 1-2-3 1-2-4 で、S[4]=1 S[3]=1 S[2]=3 S[1]=4みたいな感じか // S[親]+=S[子1]+S[子2]か struct UnionFind { vector<ll> par; vector<ll> siz; UnionFind(ll N) : par(N), siz(N, 1) { for (int i = 0; i < N; i++) { par[i] = i; } } ll root(ll x) { if (par[x] == x) return x; par[x] = root(par[x]); } void unite(ll x, ll y) { ll rx = root(x), ry = root(y); if (rx == ry) return; par[ry] = rx; // x<yとしたい siz[rx] += siz[ry]; // 小さい数に集める } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vector<ll> A(M, 0), B(M, 0); for (int i = 0; i < M; i++) { ll a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } UnionFind tree(N); ll all = N * (N - 1) / 2; vector<ll> ans(M, 0); for (int i = M - 1; i >= 0; i--) { ans[i] = all; if (all == 0) break; // 連結成分の大きさってなんだ // all-N1*N2をしたい // いみわかった。 // 1 2-3 4 5 9 10-1*1 // 1 2-3 4-5 8 9-1*1 // 1-2-3 4-5 6 8-1*2 // 1-2-3-4-5 0 6-3*2 if (tree.issame(A[i], B[i]) == false) { all -= tree.size(A[i]) * tree.size(B[i]); } tree.unite(A[i], B[i]); } for (int i = 0; i < M; i++) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // しょっぱなやばい、とけなそう感・・・ // 方針たたんな // 最後から見るか。 // 連結数はN(N-1)/2。最後は-1; // 毎回すべての連結確認する、はむりむり // 1[2,3,,,10] 2[3,4,,,10] 3[4,5,,,10]みたいな // 和を足せばいい? // んなことないか。1-2,1-3,2-3で1-2きれても3連結 // 孤立した島の積をとるか // 1-2-3,4-5-6だったら3*3=9がだめ 3+3+3 // 1,2,3,4,5だったら?5*4/2 4+4+4+4+4)/2 // 1-2,3-4,5だったら8 3+3+3+3+4)/2 // はしを1本折るごとに探索してたんじゃ絶対むりむりちん10^5でしょ // DP,どんなふうに・・・。 // 俺のもってる知識じゃまったくあてにならなそうだな。 // 解答みちゃう。 // ミカサがどうしたって? // Union-Findすげえ。sizeを調べるのはどうしたらいいか // 子どものsizeの配列をつねに用意。親に+1しとく // 1-2でS[1]=2 S[2]=1 // 1-2-3 1-2-4 で、S[4]=1 S[3]=1 S[2]=3 S[1]=4みたいな感じか // S[親]+=S[子1]+S[子2]か struct UnionFind { vector<ll> par; vector<ll> siz; UnionFind(ll N) : par(N), siz(N, 1) { for (int i = 0; i < N; i++) { par[i] = i; } } ll root(ll x) { if (par[x] == x) return x; par[x] = root(par[x]); return par[x]; } void unite(ll x, ll y) { ll rx = root(x), ry = root(y); if (rx == ry) return; par[ry] = rx; // x<yとしたい siz[rx] += siz[ry]; // 小さい数に集める } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vector<ll> A(M, 0), B(M, 0); for (int i = 0; i < M; i++) { ll a, b; cin >> a >> b; A[i] = a - 1; B[i] = b - 1; } UnionFind tree(N); ll all = N * (N - 1) / 2; vector<ll> ans(M, 0); for (int i = M - 1; i >= 0; i--) { ans[i] = all; if (all == 0) break; // 連結成分の大きさってなんだ // all-N1*N2をしたい // いみわかった。 // 1 2-3 4 5 9 10-1*1 // 1 2-3 4-5 8 9-1*1 // 1-2-3 4-5 6 8-1*2 // 1-2-3-4-5 0 6-3*2 if (tree.issame(A[i], B[i]) == false) { all -= tree.size(A[i]) * tree.size(B[i]); } tree.unite(A[i], B[i]); } for (int i = 0; i < M; i++) cout << ans[i] << endl; }
insert
39
39
39
40
-11
p03108
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; #define rep(i, N) for (int i = 0; i < N; i++) int node[100010] = {}; class UnionFind { public: vector<int> node; UnionFind(int N) { node = vector<int>(N, -1); } int unite(int a, int b) { a = find(a); b = find(b); if (a == b) { return false; } if (size(a) < size(b)) swap(a, b); node[a] += node[b]; node[b] = a; return true; } int find(int a) { if (node[a] < 0) return a; else return node[a] = find(node[a]); } int size(int a) { return -node[find(a)]; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); rep(i, M) cin >> A[i] >> B[i]; vector<ll> ans(M); ans[M - 1] = (ll)N * (N - 1) / 2; UnionFind uf(N); for (int i = M - 1; i >= 1; i--) { if (uf.find(A[i]) != uf.find(B[i])) { ans[i - 1] = ans[i] - (ll)uf.size(A[i]) * uf.size(B[i]); uf.unite(A[i], B[i]); } else ans[i - 1] = ans[i]; } rep(i, M) cout << ans[i] << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; #define rep(i, N) for (int i = 0; i < N; i++) int node[100010] = {}; class UnionFind { public: vector<int> node; UnionFind(int N) { node = vector<int>(N, -1); } int unite(int a, int b) { a = find(a); b = find(b); if (a == b) { return false; } if (size(a) < size(b)) swap(a, b); node[a] += node[b]; node[b] = a; return true; } int find(int a) { if (node[a] < 0) return a; else return node[a] = find(node[a]); } int size(int a) { return -node[find(a)]; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); rep(i, M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> ans(M); ans[M - 1] = (ll)N * (N - 1) / 2; UnionFind uf(N); for (int i = M - 1; i >= 1; i--) { if (uf.find(A[i]) != uf.find(B[i])) { ans[i - 1] = ans[i] - (ll)uf.size(A[i]) * uf.size(B[i]); uf.unite(A[i], B[i]); } else ans[i - 1] = ans[i]; } rep(i, M) cout << ans[i] << endl; }
replace
54
55
54
59
0
p03108
C++
Time Limit Exceeded
#include <iostream> #include <vector> typedef long long LL; using namespace std; LL ans; vector<LL> p; vector<LL> cnt; LL find_root(LL x) { if (p[x] != x) x = find_root(p[x]); return p[x]; } void unite(LL x, LL y) { LL nx = find_root(x); LL ny = find_root(y); ans = 0; if (nx == ny) return; else { p[ny] = nx; ans = cnt[nx] * cnt[ny]; cnt[nx] += cnt[ny]; } } int main() { LL n, m; cin >> n >> m; p = vector<LL>(n); cnt = vector<LL>(n, 1); for (LL i = 0; i < n; i++) p[i] = i; vector<LL> a(m), b(m); for (LL i = 0; i < m; i++) { LL s, t; scanf("%lld%lld", &s, &t); s--, t--; a[i] = s, b[i] = t; } vector<LL> res(m); res[m - 1] = n * (n - 1) / 2; for (LL i = m - 1; i >= 1; i--) { unite(a[i], b[i]); res[i - 1] = res[i] - ans; } for (LL i = 0; i < m; i++) { cout << res[i] << endl; } }
#include <iostream> #include <vector> typedef long long LL; using namespace std; LL ans; vector<LL> p; vector<LL> cnt; LL find_root(LL x) { if (p[x] != x) p[x] = find_root(p[x]); return p[x]; } void unite(LL x, LL y) { LL nx = find_root(x); LL ny = find_root(y); ans = 0; if (nx == ny) return; else { p[ny] = nx; ans = cnt[nx] * cnt[ny]; cnt[nx] += cnt[ny]; } } int main() { LL n, m; cin >> n >> m; p = vector<LL>(n); cnt = vector<LL>(n, 1); for (LL i = 0; i < n; i++) p[i] = i; vector<LL> a(m), b(m); for (LL i = 0; i < m; i++) { LL s, t; scanf("%lld%lld", &s, &t); s--, t--; a[i] = s, b[i] = t; } vector<LL> res(m); res[m - 1] = n * (n - 1) / 2; for (LL i = m - 1; i >= 1; i--) { unite(a[i], b[i]); res[i - 1] = res[i] - ans; } for (LL i = 0; i < m; i++) { cout << res[i] << endl; } }
replace
11
12
11
12
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" struct UnionFind { UnionFind() {} UnionFind(int n) { resize(n); } void resize(int n) { par.resize(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } bool merge(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (par[rx] > par[ry]) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool is_same(int x, int y) { return (root(x) == root(y)); } int size(int x) { return -par[root(x)]; } private: vector<int> par; }; int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll n, m; cin >> n >> m; vector<ll> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind uf(n); vector<ll> ans(m + 1); ans[n] = 0; rrep(i, m) { if (uf.is_same(a[i], b[i])) { ans[i] = ans[i + 1]; } else { ans[i] = ans[i + 1] + uf.size(a[i]) * uf.size(b[i]); uf.merge(a[i], b[i]); } } rep(i, m) printf("%lld\n", n * (n - 1) / 2 - ans[i + 1]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" struct UnionFind { UnionFind() {} UnionFind(int n) { resize(n); } void resize(int n) { par.resize(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } bool merge(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (par[rx] > par[ry]) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool is_same(int x, int y) { return (root(x) == root(y)); } int size(int x) { return -par[root(x)]; } private: vector<int> par; }; int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll n, m; cin >> n >> m; vector<ll> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } UnionFind uf(n); vector<ll> ans(m + 1); ans[m] = 0; rrep(i, m) { if (uf.is_same(a[i], b[i])) { ans[i] = ans[i + 1]; } else { ans[i] = ans[i + 1] + uf.size(a[i]) * uf.size(b[i]); uf.merge(a[i], b[i]); } } rep(i, m) printf("%lld\n", n * (n - 1) / 2 - ans[i + 1]); return 0; }
replace
60
61
60
61
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0, i##_len = int(n); i < i##_len; ++i) #define rep(i, a, b) for (int i = int(a); i < int(b); ++i) #define All(x) (x).begin(), (x).end() #define rAll(x) (x).rbegin(), (x).rend() using namespace std; using ll = long long; struct UnionFind { std::vector<int> par; ll huben; UnionFind(ll N) { par.resize(N, -1); huben = N * (N - 1) / 2; } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } int size(int x) { return -par[root(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 (same(x, y)) return false; if (size(x) < size(y)) swap(x, y); huben -= par[x] * par[y]; par[x] += par[y]; par[y] = x; return true; } }; int main() { ll N, M; cin >> N >> M; vector<int> a(N), b(N); REP(i, M) cin >> a[i] >> b[i]; vector<ll> ans(M); UnionFind uni(N); for (int i = M - 1; i >= 0; --i) { ans[i] = uni.huben; uni.unite(a[i] - 1, b[i] - 1); } REP(i, M) cout << ans[i] << endl; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0, i##_len = int(n); i < i##_len; ++i) #define rep(i, a, b) for (int i = int(a); i < int(b); ++i) #define All(x) (x).begin(), (x).end() #define rAll(x) (x).rbegin(), (x).rend() using namespace std; using ll = long long; struct UnionFind { std::vector<int> par; ll huben; UnionFind(ll N) { par.resize(N, -1); huben = N * (N - 1) / 2; } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } int size(int x) { return -par[root(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 (same(x, y)) return false; if (size(x) < size(y)) swap(x, y); huben -= par[x] * par[y]; par[x] += par[y]; par[y] = x; return true; } }; int main() { ll N, M; cin >> N >> M; vector<int> a(M), b(M); REP(i, M) cin >> a[i] >> b[i]; vector<ll> ans(M); UnionFind uni(N); for (int i = M - 1; i >= 0; --i) { ans[i] = uni.huben; uni.unite(a[i] - 1, b[i] - 1); } REP(i, M) cout << ans[i] << endl; }
replace
39
40
39
40
0
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 9; long long result[maxn]; class dsu { public: vector<int> parent, size; dsu(int n) : parent(n + 1), size(n + 1, 1) { for (int i = 0; i < n + 1; i++) { parent[i] = i; } } int find(int u) { if (u == parent[u]) return u; return u = find(parent[u]); } bool same(int u, int v) { u = find(u); v = find(v); return (u == v); } void merge(int u, int v) { u = find(u); v = find(v); if (same(u, v)) return; parent[u] = v; size[v] += size[u]; size[u] = 0; } int get_size(int u) { u = find(u); return size[u]; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edges.push_back(make_pair(a, b)); } long long inf = n * (n - 1LL) * 1LL / 2LL, tmp = inf; dsu uf(n); for (int i = 0; i < m; i++) { result[m - i - 1] = tmp; pair<int, int> edge = edges[m - i - 1]; if (uf.same(edge.first, edge.second)) { continue; } int a = uf.get_size(edge.first), b = uf.get_size(edge.second); tmp += a * (a - 1LL) * 1LL / 2LL; tmp += b * (b - 1LL) * 1LL / 2LL; uf.merge(edge.first, edge.second); int size = uf.get_size(edge.first); tmp -= size * (size - 1LL) * 1LL / 2LL; } for (int i = 0; i < m; i++) { cout << result[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 9; long long result[maxn]; class dsu { public: vector<int> parent, size; dsu(int n) : parent(n + 1), size(n + 1, 1) { for (int i = 0; i < n + 1; i++) { parent[i] = i; } } int find(int u) { if (u == parent[u]) return u; return parent[u] = find(parent[u]); } bool same(int u, int v) { u = find(u); v = find(v); return (u == v); } void merge(int u, int v) { u = find(u); v = find(v); if (same(u, v)) return; parent[u] = v; size[v] += size[u]; size[u] = 0; } int get_size(int u) { u = find(u); return size[u]; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edges.push_back(make_pair(a, b)); } long long inf = n * (n - 1LL) * 1LL / 2LL, tmp = inf; dsu uf(n); for (int i = 0; i < m; i++) { result[m - i - 1] = tmp; pair<int, int> edge = edges[m - i - 1]; if (uf.same(edge.first, edge.second)) { continue; } int a = uf.get_size(edge.first), b = uf.get_size(edge.second); tmp += a * (a - 1LL) * 1LL / 2LL; tmp += b * (b - 1LL) * 1LL / 2LL; uf.merge(edge.first, edge.second); int size = uf.get_size(edge.first); tmp -= size * (size - 1LL) * 1LL / 2LL; } for (int i = 0; i < m; i++) { cout << result[i] << '\n'; } return 0; }
replace
18
19
18
19
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // types typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pdd; typedef vector<ll> vll; // macros #define ALL(a) a.begin(), a.end() #define SZ(a) ((int)a.size()) #define FI first #define SE second #define REP(i, n) for (int i = 0; i < ((int)n); i++) #define REP1(i, n) for (int i = 1; i < ((int)n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define PB push_back #define EB emplace_back #define MP(a, b) make_pair(a, b) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define Decimal fixed << setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL // constants const int inf = 1e9; const ll linf = 1LL << 50; const double eps = 1e-10; const int MOD = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; #define MAX_N 100000 struct UnionFind { vll tree; vll rank; vll size; UnionFind(ll n = 1) { init(n); } void init(ll n) { tree.resize(n); rank.resize(n); size.resize(n); REP(i, n) { tree[i] = i; rank[i] = 0; size[i] = 1; } } ll root(ll i) { if (tree[i] == i) return i; else { tree[i] = root(tree[i]); return tree[i]; } } bool same(ll a, ll b) { return root(a) == root(b); } ll len(ll a) { return size[root(a)]; } bool merge(ll ga, ll gb) { ll x = root(ga); ll y = root(gb); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; size[x] += size[y]; tree[y] = x; return true; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, m; cin >> n >> m; vll a(m), b(m); REP(i, m) cin >> a[i] >> b[i]; vll ans(m, 0); ans[m - 1] = n * (n - 1) / 2; UnionFind uf(n); REP(i, m - 1) { ll x = a[m - 1 - i]; ll y = b[m - 1 - i]; if (uf.same(x, y)) ans[m - 2 - i] = ans[m - 1 - i]; else ans[m - 2 - i] = ans[m - 1 - i] - uf.len(x) * uf.len(y); uf.merge(x, y); } REP(i, m) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; // types typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pdd; typedef vector<ll> vll; // macros #define ALL(a) a.begin(), a.end() #define SZ(a) ((int)a.size()) #define FI first #define SE second #define REP(i, n) for (int i = 0; i < ((int)n); i++) #define REP1(i, n) for (int i = 1; i < ((int)n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define PB push_back #define EB emplace_back #define MP(a, b) make_pair(a, b) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define Decimal fixed << setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL // constants const int inf = 1e9; const ll linf = 1LL << 50; const double eps = 1e-10; const int MOD = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; #define MAX_N 100000 struct UnionFind { vll tree; vll rank; vll size; UnionFind(ll n = 1) { init(n); } void init(ll n) { tree.resize(n); rank.resize(n); size.resize(n); REP(i, n) { tree[i] = i; rank[i] = 0; size[i] = 1; } } ll root(ll i) { if (tree[i] == i) return i; else { tree[i] = root(tree[i]); return tree[i]; } } bool same(ll a, ll b) { return root(a) == root(b); } ll len(ll a) { return size[root(a)]; } bool merge(ll ga, ll gb) { ll x = root(ga); ll y = root(gb); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; size[x] += size[y]; tree[y] = x; return true; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, m; cin >> n >> m; vll a(m), b(m); REP(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } vll ans(m, 0); ans[m - 1] = n * (n - 1) / 2; UnionFind uf(n); REP(i, m - 1) { ll x = a[m - 1 - i]; ll y = b[m - 1 - i]; if (uf.same(x, y)) ans[m - 2 - i] = ans[m - 1 - i]; else ans[m - 2 - i] = ans[m - 1 - i] - uf.len(x) * uf.len(y); uf.merge(x, y); } REP(i, m) cout << ans[i] << endl; }
replace
94
96
94
99
0
p03108
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } int size(int A) { return -Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) return false; if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; UnionFind Uni(N); for (int i = M - 1; i >= 0; i--) { ans[i - 1] = ans[i]; if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]); Uni.connect(A[i], B[i]); } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } int size(int A) { return -Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) return false; if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int N, M; cin >> N >> M; vector<int> A(M), B(M); for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<long long> ans(M); ans[M - 1] = (long long)N * (N - 1) / 2; UnionFind Uni(N); for (int i = M - 1; i >= 1; i--) { ans[i - 1] = ans[i]; if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]); Uni.connect(A[i], B[i]); } } for (int i = 0; i < M; i++) { cout << ans[i] << endl; } return 0; }
replace
47
48
47
48
-6
free(): invalid pointer
p03108
C++
Runtime Error
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) #define fi first #define se second #define pb push_back #define mp make_pair #define eb emplace_back #define bcnt __builtin_popcountll using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<ll, ll> Pll; typedef pair<int, int> Pin; ll INF = 1e16; int inf = 1e9; ll MOD = 1e9 + 7; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{ "; REP(i, vec.size()) { os << vec[i] << " "; } os << "}"; return os; } ll pow_mod(ll a, ll p) { ll res = 1; while (p) { if (p & 1) res = (res * a) % MOD; a = (a * a) % MOD; p >>= 1; } return res; } ll mod_inv(ll a, ll m) { ll b = m, u = 0, v = 1; while (a) { ll t = b / a; swap(b -= t * a, a); swap(u -= t * v, v); } return (u % m + m) % m; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } void d_err() { cerr << endl; } template <typename H, typename... T> void d_err(H h, T... t) { cerr << h << " "; d_err(t...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__); #else #define debug(...) 83; #endif struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(x); } int issame(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; // merge by size // force size of x to be bigger than that of y if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; } int size(int x) { return -par[root(x)]; } }; int main() { cin.tie(0); ios_base::sync_with_stdio(false); ll N, M; cin >> N >> M; int a[M], b[M]; REP(i, M) { cin >> a[i] >> b[i], --a[i], --b[i]; } UnionFind uf(N); ll ans[M]; ll cur = N * (N - 1) / 2; REP(i, M) { ans[M - i - 1] = cur; int ca = a[M - i - 1], cb = b[M - i - 1]; if (uf.issame(ca, cb)) continue; ll as = uf.size(ca), bs = uf.size(cb); cur -= as * bs; uf.merge(ca, cb); } cout << fixed << setprecision(20); REP(i, M) cout << ans[i] << endl; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) #define fi first #define se second #define pb push_back #define mp make_pair #define eb emplace_back #define bcnt __builtin_popcountll using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<ll, ll> Pll; typedef pair<int, int> Pin; ll INF = 1e16; int inf = 1e9; ll MOD = 1e9 + 7; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{ "; REP(i, vec.size()) { os << vec[i] << " "; } os << "}"; return os; } ll pow_mod(ll a, ll p) { ll res = 1; while (p) { if (p & 1) res = (res * a) % MOD; a = (a * a) % MOD; p >>= 1; } return res; } ll mod_inv(ll a, ll m) { ll b = m, u = 0, v = 1; while (a) { ll t = b / a; swap(b -= t * a, a); swap(u -= t * v, v); } return (u % m + m) % m; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } void d_err() { cerr << endl; } template <typename H, typename... T> void d_err(H h, T... t) { cerr << h << " "; d_err(t...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__); #else #define debug(...) 83; #endif struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } int issame(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; // merge by size // force size of x to be bigger than that of y if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; } int size(int x) { return -par[root(x)]; } }; int main() { cin.tie(0); ios_base::sync_with_stdio(false); ll N, M; cin >> N >> M; int a[M], b[M]; REP(i, M) { cin >> a[i] >> b[i], --a[i], --b[i]; } UnionFind uf(N); ll ans[M]; ll cur = N * (N - 1) / 2; REP(i, M) { ans[M - i - 1] = cur; int ca = a[M - i - 1], cb = b[M - i - 1]; if (uf.issame(ca, cb)) continue; ll as = uf.size(ca), bs = uf.size(cb); cur -= as * bs; uf.merge(ca, cb); } cout << fixed << setprecision(20); REP(i, M) cout << ans[i] << endl; }
replace
84
85
84
85
-11
p03108
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for (long i = m; i < n; i++) #define repr(i, m, n) for (long i = n - 1; i >= m; i--) struct UnionFind { vector<int> par, si; UnionFind(int N) : par(N), si(N, 1) { rep(i, 0, N) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; si[y] += si[x]; par[x] = y; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return si[root(x)]; } }; int main() { long n, m; cin >> n >> m; vector<long> a(n), b(n); UnionFind u(n); rep(i, 0, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } long num = n * (n - 1) / 2; vector<long> res; repr(i, 0, m) { res.push_back(num); if (!u.same(a[i], b[i])) { num -= u.size(a[i]) * u.size(b[i]); u.unite(a[i], b[i]); } } repr(i, 0, res.size()) cout << res[i] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for (long i = m; i < n; i++) #define repr(i, m, n) for (long i = n - 1; i >= m; i--) struct UnionFind { vector<int> par, si; UnionFind(int N) : par(N), si(N, 1) { rep(i, 0, N) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; si[y] += si[x]; par[x] = y; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return si[root(x)]; } }; int main() { long n, m; cin >> n >> m; vector<long> a(m), b(m); UnionFind u(n); rep(i, 0, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } long num = n * (n - 1) / 2; vector<long> res; repr(i, 0, m) { res.push_back(num); if (!u.same(a[i], b[i])) { num -= u.size(a[i]) * u.size(b[i]); u.unite(a[i], b[i]); } } repr(i, 0, res.size()) cout << res[i] << endl; }
replace
34
35
34
35
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (a); i <= (b); i++) #define repR(i, n) for (ll i = n; i >= 0; i--) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define F first #define S second #define pb push_back #define pu push #define COUT(x) cout << (x) << endl #define PQ priority_queue<ll> #define PQR priority_queue<ll, vector<ll>, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define mp make_pair #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define sz(x) (int)(x).size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll MOD = 1000000007LL; const ll INF = 1LL << 60; using vll = vector<ll>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pll = pair<ll, ll>; using vc = vector<char>; using vvc = vector<vc>; ll dx[4] = {0, 1, 0, -1}; ll dy[4] = {1, 0, -1, 0}; template <typename T> struct UnionFind { vector<T> par; vector<T> rank; vector<T> sizes; UnionFind(T n) : par(n), rank(n, 0), sizes(n, 1) { for (T i = 0; i < n; i++) { par[i] = i; } } T root(T x) { return par[x] == x ? x : par[x] = root(par[x]); } bool unite(T x, T y) { if (x == y) return false; 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[y] = x; sizes[x] = sizes[x] + sizes[y]; return true; } bool same(T x, T y) { return root(x) == root(y); } T size(T x) { return sizes[root(x)]; } }; int main() { ll n, m; cin >> n >> m; vll a(m), b(m); rep(i, m) cin >> a[i] >> b[i]; vll ans(m); UnionFind<ll> uf(n + 1); ans[m - 1] = (n * (n - 1) / 2); for (int i = m - 1; i > 0; i++) { if (uf.same(a[i], b[i])) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } rep(i, m) { COUT(ans[i]); } }
#include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (a); i <= (b); i++) #define repR(i, n) for (ll i = n; i >= 0; i--) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define F first #define S second #define pb push_back #define pu push #define COUT(x) cout << (x) << endl #define PQ priority_queue<ll> #define PQR priority_queue<ll, vector<ll>, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define mp make_pair #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define sz(x) (int)(x).size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll MOD = 1000000007LL; const ll INF = 1LL << 60; using vll = vector<ll>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pll = pair<ll, ll>; using vc = vector<char>; using vvc = vector<vc>; ll dx[4] = {0, 1, 0, -1}; ll dy[4] = {1, 0, -1, 0}; template <typename T> struct UnionFind { vector<T> par; vector<T> rank; vector<T> sizes; UnionFind(T n) : par(n), rank(n, 0), sizes(n, 1) { for (T i = 0; i < n; i++) { par[i] = i; } } T root(T x) { return par[x] == x ? x : par[x] = root(par[x]); } bool unite(T x, T y) { if (x == y) return false; 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[y] = x; sizes[x] = sizes[x] + sizes[y]; return true; } bool same(T x, T y) { return root(x) == root(y); } T size(T x) { return sizes[root(x)]; } }; int main() { ll n, m; cin >> n >> m; vll a(m), b(m); rep(i, m) cin >> a[i] >> b[i]; vll ans(m); UnionFind<ll> uf(n + 1); ans[m - 1] = (n * (n - 1) / 2); for (int i = m - 1; i > 0; i--) { if (uf.same(a[i], b[i])) { ans[i - 1] = ans[i]; } else { ans[i - 1] = ans[i] - uf.size(a[i]) * uf.size(b[i]); uf.unite(a[i], b[i]); } } rep(i, m) { COUT(ans[i]); } }
replace
77
78
77
78
-11
p03108
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; const int NIL = -1; long long n, m; long long p[100009], num[100009]; void init() { for (int i = 0; i < n; i++) { p[i] = NIL; num[i] = 1; } } int parent(int a) { if (p[a] == NIL) return a; return parent(p[a]); } bool check(int a, int b) { if (parent(a) == parent(b)) return true; return false; } int add(int a, int b) { if (check(a, b)) return 0; long long pa = parent(a), pb = parent(b); long long ret = num[pa] * num[pb]; num[pa] += num[pb]; p[pb] = pa; return ret; } int main() { cin >> n >> m; int a[100009], b[100009]; vector<long long> ans; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } init(); ans.push_back(n * (n - 1) / 2); for (int i = m - 1; i >= 0; i--) { ans.push_back(ans[m - 1 - i] - add(a[i], b[i])); } for (int i = 0; i < m; i++) { ans[i] -= ans[m]; } for (int i = m - 1; i >= 0; i--) { cout << ans[i] << endl; } }
#include <iostream> #include <vector> using namespace std; const int NIL = -1; long long n, m; long long p[100009], num[100009]; void init() { for (int i = 0; i < n; i++) { p[i] = NIL; num[i] = 1; } } int parent(int a) { if (p[a] == NIL) return a; return parent(p[a]); } bool check(int a, int b) { if (parent(a) == parent(b)) return true; return false; } int add(int a, int b) { if (check(a, b)) return 0; long long pa = parent(a), pb = parent(b); long long ret = num[pa] * num[pb]; if (num[pa] < num[pb]) swap(pa, pb); num[pa] += num[pb]; p[pb] = pa; return ret; } int main() { cin >> n >> m; int a[100009], b[100009]; vector<long long> ans; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } init(); ans.push_back(n * (n - 1) / 2); for (int i = m - 1; i >= 0; i--) { ans.push_back(ans[m - 1 - i] - add(a[i], b[i])); } for (int i = 0; i < m; i++) { ans[i] -= ans[m]; } for (int i = m - 1; i >= 0; i--) { cout << ans[i] << endl; } }
insert
33
33
33
35
TLE
p03108
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define ll int64_t const ll mod = 1000000007; const ll LINF = 1e13; const ll LLINF = 1e18; const ll ALPHABET = 26; template <class T> void Swap(T &r, T &l) { T tmp = r; r = l; l = tmp; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; // for(int i=0; i<vec.size(); i++) is >> x[i]; とかでもいい return is; } struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) { } // 最初は全て-1 :-1なら根なので全て根:木のサイズが増えると-2-3-4となる bool unionSet(int x, int y) { x = root(x); y = root(y); // 両方のルートを出して、それをつなげる if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { // 要素xとyが同じ集合か → rootを調べれば解決 return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root( data[x]); //-1なら根、そうでないなら配列が指す場所へ移動 } int size(int x) { return -data[root(x)]; } }; int main() { long n, m; cin >> n >> m; vector<pair<long, long>> mp(m); for (int i = 0; i < m; ++i) { long a, b; cin >> a >> b; mp[i] = make_pair(a - 1, b - 1); } struct UnionFind uftree1(12); // 全部で12こ//最初は全部が根 // 1と同じグループ // uftree1.unionSet(1, 3); vector<long> ans; ll tmp = n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { ans.push_back(tmp); long x = mp[i].first; long y = mp[i].second; if (uftree1.findSet(x, y)) { } else { long xs = uftree1.size(x); long ys = uftree1.size(y); tmp -= xs * ys; uftree1.unionSet(x, y); } } for (int i = m - 1; i >= 0; --i) { cout << ans[i] << endl; } // cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define ll int64_t const ll mod = 1000000007; const ll LINF = 1e13; const ll LLINF = 1e18; const ll ALPHABET = 26; template <class T> void Swap(T &r, T &l) { T tmp = r; r = l; l = tmp; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; // for(int i=0; i<vec.size(); i++) is >> x[i]; とかでもいい return is; } struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) { } // 最初は全て-1 :-1なら根なので全て根:木のサイズが増えると-2-3-4となる bool unionSet(int x, int y) { x = root(x); y = root(y); // 両方のルートを出して、それをつなげる if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { // 要素xとyが同じ集合か → rootを調べれば解決 return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root( data[x]); //-1なら根、そうでないなら配列が指す場所へ移動 } int size(int x) { return -data[root(x)]; } }; int main() { long n, m; cin >> n >> m; vector<pair<long, long>> mp(m); for (int i = 0; i < m; ++i) { long a, b; cin >> a >> b; mp[i] = make_pair(a - 1, b - 1); } struct UnionFind uftree1(n); // 全部で12こ//最初は全部が根 // 1と同じグループ // uftree1.unionSet(1, 3); vector<long> ans; ll tmp = n * (n - 1) / 2; for (int i = m - 1; i >= 0; --i) { ans.push_back(tmp); long x = mp[i].first; long y = mp[i].second; if (uftree1.findSet(x, y)) { } else { long xs = uftree1.size(x); long ys = uftree1.size(y); tmp -= xs * ys; uftree1.unionSet(x, y); } } for (int i = m - 1; i >= 0; --i) { cout << ans[i] << endl; } // cout << ans << endl; }
replace
89
90
89
90
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define REP(i, num, n) for (ll i = num, i##_len = (n); i < i##_len; ++i) #define repprev(i, a, b) for (int i = b - 1; i >= a; i--) #define reprev(i, n) repprev(i, 0, n) using namespace std; #define sz(x) ((int)(x).size()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMSET(v, h) memset((v), h, sizeof(v)) 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 (b < a) { a = b; return 1; } return 0; } #define pb push_back #define mp make_pair #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } const ll INF = 1LL << 60; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } struct UnionFind { vector<ll> par; UnionFind(ll n) : par(n, -1) {} void init(ll n) { par.assign(n, -1); } int root(ll x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(ll x, ll y) { return root(x) == root(y); } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(ll x) { return -par[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vector<ll> A(M), B(M); rep(i, M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> ans(M); ans[M - 1] = N * (N - 1) / 2; UnionFind Uni(N); reprev(i, M - 1) { // 繋がってなかったのが繋がった時 if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] = ans[i] - Uni.size(A[i]) * Uni.size(B[i]); Uni.merge(A[i], B[i]); } else ans[i - 1] = ans[i]; } rep(i, M) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define REP(i, num, n) for (ll i = num, i##_len = (n); i < i##_len; ++i) #define repprev(i, a, b) for (int i = b - 1; i >= a; i--) #define reprev(i, n) repprev(i, 0, n) using namespace std; #define sz(x) ((int)(x).size()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMSET(v, h) memset((v), h, sizeof(v)) 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 (b < a) { a = b; return 1; } return 0; } #define pb push_back #define mp make_pair #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } const ll INF = 1LL << 60; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } struct UnionFind { vector<ll> par; UnionFind(ll n) : par(n, -1) {} void init(ll n) { par.assign(n, -1); } int root(ll x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(ll x, ll y) { return root(x) == root(y); } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(ll x) { return -par[root(x)]; } }; int main() { ll N, M; cin >> N >> M; vector<ll> A(M), B(M); rep(i, M) { cin >> A[i] >> B[i]; A[i]--; B[i]--; } vector<ll> ans(M); ans[M - 1] = N * (N - 1) / 2; UnionFind Uni(N); repprev(i, 1, M) { // 繋がってなかったのが繋がった時 if (Uni.root(A[i]) != Uni.root(B[i])) { ans[i - 1] = ans[i] - Uni.size(A[i]) * Uni.size(B[i]); Uni.merge(A[i], B[i]); } else ans[i - 1] = ans[i]; } rep(i, M) { cout << ans[i] << endl; } }
replace
106
107
106
107
-6
munmap_chunk(): invalid pointer
p03108
C++
Runtime Error
#pragma gcc optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define PER(i, n) for (int i = (n - 1); i >= 0; --i) #define ALL(V) (V).begin(), (V).end() #define SORT(V) sort(ALL(V)) // 小さい方からソート #define REV(V) reverse(ALL(V)) // リバース #define RSORT(V) \ SORT(V); \ REV(V) // 大きい方からソート #define NEXP(V) next_permutation(ALL(V)) // 順列 #define pb(n) push_back(n) #define popb(n) pop_back(n) #define endl '\n' #define Endl '\n' #define DUMP(x) cout << #x << " = " << (x) << endl #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define yes(n) cout << ((n) ? "yes" : "no") << endl #define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl #define VSUM(V) accumulate(ALL(V), 0) #define MID(a, b, c) (a) < (b) && (b) < (c) #define MIDe(a, b, c) (a) <= (b) && (b) <= (c) #define IN(n) cin >> n #define IN2(a, b) cin >> a >> b #define IN3(a, b, c) cin >> a >> b >> c #define IN4(a, b, c, d) cin >> a >> b >> c >> d #define VIN(V) \ for (int i = 0; i < (V).size(); i++) { \ cin >> (V).at(i); \ } #define OUT(n) cout << n << endl #define VOUT(V) \ REP(i, (V).size()) { cout << (V)[i] << endl; } #define VOUT2(V) \ REP(i, (V).size()) { \ if ((V).size() - 1 != i) { \ cout << (V)[i] << " "; \ } else { \ cout << (V)[i] << endl; \ } \ } #define int long long #define P pair<ll, ll> #define Vi vector<ll> #define VVi vector<vector<ll>> #define Vd vector<double> #define Vb vector<bool> #define Vs vector<string> #define Vc vector<char> #define M map<ll, ll> #define S set<ll> #define PQ priority_queue<ll> #define PQG priority_queue < ll, V, greater<ll> using ll = long long; using Graph = vector<vector<int>>; const int MOD = 1000000007; const int INF = 1061109567; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; class UnionFind { public: vector<int> par; vector<int> siz; UnionFind(int sz_) : par(sz_), siz(sz_, 1LL) { for (int i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (int i = 0; i < sz_; ++i) par[i] = i; } int root(int x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return siz[root(x)]; } }; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a * b / gcd(a, b); } // 文字を全て大文字にします string toStrUp(string str) { char diff = 'A' - 'a'; REP(i, str.size()) str[i] += diff; return str; } // 文字をstring型で一文字取得します string get1ch(string str, int key) { return str.substr(key, 1); } // 素因数分解 (O(sqrt(n))) map<int, int> prime_factor(int n) { map<int, int> ret; for (int i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } // 素数判定 (O(sqrt(n))) bool is_prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } // 進数変換 (O(log n)) template <typename T> vector<T> convert_base(T x, T b) { vector<T> ret; T t = 1, k = abs(b); while (x) { ret.emplace_back((x * t) % k); if (ret.back() < 0) ret.back() += k; x -= ret.back() * t; x /= k; t *= b / k; } if (ret.empty()) ret.emplace_back(0); reverse(begin(ret), end(ret)); return ret; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); // デフォルト変数定義 int n = 0, m = 0, a = 0, b = 0, c = 0, d = 0, x = 0, y = 0, z = 0; string s = "", t = ""; // // ここから IN2(n, m); Vi A(n); Vi B(n); UnionFind uni(n); REP(i, m) { IN2(A[i], B[i]); A[i]--; B[i]--; } Vi ans; int patern = n * (n - 1) / 2; PER(i, m) { ans.pb(patern); if (uni.issame(A[i], B[i])) continue; patern -= uni.size(A[i]) * uni.size(B[i]); uni.merge(A[i], B[i]); } REV(ans); VOUT(ans) }
#pragma gcc optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define PER(i, n) for (int i = (n - 1); i >= 0; --i) #define ALL(V) (V).begin(), (V).end() #define SORT(V) sort(ALL(V)) // 小さい方からソート #define REV(V) reverse(ALL(V)) // リバース #define RSORT(V) \ SORT(V); \ REV(V) // 大きい方からソート #define NEXP(V) next_permutation(ALL(V)) // 順列 #define pb(n) push_back(n) #define popb(n) pop_back(n) #define endl '\n' #define Endl '\n' #define DUMP(x) cout << #x << " = " << (x) << endl #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define yes(n) cout << ((n) ? "yes" : "no") << endl #define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl #define VSUM(V) accumulate(ALL(V), 0) #define MID(a, b, c) (a) < (b) && (b) < (c) #define MIDe(a, b, c) (a) <= (b) && (b) <= (c) #define IN(n) cin >> n #define IN2(a, b) cin >> a >> b #define IN3(a, b, c) cin >> a >> b >> c #define IN4(a, b, c, d) cin >> a >> b >> c >> d #define VIN(V) \ for (int i = 0; i < (V).size(); i++) { \ cin >> (V).at(i); \ } #define OUT(n) cout << n << endl #define VOUT(V) \ REP(i, (V).size()) { cout << (V)[i] << endl; } #define VOUT2(V) \ REP(i, (V).size()) { \ if ((V).size() - 1 != i) { \ cout << (V)[i] << " "; \ } else { \ cout << (V)[i] << endl; \ } \ } #define int long long #define P pair<ll, ll> #define Vi vector<ll> #define VVi vector<vector<ll>> #define Vd vector<double> #define Vb vector<bool> #define Vs vector<string> #define Vc vector<char> #define M map<ll, ll> #define S set<ll> #define PQ priority_queue<ll> #define PQG priority_queue < ll, V, greater<ll> using ll = long long; using Graph = vector<vector<int>>; const int MOD = 1000000007; const int INF = 1061109567; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; class UnionFind { public: vector<int> par; vector<int> siz; UnionFind(int sz_) : par(sz_), siz(sz_, 1LL) { for (int i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (int i = 0; i < sz_; ++i) par[i] = i; } int root(int x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(int x, int y) { return root(x) == root(y); } int size(int x) { return siz[root(x)]; } }; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a * b / gcd(a, b); } // 文字を全て大文字にします string toStrUp(string str) { char diff = 'A' - 'a'; REP(i, str.size()) str[i] += diff; return str; } // 文字をstring型で一文字取得します string get1ch(string str, int key) { return str.substr(key, 1); } // 素因数分解 (O(sqrt(n))) map<int, int> prime_factor(int n) { map<int, int> ret; for (int i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } // 素数判定 (O(sqrt(n))) bool is_prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } // 進数変換 (O(log n)) template <typename T> vector<T> convert_base(T x, T b) { vector<T> ret; T t = 1, k = abs(b); while (x) { ret.emplace_back((x * t) % k); if (ret.back() < 0) ret.back() += k; x -= ret.back() * t; x /= k; t *= b / k; } if (ret.empty()) ret.emplace_back(0); reverse(begin(ret), end(ret)); return ret; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); // デフォルト変数定義 int n = 0, m = 0, a = 0, b = 0, c = 0, d = 0, x = 0, y = 0, z = 0; string s = "", t = ""; // // ここから IN2(n, m); Vi A(m); Vi B(m); UnionFind uni(n); REP(i, m) { IN2(A[i], B[i]); A[i]--; B[i]--; } Vi ans; int patern = n * (n - 1) / 2; PER(i, m) { ans.pb(patern); if (uni.issame(A[i], B[i])) continue; patern -= uni.size(A[i]) * uni.size(B[i]); uni.merge(A[i], B[i]); } REV(ans); VOUT(ans) }
replace
192
194
192
194
0
p03108
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int MAX_N = 10000; vector<pair<int, int>> edge; int N, M; int par[MAX_N]; // 親 ll sizes[MAX_N]; // 木の深さ void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; sizes[i] = 1; } } // 木の根を求める 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 (par[x] > par[y]) swap(x, y); // merge technique par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } void solve() { init(N); ll ans[MAX_N]; ans[M - 1] = (ll)N * (N - 1) / 2; for (int i = M - 1; i > 0; i--) { int a = edge[i].first; int b = edge[i].second; if (!same(a, b)) { ans[i - 1] = ans[i] - (ll)(sizes[find(a)] * sizes[find(b)]); unite(a, b); } else ans[i - 1] = ans[i]; } for (int i = 0; i < M; i++) cout << ans[i] << '\n'; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a = a - 1; b = b - 1; edge.push_back(make_pair(a, b)); } solve(); return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int MAX_N = 100000; vector<pair<int, int>> edge; int N, M; int par[MAX_N]; // 親 ll sizes[MAX_N]; // 木の深さ void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; sizes[i] = 1; } } // 木の根を求める 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 (par[x] > par[y]) swap(x, y); // merge technique par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } void solve() { init(N); ll ans[MAX_N]; ans[M - 1] = (ll)N * (N - 1) / 2; for (int i = M - 1; i > 0; i--) { int a = edge[i].first; int b = edge[i].second; if (!same(a, b)) { ans[i - 1] = ans[i] - (ll)(sizes[find(a)] * sizes[find(b)]); unite(a, b); } else ans[i - 1] = ans[i]; } for (int i = 0; i < M; i++) cout << ans[i] << '\n'; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a = a - 1; b = b - 1; edge.push_back(make_pair(a, b)); } solve(); return 0; }
replace
20
21
20
21
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int using namespace std; const int maxn = 1500 + 5; ll n1 = 0, n2 = 0; ll root(ll arr[], ll a) { while (arr[a] != a) { arr[a] = arr[arr[a]]; a = arr[a]; } return a; } void unioun(ll arr[], ll size[], ll a, ll b) { int roota, rootb; roota = root(arr, a); rootb = root(arr, b); if (size[roota] < size[rootb]) { arr[roota] = arr[rootb]; n1 = size[roota]; n2 = size[rootb]; size[rootb] += size[roota]; size[roota] = 0; } else { arr[rootb] = arr[roota]; n1 = size[roota]; n2 = size[rootb]; size[roota] += size[rootb]; } } bool find(ll arr[], ll a, ll b) { int roota, rootb; roota = root(arr, a); rootb = root(arr, b); if (roota == rootb) { return true; } return false; } int main() { ll size[maxn], a[maxn], count[maxn], ans[maxn], i, x, y, n, m; cin >> n >> m; vector<pair<int, int>> v; for (i = 1; i < n + 1; i++) { a[i] = i; size[i] = 1; count[i] = 1; } // v.push_back(make_pair(0,0)); for (i = 0; i < m; i++) { cin >> x >> y; v.push_back(make_pair(x, y)); } // cout<<v[m-1].first<<' '<<v[m-1].second<<endl; ans[m - 1] = n * (n - 1) / 2; for (i = m - 2; i >= 0; i--) { if (!find(a, v[i + 1].first, v[i + 1].second)) { unioun(a, size, v[i + 1].first, v[i + 1].second); ans[i] = ans[i + 1] - n1 * n2; } else { ans[i] = ans[i + 1]; // cout<<"anudeep"<<' '<<i; } } for (i = 0; i < m; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> #define ll long long int using namespace std; const int maxn = 100000 + 5; ll n1 = 0, n2 = 0; ll root(ll arr[], ll a) { while (arr[a] != a) { arr[a] = arr[arr[a]]; a = arr[a]; } return a; } void unioun(ll arr[], ll size[], ll a, ll b) { int roota, rootb; roota = root(arr, a); rootb = root(arr, b); if (size[roota] < size[rootb]) { arr[roota] = arr[rootb]; n1 = size[roota]; n2 = size[rootb]; size[rootb] += size[roota]; size[roota] = 0; } else { arr[rootb] = arr[roota]; n1 = size[roota]; n2 = size[rootb]; size[roota] += size[rootb]; } } bool find(ll arr[], ll a, ll b) { int roota, rootb; roota = root(arr, a); rootb = root(arr, b); if (roota == rootb) { return true; } return false; } int main() { ll size[maxn], a[maxn], count[maxn], ans[maxn], i, x, y, n, m; cin >> n >> m; vector<pair<int, int>> v; for (i = 1; i < n + 1; i++) { a[i] = i; size[i] = 1; count[i] = 1; } // v.push_back(make_pair(0,0)); for (i = 0; i < m; i++) { cin >> x >> y; v.push_back(make_pair(x, y)); } // cout<<v[m-1].first<<' '<<v[m-1].second<<endl; ans[m - 1] = n * (n - 1) / 2; for (i = m - 2; i >= 0; i--) { if (!find(a, v[i + 1].first, v[i + 1].second)) { unioun(a, size, v[i + 1].first, v[i + 1].second); ans[i] = ans[i + 1] - n1 * n2; } else { ans[i] = ans[i + 1]; // cout<<"anudeep"<<' '<<i; } } for (i = 0; i < m; i++) { cout << ans[i] << endl; } }
replace
3
4
3
4
0
p03108
Python
Runtime Error
N, M = [int(_) for _ in input().split()] bridge = [[int(_) for _ in input().split()] for _ in range(M)] bridge.reverse() bridge = bridge[:-1] inconvenience = N * (N - 1) // 2 inconvenience_history = [inconvenience] class UnionFind: def __init__(self, N): self.par = [i for i in range(N + 1)] self.rank = [0] * (N + 1) self._number = [1] * (N + 1) def root(self, i): if self.par[i] == i: return i self.par[i] = self.root(self.par[i]) return self.par[i] def tree_rank(self, i): return self.rank[self.root(i)] def number(self, i): return self._number[self.root(i)] def union(self, i, j): _root_i = self.root(i) _root_j = self.root(j) if _root_i == _root_j: return else: if self.tree_rank(i) < self.tree_rank(j): self.par[_root_i] = _root_j self._number[_root_j] += self._number[_root_i] else: self.par[_root_j] = _root_i self._number[_root_i] += self._number[_root_j] if self.tree_rank(i) == self.tree_rank(j): self.rank[_root_i] += 1 islands = UnionFind(N) for A_i, B_i in bridge: islands.pri() root_A = islands.root(A_i) root_B = islands.root(B_i) if root_A == root_B: inconvenience_history.append(inconvenience) continue else: improvement = islands.number(A_i) * islands.number(B_i) islands.union(A_i, B_i) inconvenience -= improvement if inconvenience == 0: inconvenience_history += [0] * (M - len(inconvenience_history)) break inconvenience_history.append(inconvenience) print(*inconvenience_history[::-1], sep="\n")
N, M = [int(_) for _ in input().split()] bridge = [[int(_) for _ in input().split()] for _ in range(M)] bridge.reverse() bridge = bridge[:-1] inconvenience = N * (N - 1) // 2 inconvenience_history = [inconvenience] class UnionFind: def __init__(self, N): self.par = [i for i in range(N + 1)] self.rank = [0] * (N + 1) self._number = [1] * (N + 1) def root(self, i): if self.par[i] == i: return i self.par[i] = self.root(self.par[i]) return self.par[i] def tree_rank(self, i): return self.rank[self.root(i)] def number(self, i): return self._number[self.root(i)] def union(self, i, j): _root_i = self.root(i) _root_j = self.root(j) if _root_i == _root_j: return else: if self.tree_rank(i) < self.tree_rank(j): self.par[_root_i] = _root_j self._number[_root_j] += self._number[_root_i] else: self.par[_root_j] = _root_i self._number[_root_i] += self._number[_root_j] if self.tree_rank(i) == self.tree_rank(j): self.rank[_root_i] += 1 islands = UnionFind(N) for A_i, B_i in bridge: root_A = islands.root(A_i) root_B = islands.root(B_i) if root_A == root_B: inconvenience_history.append(inconvenience) continue else: improvement = islands.number(A_i) * islands.number(B_i) islands.union(A_i, B_i) inconvenience -= improvement if inconvenience == 0: inconvenience_history += [0] * (M - len(inconvenience_history)) break inconvenience_history.append(inconvenience) print(*inconvenience_history[::-1], sep="\n")
delete
47
49
47
47
AttributeError: 'UnionFind' object has no attribute 'pri'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03108/Python/s568431545.py", line 49, in <module> islands.pri() AttributeError: 'UnionFind' object has no attribute 'pri'
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; // UnionFind struct UnionFind { vector<int> d; UnionFind(int n = 0) : d(n, -1) {} // each node has "minus-size" 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 component() { set<int> S; rep(x, d.size()) S.insert(find(x)); return S.size(); } }; ll comb2(ll n) { return n * (n - 1) / 2; } int main() { int n, m; cin >> n >> m; vector<pair<int, int>> v; rep(i, m) { int a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } ll S[m + 1]; rep(i, m + 1) S[i] = 0; S[m] = comb2(n); UnionFind un(n); for (int i = m - 1; i >= 0; i--) { int a = v[i].first; int b = v[i].second; if (un.same(a, b)) { S[i] = S[i + 1]; continue; } int x = un.size(a); int y = un.size(b); un.unite(a, b); int z = un.size(a); S[i] = S[i + 1] + comb2(x) + comb2(y) - comb2(z); } for (int i = 1; i <= m; i++) { cout << S[i] << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; // UnionFind struct UnionFind { vector<int> d; UnionFind(int n = 0) : d(n, -1) {} // each node has "minus-size" 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 component() { set<int> S; rep(x, d.size()) S.insert(find(x)); return S.size(); } }; ll comb2(ll n) { return n * (n - 1) / 2; } int main() { int n, m; cin >> n >> m; vector<pair<int, int>> v; rep(i, m) { int a, b; cin >> a >> b; a--; b--; v.push_back(make_pair(a, b)); } ll S[m + 1]; rep(i, m + 1) S[i] = 0; S[m] = comb2(n); UnionFind un(n); for (int i = m - 1; i >= 0; i--) { int a = v[i].first; int b = v[i].second; if (un.same(a, b)) { S[i] = S[i + 1]; continue; } int x = un.size(a); int y = un.size(b); un.unite(a, b); int z = un.size(a); S[i] = S[i + 1] + comb2(x) + comb2(y) - comb2(z); } for (int i = 1; i <= m; i++) { cout << S[i] << endl; } return 0; }
insert
43
43
43
45
0
p03108
Python
Time Limit Exceeded
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**7) class UnionFind: def __init__(self, n): # parent[x] < 0 means x is root and abs(parent[x]) == size[x] self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1) def find(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def isSame(self, x, y): return self.find(x) == self.find(y) def unite(self, x, y): x, y = self.find(x), self.find(y) if self.depth[x] > self.depth[y]: self.parent[x] += self.parent[y] self.parent[y] = x else: self.parent[y] += self.parent[x] self.parent[x] = y if self.depth[x] == self.depth[y]: self.depth[y] += 1 def count(self, x): return -self.parent[self.find(x)] N, M = map(int, input().split()) AB = [tuple(map(int, input().split())) for _ in range(M)] AB.reverse() groups = UnionFind(N) ans = [N * (N - 1) // 2] for i in range(M): if groups.isSame(AB[i][0], AB[i][1]): ans.append(ans[-1]) else: ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1])) groups.unite(AB[i][0], AB[i][1]) for i in range(M): print(ans[::-1][i + 1])
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**7) class UnionFind: def __init__(self, n): # parent[x] < 0 means x is root and abs(parent[x]) == size[x] self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1) def find(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def isSame(self, x, y): return self.find(x) == self.find(y) def unite(self, x, y): x, y = self.find(x), self.find(y) if self.depth[x] > self.depth[y]: self.parent[x] += self.parent[y] self.parent[y] = x else: self.parent[y] += self.parent[x] self.parent[x] = y if self.depth[x] == self.depth[y]: self.depth[y] += 1 def count(self, x): return -self.parent[self.find(x)] N, M = map(int, input().split()) AB = [tuple(map(int, input().split())) for _ in range(M)] AB.reverse() groups = UnionFind(N) ans = [N * (N - 1) // 2] for i in range(M): if groups.isSame(AB[i][0], AB[i][1]): ans.append(ans[-1]) else: ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1])) groups.unite(AB[i][0], AB[i][1]) for i in range(M): print(ans[M - 1 - i])
replace
53
54
53
54
TLE
p03108
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; vector<long long> dsu(100005), cnt(100005, 1); long long parent(long long x) { while (dsu[x] != x) { x = dsu[x]; } return x; } int main() { srand(42); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m, a1, r1, r2; cin >> n >> m; vector<pair<long long, long long>> edges; for (a1 = 1; a1 <= m; ++a1) { dsu[a1] = a1; cin >> r1 >> r2; edges.push_back(make_pair(r1, r2)); } long long ans = (n * (n - 1)) / 2; vector<long long> res; for (a1 = m - 1; a1 > -1; --a1) { res.push_back(ans); long long p1 = parent(edges[a1].first), p2 = parent(edges[a1].second); if (p1 != p2 && rand() & 1) { ans -= (cnt[p1] * cnt[p2]); dsu[p1] = p2; cnt[p2] += cnt[p1]; } else if (p1 != p2) { ans -= (cnt[p1] * cnt[p2]); dsu[p2] = p1; cnt[p1] += cnt[p2]; } // cout<<ans<<"\n"; } for (a1 = m - 1; a1 > -1; --a1) cout << res[a1] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> dsu(100005), cnt(100005, 1); long long parent(long long x) { while (dsu[x] != x) { dsu[x] = dsu[dsu[x]]; x = dsu[x]; } return x; } int main() { srand(42); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m, a1, r1, r2; cin >> n >> m; vector<pair<long long, long long>> edges; for (a1 = 1; a1 <= m; ++a1) { dsu[a1] = a1; cin >> r1 >> r2; edges.push_back(make_pair(r1, r2)); } long long ans = (n * (n - 1)) / 2; vector<long long> res; for (a1 = m - 1; a1 > -1; --a1) { res.push_back(ans); long long p1 = parent(edges[a1].first), p2 = parent(edges[a1].second); if (p1 != p2 && rand() & 1) { ans -= (cnt[p1] * cnt[p2]); dsu[p1] = p2; cnt[p2] += cnt[p1]; } else if (p1 != p2) { ans -= (cnt[p1] * cnt[p2]); dsu[p2] = p1; cnt[p1] += cnt[p2]; } // cout<<ans<<"\n"; } for (a1 = m - 1; a1 > -1; --a1) cout << res[a1] << "\n"; return 0; }
insert
5
5
5
6
TLE
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rp(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; using P = pair<int, int>; class UnionFind { public: // 親の番号を格納する。親だった場合は-(その集合のサイズ) vector<int> Parent; // 作るときはParentの値を全て−1にする // こうすると全てバラバラになる UnionFind(int N) { Parent = vector<int>(N, -1); } // Aがどのグループに属しているか調べる int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } // 自分のいるグループの頂点数を調べる int size(int A) { return -Parent[root(A)]; // 親をとってきたい } // AとBをくっつける bool connect(int A, int B) { // AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); rp(i, m) { cin >> a.at(i) >> b.at(i); } UnionFind uni(n); vector<ll> ans(m, 0ll); ans.at(m - 1) = (ll)n * (n - 1) / 2; for (int i = m - 1; i >= 1; i--) { ans[i - 1] = ans[i]; int as = uni.size(a[i]), bs = uni.size(b[i]); if (uni.connect(a[i], b[i])) { // cout << "here" << endl; ans[i - 1] -= (ll)as * bs; } } rp(i, m) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> #define rp(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; using P = pair<int, int>; class UnionFind { public: // 親の番号を格納する。親だった場合は-(その集合のサイズ) vector<int> Parent; // 作るときはParentの値を全て−1にする // こうすると全てバラバラになる UnionFind(int N) { Parent = vector<int>(N, -1); } // Aがどのグループに属しているか調べる int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } // 自分のいるグループの頂点数を調べる int size(int A) { return -Parent[root(A)]; // 親をとってきたい } // AとBをくっつける bool connect(int A, int B) { // AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); rp(i, m) { cin >> a.at(i) >> b.at(i); a.at(i)--; b.at(i)--; } UnionFind uni(n); vector<ll> ans(m, 0ll); ans.at(m - 1) = (ll)n * (n - 1) / 2; for (int i = m - 1; i >= 1; i--) { ans[i - 1] = ans[i]; int as = uni.size(a[i]), bs = uni.size(b[i]); if (uni.connect(a[i], b[i])) { // cout << "here" << endl; ans[i - 1] -= (ll)as * bs; } } rp(i, m) cout << ans[i] << endl; return 0; }
replace
47
48
47
52
0
p03108
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i < (n); i++) #define all(v) (v).begin(), (v).end() #define pb(a) push_back(a) using namespace std; typedef long long LL; #define int long long const LL INF = 999999999999; 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)]); } }; signed main() { LL n, m; cin >> n >> m; UnionFind UF(n); vector<LL> ans(m, 0); ans[m - 1] = n * (n - 1) / 2; vector<LL> a(m), b(m); rep(i, m) cin >> a[i] >> b[i]; for (int i = m - 1; i > 0; i--) { if (UF.find(a[i]) == UF.find(b[i])) { ans[i - 1] = ans[i]; continue; } LL tmp_a = UF.size(a[i]); tmp_a = tmp_a * (tmp_a - 1) / 2; LL tmp_b = UF.size(b[i]); tmp_b = tmp_b * (tmp_b - 1) / 2; UF.unite(a[i], b[i]); LL tmp_mg = UF.size(a[i]); tmp_mg = tmp_mg * (tmp_mg - 1) / 2; ans[i - 1] = max(0LL, ans[i] - (tmp_mg - tmp_a - tmp_b)); } rep(i, m) cout << ans[i] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i < (n); i++) #define all(v) (v).begin(), (v).end() #define pb(a) push_back(a) using namespace std; typedef long long LL; #define int long long const LL INF = 999999999999; 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)]); } }; signed main() { LL n, m; cin >> n >> m; UnionFind UF(n + 1); vector<LL> ans(m, 0); ans[m - 1] = n * (n - 1) / 2; vector<LL> a(m), b(m); rep(i, m) cin >> a[i] >> b[i]; for (int i = m - 1; i > 0; i--) { if (UF.find(a[i]) == UF.find(b[i])) { ans[i - 1] = ans[i]; continue; } LL tmp_a = UF.size(a[i]); tmp_a = tmp_a * (tmp_a - 1) / 2; LL tmp_b = UF.size(b[i]); tmp_b = tmp_b * (tmp_b - 1) / 2; UF.unite(a[i], b[i]); LL tmp_mg = UF.size(a[i]); tmp_mg = tmp_mg * (tmp_mg - 1) / 2; ans[i - 1] = max(0LL, ans[i] - (tmp_mg - tmp_a - tmp_b)); } rep(i, m) cout << ans[i] << endl; }
replace
39
40
39
40
0
p03108
Python
Runtime Error
#!/usr/bin/env python3 import sys try: from typing import List except ImportError: pass def solve(N: int, M: int, A: "List[int]", B: "List[int]"): uf = list(range(N)) n = [1] * N def getpar(a: int): if a == uf[a]: return a uf[a] = getpar(uf[a]) return uf[a] def union(a: int, b: int): pa = getpar(a) pb = getpar(b) if pa == pb: return 0, 0 ra = n[pa] rb = n[pb] n[pa] += n[pb] uf[pb] = uf[pa] return ra, rb k = N * (N - 1) // 2 ans = [] A.reverse() B.reverse() for Ai, Bi in zip(A, B): Ai -= 1 Bi -= 1 ra, rb = union(Ai, Bi) ans.append(k) k -= ra * rb assert k == 0, k for ansi in reversed(ans): print(ansi) def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int M = int(next(tokens)) # type: int A = [int()] * (M) # type: "List[int]" B = [int()] * (M) # type: "List[int]" for i in range(M): A[i] = int(next(tokens)) B[i] = int(next(tokens)) solve(N, M, A, B) if __name__ == "__main__": main()
#!/usr/bin/env python3 import sys try: from typing import List except ImportError: pass sys.setrecursionlimit(1000000) def solve(N: int, M: int, A: "List[int]", B: "List[int]"): uf = list(range(N)) n = [1] * N def getpar(a: int): if a == uf[a]: return a uf[a] = getpar(uf[a]) return uf[a] def union(a: int, b: int): pa = getpar(a) pb = getpar(b) if pa == pb: return 0, 0 ra = n[pa] rb = n[pb] n[pa] += n[pb] uf[pb] = uf[pa] return ra, rb k = N * (N - 1) // 2 ans = [] A.reverse() B.reverse() for Ai, Bi in zip(A, B): Ai -= 1 Bi -= 1 ra, rb = union(Ai, Bi) ans.append(k) k -= ra * rb assert k == 0, k for ansi in reversed(ans): print(ansi) def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int M = int(next(tokens)) # type: int A = [int()] * (M) # type: "List[int]" B = [int()] * (M) # type: "List[int]" for i in range(M): A[i] = int(next(tokens)) B[i] = int(next(tokens)) solve(N, M, A, B) if __name__ == "__main__": main()
insert
7
7
7
10
0
p03108
Python
Runtime Error
# -*- coding: utf-8 -*- from scipy.misc import comb import sys sys.setrecursionlimit(10000000000) class UnionFind: def __init__(self, n): self.parent = [i for i in range(n + 1)] self.depth = [1] * (n + 1) self.count = [1] * (n + 1) def find(self, x): if self.parent[x] == x: if self.depth[x] > 2: self.depth[x] = 2 return x else: self.parent[x] = self.find(self.parent[x]) self.depth[x], self.count[x] = 0, 0 return self.parent[x] def isSame(self, x, y): return self.find(x) == self.find(y) def union(self, x, y): x, y = self.find(x), self.find(y) if self.depth[x] < self.depth[y]: self.parent[x] = self.parent[y] self.count[y] += self.count[x] self.depth[x], self.count[x] = 0, 0 else: self.parent[y] = self.parent[x] self.count[x] += self.count[y] self.depth[y], self.count[y] = 0, 0 if self.depth[x] == self.depth[y]: self.depth[x] += 1 N, M = map(int, input().split()) A, B = [], [] for _ in range(M): a, b = map(int, input().split()) A.append(a) B.append(b) A.reverse() B.reverse() groups = UnionFind(N) ans = [comb(N, 2, exact=True)] for i in range(M): tmp = ans[-1] if not groups.isSame(A[i], B[i]): nA, nB = groups.count[groups.find(A[i])], groups.count[groups.find(B[i])] tmp -= nA * nB groups.union(A[i], B[i]) ans.append(tmp) for i in range(M): print(ans[-(i + 2)])
# -*- coding: utf-8 -*- from scipy.misc import comb import sys sys.setrecursionlimit(1000000000) class UnionFind: def __init__(self, n): self.parent = [i for i in range(n + 1)] self.depth = [1] * (n + 1) self.count = [1] * (n + 1) def find(self, x): if self.parent[x] == x: if self.depth[x] > 2: self.depth[x] = 2 return x else: self.parent[x] = self.find(self.parent[x]) self.depth[x], self.count[x] = 0, 0 return self.parent[x] def isSame(self, x, y): return self.find(x) == self.find(y) def union(self, x, y): x, y = self.find(x), self.find(y) if self.depth[x] < self.depth[y]: self.parent[x] = self.parent[y] self.count[y] += self.count[x] self.depth[x], self.count[x] = 0, 0 else: self.parent[y] = self.parent[x] self.count[x] += self.count[y] self.depth[y], self.count[y] = 0, 0 if self.depth[x] == self.depth[y]: self.depth[x] += 1 N, M = map(int, input().split()) A, B = [], [] for _ in range(M): a, b = map(int, input().split()) A.append(a) B.append(b) A.reverse() B.reverse() groups = UnionFind(N) ans = [comb(N, 2, exact=True)] for i in range(M): tmp = ans[-1] if not groups.isSame(A[i], B[i]): nA, nB = groups.count[groups.find(A[i])], groups.count[groups.find(B[i])] tmp -= nA * nB groups.union(A[i], B[i]) ans.append(tmp) for i in range(M): print(ans[-(i + 2)])
replace
5
6
5
6
ModuleNotFoundError: No module named 'scipy'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03108/Python/s305348500.py", line 3, in <module> from scipy.misc import comb ModuleNotFoundError: No module named 'scipy'
p03108
Python
Runtime Error
# -*- coding: utf-8 -*- from scipy.misc import comb import sys sys.setrecursionlimit(1000000000) class UnionFind: def __init__(self, n): # parent[x] < 0 means x is root and abs(parent[x]) == size[x] self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1) def find(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def isSame(self, x, y): return self.find(x) == self.find(y) def unite(self, x, y): x, y = self.find(x), self.find(y) if self.depth[x] > self.depth[y]: self.parent[x] += self.parent[y] self.parent[y] = x else: self.parent[y] += self.parent[x] self.parent[x] = y if self.depth[x] == self.depth[y]: self.depth[y] += 1 def count(self, x): return -self.parent[self.find(x)] N, M = map(int, input().split()) AB = [tuple(map(int, input().split())) for _ in range(M)] AB.reverse() groups = UnionFind(N) ans = [comb(N, 2, exact=True)] for i in range(M): if groups.isSame(AB[i][0], AB[i][1]): ans.append(ans) else: ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1])) groups.unite(AB[i][0], AB[i][1]) for i in range(M): print(ans[-(i + 2)])
# -*- coding: utf-8 -*- from scipy.misc import comb import sys sys.setrecursionlimit(1000000000) class UnionFind: def __init__(self, n): # parent[x] < 0 means x is root and abs(parent[x]) == size[x] self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1) def find(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def isSame(self, x, y): return self.find(x) == self.find(y) def unite(self, x, y): x, y = self.find(x), self.find(y) if self.depth[x] > self.depth[y]: self.parent[x] += self.parent[y] self.parent[y] = x else: self.parent[y] += self.parent[x] self.parent[x] = y if self.depth[x] == self.depth[y]: self.depth[y] += 1 def count(self, x): return -self.parent[self.find(x)] N, M = map(int, input().split()) AB = [tuple(map(int, input().split())) for _ in range(M)] AB.reverse() groups = UnionFind(N) ans = [comb(N, 2, exact=True)] for i in range(M): if groups.isSame(AB[i][0], AB[i][1]): ans.append(ans[-1]) else: ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1])) groups.unite(AB[i][0], AB[i][1]) for i in range(M): print(ans[-(i + 2)])
replace
48
49
48
49
ModuleNotFoundError: No module named 'scipy'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03108/Python/s430341119.py", line 3, in <module> from scipy.misc import comb ModuleNotFoundError: No module named 'scipy'
p03108
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define aut(r, v) __typeof(v) r = (v) #define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it) #define reach(it, o) for (aut(it, (o).rbegin()); it != (o).rend(); ++it) #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T1, typename T2> ostream &operator<<(ostream &o, const pair<T1, T2> &p) { return o << "(" << p.first << ", " << p.second << ")"; } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; rep(i, v.size()) s << del << v[i]; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) { o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } } return o; } template <typename T> ostream &operator<<(ostream &o, const set<T> &st) { vector<T> v(st.begin(), st.end()); o << "{ " << join(v) << " }"; return o; } template <typename T1, typename T2> ostream &operator<<(ostream &o, const map<T1, T2> &m) { each(p, m) { o << (p == m.begin() ? "{ " : ",\n ") << *p << (p == --m.end() ? " }" : ""); } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double INF = 0x3f3f3f3f; const double INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const double mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } struct UnionFind { vector<int> dat; UnionFind(int _size) : dat(_size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (dat[y] < dat[x]) swap(x, y); dat[x] += dat[y]; dat[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return dat[x] < 0 ? x : dat[x] = root(dat[x]); } int size(int x) { return -dat[root(x)]; } }; int a[10005], b[10005]; int n, m; int main() { ios_base::sync_with_stdio(0); // finput("./input"); cin >> n >> m; rep(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } vl answers; ll ans = n * (ll)(n - 1) / 2; UnionFind uf = UnionFind(n); rrep(i, m) { answers.push_back(ans); if (uf.findSet(a[i], b[i])) continue; ans -= uf.size(a[i]) * (ll)uf.size(b[i]); uf.unionSet(a[i], b[i]); } rrep(i, m) cout << answers[i] << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define aut(r, v) __typeof(v) r = (v) #define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it) #define reach(it, o) for (aut(it, (o).rbegin()); it != (o).rend(); ++it) #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T1, typename T2> ostream &operator<<(ostream &o, const pair<T1, T2> &p) { return o << "(" << p.first << ", " << p.second << ")"; } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; rep(i, v.size()) s << del << v[i]; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) { o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } } return o; } template <typename T> ostream &operator<<(ostream &o, const set<T> &st) { vector<T> v(st.begin(), st.end()); o << "{ " << join(v) << " }"; return o; } template <typename T1, typename T2> ostream &operator<<(ostream &o, const map<T1, T2> &m) { each(p, m) { o << (p == m.begin() ? "{ " : ",\n ") << *p << (p == --m.end() ? " }" : ""); } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double INF = 0x3f3f3f3f; const double INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const double mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } struct UnionFind { vector<int> dat; UnionFind(int _size) : dat(_size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (dat[y] < dat[x]) swap(x, y); dat[x] += dat[y]; dat[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return dat[x] < 0 ? x : dat[x] = root(dat[x]); } int size(int x) { return -dat[root(x)]; } }; int a[100005], b[100005]; int n, m; int main() { ios_base::sync_with_stdio(0); // finput("./input"); cin >> n >> m; rep(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } vl answers; ll ans = n * (ll)(n - 1) / 2; UnionFind uf = UnionFind(n); rrep(i, m) { answers.push_back(ans); if (uf.findSet(a[i], b[i])) continue; ans -= uf.size(a[i]) * (ll)uf.size(b[i]); uf.unionSet(a[i], b[i]); } rrep(i, m) cout << answers[i] << endl; return 0; }
replace
110
111
110
111
0
p03108
Python
Runtime Error
from collections import deque, Counter class UnionFind: def __init__(self, v): self.v = v self._tree = list(range(v + 1)) self._counter = Counter(self._tree) def _root(self, a): queue = deque() while self._tree[a] != a: queue.append(a) a = self._tree[a] while queue: index = queue.popleft() self._tree[index] = a return a def union(self, a, b): root_a = self._root(a) root_b = self._root(b) if root_a == root_b: return self._tree[root_b] = root_a ca = self._counter[root_a] cb = self._counter[root_b] self._counter[root_a] += cb self._counter[root_b] = 0 return ca * cb def find(self, a, b): return self._root(a) == self._root(b) N, M = map(int, input().split(" ")) edges = [tuple(map(int, input().split(" "))) for _ in range(M)] dp = [0] * (M + 1) dp[-1] = N * (N - 1) // 2 union_find = UnionFind(N) for i in range(M - 1, 0, -1): if dp[i + 1] == 0: break a, b = edges[i] minus = union_find.union(a, b) dp[i] = dp[i + 1] - minus for i in range(M): print(dp[i + 1])
from collections import deque, Counter class UnionFind: def __init__(self, v): self.v = v self._tree = list(range(v + 1)) self._counter = Counter(self._tree) def _root(self, a): queue = deque() while self._tree[a] != a: queue.append(a) a = self._tree[a] while queue: index = queue.popleft() self._tree[index] = a return a def union(self, a, b): root_a = self._root(a) root_b = self._root(b) if root_a == root_b: return 0 self._tree[root_b] = root_a ca = self._counter[root_a] cb = self._counter[root_b] self._counter[root_a] += cb self._counter[root_b] = 0 return ca * cb def find(self, a, b): return self._root(a) == self._root(b) N, M = map(int, input().split(" ")) edges = [tuple(map(int, input().split(" "))) for _ in range(M)] dp = [0] * (M + 1) dp[-1] = N * (N - 1) // 2 union_find = UnionFind(N) for i in range(M - 1, 0, -1): if dp[i + 1] == 0: break a, b = edges[i] minus = union_find.union(a, b) dp[i] = dp[i + 1] - minus for i in range(M): print(dp[i + 1])
replace
23
24
23
24
0