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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p03107 | Python | Runtime Error | s = input()
print(2 * min(s.count(0), s.count(1)))
| s = input()
print(2 * min(s.count("0"), s.count("1")))
| replace | 1 | 2 | 1 | 2 | TypeError: must be str, not int | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03107/Python/s119508612.py", line 2, in <module>
print(2 * min(s.count(0), s.count(1)))
TypeError: must be str, not int
|
p03107 | Python | Time Limit Exceeded | s = list(input())
n = len(s)
pos = 0
cnt = 0
while pos != n - 1 and n != 0:
if s[pos] != s[pos + 1]:
del s[pos]
del s[pos]
pos = 0
n -= 2
cnt += 2
else:
pos += 1
print(cnt)
| s = input()
a = s.count("0")
b = s.count("1")
print(min(a, b) * 2)
| replace | 0 | 16 | 0 | 4 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 7;
char str[10005];
int main() {
scanf("%s", str);
int len = strlen(str);
int l = 0, r = 0;
int s = 0;
for (int q = 0; q < len; q++) {
if (str[q] == '1') {
if (l > 0)
l--, s += 2;
else
r++;
} else {
if (r > 0)
r--, s += 2;
else
l++;
}
}
printf("%d\n", s);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 7;
char str[200000];
int main() {
scanf("%s", str);
int len = strlen(str);
int l = 0, r = 0;
int s = 0;
for (int q = 0; q < len; q++) {
if (str[q] == '1') {
if (l > 0)
l--, s += 2;
else
r++;
} else {
if (r > 0)
r--, s += 2;
else
l++;
}
}
printf("%d\n", s);
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p03107 | Python | Runtime Error | def solve(s):
count = 0
i = 0
while True:
if i >= len(s) - 1:
break
if s[i] != s[i + 1]:
count += 1
s = s[0:i] + s[i + 2 :]
i -= 1
continue
i += 1
return count
def main():
S = input()
# a, b, k = [int(a) for a in input().split()]
print(solve(S) * 2)
if __name__ == "__main__":
main()
| def solve(s):
one = 0
zero = 0
for i in range(len(s)):
if s[i] == "1":
one += 1
elif s[i] == "0":
zero += 1
return min(zero, one)
def main():
S = input()
# a, b, k = [int(a) for a in input().split()]
print(solve(S) * 2)
if __name__ == "__main__":
main()
| replace | 1 | 14 | 1 | 9 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int count0 = 0, count1 = 0;
for (int i = 0; i = s.size(); ++i) {
if (s[i] == '0')
++count0;
else
++count1;
}
int ans = 2 * min(count0, count1);
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int count0 = 0, count1 = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '0')
++count0;
else
++count1;
}
int ans = 2 * min(count0, count1);
cout << ans << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p03107 | Python | Time Limit Exceeded | S = input()
S_list = [int(S[i]) for i in range(len(S))]
ans = 0
rm_list = []
for aaa in range(len(S) // 2):
for i in range(len(S_list) - 1):
if S_list[i] != S_list[i + 1] and i not in rm_list:
rm_list.append(i)
rm_list.append(i + 1)
S_list = [S_list[i] for i in range(len(S_list)) if i not in rm_list]
if len(rm_list) == 0:
break
ans += len(rm_list)
rm_list = []
print(ans)
| S = input()
a = sum([int(S[i]) for i in range(len(S))])
b = len(S) - a
print(2 * min(a, b))
| replace | 1 | 15 | 1 | 4 | TLE | |
p03107 | Python | Time Limit Exceeded | import re
s = input()
pattern = "(01)|(10)"
count = 0
diff = 1
while diff:
ssub = re.sub(pattern, "", s)
diff = len(s) - len(ssub)
count += diff
s = ssub
print(count)
| import re
s = input()
zero = re.findall("0", s)
one = re.findall("1", s)
print(2 * min(len(zero), len(one)))
| replace | 3 | 15 | 3 | 6 | TLE | |
p03107 | Python | Time Limit Exceeded | #!/usr/bin/env python3
import sys
def solve(S: str):
nS = len(S)
pS = ""
while pS != S:
pS = S
S = S.replace("01", "")
S = S.replace("10", "")
print(nS - len(S))
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
S = next(tokens) # type: str
solve(S)
if __name__ == "__main__":
main()
| #!/usr/bin/env python3
import sys
def solve(S: str):
print(min(S.count("0"), S.count("1")) * 2)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
S = next(tokens) # type: str
solve(S)
if __name__ == "__main__":
main()
| replace | 5 | 12 | 5 | 6 | TLE | |
p03107 | Python | Time Limit Exceeded | S = input()
subS = S
cnt = 0
while True:
flg = True
if "01" in subS:
flg = False
subS = subS.replace("01", "", 1)
cnt += 2
if "10" in subS:
flg = False
subS = subS.replace("10", "", 1)
cnt += 2
if flg:
break
print(cnt)
| S = input()
zeroSum = 0
oneSum = 0
for _ in S:
if _ == "0":
zeroSum += 1
if _ == "1":
oneSum += 1
t = max(zeroSum, oneSum) - min(zeroSum, oneSum)
print(len(S) - t)
| replace | 1 | 16 | 1 | 12 | TLE | |
p03107 | Python | Time Limit Exceeded | #!/usr/bin/env python3
s = input()
n = len(s)
while True:
ns = s.replace("01", "").replace("10", "")
if ns == s:
break
s = ns
print(n - len(s))
| #!/usr/bin/env python3
s = input()
n = len(s)
c = 0
p = ""
count = 0
for i in s:
if i == p:
count += 1
else:
c = count - c
count = 1
p = i
c = count - c
print(n - abs(c))
| replace | 3 | 9 | 3 | 15 | TLE | |
p03107 | Python | Time Limit Exceeded | import re
S = input()
raw = len(S)
prev = len(S)
while True:
S = re.sub(r"(10)+", "", S)
S = re.sub(r"(01)+", "", S)
if prev > len(S):
prev = len(S)
else:
break
print(raw - len(S))
| import re
S = input()
num_1 = S.count("1")
num_0 = S.count("0")
print(min(num_0, num_1) * 2)
| replace | 3 | 13 | 3 | 6 | TLE | |
p03107 | Python | Time Limit Exceeded | s = input()
ans = 0
while "01" in s or "10" in s:
s_next = s.replace("01", "")
ans += len(s) - len(s_next)
s = s_next
s_next = s.replace("10", "")
ans += len(s) - len(s_next)
s = s_next
print(ans)
| s = input()
ans = 0
s_0 = s.replace("0", "")
n_0 = len(s) - len(s_0)
s_1 = s.replace("1", "")
n_1 = len(s) - len(s_1)
print(2 * min(n_0, n_1))
| replace | 3 | 11 | 3 | 10 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int main() {
string str;
cin >> str;
int start = str.size(), end;
bool f = true;
while (f) {
f = false;
for (int i = 0; i < str.size() - 3; ++i) {
if (str.size() > 3) {
if (str[i] != str[i + 3] && str[i + 1] != str[i + 2]) {
str.erase(i + 1, 2);
--i;
f = true;
}
} else
break;
}
}
for (int i = 0; i < str.size() - 1; ++i) {
if (str.size() > 1) {
if (str[i] != str[i + 1]) {
str.erase(i, 2);
--i;
}
} else
break;
}
end = str.size();
cout << start - end << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int main() {
string str;
cin >> str;
int start = str.size(), end;
bool f = true;
for (int i = 0; i < str.size() - 3; ++i) {
if (str.size() > 3) {
if (str[i] != str[i + 3] && str[i + 1] != str[i + 2]) {
str.erase(i + 1, 2);
i = max(0, i - 3);
}
} else
break;
}
for (int i = 0; i < str.size() - 3; ++i) {
if (str.size() > 3) {
if (str[i] != str[i + 3] && str[i + 1] != str[i + 2]) {
str.erase(i + 1, 2);
i = max(0, i - 3);
}
} else
break;
}
for (int i = 0; i < str.size() - 1; ++i) {
if (str.size() > 1) {
if (str[i] != str[i + 1]) {
str.erase(i, 2);
--i;
}
} else
break;
}
end = str.size();
cout << start - end << endl;
return 0;
} | replace | 16 | 29 | 16 | 33 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
using M = map<int, int>;
int main() {
string s, t = "";
cin >> s;
int b;
int ans = 0;
do {
t = "";
b = 0;
rep(i, s.size()) {
if (i + 1 < s.size() && s[i] != s[i + 1]) {
ans += 2;
i++;
b = 1;
} else {
t += s[i];
}
}
s = t;
} while (b);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
using M = map<int, int>;
int main() {
string s, t = "";
cin >> s;
int a = 0, b = 0;
rep(i, s.size()) {
if (s[i] == '0')
a++;
else
b++;
}
cout << min(a, b) * 2 << endl;
return 0;
}
| replace | 11 | 28 | 11 | 19 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define pq priority_queue
#define mp make_pair
#define cauto const auto &
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define clr(ar, val) memset(ar, val, sizeof(ar))
typedef long long int64;
typedef long long lint;
typedef long long lli;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pint;
typedef pair<long, long> plong;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
const int iinf = 1 << 29;
const long long linf = 1ll << 61;
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 <class T = int> T in() {
T x;
cin >> x;
return (x);
}
template <class T> void print(T &x) {
cout << x << '\n';
return;
}
template <class T> ll sized_subset(T &comb) {
T x = comb & -comb, y = x + comb;
return ((comb & ~y) / x) >> 1 | y;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, a, b, c;
int l[10];
bool used[10];
ll ans = linf;
ll gousei;
int main() {
char c[100000];
list<char> s;
cin >> c;
for (int i = 0; c[i] != 0; i++) {
s.push_back(c[i]);
}
int ans = 0;
for (auto ite = s.begin(); ite != s.end(); ite++) {
auto itebuf = ite;
if (*ite != *(++itebuf) && itebuf != s.end()) {
ite = s.erase(ite);
ite = s.erase(ite);
ans += 2;
ite = s.begin();
ite--;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pq priority_queue
#define mp make_pair
#define cauto const auto &
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define clr(ar, val) memset(ar, val, sizeof(ar))
typedef long long int64;
typedef long long lint;
typedef long long lli;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pint;
typedef pair<long, long> plong;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
const int iinf = 1 << 29;
const long long linf = 1ll << 61;
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 <class T = int> T in() {
T x;
cin >> x;
return (x);
}
template <class T> void print(T &x) {
cout << x << '\n';
return;
}
template <class T> ll sized_subset(T &comb) {
T x = comb & -comb, y = x + comb;
return ((comb & ~y) / x) >> 1 | y;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, a, b, c;
int l[10];
bool used[10];
ll ans = linf;
ll gousei;
int main() {
char c[100000];
list<char> s;
cin >> c;
for (int i = 0; c[i] != 0; i++) {
s.push_back(c[i]);
}
int ans = 0;
for (auto ite = s.begin(); ite != s.end(); ite++) {
auto itebuf = ite;
if (*ite != *(++itebuf) && itebuf != s.end()) {
ite = s.erase(ite);
ite = s.erase(ite);
ans += 2;
if (ite == s.begin())
ite--;
else {
ite--;
ite--;
}
}
}
cout << ans << endl;
return 0;
}
| replace | 82 | 84 | 82 | 88 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
typedef pair<ll, ll> P;
#define bit(n) (1LL << (n))
// #define int long long
#define all(v) v.begin(), v.end()
#define sortAl(v) sort(all(v))
#define sortAlr(v) \
sort(v.begin(), v.end()); \
reverse(v.begin(), v.end())
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }
#define mod (ll)(1e9 + 7)
#define INF LLONG_MAX
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
string s;
cin >> s;
ll n = s.size();
while (1) {
ll prev = s.size();
regex e("(01)|(10)");
s = regex_replace(s, e, "");
if (s.size() == prev) {
break;
}
}
cout << n - s.size() << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
typedef pair<ll, ll> P;
#define bit(n) (1LL << (n))
// #define int long long
#define all(v) v.begin(), v.end()
#define sortAl(v) sort(all(v))
#define sortAlr(v) \
sort(v.begin(), v.end()); \
reverse(v.begin(), v.end())
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }
#define mod (ll)(1e9 + 7)
#define INF LLONG_MAX
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
string s;
cin >> s;
ll n = s.size();
for (int i = 1; i < n; i += 2) {
if (s[i] == '0') {
s[i] = '1';
} else {
s[i] = '0';
}
}
REP(i, n) {
if (s[i - 1] == s[i]) {
s.erase(s.begin() + i - 1, s.begin() + i + 1);
i -= 2;
if (i < 0) {
i = 0;
}
}
}
cout << n - s.size() << endl;
return 0;
}
| replace | 48 | 54 | 48 | 63 | TLE | |
p03107 | C++ | Runtime Error | /*
Task:
Author: John_05
State: Unknown
Lang: C++
Solution:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 2e9 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
int read() {
int xx = 0, ww = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-')
ww = -ww;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
xx = 10 * xx + ch - 48;
ch = getchar();
}
return xx * ww;
}
string str;
char s[10010];
int l, ans;
int main() {
scanf("%s", s);
l = strlen(s);
str = string(s);
for (int i = 0; i < l; i++)
if (s[i] == '0')
ans++;
printf("%d\n", 2 * min(ans, l - ans));
return 0;
} | /*
Task:
Author: John_05
State: Unknown
Lang: C++
Solution:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 2e9 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
int read() {
int xx = 0, ww = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-')
ww = -ww;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
xx = 10 * xx + ch - 48;
ch = getchar();
}
return xx * ww;
}
string str;
char s[100020];
int l, ans;
int main() {
scanf("%s", s);
l = strlen(s);
str = string(s);
for (int i = 0; i < l; i++)
if (s[i] == '0')
ans++;
printf("%d\n", 2 * min(ans, l - ans));
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> P;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
ll gcd(ll a, ll b) {
while (a && b) {
if (a >= b)
a %= b;
else
b %= a;
}
return a + b;
}
ll ketasum(ll n) { // 桁の数字全ての和を返す 例: 112 -> 4
ll sum = 0;
if (n < 0)
return 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
typedef pair<ll, ll> pair_t;
bool comp(const pair_t &a, const pair_t &b) { return a.first < b.first; }
void sort2vectors(
vector<ll> &av,
vector<ll> &bv) { // ふたつの配列をまとめてそーと A[2,1] B[3,4] -> 1,4 2,3
ll n = av.size();
vector<pair_t> p(n);
vector<ll> av2(n), bv2(n);
for (ll i = 0; i < n; i++)
p[i] = make_pair(av[i], bv[i]);
sort(p.begin(), p.end(), comp);
for (ll i = 0; i < n; i++) {
av2[i] = p[i].first;
bv2[i] = p[i].second;
}
av = av2;
bv = bv2;
}
ll facctorialMethod(ll k) { // 階乗
ll s;
if (k > 1) {
s = k * facctorialMethod(k - 1);
} else {
s = 1;
}
return s;
}
map<int64_t, ll> prime_factor(int64_t n) {
map<int64_t, ll> ret;
for (int64_t i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
// dp[i] := 長さが i の増加部分列として最後尾の要素のとりうる最小値
template <class T> int LIS(vector<T> a, bool is_strong = true) {
const T INF = 1 << 30; // to be set appropriately
int n = (int)a.size();
vector<T> dp(n, INF);
for (int i = 0; i < n; ++i) {
if (is_strong)
*lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
else
*upper_bound(dp.begin(), dp.end(), a[i]) = a[i];
}
return lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
void foreach_permutation(ll n, std::function<void(ll *)> f) {
ll indexes[n];
for (ll i = 0; i < n; i++)
indexes[i] = i;
do {
f(indexes);
} while (std::next_permutation(indexes, indexes + n));
}
long double dist(ll x, ll y, ll x2, ll y2) {
return sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
}
ll GetDigit(ll num) {
ll digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
const ll MOD = 1000000007;
const ll INF = 1e15;
const int MAX = 10000000;
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;
}
// Sparse Table
template <class MeetSemiLattice> struct SparseTable {
vector<vector<MeetSemiLattice>> dat;
vector<int> height;
SparseTable() {}
SparseTable(const vector<MeetSemiLattice> &vec) { init(vec); }
void init(const vector<MeetSemiLattice> &vec) {
int n = (int)vec.size(), h = 0;
while ((1 << h) < n)
++h;
dat.assign(h, vector<MeetSemiLattice>(1 << h));
height.assign(n + 1, 0);
for (int i = 2; i <= n; i++)
height[i] = height[i >> 1] + 1;
for (int i = 0; i < n; ++i)
dat[0][i] = vec[i];
for (int i = 1; i < h; ++i)
for (int j = 0; j < n; ++j)
dat[i][j] =
min(dat[i - 1][j], dat[i - 1][min(j + (1 << (i - 1)), n - 1)]);
}
MeetSemiLattice get(int a, int b) {
return min(dat[height[b - a]][a],
dat[height[b - a]][b - (1 << height[b - a])]);
}
};
// Suffix Array ( Manber&Myers: O(n (logn)^2) )
struct SuffixArray {
string str;
vector<int> sa; // sa[i] : the starting index of the i-th smallest suffix (i =
// 0, 1, ..., n)
vector<int> lcp; // lcp[i]: the lcp of sa[i] and sa[i+1] (i = 0, 1, ..., n-1)
inline int &operator[](int i) { return sa[i]; }
SuffixArray(const string &str_) : str(str_) {
buildSA();
calcLCP();
}
void init(const string &str_) {
str = str_;
buildSA();
calcLCP();
}
// build SA
vector<int> rank_sa, tmp_rank_sa;
struct CompareSA {
int n, k;
const vector<int> &rank;
CompareSA(int n, int k, const vector<int> &rank_sa)
: n(n), k(k), rank(rank_sa) {}
bool operator()(int i, int j) {
if (rank[i] != rank[j])
return (rank[i] < rank[j]);
else {
int rank_ik = (i + k <= n ? rank[i + k] : -1);
int rank_jk = (j + k <= n ? rank[j + k] : -1);
return (rank_ik < rank_jk);
}
}
};
void buildSA() {
int n = (int)str.size();
sa.resize(n + 1), lcp.resize(n + 1), rank_sa.resize(n + 1),
tmp_rank_sa.resize(n + 1);
for (int i = 0; i < n; ++i)
sa[i] = i, rank_sa[i] = (int)str[i];
sa[n] = n, rank_sa[n] = -1;
for (int k = 1; k <= n; k *= 2) {
CompareSA csa(n, k, rank_sa);
sort(sa.begin(), sa.end(), csa);
tmp_rank_sa[sa[0]] = 0;
for (int i = 1; i <= n; ++i) {
tmp_rank_sa[sa[i]] = tmp_rank_sa[sa[i - 1]];
if (csa(sa[i - 1], sa[i]))
++tmp_rank_sa[sa[i]];
}
for (int i = 0; i <= n; ++i)
rank_sa[i] = tmp_rank_sa[i];
}
}
vector<int> rsa;
SparseTable<int> st;
void calcLCP() {
int n = (int)str.size();
rsa.resize(n + 1);
for (int i = 0; i <= n; ++i)
rsa[sa[i]] = i;
lcp.resize(n + 1);
lcp[0] = 0;
int cur = 0;
for (int i = 0; i < n; ++i) {
int pi = sa[rsa[i] - 1];
if (cur > 0)
--cur;
for (; pi + cur < n && i + cur < n; ++cur) {
if (str[pi + cur] != str[i + cur])
break;
}
lcp[rsa[i] - 1] = cur;
}
st.init(lcp);
}
// calc lcp
int getLCP(int a, int b) { // lcp of str.sutstr(a) and str.substr(b)
return st.get(min(rsa[a], rsa[b]), max(rsa[a], rsa[b]));
}
};
template <unsigned mod> struct RollingHash {
vector<unsigned> hashed, power;
inline unsigned mul(unsigned a, unsigned b) const {
unsigned long long x = (unsigned long long)a * b;
unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod));
return m;
}
RollingHash(const string &s, unsigned base = 10007) {
int sz = (int)s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if (hashed[i + 1] >= mod)
hashed[i + 1] -= mod;
}
}
unsigned get(int l, int r) const {
unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if (ret >= mod)
ret -= mod;
return ret;
}
unsigned connect(unsigned h1, int h2, int h2len) const {
unsigned ret = mul(h1, power[h2len]) + h2;
if (ret >= mod)
ret -= mod;
return ret;
}
int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
int len = min(r1 - l1, r2 - l2);
int low = -1, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
low = mid;
else
high = mid;
}
return (low);
}
};
using RH = RollingHash<1000000007>;
void UFinit(ll n);
ll UFfind(ll x);
void UFunite(ll x, ll y);
bool UFsame(ll x, ll y);
ll MAX_N; // Union-Find木使う時はcinする
const double PI = 3.14159265358979;
/******変数定義start(vectorはresizeしようね)******/
string S;
/******変数定義end********************************/
void solve() {
cin >> S;
ll ans = 0;
for (ll i = 0; i < S.size() - 1 && S.size() != 0; i++) {
if (S.substr(i, 1) == "0" && S.substr(i + 1, 1) == "1") {
ans += 2;
S.erase(i, 2);
i = -1;
} else if (S.substr(i, 1) == "1" && S.substr(i + 1, 1) == "0") {
ans += 2;
S.erase(i, 2);
i = -1;
}
}
cout << ans << endl;
}
vl UFpar(MAX_N);
vl UFrank(MAX_N);
void UFinit(ll n) {
REP(i, n) {
UFpar[i] = i;
UFrank[i] = 0;
}
}
ll UFfind(ll x) {
if (UFpar[x] == x) {
return x;
} else {
return UFpar[x] = UFfind(UFpar[x]);
}
}
void UFunite(ll x, ll y) {
x = UFfind(x);
y = UFfind(y);
if (x == y) {
return;
}
if (UFrank[x] < UFrank[y]) {
UFpar[x] = y;
} else {
UFpar[y] = x;
if (UFrank[x] == UFrank[y]) {
UFrank[x]++;
}
}
}
bool UFsame(ll x, ll y) { return UFfind(x) == UFfind(y); }
int main() {
// clock_t start = clock();
cout << std::fixed << std::setprecision(20);
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
// clock_t end = clock();
// const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
// 1000.0; printf("time %lf[ms]\n", time);
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> P;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
ll gcd(ll a, ll b) {
while (a && b) {
if (a >= b)
a %= b;
else
b %= a;
}
return a + b;
}
ll ketasum(ll n) { // 桁の数字全ての和を返す 例: 112 -> 4
ll sum = 0;
if (n < 0)
return 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
typedef pair<ll, ll> pair_t;
bool comp(const pair_t &a, const pair_t &b) { return a.first < b.first; }
void sort2vectors(
vector<ll> &av,
vector<ll> &bv) { // ふたつの配列をまとめてそーと A[2,1] B[3,4] -> 1,4 2,3
ll n = av.size();
vector<pair_t> p(n);
vector<ll> av2(n), bv2(n);
for (ll i = 0; i < n; i++)
p[i] = make_pair(av[i], bv[i]);
sort(p.begin(), p.end(), comp);
for (ll i = 0; i < n; i++) {
av2[i] = p[i].first;
bv2[i] = p[i].second;
}
av = av2;
bv = bv2;
}
ll facctorialMethod(ll k) { // 階乗
ll s;
if (k > 1) {
s = k * facctorialMethod(k - 1);
} else {
s = 1;
}
return s;
}
map<int64_t, ll> prime_factor(int64_t n) {
map<int64_t, ll> ret;
for (int64_t i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
// dp[i] := 長さが i の増加部分列として最後尾の要素のとりうる最小値
template <class T> int LIS(vector<T> a, bool is_strong = true) {
const T INF = 1 << 30; // to be set appropriately
int n = (int)a.size();
vector<T> dp(n, INF);
for (int i = 0; i < n; ++i) {
if (is_strong)
*lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
else
*upper_bound(dp.begin(), dp.end(), a[i]) = a[i];
}
return lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
void foreach_permutation(ll n, std::function<void(ll *)> f) {
ll indexes[n];
for (ll i = 0; i < n; i++)
indexes[i] = i;
do {
f(indexes);
} while (std::next_permutation(indexes, indexes + n));
}
long double dist(ll x, ll y, ll x2, ll y2) {
return sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
}
ll GetDigit(ll num) {
ll digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
const ll MOD = 1000000007;
const ll INF = 1e15;
const int MAX = 10000000;
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;
}
// Sparse Table
template <class MeetSemiLattice> struct SparseTable {
vector<vector<MeetSemiLattice>> dat;
vector<int> height;
SparseTable() {}
SparseTable(const vector<MeetSemiLattice> &vec) { init(vec); }
void init(const vector<MeetSemiLattice> &vec) {
int n = (int)vec.size(), h = 0;
while ((1 << h) < n)
++h;
dat.assign(h, vector<MeetSemiLattice>(1 << h));
height.assign(n + 1, 0);
for (int i = 2; i <= n; i++)
height[i] = height[i >> 1] + 1;
for (int i = 0; i < n; ++i)
dat[0][i] = vec[i];
for (int i = 1; i < h; ++i)
for (int j = 0; j < n; ++j)
dat[i][j] =
min(dat[i - 1][j], dat[i - 1][min(j + (1 << (i - 1)), n - 1)]);
}
MeetSemiLattice get(int a, int b) {
return min(dat[height[b - a]][a],
dat[height[b - a]][b - (1 << height[b - a])]);
}
};
// Suffix Array ( Manber&Myers: O(n (logn)^2) )
struct SuffixArray {
string str;
vector<int> sa; // sa[i] : the starting index of the i-th smallest suffix (i =
// 0, 1, ..., n)
vector<int> lcp; // lcp[i]: the lcp of sa[i] and sa[i+1] (i = 0, 1, ..., n-1)
inline int &operator[](int i) { return sa[i]; }
SuffixArray(const string &str_) : str(str_) {
buildSA();
calcLCP();
}
void init(const string &str_) {
str = str_;
buildSA();
calcLCP();
}
// build SA
vector<int> rank_sa, tmp_rank_sa;
struct CompareSA {
int n, k;
const vector<int> &rank;
CompareSA(int n, int k, const vector<int> &rank_sa)
: n(n), k(k), rank(rank_sa) {}
bool operator()(int i, int j) {
if (rank[i] != rank[j])
return (rank[i] < rank[j]);
else {
int rank_ik = (i + k <= n ? rank[i + k] : -1);
int rank_jk = (j + k <= n ? rank[j + k] : -1);
return (rank_ik < rank_jk);
}
}
};
void buildSA() {
int n = (int)str.size();
sa.resize(n + 1), lcp.resize(n + 1), rank_sa.resize(n + 1),
tmp_rank_sa.resize(n + 1);
for (int i = 0; i < n; ++i)
sa[i] = i, rank_sa[i] = (int)str[i];
sa[n] = n, rank_sa[n] = -1;
for (int k = 1; k <= n; k *= 2) {
CompareSA csa(n, k, rank_sa);
sort(sa.begin(), sa.end(), csa);
tmp_rank_sa[sa[0]] = 0;
for (int i = 1; i <= n; ++i) {
tmp_rank_sa[sa[i]] = tmp_rank_sa[sa[i - 1]];
if (csa(sa[i - 1], sa[i]))
++tmp_rank_sa[sa[i]];
}
for (int i = 0; i <= n; ++i)
rank_sa[i] = tmp_rank_sa[i];
}
}
vector<int> rsa;
SparseTable<int> st;
void calcLCP() {
int n = (int)str.size();
rsa.resize(n + 1);
for (int i = 0; i <= n; ++i)
rsa[sa[i]] = i;
lcp.resize(n + 1);
lcp[0] = 0;
int cur = 0;
for (int i = 0; i < n; ++i) {
int pi = sa[rsa[i] - 1];
if (cur > 0)
--cur;
for (; pi + cur < n && i + cur < n; ++cur) {
if (str[pi + cur] != str[i + cur])
break;
}
lcp[rsa[i] - 1] = cur;
}
st.init(lcp);
}
// calc lcp
int getLCP(int a, int b) { // lcp of str.sutstr(a) and str.substr(b)
return st.get(min(rsa[a], rsa[b]), max(rsa[a], rsa[b]));
}
};
template <unsigned mod> struct RollingHash {
vector<unsigned> hashed, power;
inline unsigned mul(unsigned a, unsigned b) const {
unsigned long long x = (unsigned long long)a * b;
unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod));
return m;
}
RollingHash(const string &s, unsigned base = 10007) {
int sz = (int)s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if (hashed[i + 1] >= mod)
hashed[i + 1] -= mod;
}
}
unsigned get(int l, int r) const {
unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if (ret >= mod)
ret -= mod;
return ret;
}
unsigned connect(unsigned h1, int h2, int h2len) const {
unsigned ret = mul(h1, power[h2len]) + h2;
if (ret >= mod)
ret -= mod;
return ret;
}
int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
int len = min(r1 - l1, r2 - l2);
int low = -1, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
low = mid;
else
high = mid;
}
return (low);
}
};
using RH = RollingHash<1000000007>;
void UFinit(ll n);
ll UFfind(ll x);
void UFunite(ll x, ll y);
bool UFsame(ll x, ll y);
ll MAX_N; // Union-Find木使う時はcinする
const double PI = 3.14159265358979;
/******変数定義start(vectorはresizeしようね)******/
string S;
/******変数定義end********************************/
void solve() {
cin >> S;
ll C0 = 0;
ll C1 = 0;
REP(i, S.size()) {
if (S.substr(i, 1) == "0") {
C0++;
} else {
C1++;
}
}
cout << 2 * min(C0, C1) << endl;
}
vl UFpar(MAX_N);
vl UFrank(MAX_N);
void UFinit(ll n) {
REP(i, n) {
UFpar[i] = i;
UFrank[i] = 0;
}
}
ll UFfind(ll x) {
if (UFpar[x] == x) {
return x;
} else {
return UFpar[x] = UFfind(UFpar[x]);
}
}
void UFunite(ll x, ll y) {
x = UFfind(x);
y = UFfind(y);
if (x == y) {
return;
}
if (UFrank[x] < UFrank[y]) {
UFpar[x] = y;
} else {
UFpar[y] = x;
if (UFrank[x] == UFrank[y]) {
UFrank[x]++;
}
}
}
bool UFsame(ll x, ll y) { return UFfind(x) == UFfind(y); }
int main() {
// clock_t start = clock();
cout << std::fixed << std::setprecision(20);
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
// clock_t end = clock();
// const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
// 1000.0; printf("time %lf[ms]\n", time);
} | replace | 342 | 355 | 342 | 352 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
struct Edge {
long long to;
long long cost;
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, ll>;
const long long INF = 1LL << 60;
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < n; i++)
bool is_prime(ll x) {
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
long long modpow(long long a, long long n, long long m) {
long long ans = 1;
while (n) {
if (n & 1) {
ans = (ans * a) % m;
}
a = (a * a) % m;
n >>= 1;
}
return ans;
}
long long combi(long long n, long long a) {
long long ans = 1, ans1 = 1;
for (long long i = n - a + 1; i <= n; i++) {
ans *= i % MOD;
ans %= MOD;
}
for (long long i = 2; i <= a; i++)
ans1 = (ans1 * i) % MOD;
ans1 = modpow(ans1, MOD - 2, MOD);
return ((ans % MOD) * ans1) % MOD;
}
template <typename T>
bool next_combination(const T first, const T last, int k) {
const T subset = first + k;
// empty container | k = 0 | k == n
if (first == last || first == subset || last == subset) {
return false;
}
T src = subset;
while (first != src) {
src--;
if (*src < *(last - 1)) {
T dest = subset;
while (*src >= *dest) {
dest++;
}
iter_swap(src, dest);
rotate(src + 1, dest + 1, last);
rotate(subset, subset + (last - dest) - 1, last);
return true;
}
}
// restore
rotate(first, subset, last);
return false;
}
void dfs(vector<ll> s, ll mi, ll mx, ll N, vector<vector<ll>> &arr) {
if (s.size() == (size_t)N) {
// cout << s.c_str() << endl;
arr.push_back(s);
} else {
for (ll c = s.size() > 0 ? s[s.size() - 1] : mi; c <= mx; c++) {
s.push_back(c);
dfs(s, mi, mx, N, arr);
s.pop_back();
}
}
return;
}
int bfs(int sx, int sy, int gx, int gy, int h, int w,
vector<vector<char>> map) {
queue<pair<int, int>> s;
vector<vector<ll>> ans;
for (int i = 0; i < h; i++) {
vector<ll> aa(w);
ans.push_back(aa);
}
s.push(make_pair(sx, sy));
while (s.size() > 0) {
pair<int, int> tmp = s.front();
s.pop();
map[tmp.first][tmp.second] = '#';
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (tmp.first + i < 0 || tmp.first + i >= h) {
continue;
}
if (tmp.second + j < 0 || tmp.second + j >= w) {
continue;
}
if (i != 0 && j != 0) {
continue;
}
if (i == 0 && j == 0) {
continue;
}
if (map[tmp.first + i][tmp.second + j] == '#') {
continue;
}
map[tmp.first + i][tmp.second + j] = '#';
if (ans[tmp.first + i][tmp.second + j] == 0) {
ans[tmp.first + i][tmp.second + j] = ans[tmp.first][tmp.second] + 1;
} else {
ans[tmp.first + i][tmp.second + j] =
min(ans[tmp.first + i][tmp.second + j],
ans[tmp.first][tmp.second] + 1);
}
s.push(make_pair(tmp.first + i, tmp.second + j));
}
}
}
return ans[gy][gx];
}
ll modfactorial(ll a) {
if (a == 1)
return 1;
return (a % MOD) * (modfactorial(a - 1) % MOD);
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
map<ll, ll> prime_factor(ll n) {
map<ll, ll> ret;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
/* dijkstra(G,s,dis)
入力:グラフ G, 開始点 s, 距離を格納する dis
計算量:O(|E|log|V|)
副作用:dis が書き換えられる
*/
void dijkstra(const Graph &G, int s, vector<ll> &dis, vector<ll> &prev) {
int N = G.size();
dis.resize(N, INF);
prev.resize(N, -1); // 初期化
priority_queue<P, vector<P>, greater<P>> pq;
dis[s] = 0;
pq.emplace(dis[s], s);
while (!pq.empty()) {
P p = pq.top();
pq.pop();
int v = p.second;
if (dis[v] < p.first) {
continue;
}
for (auto &e : G[v]) {
if (dis[e.to] > dis[v] + e.cost) {
dis[e.to] = dis[v] + e.cost;
prev[e.to] = v; // 頂点 v を通って e.to にたどり着いた
pq.emplace(dis[e.to], e.to);
}
}
}
}
vector<ll> get_path(const vector<ll> &prev, ll t) {
vector<ll> path;
for (ll cur = t; cur != -1; cur = prev[cur]) {
path.push_back(cur);
}
reverse(path.begin(), path.end()); // 逆順なのでひっくり返す
return path;
}
vector<string> split(string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
return elems;
}
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
int main() {
string s;
cin >> s;
ll n = s.size();
ll ans = 0;
while (1) {
string tmp = "";
bool f = false;
ll i = 0;
while (s.size() > 1) {
if (s[i] == '0' && s[i + 1] == '1') {
f = true;
auto t = s.substr(0, i);
tmp = s.substr(0, i) + s.substr(i + 2);
break;
} else if (s[i] == '1' && s[i + 1] == '0') {
f = true;
tmp = s.substr(0, i) + s.substr(i + 2);
break;
}
i++;
if (i == s.size() - 1)
break;
}
if (!f) {
break;
}
s = tmp;
}
cout << n - s.size() << endl;
return 0;
}
// cout << std::fixed << std::setprecision(15)
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
struct Edge {
long long to;
long long cost;
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, ll>;
const long long INF = 1LL << 60;
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < n; i++)
bool is_prime(ll x) {
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
long long modpow(long long a, long long n, long long m) {
long long ans = 1;
while (n) {
if (n & 1) {
ans = (ans * a) % m;
}
a = (a * a) % m;
n >>= 1;
}
return ans;
}
long long combi(long long n, long long a) {
long long ans = 1, ans1 = 1;
for (long long i = n - a + 1; i <= n; i++) {
ans *= i % MOD;
ans %= MOD;
}
for (long long i = 2; i <= a; i++)
ans1 = (ans1 * i) % MOD;
ans1 = modpow(ans1, MOD - 2, MOD);
return ((ans % MOD) * ans1) % MOD;
}
template <typename T>
bool next_combination(const T first, const T last, int k) {
const T subset = first + k;
// empty container | k = 0 | k == n
if (first == last || first == subset || last == subset) {
return false;
}
T src = subset;
while (first != src) {
src--;
if (*src < *(last - 1)) {
T dest = subset;
while (*src >= *dest) {
dest++;
}
iter_swap(src, dest);
rotate(src + 1, dest + 1, last);
rotate(subset, subset + (last - dest) - 1, last);
return true;
}
}
// restore
rotate(first, subset, last);
return false;
}
void dfs(vector<ll> s, ll mi, ll mx, ll N, vector<vector<ll>> &arr) {
if (s.size() == (size_t)N) {
// cout << s.c_str() << endl;
arr.push_back(s);
} else {
for (ll c = s.size() > 0 ? s[s.size() - 1] : mi; c <= mx; c++) {
s.push_back(c);
dfs(s, mi, mx, N, arr);
s.pop_back();
}
}
return;
}
int bfs(int sx, int sy, int gx, int gy, int h, int w,
vector<vector<char>> map) {
queue<pair<int, int>> s;
vector<vector<ll>> ans;
for (int i = 0; i < h; i++) {
vector<ll> aa(w);
ans.push_back(aa);
}
s.push(make_pair(sx, sy));
while (s.size() > 0) {
pair<int, int> tmp = s.front();
s.pop();
map[tmp.first][tmp.second] = '#';
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (tmp.first + i < 0 || tmp.first + i >= h) {
continue;
}
if (tmp.second + j < 0 || tmp.second + j >= w) {
continue;
}
if (i != 0 && j != 0) {
continue;
}
if (i == 0 && j == 0) {
continue;
}
if (map[tmp.first + i][tmp.second + j] == '#') {
continue;
}
map[tmp.first + i][tmp.second + j] = '#';
if (ans[tmp.first + i][tmp.second + j] == 0) {
ans[tmp.first + i][tmp.second + j] = ans[tmp.first][tmp.second] + 1;
} else {
ans[tmp.first + i][tmp.second + j] =
min(ans[tmp.first + i][tmp.second + j],
ans[tmp.first][tmp.second] + 1);
}
s.push(make_pair(tmp.first + i, tmp.second + j));
}
}
}
return ans[gy][gx];
}
ll modfactorial(ll a) {
if (a == 1)
return 1;
return (a % MOD) * (modfactorial(a - 1) % MOD);
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
map<ll, ll> prime_factor(ll n) {
map<ll, ll> ret;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
/* dijkstra(G,s,dis)
入力:グラフ G, 開始点 s, 距離を格納する dis
計算量:O(|E|log|V|)
副作用:dis が書き換えられる
*/
void dijkstra(const Graph &G, int s, vector<ll> &dis, vector<ll> &prev) {
int N = G.size();
dis.resize(N, INF);
prev.resize(N, -1); // 初期化
priority_queue<P, vector<P>, greater<P>> pq;
dis[s] = 0;
pq.emplace(dis[s], s);
while (!pq.empty()) {
P p = pq.top();
pq.pop();
int v = p.second;
if (dis[v] < p.first) {
continue;
}
for (auto &e : G[v]) {
if (dis[e.to] > dis[v] + e.cost) {
dis[e.to] = dis[v] + e.cost;
prev[e.to] = v; // 頂点 v を通って e.to にたどり着いた
pq.emplace(dis[e.to], e.to);
}
}
}
}
vector<ll> get_path(const vector<ll> &prev, ll t) {
vector<ll> path;
for (ll cur = t; cur != -1; cur = prev[cur]) {
path.push_back(cur);
}
reverse(path.begin(), path.end()); // 逆順なのでひっくり返す
return path;
}
vector<string> split(string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
return elems;
}
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
int main() {
string s;
cin >> s;
map<ll, ll> m;
rep(i, s.size()) { m[s[i]]++; }
cout << 2 * min(m['1'], m['0']) << endl;
return 0;
}
// cout << std::fixed << std::setprecision(15)
| replace | 236 | 266 | 236 | 242 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("mmx,avx,fma")
// #pragma GCC optimize ("unroll-loops")
using namespace __gnu_pbds;
using namespace std;
template <class T> istream &operator>>(istream &in, vector<T> &v) {
for (auto &e : v) {
in >> e;
}
return in;
}
template <char d = ' ', class T>
ostream &operator<<(ostream &out, vector<T> &v) {
for (auto &e : v) {
out << e << d;
}
return out;
}
template <char d = ' ', class T> ostream &operator<<(ostream &out, set<T> &v) {
for (auto &e : v) {
out << e << d;
}
return out;
}
#define pb push_back
#define mk make_pair
#define X first
#define Y second
#define cont continue
#define ret return
#define For(i, a, b) for (int i = a; i < b; i++)
#define forn(i, a) for (int i = 0; i < a; i++)
#define ford(i, a, b) for (int i = b - 1; i >= a; i--)
#define fordn(i, a) for (int i = a - 1; i >= 0; i--)
#define forr(x, arr) for (auto &x : arr)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REE(s_) \
{ \
cout << s_ << '\n'; \
exit(0); \
}
#define endl '\n'
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define int long long
typedef double dd;
typedef long double ldd;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef set<int> si;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <typename T> std::string toString(T val) {
std::ostringstream oss;
oss << val;
return oss.str();
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
// const int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
// const int dx[8] = {2, 2, 1, 1, -1, -1, -2, -2};
// const int dy[8] = {1, -1, 2, -2, 2, -2, 1, -1};
const int mod = 1e9 + 7;
// int powq(int a, int n) {
// int res = 1;
// while (n){
// if (n & 1) {
// res *= a;
// res %= mod;
// --n;
// }
// else {
// a *= a;
// a %= mod;
// n >>= 1;
// }
//
// }
// res %= mod;
// return res;
// }
signed main() {
// ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef KULA
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#else
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
#endif
char x;
cin >> x;
deque<char> q;
q.pb(x);
int gg = 0;
while (cin >> x) {
if (q.back() != x) {
gg += 2;
q.pop_front();
} else
q.pb(x);
}
cout << gg;
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("mmx,avx,fma")
// #pragma GCC optimize ("unroll-loops")
using namespace __gnu_pbds;
using namespace std;
template <class T> istream &operator>>(istream &in, vector<T> &v) {
for (auto &e : v) {
in >> e;
}
return in;
}
template <char d = ' ', class T>
ostream &operator<<(ostream &out, vector<T> &v) {
for (auto &e : v) {
out << e << d;
}
return out;
}
template <char d = ' ', class T> ostream &operator<<(ostream &out, set<T> &v) {
for (auto &e : v) {
out << e << d;
}
return out;
}
#define pb push_back
#define mk make_pair
#define X first
#define Y second
#define cont continue
#define ret return
#define For(i, a, b) for (int i = a; i < b; i++)
#define forn(i, a) for (int i = 0; i < a; i++)
#define ford(i, a, b) for (int i = b - 1; i >= a; i--)
#define fordn(i, a) for (int i = a - 1; i >= 0; i--)
#define forr(x, arr) for (auto &x : arr)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REE(s_) \
{ \
cout << s_ << '\n'; \
exit(0); \
}
#define endl '\n'
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define int long long
typedef double dd;
typedef long double ldd;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef set<int> si;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <typename T> std::string toString(T val) {
std::ostringstream oss;
oss << val;
return oss.str();
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
// const int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
// const int dx[8] = {2, 2, 1, 1, -1, -1, -2, -2};
// const int dy[8] = {1, -1, 2, -2, 2, -2, 1, -1};
const int mod = 1e9 + 7;
// int powq(int a, int n) {
// int res = 1;
// while (n){
// if (n & 1) {
// res *= a;
// res %= mod;
// --n;
// }
// else {
// a *= a;
// a %= mod;
// n >>= 1;
// }
//
// }
// res %= mod;
// return res;
// }
signed main() {
// ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef KULA
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#else
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
#endif
char x;
cin >> x;
deque<char> q;
q.pb(x);
int gg = 0;
while (cin >> x) {
if (q.size() && q.back() != x) {
gg += 2;
q.pop_front();
} else
q.pb(x);
}
cout << gg;
return 0;
}
| replace | 115 | 116 | 115 | 116 | 0 | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int ctoi(const char c) {
switch (c) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
}
int main() {
string a;
int sum = 0;
int flag = 0;
vector<int> v;
cin >> a;
for (int i = 0; i < a.size(); i++) {
v.push_back(ctoi(a[i]));
}
int length;
length = v.size();
while (1) {
int tmp;
tmp = v.at(0);
for (int i = 0; i < v.size(); i++) {
if (tmp != v.at(i)) {
if (v.size() != 2) {
v.erase(v.begin() + i - 1);
v.erase(v.begin() + i);
sum += 2;
flag = 1;
break;
} else {
sum += 2;
flag = 0;
break;
}
} else {
flag = 0;
tmp = v.at(i);
}
}
if (flag == 0) {
break;
}
}
cout << sum << endl;
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int ctoi(const char c) {
switch (c) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
}
int main() {
string a;
int sum = 0;
int flag = 0;
vector<int> v;
cin >> a;
for (int i = 0; i < a.size(); i++) {
v.push_back(ctoi(a[i]));
}
int length;
length = v.size();
while (1) {
int tmp;
tmp = v.at(0);
for (int i = 0; i < v.size(); i++) {
if (tmp != v.at(i)) {
if (v.size() != 2) {
v.erase(v.begin() + i - 1);
v.erase(v.begin() + i - 1);
sum += 2;
flag = 1;
break;
} else {
sum += 2;
flag = 0;
break;
}
} else {
flag = 0;
tmp = v.at(i);
}
}
if (flag == 0) {
break;
}
}
cout << sum << endl;
return 0;
} | replace | 54 | 55 | 54 | 55 | 0 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
char str[10005];
int main() {
int a = 0, b = 0;
scanf("%s", str);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == '1')
a++;
else
b++;
}
printf("%d\n", a > b ? 2 * b : 2 * a);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
char str[100005];
int main() {
int a = 0, b = 0;
scanf("%s", str);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == '1')
a++;
else
b++;
}
printf("%d\n", a > b ? 2 * b : 2 * a);
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define check(v) \
rep(i, v.size()) cout << v[i] << " "; \
cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
// オーバーフローに気をつけろよおおおおおお
// 確認忘れるなよおおおおおお
int main() {
string s;
cin >> s;
int count = 0;
bool flag = true;
while (flag) {
int count2 = 0;
REP(i, 1, s.size()) {
if (s[i] != s[i - 1]) {
s.erase(i - 1, 2);
count2++;
count += 2;
}
}
if (count2 == 0)
flag = false;
}
cout << count << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define check(v) \
rep(i, v.size()) cout << v[i] << " "; \
cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
// オーバーフローに気をつけろよおおおおおお
// 確認忘れるなよおおおおおお
int main() {
string s;
cin >> s;
vector<int> v(2, 0);
rep(i, s.size()) { v[s[i] - '0']++; }
cout << min(v[0], v[1]) * 2 << endl;
return 0;
} | replace | 25 | 40 | 25 | 28 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
while (!s.empty()) {
int n = s.size();
vector<int> v;
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
v.emplace_back(i++);
}
}
reverse(v.begin(), v.end());
for (auto &i : v) {
s.erase(s.begin() + i + 1);
s.erase(s.begin() + i);
}
ans += 2 * v.size();
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int a = count(s.begin(), s.end(), '0'), b = count(s.begin(), s.end(), '1');
cout << 2 * min(a, b) << endl;
return 0;
} | replace | 6 | 23 | 6 | 8 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main(void) {
IOS string s;
cin >> s;
stack<char> st;
REP(i, s.size()) {
if (st.top() != s[i])
st.pop();
else
st.push(s[i]);
}
OUT(s.size() - st.size())
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main(void) {
IOS string s;
cin >> s;
stack<char> st;
REP(i, s.size()) {
if (!st.empty() && st.top() != s[i])
st.pop();
else
st.push(s[i]);
}
OUT(s.size() - st.size())
return 0;
} | replace | 23 | 24 | 23 | 24 | -11 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string s;
cin >> s;
vector<int> a;
for (int i = 0; i < s.size(); i++) {
a.push_back(s[i] - '0');
while (a.size() > 1 && abs(a[a.size() - 1] - a[a.size() - 2]) > 0)
a.erase(a.begin() + a.size() - 1), a.erase(a.begin() - 1);
}
cout << (int)s.size() - (int)a.size() << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string s;
cin >> s;
vector<int> a;
for (int i = 0; i < s.size(); i++) {
a.push_back(s[i] - '0');
while (a.size() > 1 && abs(a[a.size() - 1] - a[a.size() - 2]) > 0)
a.erase(a.begin() + a.size() - 1), a.erase(a.begin() + a.size() - 1);
}
cout << (int)s.size() - (int)a.size() << endl;
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MAX 100005
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
string str;
cin >> str;
int len = str.size(), ans = 0;
set<int> s;
int i = 0, j = 1;
while (true) {
if (i >= len || j >= len)
break;
if (str[i] == str[j] || i < 0) {
if (i >= 0)
s.insert(i);
i = j;
j = j + 1;
} else {
i = *s.rbegin();
j++;
ans += 2;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define MAX 100005
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
string str;
cin >> str;
int len = str.size(), ans = 0;
set<int> s;
int i = 0, j = 1;
while (true) {
if (i >= len || j >= len)
break;
if (str[i] == str[j] || i < 0) {
if (i >= 0)
s.insert(i);
i = j;
j = j + 1;
} else {
if (s.count(i)) {
s.erase(i);
}
if (!s.empty()) {
i = *s.rbegin();
} else
i = -1;
j++;
ans += 2;
}
}
cout << ans << endl;
return 0;
} | replace | 29 | 30 | 29 | 36 | 0 | |
p03107 | C++ | Runtime Error | #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC push_options
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define __V vector
#define all(x) x.begin(), x.end()
#define oit ostream_iterator
#define mod 998244353ll
using namespace std;
void doin() {
cin.tie();
cout.tie();
ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
}
template <typename X, typename Y>
istream &operator>>(istream &in, pair<X, Y> &a) {
in >> a.first >> a.second;
return in;
}
template <typename T> void getv(T &i) { cin >> i; }
template <typename T, typename... Ns> void getv(vector<T> &v, int n, Ns... ns) {
v.resize(n);
for (auto &i : v)
getv(i, ns...);
}
template <typename T> void getv1(T &i) { cin >> i; }
template <typename T, typename... Ns>
void getv1(vector<T> &v, int n, Ns... ns) {
v.resize(n + 1);
for (int i = 1; i <= n; i++)
getv(v[i], ns...);
}
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
inline void getch(char &x) {
while (x = getchar_unlocked(), x < 33) {
;
}
}
inline void getstr(string &str) {
str.clear();
char cur;
while (cur = getchar_unlocked(), cur < 33) {
;
}
while (cur > 32) {
str += cur;
cur = getchar_unlocked();
}
}
template <typename T> inline bool sc(T &num) {
bool neg = 0;
int c;
num = 0;
while (c = getchar_unlocked(), c < 33) {
if (c == EOF)
return false;
}
if (c == '-') {
neg = 1;
c = getchar_unlocked();
}
for (; c > 47; c = getchar_unlocked())
num = num * 10 + c - 48;
if (neg)
num *= -1;
return true;
}
typedef unsigned long long ull;
typedef long long ll;
typedef float ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V<_I> vi;
typedef __V<char> vc;
typedef __V<ld> vd;
typedef __V<vd> vvd;
typedef __V<pi> vpi;
typedef __V<__V<_I>> vvi;
typedef __V<__V<char>> vvc;
typedef __V<__V<pi>> vvpi;
using AntonTsypko = void;
using arsijo = AntonTsypko;
using god = arsijo;
uniform_real_distribution<double> double_dist(0, 1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int main() {
doin();
string s;
stack<char> a;
cin >> s;
int ans = 0;
for (auto i : s) {
if (!a.empty() && a.top() != i)
a.pop(), ans += 2;
else
a.push(i);
}
cout << ans;
return 0;
}
| // #pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC push_options
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define __V vector
#define all(x) x.begin(), x.end()
#define oit ostream_iterator
#define mod 998244353ll
using namespace std;
void doin() {
cin.tie();
cout.tie();
ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
}
template <typename X, typename Y>
istream &operator>>(istream &in, pair<X, Y> &a) {
in >> a.first >> a.second;
return in;
}
template <typename T> void getv(T &i) { cin >> i; }
template <typename T, typename... Ns> void getv(vector<T> &v, int n, Ns... ns) {
v.resize(n);
for (auto &i : v)
getv(i, ns...);
}
template <typename T> void getv1(T &i) { cin >> i; }
template <typename T, typename... Ns>
void getv1(vector<T> &v, int n, Ns... ns) {
v.resize(n + 1);
for (int i = 1; i <= n; i++)
getv(v[i], ns...);
}
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
inline void getch(char &x) {
while (x = getchar_unlocked(), x < 33) {
;
}
}
inline void getstr(string &str) {
str.clear();
char cur;
while (cur = getchar_unlocked(), cur < 33) {
;
}
while (cur > 32) {
str += cur;
cur = getchar_unlocked();
}
}
template <typename T> inline bool sc(T &num) {
bool neg = 0;
int c;
num = 0;
while (c = getchar_unlocked(), c < 33) {
if (c == EOF)
return false;
}
if (c == '-') {
neg = 1;
c = getchar_unlocked();
}
for (; c > 47; c = getchar_unlocked())
num = num * 10 + c - 48;
if (neg)
num *= -1;
return true;
}
typedef unsigned long long ull;
typedef long long ll;
typedef float ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V<_I> vi;
typedef __V<char> vc;
typedef __V<ld> vd;
typedef __V<vd> vvd;
typedef __V<pi> vpi;
typedef __V<__V<_I>> vvi;
typedef __V<__V<char>> vvc;
typedef __V<__V<pi>> vvpi;
using AntonTsypko = void;
using arsijo = AntonTsypko;
using god = arsijo;
uniform_real_distribution<double> double_dist(0, 1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int main() {
doin();
string s;
stack<char> a;
cin >> s;
int ans = 0;
for (auto i : s) {
if (!a.empty() && a.top() != i)
a.pop(), ans += 2;
else
a.push(i);
}
cout << ans;
return 0;
}
| replace | 0 | 1 | 0 | 1 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<long long> func(vector<long long> &vv) {
long long temp;
temp = vv[0];
bool flag = false;
int fl = 0;
for (long long j = 1; j < vv.size(); j++) {
if (temp != vv[j]) {
vv.erase(vv.begin() + j - 1);
vv.erase(vv.begin() + j - 1);
fl = 1;
}
temp = vv[j];
}
if (fl == 0) {
return vv;
}
return func(vv);
}
int main() {
std::string s;
cin >> s;
long long size = s.length();
char cary[size];
s.copy(cary, size);
vector<long long> v(size);
for (long long i = 0; i < size; i++) {
v[i] = cary[i];
}
vector<long long> v2;
v2 = func(v);
cout << size - v2.size() << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<long long> func(vector<long long> &vv) {
long long temp;
temp = vv[0];
bool flag = false;
int fl = 0;
for (long long j = 1; j < vv.size(); j++) {
if (temp != vv[j]) {
vv.erase(vv.begin() + j - 1);
vv.erase(vv.begin() + j - 1);
fl = 1;
break;
}
temp = vv[j];
}
if (fl == 0) {
return vv;
}
return func(vv);
}
int main() {
std::string s;
cin >> s;
long long size = s.length();
char cary[size];
s.copy(cary, size);
vector<long long> v(size);
for (long long i = 0; i < size; i++) {
v[i] = cary[i];
}
vector<long long> v2;
v2 = func(v);
cout << size - v2.size() << endl;
return 0;
} | insert | 16 | 16 | 16 | 17 | TLE | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin >> S;
vector<char> vec;
vec.push_back(S[0]);
ll count = 0;
for (int i = 1; i < S.length(); ++i) {
if (vec.front() != S[i]) {
count += 2;
vec.pop_back();
} else {
vec.push_back(S[i]);
}
}
cout << count << endl;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin >> S;
vector<char> vec;
vec.push_back(S[0]);
ll count = 0;
for (int i = 1; i < S.length(); ++i) {
if (vec.size() == 0) {
vec.push_back(S[i]);
} else if (vec.front() != S[i]) {
count += 2;
vec.pop_back();
} else {
vec.push_back(S[i]);
}
}
cout << count << endl;
} | replace | 25 | 26 | 25 | 28 | 0 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
map<char, int> c;
for (int i = 0; i < s.size(); i++)
c[s.at(i)]++;
cout << min(c.at('0'), c.at('1')) * 2 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
map<char, int> c;
for (int i = 0; i < s.size(); i++)
c[s.at(i)]++;
cout << min(c['0'], c['1']) * 2 << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
for (int i = 0; i < s.size() - 1; i++) {
if (s.substr(i, 2) == "01" || s.substr(i, 2) == "10") {
s.erase(s.begin() + i, s.begin() + i + 2);
ans += 2;
i -= 3;
i = max(-1, i);
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
for (int i = 0; i < s.size() - 1; i++) {
if (s.substr(i, 2) == "01" || s.substr(i, 2) == "10") {
s.erase(s.begin() + i, s.begin() + i + 2);
ans += 2;
i -= 3;
i = max(-1, i);
if (s.size() == 0) {
break;
}
}
}
cout << ans;
} | insert | 14 | 14 | 14 | 17 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
|
p03107 | C++ | Time Limit Exceeded | // AtCoder ABC120 C - Unification
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
std::string replaceStringAll(std::string str, const std::string &replace,
const std::string &with) {
if (!replace.empty()) {
std::size_t pos = 0;
while ((pos = str.find(replace, 0)) != std::string::npos) {
str.replace(pos, replace.length(), with);
// pos += with.length();
}
}
return str;
}
int main() {
#if LOCAL & 0
std::ifstream in("./test/sample-1.in"); // input.txt
std::cin.rdbuf(in.rdbuf());
#endif
string S = "0011";
cin >> S;
auto ret = replaceStringAll(S, "01", "");
ret = replaceStringAll(ret, "10", "");
cout << S.size() - ret.size() << endl;
return 0;
} | // AtCoder ABC120 C - Unification
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
std::string replaceStringAll(std::string str, const std::string &replace,
const std::string &with) {
if (!replace.empty()) {
std::size_t pos = 0;
while ((pos = str.find(replace, 0)) != std::string::npos) {
str.replace(pos, replace.length(), with);
// pos += with.length();
}
}
return str;
}
int main() {
#if LOCAL & 0
std::ifstream in("./test/sample-1.in"); // input.txt
std::cin.rdbuf(in.rdbuf());
#endif
string S = "0011";
cin >> S;
ll cnt0 = 0;
for (const auto &c : S) {
if (c == '0') {
++cnt0;
}
}
ll cnt1 = S.size() - cnt0;
cout << min(cnt0, cnt1) * 2 << endl;
return 0;
} | replace | 24 | 27 | 24 | 32 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(a) cout << #a << ": " << a << endl;
int main() {
string S;
cin >> S;
int cnt = 0;
while ((int)S.size() != 0) {
int find01 = S.find("01");
int find10 = S.find("10");
if (find01 == -1 && find10 == -1) {
break;
} else {
if (find01 == -1) {
S.erase(find10, 2);
} else if (find10 == -1) {
S.erase(find01, 2);
} else {
S.erase(min(find01, find10), 2);
}
cnt += 1;
}
}
cout << cnt * 2 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(a) cout << #a << ": " << a << endl;
int main() {
string S;
cin >> S;
int cnt0 = count(S.begin(), S.end(), '0');
int cnt1 = count(S.begin(), S.end(), '1');
cout << min(cnt0, cnt1) * 2 << endl;
}
| replace | 12 | 30 | 12 | 15 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
while (cin >> s) {
int sz = s.size();
while (s.size() > 1) {
int flag = 0;
for (int i = 0; i < s.size() - 1; i++) {
if ((s[i] == '0' && s[i + 1] == '1') ||
(s[i] == '1' && s[i + 1] == '0')) {
s.erase(i, 2);
i--;
flag = 1;
}
if (s.size() < 2)
break;
}
if (flag == 0)
break;
}
cout << sz - s.size() << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
while (cin >> s) {
int sz = s.size();
while (s.size() > 1) {
int flag = 0;
for (int i = 0; i < s.size() - 1; i++) {
if ((s[i] == '0' && s[i + 1] == '1') ||
(s[i] == '1' && s[i + 1] == '0')) {
s.erase(i, 2);
i -= 2;
if (i == -2)
i = -1;
flag = 1;
}
if (s.size() < 2)
break;
}
if (flag == 0)
break;
}
cout << sz - s.size() << endl;
}
return 0;
}
| replace | 15 | 16 | 15 | 20 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
while (s.size() > 0 && count(s.begin(), s.end(), s[0]) < s.size()) {
for (int i = 0; i < s.size() - 1; ++i) {
if ((s[i] == '0' && s[i + 1] == '1') ||
(s[i] == '1' && s[i + 1] == '0')) {
s.erase(i, 2);
ans += 2;
break;
}
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int zero = count(s.begin(), s.end(), '0');
int one = count(s.begin(), s.end(), '1');
int ans = 2 * min(zero, one);
cout << ans << endl;
return 0;
}
| replace | 10 | 21 | 10 | 13 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
int a = 0;
int b = 0;
for (int i = 0; i < n; i++) {
if (s.at(i) == '0')
a++;
else
b++;
}
int m = n;
int r = 0;
while (a > 0 && b > 0) {
string t = s;
int m = t.size();
for (int i = 0; i < m - 1; i++) {
if (t.at(i) != t.at(i + 1)) {
r += 2;
s.erase(s.begin() + i, s.begin() + i + 1);
a--;
b--;
break;
}
}
}
cout << r << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
int a = 0;
int b = 0;
for (int i = 0; i < n; i++) {
if (s.at(i) == '0')
a++;
else
b++;
}
cout << min(a, b) * 2 << endl;
}
| replace | 16 | 34 | 16 | 17 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <iostream>
#include <string>
using namespace std;
int main(void) {
string S;
cin >> S;
int N = S.size();
int first = 0, second = 1;
int ans = 0;
while (second < N) {
if (S[first] != S[second]) {
ans += 2;
S[first] = S[second] = 'x';
while (first >= 0 && S[first] == 'x')
first--;
if (first < 0) {
first = second + 1;
second = first + 1;
} else
second++;
} else {
first = second;
second++;
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main(void) {
string S;
cin >> S;
int sum[2] = {0, 0};
for (auto i : S)
sum[i - '0']++;
cout << (S.size() - (max(sum[0], sum[1]) - min(sum[0], sum[1]))) << endl;
return 0;
} | replace | 8 | 28 | 8 | 12 | TLE | |
p03107 | C++ | Runtime Error | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
int main(void) {
char input[N];
char stack[N];
scanf("%s", input);
int eraseCnt = 0;
stack[0] = input[0];
int sCnt, iCnt;
for (sCnt = 0, iCnt = 1; input[iCnt] != '\0';) {
// printf("%c %c\n",stack[sCnt],input [iCnt]);
if (stack[sCnt] != input[iCnt]) { // erase
eraseCnt++;
if (sCnt == 0) { // stack becomes empty
stack[sCnt] = input[iCnt + 1];
sCnt = 0;
iCnt = iCnt + 2;
} else { // stack still not empty
sCnt = sCnt - 1;
iCnt = iCnt + 1;
}
} else { // dont erase
stack[sCnt + 1] = input[iCnt];
sCnt++;
iCnt++;
}
}
printf("%d", eraseCnt * 2);
return 0;
} | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
int main(void) {
char input[N];
char stack[N];
scanf("%s", input);
int eraseCnt = 0;
stack[0] = input[0];
int sCnt, iCnt;
for (sCnt = 0, iCnt = 1; input[iCnt] != '\0';) {
// printf("%c %c\n",stack[sCnt],input [iCnt]);
if (stack[sCnt] != input[iCnt]) { // erase
eraseCnt++;
if (sCnt == 0) { // stack becomes empty
stack[sCnt] = input[iCnt + 1];
sCnt = 0;
iCnt = iCnt + 2;
} else { // stack still not empty
sCnt = sCnt - 1;
iCnt = iCnt + 1;
}
} else { // dont erase
stack[sCnt + 1] = input[iCnt];
sCnt++;
iCnt++;
}
}
printf("%d", eraseCnt * 2);
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int main() {
string s;
cin >> s;
int num0, num1;
num0 = count(s.begin(), s.end(), '0');
num1 = count(s.begin(), s.end(), '1');
cout << min(num0, num1) * 2;
while (1)
;
return 0;
}
| #include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int main() {
string s;
cin >> s;
int num0, num1;
num0 = count(s.begin(), s.end(), '0');
num1 = count(s.begin(), s.end(), '1');
cout << min(num0, num1) * 2;
return 0;
}
| delete | 12 | 14 | 12 | 12 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int b, w;
b = 0;
w = 0;
for (int i = 0;; i++) {
if (s.at(i) == '1') {
b++;
} else {
w++;
}
}
int Ans = min(b, w) * 2;
cout << Ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int b, w;
b = 0;
w = 0;
for (int i = 0; i < s.size(); i++) {
if (s.at(i) == '1') {
b++;
} else {
w++;
}
}
int Ans = min(b, w) * 2;
cout << Ans << endl;
}
| replace | 11 | 12 | 11 | 12 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 4) >= this->size() (which is 4)
|
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
int main() {
string s;
cin >> s;
int cnt = 0;
int i = 0;
while (s.size() != 0 && s.size() != 1) {
if ((s.at(i) - '0') + (s.at(i + 1) - '0') == 1) {
s.erase(i, 2);
i = -1;
cnt += 2;
}
i++;
if (i == s.size() - 1)
break;
}
cout << cnt << 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;
int main() {
string s;
cin >> s;
vector<int> cnt(2, 0);
rep(i, s.size()) { cnt[s.at(i) - '0']++; }
cout << 2 * min(cnt[0], cnt[1]) << endl;
return 0;
}
| replace | 8 | 21 | 8 | 11 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int main() {
string S;
cin >> S;
int e = 0, ne = 0;
do {
ne = 0;
int i = 0;
while (i < int(S.size()) - 1) {
if (S.at(i) != S.at(i + 1)) {
S.erase(i, 2);
e += 2;
ne += 1;
}
i++;
}
} while (ne != 0);
cout << e;
} | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int main() {
string S;
cin >> S;
int z = 0, o = 0;
for (size_t i = 0; i < S.size(); i++) {
if (S.at(i) == '0') {
z++;
} else
o++;
}
cout << min(o, z) * 2;
} | replace | 6 | 21 | 6 | 14 | TLE | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
typedef long long ll;
const int xmax = 1e5 + 7;
const int INF = 1e9 + 7;
using namespace std;
int main() {
char s[1000];
scanf("%s", s);
int ans1 = 0, ans2 = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0')
ans1++;
else
ans2++;
}
if (ans1 == 0 || ans2 == 0)
printf("0");
else
printf("%d", min(ans1, ans2) * 2);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
typedef long long ll;
const int xmax = 1e5 + 7;
const int INF = 1e9 + 7;
using namespace std;
int main() {
char s[100007];
scanf("%s", s);
int ans1 = 0, ans2 = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0')
ans1++;
else
ans2++;
}
if (ans1 == 0 || ans2 == 0)
printf("0");
else
printf("%d", min(ans1, ans2) * 2);
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ui = unsigned int;
const ll MOD = 1000000007;
// 120
int main() {
static char S[10010];
scanf("%s", S);
ll len = strlen(S);
ll countOne = 0;
for (ll i = 0; i < len; i++) {
if (S[i] == '1') {
countOne++;
}
}
printf("%lld", std::min(countOne, len - countOne) * 2);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ui = unsigned int;
const ll MOD = 1000000007;
// 120
int main() {
static char S[100010];
scanf("%s", S);
ll len = strlen(S);
ll countOne = 0;
for (ll i = 0; i < len; i++) {
if (S[i] == '1') {
countOne++;
}
}
printf("%lld", std::min(countOne, len - countOne) * 2);
return 0;
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p03107 | C++ | Time Limit Exceeded | #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 FOR(i, m, n) for (ll i = m; i < n; 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;
int main() {
string s;
cin >> s;
ll total = 0;
while (1) {
ll count = 0;
REP(i, max((signed)0, (signed)s.size() - 1)) {
if (s.substr(i, 2) == "01" || s.substr(i, 2) == "10") {
s.erase(i, 2);
i--;
count += 2;
}
}
if (count == 0)
break;
else
total += count;
}
cout << total << 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 FOR(i, m, n) for (ll i = m; i < n; 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;
int main() {
string s;
cin >> s;
cout << 2 * min(count(ALL(s), '0'), count(ALL(s), '1')) << endl;
return 0;
} | replace | 14 | 31 | 14 | 15 | TLE | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
char s[10020];
int ans, tot1, tot2;
int main() {
cin >> s;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0')
tot1++;
else
tot2++;
}
ans = min(tot1, tot2) * 2;
cout << ans << endl;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
char s[100200];
int ans, tot1, tot2;
int main() {
cin >> s;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0')
tot1++;
else
tot2++;
}
ans = min(tot1, tot2) * 2;
cout << ans << endl;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int zero, one;
int main() {
char ch[1000];
scanf("%s", &ch);
int len = strlen(ch);
for (int i = 0; i < len; i++) {
if (ch[i] == '0')
++zero;
else if (ch[i] == '1')
++one;
}
printf("%d\n", min(zero, one) * 2);
}
| #include <bits/stdc++.h>
using namespace std;
int zero, one;
int main() {
char ch[100009];
scanf("%s", &ch);
int len = strlen(ch);
for (int i = 0; i < len; i++) {
if (ch[i] == '0')
++zero;
else if (ch[i] == '1')
++one;
}
printf("%d\n", min(zero, one) * 2);
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string homu;
cin >> homu;
int n = homu.size();
while (1) {
bool ok = false;
string ns = "";
for (int i = 0; i < (int)homu.size(); ++i) {
if (i + 1 < homu.size() && homu[i] != homu[i + 1]) {
++i;
ok = true;
} else
ns += homu[i];
}
if (!ok)
break;
// cout<<ns<<endl;
homu.swap(ns);
}
cout << n - homu.size() << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string homu;
cin >> homu;
int n = homu.size();
random_shuffle(homu.begin(), homu.end());
while (1) {
bool ok = false;
string ns = "";
for (int i = 0; i < (int)homu.size(); ++i) {
if (i + 1 < homu.size() && homu[i] != homu[i + 1]) {
++i;
ok = true;
} else
ns += homu[i];
}
if (!ok)
break;
// cout<<ns<<endl;
homu.swap(ns);
}
cout << n - homu.size() << endl;
return 0;
}
| insert | 7 | 7 | 7 | 8 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
bool f = true;
while (f) {
f = false;
for (int i = 1; i < s.size(); i++) {
if (s[i] != s[i - 1]) {
s.erase(i - 1, 2);
ans += 2;
i -= 2;
f = true;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
bool f = true;
while (f) {
f = false;
for (int i = 1; i < s.size(); i++) {
if (s[i] != s[i - 1]) {
s.erase(i - 1, 2);
ans += 2;
i--;
f = true;
}
}
}
cout << ans << endl;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p03107 | C++ | Time Limit Exceeded | #include <iostream>
#include <string>
using namespace std;
int main() {
string s;
int n = 0;
cin >> s;
for (int i = 0; i < s.size();) {
if (s.substr(i, 2) == "01" || s.substr(i, 2) == "10") {
n += 2;
s.erase(i, 2);
i = 0;
} else {
i++;
}
}
cout << n << endl;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
string s;
int n = 0;
cin >> s;
for (int i = 0; i < s.size();) {
if (s.substr(i, 2) == "01" || s.substr(i, 2) == "10") {
n += 2;
s.erase(i, 2);
if (i != 0)
i--;
} else {
i++;
}
}
cout << n << endl;
return 0;
} | replace | 15 | 16 | 15 | 17 | TLE | |
p03107 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long INF = 1LL << 60;
int main() {
string S;
cin >> S;
int ssize = S.size();
int counter = 0;
vector<bool> flag(ssize, true);
for (int i = 0; i < ssize - 1; i++) {
if (!flag.at(i))
continue;
int next = -1;
for (int j = i + 1; j < ssize; j++) {
if (flag.at(j)) {
next = j;
break;
}
}
if (next == -1)
break;
if (S.at(i) != S.at(next)) {
flag.at(i) = false;
flag.at(next) = false;
counter += 2;
i = -1;
}
}
cout << counter << endl;
}
| #include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long INF = 1LL << 60;
int main() {
string S;
cin >> S;
cout << 2 * min(count(ALL(S), '0'), count(ALL(S), '1')) << endl;
}
| replace | 29 | 53 | 29 | 30 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int start = 0;
int compare = start + 1;
int count = 0;
priority_queue<int> bs;
while (compare < S.size()) {
if (S[start] != S[compare]) {
count += 2;
while (true) {
if (start > bs.top()) {
start = bs.top();
compare++;
break;
}
if (bs.top() == 0) {
start = compare + 1;
compare = start + 1;
break;
}
bs.pop();
}
} else {
bs.push(start);
start = compare;
compare = start + 1;
}
}
cout << count << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int start = 0;
int compare = start + 1;
int count = 0;
priority_queue<int> bs;
while (compare < S.size()) {
if (S[start] != S[compare]) {
count += 2;
while (true) {
if (bs.size() == 0) {
start = compare + 1;
compare = start + 1;
break;
}
if (start > bs.top()) {
start = bs.top();
compare++;
break;
}
if (bs.top() == 0) {
start = compare + 1;
compare = start + 1;
break;
}
bs.pop();
}
} else {
bs.push(start);
start = compare;
compare = start + 1;
}
}
cout << count << endl;
return 0;
} | insert | 14 | 14 | 14 | 19 | 0 | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n, red = 0, blue = 0;
string s;
cin >> n >> s;
REP(i, n) {
if (s[i] == '0') {
red++;
} else {
blue++;
}
}
cout << min(red, blue) * 2;
return (0);
} | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n, red = 0, blue = 0;
string s;
cin >> s;
n = s.size();
REP(i, n) {
if (s[i] == '0') {
red++;
} else {
blue++;
}
}
cout << min(red, blue) * 2;
return (0);
} | replace | 14 | 15 | 14 | 16 | 0 | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
using namespace std;
const int M = 1e6 + 10;
int n, cnt, ans;
bool a[M], st[M];
int main() {
string s;
cin >> n >> s;
for (int i = 0; i < n; i++)
a[i] = s[i] - '0';
for (int i = 0; i < n; i++) {
if (cnt && (st[cnt - 1] ^ a[i]) == 1)
cnt--, ans += 2;
else
st[cnt++] = a[i];
}
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
using namespace std;
const int M = 1e6 + 10;
int n, cnt, ans;
bool a[M], st[M];
int main() {
string s;
cin >> s;
n = s.length();
for (int i = 0; i < n; i++)
a[i] = s[i] - '0';
for (int i = 0; i < n; i++) {
if (cnt && (st[cnt - 1] ^ a[i]) == 1)
cnt--, ans += 2;
else
st[cnt++] = a[i];
}
printf("%d\n", ans);
return 0;
}
| replace | 11 | 12 | 11 | 13 | 0 | |
p03107 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
if (x < y)
swap(x, y);
if (y == 0)
return x;
return gcd(y, x % y);
}
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
int val = ((a % mod) + (b % mod)) % mod;
if (val < 0) {
val += mod;
}
a = val;
}
////////////////////////////////////////
int main() {
string S;
int count = 0;
cin >> S;
vector<int> V;
for (int i = 0; i < S.size(); ++i) {
if (S[i] == '0') {
V.push_back(0);
} else {
V.push_back(1);
}
}
while (true) {
bool can = false;
if (V.size() == 1)
break;
for (int i = 0; i < V.size() - 1; ++i) {
if (V[i] != V[i + 1]) {
can = true;
V.erase(V.begin() + i + 1);
V.erase(V.begin() + i);
count += 2;
break;
}
}
if (!can)
break;
}
cout << count << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
if (x < y)
swap(x, y);
if (y == 0)
return x;
return gcd(y, x % y);
}
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
int val = ((a % mod) + (b % mod)) % mod;
if (val < 0) {
val += mod;
}
a = val;
}
////////////////////////////////////////
int main() {
string S;
int count = 0;
cin >> S;
vector<int> V;
for (int i = 0; i < S.size(); ++i) {
if (S[i] == '0') {
V.push_back(0);
} else {
V.push_back(1);
}
}
while (true) {
bool can = false;
if (V.size() == 0)
break;
for (int i = 0; i < V.size() - 1; ++i) {
if (V[i] != V[i + 1]) {
can = true;
V.erase(V.begin() + i + 1);
V.erase(V.begin() + i);
count += 2;
break;
}
}
if (!can)
break;
}
cout << count << endl;
return 0;
}
| replace | 66 | 67 | 66 | 67 | -11 | |
p03107 | C++ | Runtime Error | #include <iostream>
int A[] = {0, 0};
int main(void) {
while (true) {
char c = getchar();
if (c == EOF)
break;
A[c - '0']++;
}
printf("%d\n", (A[0] > A[1] ? A[1] : A[0]) << 1);
return 0;
} | #include <iostream>
int A[] = {0, 0};
int main(void) {
while (true) {
char c = getchar();
if (c == '\n')
break;
A[c - '0']++;
}
printf("%d\n", (A[0] > A[1] ? A[1] : A[0]) << 1);
return 0;
}
| replace | 7 | 8 | 7 | 8 | -11 | |
p03107 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
bool flag = true;
bool done = false;
int count = 0;
int len = s.size();
while (flag) {
done = false;
for (int i = 0; i < s.size(); ++i) {
if ((s[i] == '0' && s[i + 1] == '1') or
(s[i] == '1' && s[i + 1] == '0')) {
s.erase(s.begin() + i, s.begin() + i + 2);
count += 2;
done = true;
}
}
if (!done)
flag = false;
}
cout << count << endl;
}
| #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
bool flag = true;
bool done = false;
int count = 0;
int len = s.size();
while (flag) {
done = false;
for (int i = 0; i < s.size(); ++i) {
if ((s[i] == '0' && s[i + 1] == '1') or
(s[i] == '1' && s[i + 1] == '0')) {
s.erase(s.begin() + i, s.begin() + i + 2);
count += 2;
done = true;
if (i >= 1)
i -= 2;
}
}
if (!done)
flag = false;
}
cout << count << endl;
}
| insert | 20 | 20 | 20 | 22 | TLE | |
p03107 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll INF = 1e18;
const int MOD = 1e9 + 7;
const double pi = acos(-1);
int main() {
string s;
cin >> s;
ll n = s.size();
vector<int> vec;
rep(i, n) vec.push_back(s[i] - '0');
ll now = 0;
ll have = n;
while (now <= have - 1 && 0 <= now) {
if (vec[now] != vec[now + 1]) {
vec.erase(vec.begin() + now);
vec.erase(vec.begin() + now);
have -= 2;
if (now != 0)
now--;
} else {
now++;
}
}
cout << n - have << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll INF = 1e18;
const int MOD = 1e9 + 7;
const double pi = acos(-1);
int main() {
string s;
cin >> s;
ll n = s.size();
vector<int> vec;
rep(i, n) vec.push_back(s[i] - '0');
ll now = 0;
ll have = n;
while (now <= have - 2 && 0 <= now) {
if (vec[now] != vec[now + 1]) {
vec.erase(vec.begin() + now);
vec.erase(vec.begin() + now);
have -= 2;
if (now != 0)
now--;
} else {
now++;
}
}
cout << n - have << endl;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p03107 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
#pragma GCC target("avx2,tune=native")
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
string backup = s;
auto i = s.find("01", 0);
while (i != -1) {
s = s.substr(0, i) + s.substr(i + 2, -1);
i = s.find("01", 0);
}
i = s.find("10", 0);
while (i != -1) {
s = s.substr(0, i) + s.substr(i + 2, -1);
i = s.find("10", 0);
}
cout << backup.length() - s.length();
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx2,tune=native")
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < s.length(); i++)
(s[i] == '1') ? cnt1++ : cnt0++;
cout << 2 * min(cnt0, cnt1);
return 0;
} | replace | 13 | 25 | 13 | 17 | TLE | |
p03107 | C++ | Runtime Error | #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int max(int a, int b) { return a >= b ? a : b; }
int lmax(long int a, long int b) { return a >= b ? a : b; }
int min(int a, int b) { return b >= a ? a : b; }
int lmin(long int a, long int b) { return b >= a ? a : b; }
int sei(int a) { return a > 0 ? a : 0; }
long int power(int a, int b) {
long int count = 1;
for (int i = 0; i < b; i++) {
count *= 2;
}
return count;
}
int factorial(int n) {
if (n > 0)
return n * factorial(n - 1);
else
return 1;
}
int compare_up_int(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int compare_down_int(const void *a, const void *b) {
return *(int *)b - *(int *)a;
}
int euclid(int a, int b) {
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
int r = a % b;
if (r < 0)
r += b;
while (r != 0) {
a = b;
b = r;
r = a % b;
if (r < 0)
r += b;
}
return b;
}
int main() {
char s[10001];
int count0 = 0, count1 = 0;
scanf("%s", s);
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0')
count0++;
else
count1++;
}
if (count1 >= 1 && count0 >= 1) {
printf("%d", 2 * min(count0, count1));
return 0;
} else
printf("%d", 0);
return 0;
}
| #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int max(int a, int b) { return a >= b ? a : b; }
int lmax(long int a, long int b) { return a >= b ? a : b; }
int min(int a, int b) { return b >= a ? a : b; }
int lmin(long int a, long int b) { return b >= a ? a : b; }
int sei(int a) { return a > 0 ? a : 0; }
long int power(int a, int b) {
long int count = 1;
for (int i = 0; i < b; i++) {
count *= 2;
}
return count;
}
int factorial(int n) {
if (n > 0)
return n * factorial(n - 1);
else
return 1;
}
int compare_up_int(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int compare_down_int(const void *a, const void *b) {
return *(int *)b - *(int *)a;
}
int euclid(int a, int b) {
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
int r = a % b;
if (r < 0)
r += b;
while (r != 0) {
a = b;
b = r;
r = a % b;
if (r < 0)
r += b;
}
return b;
}
int main() {
char s[100001];
int count0 = 0, count1 = 0;
scanf("%s", s);
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0')
count0++;
else
count1++;
}
if (count1 >= 1 && count0 >= 1) {
printf("%d", 2 * min(count0, count1));
return 0;
} else
printf("%d", 0);
return 0;
}
| replace | 50 | 51 | 50 | 51 | 0 | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
static const int MAX_N = 100, MAX_M = 10e2;
struct UnionFind { // MAX_N定義必要要素数
int Par[MAX_N]; // 親は"-子供(自分含め)"を所持
int N;
UnionFind(int n) {
N = n;
for (int i = 0; i < N; i++)
Par[i] = -1;
}
bool unite(int a, int b) {
int par_a = find(a);
int par_b = find(b);
if (par_a == par_b)
return true;
if (Par[par_a] > Par[par_b])
swap(par_a, par_b);
Par[par_a] += Par[par_b];
Par[par_b] = par_a;
return false;
}
int find(int a) {
if (Par[a] < 0)
return a;
else
return find(Par[a]);
}
long sizek(int a) {
if (Par[a] < 0)
return Par[a] * (-1);
else
return sizek(Par[a]);
}
};
/*
UnionFind XX(頂点の数)
XX.unite(a,b);a,b接続 既接続->true 新接続->false
find(a)aの親の番号返却
sizek(a)aの群れの頂点の数返却
*/
int A[MAX_M], B[MAX_M];
long ANS[MAX_M];
int main(void) {
int N, M;
cin >> N >> M;
UnionFind uni(N);
for (int i = 0; i < M; i++)
cin >> A[i] >> B[i];
ANS[M - 1] = (long)N * (N - 1) / 2;
for (int i = M - 1; i >= 1; i--) {
A[i]--;
B[i]--;
int ba = uni.find(A[i]);
int bb = uni.find(B[i]);
if (ba == bb) {
ANS[i - 1] = ANS[i];
continue;
}
long res = uni.sizek(A[i]);
res *= (long)uni.sizek(B[i]);
ANS[i - 1] = ANS[i] - res;
uni.unite(A[i], B[i]);
}
for (int i = 0; i < M; i++)
cout << ANS[i] << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
using namespace std;
static const int MAX_N = 10e5, MAX_M = 10e5;
struct UnionFind { // MAX_N定義必要要素数
int Par[MAX_N]; // 親は"-子供(自分含め)"を所持
int N;
UnionFind(int n) {
N = n;
for (int i = 0; i < N; i++)
Par[i] = -1;
}
bool unite(int a, int b) {
int par_a = find(a);
int par_b = find(b);
if (par_a == par_b)
return true;
if (Par[par_a] > Par[par_b])
swap(par_a, par_b);
Par[par_a] += Par[par_b];
Par[par_b] = par_a;
return false;
}
int find(int a) {
if (Par[a] < 0)
return a;
else
return find(Par[a]);
}
long sizek(int a) {
if (Par[a] < 0)
return Par[a] * (-1);
else
return sizek(Par[a]);
}
};
/*
UnionFind XX(頂点の数)
XX.unite(a,b);a,b接続 既接続->true 新接続->false
find(a)aの親の番号返却
sizek(a)aの群れの頂点の数返却
*/
int A[MAX_M], B[MAX_M];
long ANS[MAX_M];
int main(void) {
int N, M;
cin >> N >> M;
UnionFind uni(N);
for (int i = 0; i < M; i++)
cin >> A[i] >> B[i];
ANS[M - 1] = (long)N * (N - 1) / 2;
for (int i = M - 1; i >= 1; i--) {
A[i]--;
B[i]--;
int ba = uni.find(A[i]);
int bb = uni.find(B[i]);
if (ba == bb) {
ANS[i - 1] = ANS[i];
continue;
}
long res = uni.sizek(A[i]);
res *= (long)uni.sizek(B[i]);
ANS[i - 1] = ANS[i] - res;
uni.unite(A[i], B[i]);
}
for (int 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;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define lper(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define at(x, i) get<i>(x);
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
ll modfac(ll a) {
ll ans = 1;
while (a > 1) {
ans *= a;
ans %= 1000000007;
a--;
}
return ans;
}
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
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 same(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ll n, m;
cin >> n >> m;
vl l(m), r(m);
rep(i, m) {
cin >> l[i] >> r[i];
l[i]--;
r[i]--;
}
rev(all(l));
rev(all(r));
vl ans(m);
ans[0] = n * (n - 1) / 2;
UnionFind uf(n);
rep(i, n - 1) {
ll a = l[i], b = r[i];
ll s = uf.size(a) * uf.size(b);
if (!uf.same(a, b))
ans[i + 1] = ans[i] - s;
else
ans[i + 1] = ans[i];
uf.merge(a, b);
}
rev(all(ans));
rep(i, m) { cout << ans[i] << endl; }
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define lper(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define at(x, i) get<i>(x);
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
ll modfac(ll a) {
ll ans = 1;
while (a > 1) {
ans *= a;
ans %= 1000000007;
a--;
}
return ans;
}
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
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 same(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ll n, m;
cin >> n >> m;
vl l(m), r(m);
rep(i, m) {
cin >> l[i] >> r[i];
l[i]--;
r[i]--;
}
rev(all(l));
rev(all(r));
vl ans(m);
ans[0] = n * (n - 1) / 2;
UnionFind uf(n);
rep(i, m - 1) {
ll a = l[i], b = r[i];
ll s = uf.size(a) * uf.size(b);
if (!uf.same(a, b))
ans[i + 1] = ans[i] - s;
else
ans[i + 1] = ans[i];
uf.merge(a, b);
}
rev(all(ans));
rep(i, m) { cout << ans[i] << endl; }
} | replace | 118 | 119 | 118 | 119 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REV(v) reverse(v.begin(), v.end());
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, start, stop) for (int i = start; i < stop; i++)
#define FORR(i, start, stop) for (int i = start; i > stop; i--)
#define SORT(v, n) sort(v, v + n);
#define SORTR(v, n) sort(v, v + n, greater<int>());
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.begin(), v.end(), greater<int>());
#define REMOVE(v, n) remove(vector<int> v, v + v.size(), int n)
#define ll long long
#define ull unsigned long long
#define pb(a) push_back(a)
#define INF 999999999
#define V(v, i, j) vector(v.begin() + i, v.begin() + j)
#define INSERT(va, vb) va.insert(va.end(), vb.begin(), vb.end())
using namespace std;
typedef vector<int> vint;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
// vector< vector<int> > v (size1, vector<int>(size2) );
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() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
vector<ll> A(m), B(m);
FOR(i, 0, m) cin >> A[i] >> B[i];
UnionFind uf(n);
ll ans = n * (n - 1) / 2;
vector<ll> res;
FOR(i, 0, m) {
res.pb(ans);
ll a = A[m - i - 1], b = B[m - i - 1];
if (uf.issame(a, b))
continue;
ll sa = uf.size(a), sb = uf.size(b);
ans -= sa * sb;
uf.merge(a, b);
}
REV(res);
FOR(i, 0, res.size()) cout << res[i] << endl;
}
// 73 | #include <bits/stdc++.h>
#define REV(v) reverse(v.begin(), v.end());
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, start, stop) for (int i = start; i < stop; i++)
#define FORR(i, start, stop) for (int i = start; i > stop; i--)
#define SORT(v, n) sort(v, v + n);
#define SORTR(v, n) sort(v, v + n, greater<int>());
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.begin(), v.end(), greater<int>());
#define REMOVE(v, n) remove(vector<int> v, v + v.size(), int n)
#define ll long long
#define ull unsigned long long
#define pb(a) push_back(a)
#define INF 999999999
#define V(v, i, j) vector(v.begin() + i, v.begin() + j)
#define INSERT(va, vb) va.insert(va.end(), vb.begin(), vb.end())
using namespace std;
typedef vector<int> vint;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
// vector< vector<int> > v (size1, vector<int>(size2) );
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() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
vector<ll> A(m), B(m);
FOR(i, 0, m) cin >> A[i] >> B[i], A[i]--, B[i]--;
UnionFind uf(n);
ll ans = n * (n - 1) / 2;
vector<ll> res;
FOR(i, 0, m) {
res.pb(ans);
ll a = A[m - i - 1], b = B[m - i - 1];
if (uf.issame(a, b))
continue;
ll sa = uf.size(a), sb = uf.size(b);
ans -= sa * sb;
uf.merge(a, b);
}
REV(res);
FOR(i, 0, res.size()) cout << res[i] << endl;
}
// 73 | replace | 66 | 67 | 66 | 67 | 0 | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())
using namespace std;
class UnionFind {
public:
vector<int> Parent;
// コンストラクタ
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)]; }
// 二つの木が同じ場合はtrueを返す
bool sameTree(int a, int b) {
int ra = root(a);
int rb = root(b);
return ra == rb;
}
// 異なる二つの木を結合する
void unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb)
return;
if (size(ra) < size(rb))
swap(ra, rb);
Parent[ra] += Parent[rb];
Parent[rb] = a;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
// 配列を用意しているのは0-Nまでだからそこの位置に合わせている
A[i]--;
B[i]--;
}
vector<long long> ans(M);
// 引き始めの初期値はNC2を採用(Nこの中から2個を選択する繋げからからそのパターン通り存在)
ans[M - 1] = (ll)N * (N - 1) / 2;
UnionFind Uni(N);
for (int i = M - 1; i > 0; --i) {
ans[i - 1] = ans[i];
if (!Uni.sameTree(A[i], B[i])) {
ans[i - 1] -= (ll)Uni.size(A[i]) * Uni.size(B[i]);
Uni.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 <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(), (c).end())
#define SZ(x) ((int)(x).size())
using namespace std;
class UnionFind {
public:
vector<int> Parent;
// コンストラクタ
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)]; }
// 二つの木が同じ場合はtrueを返す
bool sameTree(int a, int b) {
int ra = root(a);
int rb = root(b);
return ra == rb;
}
// 異なる二つの木を結合する
void unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb)
return;
if (size(ra) < size(rb))
swap(ra, rb);
Parent[ra] += Parent[rb];
Parent[rb] = ra;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
// 配列を用意しているのは0-Nまでだからそこの位置に合わせている
A[i]--;
B[i]--;
}
vector<long long> ans(M);
// 引き始めの初期値はNC2を採用(Nこの中から2個を選択する繋げからからそのパターン通り存在)
ans[M - 1] = (ll)N * (N - 1) / 2;
UnionFind Uni(N);
for (int i = M - 1; i > 0; --i) {
ans[i - 1] = ans[i];
if (!Uni.sameTree(A[i], B[i])) {
ans[i - 1] -= (ll)Uni.size(A[i]) * Uni.size(B[i]);
Uni.unite(A[i], B[i]);
}
}
for (int i = 0; i < M; i++) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 62 | 63 | 62 | 63 | 0 | |
p03108 | C++ | Runtime Error | /*
このコード、と~おれ!
Be accepted!
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
.· ´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·'* ☆
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
////多倍長整数, cpp_intで宣言
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #pragma gcc target ("avx2")
// #pragma gcc optimization ("Ofast")
// #pragma gcc optimization ("unroll-loops")
#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, n) for (int i = 2; i < (n); ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define reprm(i, n) for (int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) // 少数出力
#define printLdb(a) printf("%.50Lf\n", a) // 少数出力
#define printdbd(a) printf("%.16lf\n", a) // 少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) // string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg) / 360.0L) * 2.0L * PI)
#define rad_to_deg(rad) (((rad) / 2.0L / PI) * 360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) \
(abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);
using namespace std;
void scans(string &str) {
char c;
str = "";
scanf("%c", &c);
if (c == '\n')
scanf("%c", &c);
while (c != '\n' && c != -1 && c != ' ') {
str += c;
scanf("%c", &c);
}
}
void scanc(char &str) {
char c;
scanf("%c", &c);
if (c == -1)
return;
while (c == '\n') {
scanf("%c", &c);
}
str = c;
}
double acot(double x) { return PI / 2 - atan(x); }
ll LSB(ll n) { return (n & (-n)); }
/*-----------------------------------------ここからコード-----------------------------------------*/
/*
* @title unionfind
* @docs kyopro/docs/unionfind.md
*/
// 0-indexed
struct unionfind {
vector<ll> par, siz;
unionfind(ll n) : par(n), siz(n) {
for (ll i = 0; i < n; ++i) {
// 全部根で初期化
par[i] = i;
// サイズは1
siz[i] = 1;
}
}
void init(ll n) {
par.resize(n);
siz.resize(n);
for (ll i = 0; i < n; ++i) {
// 全部根で初期化
par[i] = i;
// サイズは1
siz[i] = 1;
}
}
// 根を返す
ll find(ll a) { return par[a] == a ? a : par[a] = find(par[a]); }
// くっつける。元から同じだったらfalseを返す
bool unite(ll a, ll b) {
ll x = find(a), y = find(b);
if (x == y)
return false;
else if (siz[x] < siz[y]) {
par[x] = y;
siz[y] += siz[x];
} else if (siz[x] > siz[y]) {
par[y] = x;
siz[x] += siz[y];
} else {
par[x] = y;
siz[y] += siz[x];
}
return true;
}
// 同じ集合か判定する
bool same(ll a, ll b) { return find(a) == find(b); }
// サイズを返す
ll size(ll a) { return siz[find(a)]; }
// 同じ集合に属す葉を纏めて返す
vector<ll> leaf(ll a) {
vector<ll> x;
ll n = par.size();
for (ll i = 0; i < n; ++i)
if (same(a, i))
x.push_back(i);
return x;
}
};
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<pair<int, int>> p(m);
for (auto &[a, b] : p)
scanf("%d%d", &a, &b);
reverse(all(p));
vector<ll> ans(m);
ans[0] = (ll)n * (ll)(n - 1) / 2LL;
unionfind uni(n);
rep(i, m) {
if (i == m - 1)
break;
ans[i + 1] = ans[i];
const auto &[a, b] = p[i];
if (uni.find(a) != uni.find(b)) {
ans[i + 1] -= uni.size(a) * uni.size(b);
}
uni.unite(a, b);
}
reverse(all(ans));
for (const auto &aa : ans)
printf("%lld\n", aa);
Please AC;
} | /*
このコード、と~おれ!
Be accepted!
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
.· ´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·'* ☆
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
////多倍長整数, cpp_intで宣言
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #pragma gcc target ("avx2")
// #pragma gcc optimization ("Ofast")
// #pragma gcc optimization ("unroll-loops")
#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, n) for (int i = 2; i < (n); ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define reprm(i, n) for (int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) // 少数出力
#define printLdb(a) printf("%.50Lf\n", a) // 少数出力
#define printdbd(a) printf("%.16lf\n", a) // 少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) // string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg) / 360.0L) * 2.0L * PI)
#define rad_to_deg(rad) (((rad) / 2.0L / PI) * 360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) \
(abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);
using namespace std;
void scans(string &str) {
char c;
str = "";
scanf("%c", &c);
if (c == '\n')
scanf("%c", &c);
while (c != '\n' && c != -1 && c != ' ') {
str += c;
scanf("%c", &c);
}
}
void scanc(char &str) {
char c;
scanf("%c", &c);
if (c == -1)
return;
while (c == '\n') {
scanf("%c", &c);
}
str = c;
}
double acot(double x) { return PI / 2 - atan(x); }
ll LSB(ll n) { return (n & (-n)); }
/*-----------------------------------------ここからコード-----------------------------------------*/
/*
* @title unionfind
* @docs kyopro/docs/unionfind.md
*/
// 0-indexed
struct unionfind {
vector<ll> par, siz;
unionfind(ll n) : par(n), siz(n) {
for (ll i = 0; i < n; ++i) {
// 全部根で初期化
par[i] = i;
// サイズは1
siz[i] = 1;
}
}
void init(ll n) {
par.resize(n);
siz.resize(n);
for (ll i = 0; i < n; ++i) {
// 全部根で初期化
par[i] = i;
// サイズは1
siz[i] = 1;
}
}
// 根を返す
ll find(ll a) { return par[a] == a ? a : par[a] = find(par[a]); }
// くっつける。元から同じだったらfalseを返す
bool unite(ll a, ll b) {
ll x = find(a), y = find(b);
if (x == y)
return false;
else if (siz[x] < siz[y]) {
par[x] = y;
siz[y] += siz[x];
} else if (siz[x] > siz[y]) {
par[y] = x;
siz[x] += siz[y];
} else {
par[x] = y;
siz[y] += siz[x];
}
return true;
}
// 同じ集合か判定する
bool same(ll a, ll b) { return find(a) == find(b); }
// サイズを返す
ll size(ll a) { return siz[find(a)]; }
// 同じ集合に属す葉を纏めて返す
vector<ll> leaf(ll a) {
vector<ll> x;
ll n = par.size();
for (ll i = 0; i < n; ++i)
if (same(a, i))
x.push_back(i);
return x;
}
};
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<pair<int, int>> p(m);
for (auto &[a, b] : p)
scanf("%d%d", &a, &b);
reverse(all(p));
vector<ll> ans(m);
ans[0] = (ll)n * (ll)(n - 1) / 2LL;
unionfind uni(n);
rep(i, m) {
if (i == m - 1)
break;
ans[i + 1] = ans[i];
auto &[a, b] = p[i];
--a, --b;
if (uni.find(a) != uni.find(b)) {
ans[i + 1] -= uni.size(a) * uni.size(b);
}
uni.unite(a, b);
}
reverse(all(ans));
for (const auto &aa : ans)
printf("%lld\n", aa);
Please AC;
} | replace | 186 | 187 | 186 | 188 | 0 | |
p03108 | C++ | Runtime Error | // Robs Code
#include <bits/stdc++.h>
#define speed \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ll long long int
#define fri(l, k) for (i = l; i <= k; i++)
#define frj(l, k) for (j = l; j >= k; j--)
#define all(x) x.begin(), x.end()
#define ed cout << "\n";
#define pb push_back
#define pii pair<ll, ll>
#define _1 first
#define _2 second
#define maxi 100000007
/**********************************************************/
using namespace std;
ll i, j, k, n, m, q, t, a, b, c, cnt, sum, tot;
ll part[10001], sz[100001], cur, connect;
void init(ll n) {
for (i = 1; i <= n; i++) {
part[i] = i;
sz[i] = 1;
}
connect = n;
}
ll findParent(ll k) {
while (k != part[k]) {
part[k] = part[part[k]];
k = part[k];
}
return k;
}
// ll findParent(ll k){
// return part[k]==k?k:part[k]=findParent(k);
// }
void unite(ll x, ll y) {
x = findParent(x), y = findParent(y);
if (x == y)
return;
cur -= (sz[x] * sz[y]);
sz[x] += sz[y];
sz[y] = 0;
part[y] = x;
}
int main() {
speed;
cin >> n >> m;
init(n);
vector<pii> v(m);
fri(0, m - 1) cin >> v[i]._1 >> v[i]._2;
vector<ll> ans;
cur = n * (n - 1) / 2;
for (i = m - 1; i >= 0; i--) {
ans.pb(cur);
unite(v[i]._1, v[i]._2);
}
for (i = ans.size() - 1; i >= 0; i--)
cout << ans[i] << endl;
return 0;
}
| // Robs Code
#include <bits/stdc++.h>
#define speed \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ll long long int
#define fri(l, k) for (i = l; i <= k; i++)
#define frj(l, k) for (j = l; j >= k; j--)
#define all(x) x.begin(), x.end()
#define ed cout << "\n";
#define pb push_back
#define pii pair<ll, ll>
#define _1 first
#define _2 second
#define maxi 100000007
/**********************************************************/
using namespace std;
ll i, j, k, n, m, q, t, a, b, c, cnt, sum, tot;
ll part[100001], sz[100001], cur, connect;
void init(ll n) {
for (i = 1; i <= n; i++) {
part[i] = i;
sz[i] = 1;
}
connect = n;
}
ll findParent(ll k) {
while (k != part[k]) {
part[k] = part[part[k]];
k = part[k];
}
return k;
}
// ll findParent(ll k){
// return part[k]==k?k:part[k]=findParent(k);
// }
void unite(ll x, ll y) {
x = findParent(x), y = findParent(y);
if (x == y)
return;
cur -= (sz[x] * sz[y]);
sz[x] += sz[y];
sz[y] = 0;
part[y] = x;
}
int main() {
speed;
cin >> n >> m;
init(n);
vector<pii> v(m);
fri(0, m - 1) cin >> v[i]._1 >> v[i]._2;
vector<ll> ans;
cur = n * (n - 1) / 2;
for (i = m - 1; i >= 0; i--) {
ans.pb(cur);
unite(v[i]._1, v[i]._2);
}
for (i = ans.size() - 1; i >= 0; i--)
cout << ans[i] << endl;
return 0;
}
| replace | 19 | 20 | 19 | 20 | 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); }
// 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を直接くっつけず、親どうしをくっつける
A = root(A);
B = root(B);
if (A == B) { // すでにくっついてる
return false;
}
// おおきいほうを親にしたい
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新
Parent[A] += Parent[B];
// BをAにつるす
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.connect(A[i], B[i]);
} else {
ans[i - 1] = ans[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); }
// 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を直接くっつけず、親どうしをくっつける
A = root(A);
B = root(B);
if (A == B) { // すでにくっついてる
return false;
}
// おおきいほうを親にしたい
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新
Parent[A] += Parent[B];
// BをAにつるす
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.connect(A[i], B[i]);
} else {
ans[i - 1] = ans[i];
}
}
for (int i = 0; i < M; i++) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 50 | 51 | 50 | 51 | -6 | free(): invalid pointer
|
p03108 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
using namespace std;
#define LLInt long long int
LLInt n, m;
int par[100001];
LLInt t_size[100001];
int find(int x) {
if (par[x] == x)
return x;
int res = find(par[x]);
return res;
}
void unite(int x, int y) {
int rootx = find(x), rooty = find(y);
par[rooty] = rootx;
t_size[rootx] += t_size[rooty];
}
int main() {
int a[100001], b[100001];
cin >> n >> m;
for (int i = 0; i < m; i++) {
scanf("%d %d", &a[i], &b[i]);
}
// init
for (int i = 0; i <= 100000; i++) {
par[i] = i;
t_size[i] = 1;
}
LLInt ans[100000];
ans[m - 1] = n * (n - 1) / 2;
for (int i = 2; i <= m; i++) {
int roota = find(a[m - i + 1]), rootb = find(b[m - i + 1]);
ans[m - i] = ans[m - i + 1];
if (roota != rootb) {
ans[m - i] -= t_size[roota] * t_size[rootb];
unite(roota, rootb);
}
}
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <cstdio>
#include <iostream>
using namespace std;
#define LLInt long long int
LLInt n, m;
int par[100001];
LLInt t_size[100001];
int find(int x) {
if (par[x] == x)
return x;
par[x] = find(par[x]);
return par[x];
}
void unite(int x, int y) {
int rootx = find(x), rooty = find(y);
par[rooty] = rootx;
t_size[rootx] += t_size[rooty];
}
int main() {
int a[100001], b[100001];
cin >> n >> m;
for (int i = 0; i < m; i++) {
scanf("%d %d", &a[i], &b[i]);
}
// init
for (int i = 0; i <= 100000; i++) {
par[i] = i;
t_size[i] = 1;
}
LLInt ans[100000];
ans[m - 1] = n * (n - 1) / 2;
for (int i = 2; i <= m; i++) {
int roota = find(a[m - i + 1]), rootb = find(b[m - i + 1]);
ans[m - i] = ans[m - i + 1];
if (roota != rootb) {
ans[m - i] -= t_size[roota] * t_size[rootb];
unite(roota, rootb);
}
}
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
} | replace | 14 | 16 | 14 | 16 | TLE | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
class UnionFind {
vector<int> par;
int groupCount;
public:
// UnionFind uf(n)でn要素のUF木uf
UnionFind(int n = 0) { init(n); }
// n要素で初期化
void init(int n = 0) {
par.resize(n);
fill(par.begin(), par.end(), -1);
groupCount = n;
return;
}
// xが属する集合の根を求める
int root(int x) {
if (par[x] < 0) {
return x;
}
return par[x] = root(par[x]);
}
// xとyが同じ集合に属するか調べる
bool same(int x, int y) { return root(x) == root(y); }
// xが属する集合の大きさを調べる
int size(int x) { return -par[root(x)]; }
// xが属する集合とyが属する集合を繋ぐ
// 既に繋がっていたらfalseが返る
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (par[y] < par[x]) {
swap(x, y);
}
par[x] += par[y];
par[y] = x;
--groupCount;
return true;
}
// UF内にある集合の個数を調べる
int size(void) { return groupCount; }
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
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 uf(n);
vector<long long> ans(m);
long long now = (long long)n * (n - 1LL) / 2LL;
for (int i = m - 1; i >= 0; --i) {
ans[i] = now;
long long as = uf.size(a[i]), bs = uf.size(b[i]);
if (uf.unite(a[i], b[i])) {
now -= as * bs;
}
}
for (int i = 0; i < m; ++i) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
class UnionFind {
vector<int> par;
int groupCount;
public:
// UnionFind uf(n)でn要素のUF木uf
UnionFind(int n = 0) { init(n); }
// n要素で初期化
void init(int n = 0) {
par.resize(n);
fill(par.begin(), par.end(), -1);
groupCount = n;
return;
}
// xが属する集合の根を求める
int root(int x) {
if (par[x] < 0) {
return x;
}
return par[x] = root(par[x]);
}
// xとyが同じ集合に属するか調べる
bool same(int x, int y) { return root(x) == root(y); }
// xが属する集合の大きさを調べる
int size(int x) { return -par[root(x)]; }
// xが属する集合とyが属する集合を繋ぐ
// 既に繋がっていたらfalseが返る
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (par[y] < par[x]) {
swap(x, y);
}
par[x] += par[y];
par[y] = x;
--groupCount;
return true;
}
// UF内にある集合の個数を調べる
int size(void) { return groupCount; }
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
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<long long> ans(m);
long long now = (long long)n * (n - 1LL) / 2LL;
for (int i = m - 1; i >= 0; --i) {
ans[i] = now;
long long as = uf.size(a[i]), bs = uf.size(b[i]);
if (uf.unite(a[i], b[i])) {
now -= as * bs;
}
}
for (int i = 0; i < m; ++i) {
cout << ans[i] << endl;
}
return 0;
} | replace | 61 | 62 | 61 | 62 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define INF (i64)(1e18)
#define MOD (i64)(1e9 + 7)
#define REP(i, n) for (i64 i = 0; i < (n); i++)
#define RREP(i, n) for (i64 i = (n)-1; i >= 0; i--)
#define RANGE(i, a, b) for (i64 i = (a); i < (b); i++)
#define RRANGE(i, a, b) for (i64 i = (b)-1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define SIZE(v) ((i64)(v).size())
template <class T> inline void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
struct UnionFind {
vector<i64> par, sz;
UnionFind(i64 n) : par(n), sz(n, 1) { REP(i, n) par[i] = i; }
i64 root(i64 x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(i64 x, i64 y) {
i64 rx = root(x), ry = root(y);
if (rx == ry)
return;
if (size(rx) < size(ry))
swap(rx, ry);
par[ry] = rx;
sz[rx] += sz[ry];
}
i64 size(i64 x) { return sz[root(x)]; }
bool same(i64 x, i64 y) { return root(x) == root(y); }
};
int main() {
i64 n, m;
cin >> n >> m;
vector<i64> a(m), b(m);
REP(i, m) {
cin >> a.at(i) >> b.at(i);
a[i]--;
b[i]--;
}
UnionFind uf(n);
i64 memo = n * (n - 1) / 2;
vector<i64> ans(n);
RREP(i, m) {
ans[i] = memo;
if (uf.same(a[i], b[i]))
continue;
memo -= uf.size(a[i]) * uf.size(b[i]);
uf.unite(a[i], b[i]);
}
for (auto &x : ans)
cout << x << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
#define INF (i64)(1e18)
#define MOD (i64)(1e9 + 7)
#define REP(i, n) for (i64 i = 0; i < (n); i++)
#define RREP(i, n) for (i64 i = (n)-1; i >= 0; i--)
#define RANGE(i, a, b) for (i64 i = (a); i < (b); i++)
#define RRANGE(i, a, b) for (i64 i = (b)-1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define SIZE(v) ((i64)(v).size())
template <class T> inline void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
struct UnionFind {
vector<i64> par, sz;
UnionFind(i64 n) : par(n), sz(n, 1) { REP(i, n) par[i] = i; }
i64 root(i64 x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(i64 x, i64 y) {
i64 rx = root(x), ry = root(y);
if (rx == ry)
return;
if (size(rx) < size(ry))
swap(rx, ry);
par[ry] = rx;
sz[rx] += sz[ry];
}
i64 size(i64 x) { return sz[root(x)]; }
bool same(i64 x, i64 y) { return root(x) == root(y); }
};
int main() {
i64 n, m;
cin >> n >> m;
vector<i64> a(m), b(m);
REP(i, m) {
cin >> a.at(i) >> b.at(i);
a[i]--;
b[i]--;
}
UnionFind uf(n);
i64 memo = n * (n - 1) / 2;
vector<i64> ans(m);
RREP(i, m) {
ans[i] = memo;
if (uf.same(a[i], b[i]))
continue;
memo -= uf.size(a[i]) * uf.size(b[i]);
uf.unite(a[i], b[i]);
}
for (auto &x : ans)
cout << x << endl;
return 0;
}
| replace | 54 | 55 | 54 | 55 | 0 | |
p03108 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
struct UnionFind {
vector<long> par;
UnionFind(long N) : par(N) {
for (long i = 0; i < N; ++i)
par[i] = i;
}
long root(long x) {
if (par[x] == x)
return x;
else
root(par[x]);
}
void unite(long x, long y) {
long rx = root(x);
long ry = root(y);
if (rx == ry)
return;
else
par[rx] = ry;
}
bool same(long x, long y) {
long rx = root(x);
long ry = root(y);
return rx == ry;
}
};
int main() {
long N, M;
cin >> N >> M;
long A[M], B[M];
for (long i = 0; i < M; ++i) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
UnionFind tree(N);
long ans[M];
ans[M - 1] = N * (N - 1) / 2;
map<long, long> size;
for (long i = 0; i < N; ++i) {
size[i] = 1;
}
for (long i = M - 1; i >= 0; --i) {
if (!tree.same(A[i], B[i]) & i > 0) {
ans[i - 1] = ans[i] - size[tree.root(A[i])] * size[tree.root(B[i])];
long a = size[tree.root(A[i])];
long b = size[tree.root(B[i])];
size[tree.root(A[i])] += b;
size[tree.root(B[i])] += a;
tree.unite(A[i], B[i]);
} else if (i > 0) {
ans[i - 1] = ans[i];
}
}
for (long i = 0; i < M; ++i) {
cout << ans[i] << endl;
}
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
struct UnionFind {
vector<long> par;
UnionFind(long N) : par(N) {
for (long i = 0; i < N; ++i)
par[i] = i;
}
long root(long x) {
if (par[x] == x)
return x;
else
root(par[x]);
}
void unite(long x, long y) {
long rx = root(x);
long ry = root(y);
if (rx == ry)
return;
else
par[rx] = ry;
}
bool same(long x, long y) {
long rx = root(x);
long ry = root(y);
return rx == ry;
}
};
int main() {
long N, M;
cin >> N >> M;
long A[M], B[M];
for (long i = 0; i < M; ++i) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
UnionFind tree(N);
long ans[M];
ans[M - 1] = N * (N - 1) / 2;
map<long, long> size;
for (long i = 0; i < N; ++i) {
size[i] = 1;
}
for (long i = M - 1; i >= 0; --i) {
if (!tree.same(A[i], B[i]) & i > 0) {
ans[i - 1] = ans[i] - size[tree.root(A[i])] * size[tree.root(B[i])];
long a = size[tree.root(A[i])];
long b = size[tree.root(B[i])];
size[tree.root(A[i])] += b;
size[tree.root(B[i])] += a;
if (b > a) {
tree.unite(A[i], B[i]);
} else {
tree.unite(B[i], A[i]);
}
} else if (i > 0) {
ans[i - 1] = ans[i];
}
}
for (long i = 0; i < M; ++i) {
cout << ans[i] << endl;
}
} | replace | 64 | 65 | 64 | 69 | TLE | |
p03108 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 9999999999
#define m0(x) memset(x, 0, sizeof(x))
// #define fill(x,y) memset(x,y,sizeof(x))
#define print(x) cout << x << endl;
#define pe(x) cout << x << " ";
#define lb(v, n) lower_bound(v.begin(), v.end(), n);
#define ub(v, n) upper_bound(v.begin(), v.end(), n);
#define int long long
using namespace std;
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;
}
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[9] = {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dyy[9] = {0, 1, -1, 0, 1, -1, 0, 1, -1};
ll gcd(ll x, ll y) {
ll m = max(x, y), n = min(x, y);
if (m % n == 0)
return n;
else
return gcd(m % n, n);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
long long power_mod(long long x, long long n, long long m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return power_mod(x * x % m, n / 2, m);
else
return x * power_mod(x, n - 1, m) % m;
}
long long power(long long a, long long n) { // aのn乗を計算します。
long long x = 1;
while (n > 0) { // 全てのbitが捨てられるまで。
if (n & 1) { // 1番右のbitが1のとき。
x = x * a;
}
a = a * a;
n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
}
return x;
}
long long nCr(long long n, long long r) {
if (r > n / 2)
r = n - r; // because C(n, r) == C(n, n - r)
long long ans = 1;
long long i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
const int MAX = 100010;
const int MOD = 998244353;
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;
}
bool arr[100000100];
vector<ll> sosuu;
void Eratosthenes() {
ll N = 10000010;
int c = 0;
for (int i = 0; i < N; i++) {
arr[i] = 1;
}
for (ll i = 2; i < sqrt(N); i++) {
if (arr[i]) {
for (ll j = 0; i * (j + 2) < N; j++) {
arr[i * (j + 2)] = 0;
}
}
}
for (ll i = 2; i < N; i++) {
if (arr[i]) {
sosuu.pb(i);
// cout << sosuu[c] << " ";
c++;
}
}
// cout << endl;
// cout << c << endl;
}
ll stoL(string s) {
ll n = s.length();
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += power(10, n - i - 1) * (ll)(s[i] - '0');
}
return ans;
}
typedef pair<int, int> P;
void getbit(ll x, int bits[]) {
int cnt = 0;
while (x > 0) {
if (x % 2 == 0)
bits[cnt] = 0;
else
bits[cnt] = 1;
x /= 2;
cnt++;
}
}
void addbit(ll x, int bits[]) {
int cnt = 0;
while (x > 0) {
if (x % 2 == 0)
bits[cnt] += 0;
else
bits[cnt] += 1;
x /= 2;
cnt++;
}
}
vector<int> A, B, num, num2, G[100010];
vector<int> ans;
map<int, int> mp;
int siz[MAX];
int par[MAX]; // 親
int Rank[MAX]; // 木の深さ
// n要素で初期化
void init(int n) {
REP(i, n) {
par[i] = i;
Rank[i] = 0;
siz[i] = 1;
}
}
// 木の根を求める
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]); // 経路圧縮(根に直接つなぎなおす)もしつつ
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
// 低いほうを高いほうにつなげる(効率化)
if (Rank[x] < Rank[y]) {
par[x] = y;
siz[x] += siz[y];
siz[y] = siz[x];
} else {
siz[y] += siz[x];
siz[x] = siz[y];
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
void uni(int n, int cnt) {
for (auto x : G[n]) {
if (num[x] != -1) {
num[n] = num[x];
mp[num[n]]++;
return;
}
}
num[n] = cnt;
mp[num[n]]++;
for (auto x : G[n]) {
uni(x, cnt);
}
}
void paint(int n, int cnt) {
mp[num[n]]--;
mp[cnt]++;
num[n] = cnt;
for (auto x : G[n]) {
if (num[x] != cnt) {
paint(x, cnt);
}
}
}
signed main() {
int N, M;
cin >> N >> M;
num.resize(N);
init(N);
REP(i, N) num[i] = -1;
A.resize(M);
B.resize(M);
REP(i, M) {
cin >> A[i] >> B[i];
A[i]--, B[i]--;
}
ll res = N * (N - 1) / 2;
VREVERSE(A);
VREVERSE(B);
int cnt = 1;
ans.pb(res);
REP(i, M) {
if (num[A[i]] == -1) {
num[A[i]] = cnt;
mp[cnt]++;
// print(mp[num[A[i]]]);
cnt++;
}
if (num[B[i]] == -1) {
num[B[i]] = cnt;
mp[cnt]++;
cnt++;
}
G[A[i]].pb(B[i]);
G[B[i]].pb(B[i]);
if (!same(A[i], B[i])) {
// cout<<"mp[num[A[i]]]:"<<mp[num[A[i]]]<<"mp[num[B[i]]]"<<mp[num[B[i]]]<<endl;
// res -= max((ll)1,mp[num[A[i]]]) * max((ll)1,mp[num[B[i]]]);
// res -= mp[num[A[i]]] * mp[num[B[i]]];
res -= siz[find(A[i])] * siz[find(B[i])];
unite(A[i], B[i]);
}
// res = max(res, (ll)0);
paint(B[i], num[A[i]]);
ans.pb(res);
/*if (num[A[i]] != -1 && num[B[i]] != -1) {
if(num[A[i]!=num[B[i]]])res -= mp[num[A[i]]] * mp[num[B[i]]];
}
else if (num[A[i]] != -1) {
res -= mp[num[A[i]]];
}
else if (num[B[i]] != -1) {
res -= mp[num[B[i]]];
}
else res--;*/
}
VREVERSE(ans);
FOR(i, 1, M + 1) { print(ans[i]); }
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 9999999999
#define m0(x) memset(x, 0, sizeof(x))
// #define fill(x,y) memset(x,y,sizeof(x))
#define print(x) cout << x << endl;
#define pe(x) cout << x << " ";
#define lb(v, n) lower_bound(v.begin(), v.end(), n);
#define ub(v, n) upper_bound(v.begin(), v.end(), n);
#define int long long
using namespace std;
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;
}
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[9] = {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dyy[9] = {0, 1, -1, 0, 1, -1, 0, 1, -1};
ll gcd(ll x, ll y) {
ll m = max(x, y), n = min(x, y);
if (m % n == 0)
return n;
else
return gcd(m % n, n);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
long long power_mod(long long x, long long n, long long m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return power_mod(x * x % m, n / 2, m);
else
return x * power_mod(x, n - 1, m) % m;
}
long long power(long long a, long long n) { // aのn乗を計算します。
long long x = 1;
while (n > 0) { // 全てのbitが捨てられるまで。
if (n & 1) { // 1番右のbitが1のとき。
x = x * a;
}
a = a * a;
n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
}
return x;
}
long long nCr(long long n, long long r) {
if (r > n / 2)
r = n - r; // because C(n, r) == C(n, n - r)
long long ans = 1;
long long i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
const int MAX = 100010;
const int MOD = 998244353;
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;
}
bool arr[100000100];
vector<ll> sosuu;
void Eratosthenes() {
ll N = 10000010;
int c = 0;
for (int i = 0; i < N; i++) {
arr[i] = 1;
}
for (ll i = 2; i < sqrt(N); i++) {
if (arr[i]) {
for (ll j = 0; i * (j + 2) < N; j++) {
arr[i * (j + 2)] = 0;
}
}
}
for (ll i = 2; i < N; i++) {
if (arr[i]) {
sosuu.pb(i);
// cout << sosuu[c] << " ";
c++;
}
}
// cout << endl;
// cout << c << endl;
}
ll stoL(string s) {
ll n = s.length();
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += power(10, n - i - 1) * (ll)(s[i] - '0');
}
return ans;
}
typedef pair<int, int> P;
void getbit(ll x, int bits[]) {
int cnt = 0;
while (x > 0) {
if (x % 2 == 0)
bits[cnt] = 0;
else
bits[cnt] = 1;
x /= 2;
cnt++;
}
}
void addbit(ll x, int bits[]) {
int cnt = 0;
while (x > 0) {
if (x % 2 == 0)
bits[cnt] += 0;
else
bits[cnt] += 1;
x /= 2;
cnt++;
}
}
vector<int> A, B, num, num2, G[100010];
vector<int> ans;
map<int, int> mp;
int siz[MAX];
int par[MAX]; // 親
int Rank[MAX]; // 木の深さ
// n要素で初期化
void init(int n) {
REP(i, n) {
par[i] = i;
Rank[i] = 0;
siz[i] = 1;
}
}
// 木の根を求める
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]); // 経路圧縮(根に直接つなぎなおす)もしつつ
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
// 低いほうを高いほうにつなげる(効率化)
if (Rank[x] < Rank[y]) {
par[x] = y;
siz[x] += siz[y];
siz[y] = siz[x];
} else {
siz[y] += siz[x];
siz[x] = siz[y];
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
void uni(int n, int cnt) {
for (auto x : G[n]) {
if (num[x] != -1) {
num[n] = num[x];
mp[num[n]]++;
return;
}
}
num[n] = cnt;
mp[num[n]]++;
for (auto x : G[n]) {
uni(x, cnt);
}
}
void paint(int n, int cnt) {
mp[num[n]]--;
mp[cnt]++;
num[n] = cnt;
for (auto x : G[n]) {
if (num[x] != cnt) {
paint(x, cnt);
}
}
}
signed main() {
int N, M;
cin >> N >> M;
num.resize(N);
init(N);
REP(i, N) num[i] = -1;
A.resize(M);
B.resize(M);
REP(i, M) {
cin >> A[i] >> B[i];
A[i]--, B[i]--;
}
ll res = N * (N - 1) / 2;
VREVERSE(A);
VREVERSE(B);
int cnt = 1;
ans.pb(res);
REP(i, M) {
if (num[A[i]] == -1) {
num[A[i]] = cnt;
mp[cnt]++;
// print(mp[num[A[i]]]);
cnt++;
}
if (num[B[i]] == -1) {
num[B[i]] = cnt;
mp[cnt]++;
cnt++;
}
G[A[i]].pb(B[i]);
G[B[i]].pb(B[i]);
if (!same(A[i], B[i])) {
// cout<<"mp[num[A[i]]]:"<<mp[num[A[i]]]<<"mp[num[B[i]]]"<<mp[num[B[i]]]<<endl;
// res -= max((ll)1,mp[num[A[i]]]) * max((ll)1,mp[num[B[i]]]);
// res -= mp[num[A[i]]] * mp[num[B[i]]];
res -= siz[find(A[i])] * siz[find(B[i])];
unite(A[i], B[i]);
}
// res = max(res, (ll)0);
// paint(B[i], num[A[i]]);
ans.pb(res);
/*if (num[A[i]] != -1 && num[B[i]] != -1) {
if(num[A[i]!=num[B[i]]])res -= mp[num[A[i]]] * mp[num[B[i]]];
}
else if (num[A[i]] != -1) {
res -= mp[num[A[i]]];
}
else if (num[B[i]] != -1) {
res -= mp[num[B[i]]];
}
else res--;*/
}
VREVERSE(ans);
FOR(i, 1, M + 1) { print(ans[i]); }
}
| replace | 299 | 300 | 299 | 300 | TLE | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
struct UnionFind {
vector<ll> link;
UnionFind(ll n) : link(n, -1) {}
ll find(ll v) {
if (link[v] < 0)
return v;
else
return link[v] = find(link[v]);
}
bool unite(ll v, ll w) {
v = find(v), w = find(w);
if (v == w)
return 0;
if (link[v] > link[w])
swap(v, w);
link[v] += link[w];
link[w] = v;
return 1;
}
bool same(ll v, ll w) { return find(v) == find(w); }
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> a(m), b(m), ans(m);
rep(i, m) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
}
UnionFind uf(n);
ans[m - 1] = n * (n - 1) / 2;
for (ll i = m - 1; m >= 1; m--) {
ans[i - 1] = ans[i] - uf.link[uf.find(a[i])] * uf.link[uf.find(b[i])];
uf.unite(a[i], b[i]);
}
rep(i, m) cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
struct UnionFind {
vector<ll> link;
UnionFind(ll n) : link(n, -1) {}
ll find(ll v) {
if (link[v] < 0)
return v;
else
return link[v] = find(link[v]);
}
bool unite(ll v, ll w) {
v = find(v), w = find(w);
if (v == w)
return 0;
if (link[v] > link[w])
swap(v, w);
link[v] += link[w];
link[w] = v;
return 1;
}
bool same(ll v, ll w) { return find(v) == find(w); }
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> a(m), b(m), ans(m);
rep(i, m) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
}
UnionFind uf(n);
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
ans[i - 1] = ans[i] - uf.link[uf.find(a[i])] * uf.link[uf.find(b[i])];
uf.unite(a[i], b[i]);
}
rep(i, m) cout << ans[i] << endl;
return 0;
} | replace | 38 | 40 | 38 | 43 | 0 | |
p03108 | C++ | Runtime Error | // ABC 120 D;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 10010;
struct EDGE {
int u, v;
} e[maxn];
ll sz[maxn], p[maxn], ans[maxn];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int get_set(int x) {
if (x != p[x])
p[x] = get_set(p[x]);
return p[x];
}
int main() {
// freopen("","r",stdin);
// freopen("","w",stdout);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
e[i].u = read();
e[i].v = read();
}
ans[m] = (ll)(n - 1) * n / 2;
for (int i = 1; i <= n; i++)
p[i] = i, sz[i] = 1;
for (int i = m; i >= 1; i--) {
int u = get_set(e[i].u), v = get_set(e[i].v);
if (u == v)
ans[i - 1] = ans[i];
else {
ans[i - 1] = ans[i] - sz[u] * sz[v];
p[u] = v;
sz[v] += sz[u];
}
}
for (int i = 1; i <= m; i++)
cout << ans[i] << endl;
return 0;
} | // ABC 120 D;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100010;
struct EDGE {
int u, v;
} e[maxn];
ll sz[maxn], p[maxn], ans[maxn];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int get_set(int x) {
if (x != p[x])
p[x] = get_set(p[x]);
return p[x];
}
int main() {
// freopen("","r",stdin);
// freopen("","w",stdout);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
e[i].u = read();
e[i].v = read();
}
ans[m] = (ll)(n - 1) * n / 2;
for (int i = 1; i <= n; i++)
p[i] = i, sz[i] = 1;
for (int i = m; i >= 1; i--) {
int u = get_set(e[i].u), v = get_set(e[i].v);
if (u == v)
ans[i - 1] = ans[i];
else {
ans[i - 1] = ans[i] - sz[u] * sz[v];
p[u] = v;
sz[v] += sz[u];
}
}
for (int i = 1; i <= m; i++)
cout << ans[i] << endl;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号
UnionFind(ll N) : par(N) { // 最初は全てが根であるとして初期化
for (ll i = 0; i < N; i++)
par[i] = i, mp[i]++;
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
ll unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); // xの根をrx
ll ry = root(y); // yの根をry
if (rx == ry)
return 0; // xとyの根が同じ(=同じ木にある)時はそのまま
else {
ll sx = mp[rx], sy = mp[ry];
par[rx] = ry;
mp[ry] += mp[rx];
ll s = mp[ry];
return (s * (s - 1) - sx * (sx - 1) - sy * (sy - 1)) / 2;
}
// xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> a(m), b(m), ans(m);
for (ll i = 0; i < m; i++) {
cin >> a[i] >> b[i];
}
UnionFind tree(n);
ans[m - 1] = (n * (n - 1)) / 2;
for (ll i = m - 1; i >= 1; i--) {
ans[i - 1] = ans[i] - tree.unite(a[i], b[i]);
}
for (ll i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号
UnionFind(ll N) : par(N) { // 最初は全てが根であるとして初期化
for (ll i = 0; i < N; i++)
par[i] = i, mp[i]++;
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
ll unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); // xの根をrx
ll ry = root(y); // yの根をry
if (rx == ry)
return 0; // xとyの根が同じ(=同じ木にある)時はそのまま
else {
ll sx = mp[rx], sy = mp[ry];
par[rx] = ry;
mp[ry] += mp[rx];
ll s = mp[ry];
return (s * (s - 1) - sx * (sx - 1) - sy * (sy - 1)) / 2;
}
// xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> a(m), b(m), ans(m);
for (ll i = 0; i < m; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
UnionFind tree(n);
ans[m - 1] = (n * (n - 1)) / 2;
for (ll i = m - 1; i >= 1; i--) {
ans[i - 1] = ans[i] - tree.unite(a[i], b[i]);
}
for (ll i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
} | insert | 42 | 42 | 42 | 44 | 0 | |
p03108 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
int main() {
ll n, m;
cin >> n >> m;
vector<int> a(m), b(m);
rep(i, m) {
int na, nb;
cin >> na >> nb;
na--, nb--;
a[i] = na;
b[i] = nb;
}
vector<vector<int>> V(n);
rep(j, n) V[j].push_back(j);
vector<int> idx(n);
rep(j, n) idx[j] = j;
ll cnt = n * (n - 1) / 2; // take care not to be overflow
vector<ll> ans;
ans.push_back(cnt);
reverse(all(a));
reverse(all(b));
rep(i, m - 1) {
int aj = idx[a[i]], bj = idx[b[i]];
if (aj == bj) {
ans.push_back(cnt);
continue;
}
ll sza = (ll)V[aj].size(), szb = (ll)V[bj].size();
cnt -= sza * szb;
ans.push_back(cnt);
if (sza > szb)
swap(aj, bj);
for (auto x : V[bj]) {
V[aj].push_back(x);
idx[x] = aj;
}
V[bj].clear();
}
reverse(all(ans));
rep(i, m) printf("%lld\n", ans[i]);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
int main() {
ll n, m;
cin >> n >> m;
vector<int> a(m), b(m);
rep(i, m) {
int na, nb;
cin >> na >> nb;
na--, nb--;
a[i] = na;
b[i] = nb;
}
vector<vector<int>> V(n);
rep(j, n) V[j].push_back(j);
vector<int> idx(n);
rep(j, n) idx[j] = j;
ll cnt = n * (n - 1) / 2; // take care not to be overflow
vector<ll> ans;
ans.push_back(cnt);
reverse(all(a));
reverse(all(b));
rep(i, m - 1) {
int aj = idx[a[i]], bj = idx[b[i]];
if (aj == bj) {
ans.push_back(cnt);
continue;
}
ll sza = (ll)V[aj].size(), szb = (ll)V[bj].size();
cnt -= sza * szb;
ans.push_back(cnt);
if (sza < szb)
swap(aj, bj);
for (auto x : V[bj]) {
V[aj].push_back(x);
idx[x] = aj;
}
V[bj].clear();
}
reverse(all(ans));
rep(i, m) printf("%lld\n", ans[i]);
return 0;
}
| replace | 43 | 44 | 43 | 44 | TLE | |
p03108 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
int main() {
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
rep(i, m) {
int na, nb;
cin >> na >> nb;
na--, nb--;
a[i] = na;
b[i] = nb;
}
vector<set<int>> S(n);
rep(i, n) S[i].emplace(i);
vector<int> idx(n);
rep(i, n) idx[i] = i;
ll cnt = (ll)n * (n - 1) / 2; // take care not to be overflow
vector<ll> ans;
ans.push_back(cnt);
reverse(all(a));
reverse(all(b));
rep(i, m - 1) {
if (idx[a[i]] == idx[b[i]]) {
ans.push_back(cnt);
continue;
}
ll sza = (ll)S[idx[a[i]]].size();
ll szb = (ll)S[idx[b[i]]].size();
cnt -= sza * szb;
ans.push_back(cnt);
int addto = idx[a[i]];
int removefrom = idx[b[i]];
if (sza < szb) {
} else {
swap(addto, removefrom);
}
for (auto x : S[idx[removefrom]]) {
S[idx[addto]].emplace(x);
idx[x] = idx[addto];
}
S[removefrom].clear();
}
reverse(all(ans));
rep(i, m) printf("%lld\n", ans[i]);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
int main() {
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
rep(i, m) {
int na, nb;
cin >> na >> nb;
na--, nb--;
a[i] = na;
b[i] = nb;
}
vector<set<int>> S(n);
rep(i, n) S[i].emplace(i);
vector<int> idx(n);
rep(i, n) idx[i] = i;
ll cnt = (ll)n * (n - 1) / 2; // take care not to be overflow
vector<ll> ans;
ans.push_back(cnt);
reverse(all(a));
reverse(all(b));
rep(i, m - 1) {
if (idx[a[i]] == idx[b[i]]) {
ans.push_back(cnt);
continue;
}
ll sza = (ll)S[idx[a[i]]].size();
ll szb = (ll)S[idx[b[i]]].size();
cnt -= sza * szb;
ans.push_back(cnt);
int addto = idx[a[i]];
int removefrom = idx[b[i]];
if (sza < szb) {
swap(addto, removefrom);
}
for (auto x : S[idx[removefrom]]) {
S[idx[addto]].emplace(x);
idx[x] = idx[addto];
}
S[removefrom].clear();
}
reverse(all(ans));
rep(i, m) printf("%lld\n", ans[i]);
return 0;
}
| delete | 47 | 48 | 47 | 47 | TLE | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (ll i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
#include <vector>
using namespace std;
struct UnionFind {
vector<int> 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)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// cout<<setprecision(10);
ll N, M;
cin >> N >> M;
vector<pair<int, int>> edge(N);
vector<ll> ans(M);
rep(i, M) {
ll a, b;
cin >> a >> b;
edge[i] = make_pair(a, b);
}
ans[M - 1] = ((ll)(N - 1)) * N / 2;
UnionFind uf(N);
for (ll i = M - 2; i >= 0; i--) {
// cout<<"samegroup"<<endl;
// cout<<edge[i+1].first-1<<" "<<edge[i+1].second-1<<endl;
// cout<<is_same_group(edge[i+1].first-1-1,edge[i+1].second-1-1)<<endl;
if (uf.findSet(edge[i + 1].first - 1, edge[i + 1].second - 1)) {
ans[i] = ans[i + 1];
uf.unionSet(edge[i + 1].first - 1, edge[i + 1].second - 1);
} else {
ll sa = uf.size(edge[i + 1].first - 1);
ll sb = uf.size(edge[i + 1].second - 1);
// cout<<sa<<" "<<sb<<endl;
ans[i] = ans[i + 1] - sa * sb;
uf.unionSet(edge[i + 1].first - 1, edge[i + 1].second - 1);
}
}
cout << endl;
rep(i, M) { cout << ans[i] << '\n'; }
cout << endl;
return 0;
}
| #include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (ll i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
#include <vector>
using namespace std;
struct UnionFind {
vector<int> 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)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// cout<<setprecision(10);
ll N, M;
cin >> N >> M;
vector<pair<int, int>> edge(M);
vector<ll> ans(M);
rep(i, M) {
ll a, b;
cin >> a >> b;
edge[i] = make_pair(a, b);
}
ans[M - 1] = ((ll)(N - 1)) * N / 2;
UnionFind uf(N);
for (ll i = M - 2; i >= 0; i--) {
// cout<<"samegroup"<<endl;
// cout<<edge[i+1].first-1<<" "<<edge[i+1].second-1<<endl;
// cout<<is_same_group(edge[i+1].first-1-1,edge[i+1].second-1-1)<<endl;
if (uf.findSet(edge[i + 1].first - 1, edge[i + 1].second - 1)) {
ans[i] = ans[i + 1];
uf.unionSet(edge[i + 1].first - 1, edge[i + 1].second - 1);
} else {
ll sa = uf.size(edge[i + 1].first - 1);
ll sb = uf.size(edge[i + 1].second - 1);
// cout<<sa<<" "<<sb<<endl;
ans[i] = ans[i + 1] - sa * sb;
uf.unionSet(edge[i + 1].first - 1, edge[i + 1].second - 1);
}
}
cout << endl;
rep(i, M) { cout << ans[i] << '\n'; }
cout << endl;
return 0;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long SINT64;
typedef unsigned long long UINT64;
typedef double DOUBLE;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define rep(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define rrep(i, a, b) for (int(i) = int(a); (i) >= int(b); (i)--)
#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "
#define pute(a) cout << endl
#define INF 1000000001
#define MOD 1000000007
#define INF64 1000000000000000001
#define F first
#define S second
#define Pii pair<SINT32, SINT32>
#define Pll pair<SINT64, SINT64>
#define Piii pair<SINT32, pair<SINT32, SINT32>>
#define Plll pair<SINT64, pair<SINT64, SINT64>>
#define Vll(a, b, c) vector<vector<SINT64>> (a)((b),vector<SINT64>((c))
#define Vlll(a, b, c, d) vector<vector<vector<SINT64>>> (a)((b),vector<vector<SINT64>>((c),vector<SINT64>((d)))
using namespace std;
class UnionFind {
public:
vector<SINT64> parent;
UnionFind(SINT64 N) { parent = vector<SINT64>(N, -1); }
SINT64 root(SINT64 A) {
if (parent[A] < 0) {
return A;
} else {
parent[A] = root(parent[A]);
return parent[A];
}
}
SINT64 size(SINT64 A) { return parent[root(A)] * (-1); }
bool judge(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A == B) {
return true; // 同じグループ
} else {
return false; // 違うグループ
}
}
void connect(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A != B) {
if (size(A) < size(B)) {
swap(A, B);
}
parent[A] += parent[B];
parent[B] = A;
}
}
};
int main() {
SINT64 N;
cin >> N;
SINT64 M;
cin >> M;
vector<Pll> data(M);
vector<SINT64> ans;
SINT64 cnt = N * (N - 1) / 2;
ans.emplace_back(cnt);
UnionFind Uni(N);
rep(i, 0, M) {
cin >> data[i].F;
cin >> data[i].S;
}
rrep(i, M - 1, 0) {
if (Uni.judge(data[i].F, data[i].S) == false) {
cnt -= Uni.size(data[i].F) * Uni.size(data[i].S);
Uni.connect(data[i].F, data[i].S);
}
ans.emplace_back(cnt);
}
rrep(i, M - 1, 0) { put(ans[i]); }
return 0;
}
// vector<vector<SINT64>> data(N,vector<SINT32>(3));
////2次元配列 vector<vector<vector<SINT64>>>
//data(N,vector<vector<SINT64>>(3,vector<SINT64>(3))); //3次元配列
// Vll(data,N,N); //2次元配列
// Vlll(data,N,N,N); //3次元配列
// sort(data.begin(),data.end());
// sort(data.begin(),data.end(),std::greater<SINT64>());
// __gcd(A,B);
/* 複数条件ソート
bool sortcompare(Pll A, Pll B) {
if(A.F == B.F){
return A.S > B.S;
} else {
return A.F < B.F;
}
}
sort(data.begin(),data.end(),sortcompare);
*/
// data.emplace_back(BUF); //後ろに追加
// data.erase(std::unique(data.begin(), data.end()), data.end());
// //ソート後に使用 同じ値を消す
// data.insert(data.begin() + X, 0); //X番目の要素に0を挿入
// 隣接リスト
// vector<vector<SINT64>> data(N);
// data[ A ].emplace_back( B );
/*
vector<Pll> data(N);
rep(i,0,N) {
cin >> data[i].F;
cin >> data[i].S;
}
sort(data.begin(),data.end());
*/
/*
vector<Plll> data(N);
rep(i,0,N) {
cin >> data[i].F;
cin >> data[i].S.F;
cin >> data[i].S.S;
}
sort(data.begin(),data.end());
*/
// posi = lower_bound(data.begin(),data.end(), X) - data.begin();
// // X以上を探す posi = lower_bound(data.begin(),data.end(),make_pair(X,0)) -
// data.begin(); //pair
/* 文字列回転
string N;
cin >> N;
N = N[N.length()-1] + N.substr(0,N.length()-1);
s = to_string(i); //ストリング変換
*/
/* 文字列合成
string N,M;
cin >> N >> M;
SINT64 ans = 0;
ans = stoi(N+M);
*/
/*
//ワーシャルフロイド
vector<vector<SINT32>> dist(N,vector<SINT32>(N));
rep(i,0,N) {
rep(j,0,N) {
if (i != j) {
dist[i][j] = INF;
}
}
}
rep(k,0,N) {
rep(i,0,N) {
rep(j,0,N) {
dist[i][j] = MIN(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
*/
/* 優先度付きキュー
priority_queue<SINT64, vector<SINT64>, greater<SINT64>> data;
//小さいほうから取り出せる priority_queue<SINT64, vector<SINT64>> data;
//大きいほうから取り出せる
data.push(X); //X を挿入
data.top(); //先頭データ読み
data.pop(); //先頭データ削除
*/
/* SET コンテナ
set<SINT64> data;
data.insert(X); //X を挿入
data.erase(data.begin()); //先頭を削除
data.erase(--data.end()); //末尾を削除
*data.begin(); //先頭要素にアクセス
*data.rbegin(); //末尾要素にアクセス
//全表示
set<SINT64>::iterator it; //イテレータを用意
for(it = data.begin(); it != data.end(); it++) {
cout << *it << " ";
}
cout << endl;
//N番目を一部表示
set<SINT64>::iterator it; // イテレータを用意
it = data.begin();
rep (i,0,N) {
it++;
}
cout << *it << endl;
*/
/* map
map<string,SINT32> mp;
SINT32 N = 0;
SINT32 mx = 0;
cin >> N;
for (SINT32 i = 0; i < N; i++) {
string s;
cin >> s;
mp[s]++;
}
for(auto it=mp.begin();it!=mp.end();it++) {
mx=max(mx,it->second);
}
*/
/*
//順列全表示
//sortしてからでないと全列挙にならない
sort(data.begin(),data.end());
do {
cout << buf << endl;
rep(i,0,R) {
cout << data[i] << " ";
}
cout << endl;
} while (next_permutation(data.begin(),data.end()));
*/
// 桁指定表示
// ans = ans * M_PI;
// cout << setprecision(15) << ans << endl;
// 逆元 コンビネーション
/*
SINT64 modpow(SINT64 a, SINT64 p) {
if (p == 0) return 1;
if (p % 2 == 0) {
//pが偶数の時
SINT64 halfP = p / 2;
SINT64 half = modpow(a, halfP);
//a^(p/2) をhalfとして、half*halfを計算
return half * half % MOD;
} else {
//pが奇数の時は、偶数にするために1減らす
return a * modpow(a, p - 1) % MOD;
}
}
SINT64 calcComb(SINT64 a, SINT64 b) {
SINT64 Mul = 1;
SINT64 Div = 1;
SINT64 ans = 0;
if (b > a - b) {
return calcComb(a, a - b);
}
rep(i,0,b) {
Mul *= (a - i);
Div *= (i + 1);
Mul %= MOD;
Div %= MOD;
}
ans = Mul * modpow(Div, MOD - 2) % MOD;
return ans;
}
*/ | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long SINT64;
typedef unsigned long long UINT64;
typedef double DOUBLE;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define rep(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define rrep(i, a, b) for (int(i) = int(a); (i) >= int(b); (i)--)
#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "
#define pute(a) cout << endl
#define INF 1000000001
#define MOD 1000000007
#define INF64 1000000000000000001
#define F first
#define S second
#define Pii pair<SINT32, SINT32>
#define Pll pair<SINT64, SINT64>
#define Piii pair<SINT32, pair<SINT32, SINT32>>
#define Plll pair<SINT64, pair<SINT64, SINT64>>
#define Vll(a, b, c) vector<vector<SINT64>> (a)((b),vector<SINT64>((c))
#define Vlll(a, b, c, d) vector<vector<vector<SINT64>>> (a)((b),vector<vector<SINT64>>((c),vector<SINT64>((d)))
using namespace std;
class UnionFind {
public:
vector<SINT64> parent;
UnionFind(SINT64 N) { parent = vector<SINT64>(N, -1); }
SINT64 root(SINT64 A) {
if (parent[A] < 0) {
return A;
} else {
parent[A] = root(parent[A]);
return parent[A];
}
}
SINT64 size(SINT64 A) { return parent[root(A)] * (-1); }
bool judge(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A == B) {
return true; // 同じグループ
} else {
return false; // 違うグループ
}
}
void connect(SINT64 A, SINT64 B) {
A = root(A);
B = root(B);
if (A != B) {
if (size(A) < size(B)) {
swap(A, B);
}
parent[A] += parent[B];
parent[B] = A;
}
}
};
int main() {
SINT64 N;
cin >> N;
SINT64 M;
cin >> M;
vector<Pll> data(M);
vector<SINT64> ans;
SINT64 cnt = N * (N - 1) / 2;
ans.emplace_back(cnt);
UnionFind Uni(N + 10);
rep(i, 0, M) {
cin >> data[i].F;
cin >> data[i].S;
}
rrep(i, M - 1, 0) {
if (Uni.judge(data[i].F, data[i].S) == false) {
cnt -= Uni.size(data[i].F) * Uni.size(data[i].S);
Uni.connect(data[i].F, data[i].S);
}
ans.emplace_back(cnt);
}
rrep(i, M - 1, 0) { put(ans[i]); }
return 0;
}
// vector<vector<SINT64>> data(N,vector<SINT32>(3));
////2次元配列 vector<vector<vector<SINT64>>>
//data(N,vector<vector<SINT64>>(3,vector<SINT64>(3))); //3次元配列
// Vll(data,N,N); //2次元配列
// Vlll(data,N,N,N); //3次元配列
// sort(data.begin(),data.end());
// sort(data.begin(),data.end(),std::greater<SINT64>());
// __gcd(A,B);
/* 複数条件ソート
bool sortcompare(Pll A, Pll B) {
if(A.F == B.F){
return A.S > B.S;
} else {
return A.F < B.F;
}
}
sort(data.begin(),data.end(),sortcompare);
*/
// data.emplace_back(BUF); //後ろに追加
// data.erase(std::unique(data.begin(), data.end()), data.end());
// //ソート後に使用 同じ値を消す
// data.insert(data.begin() + X, 0); //X番目の要素に0を挿入
// 隣接リスト
// vector<vector<SINT64>> data(N);
// data[ A ].emplace_back( B );
/*
vector<Pll> data(N);
rep(i,0,N) {
cin >> data[i].F;
cin >> data[i].S;
}
sort(data.begin(),data.end());
*/
/*
vector<Plll> data(N);
rep(i,0,N) {
cin >> data[i].F;
cin >> data[i].S.F;
cin >> data[i].S.S;
}
sort(data.begin(),data.end());
*/
// posi = lower_bound(data.begin(),data.end(), X) - data.begin();
// // X以上を探す posi = lower_bound(data.begin(),data.end(),make_pair(X,0)) -
// data.begin(); //pair
/* 文字列回転
string N;
cin >> N;
N = N[N.length()-1] + N.substr(0,N.length()-1);
s = to_string(i); //ストリング変換
*/
/* 文字列合成
string N,M;
cin >> N >> M;
SINT64 ans = 0;
ans = stoi(N+M);
*/
/*
//ワーシャルフロイド
vector<vector<SINT32>> dist(N,vector<SINT32>(N));
rep(i,0,N) {
rep(j,0,N) {
if (i != j) {
dist[i][j] = INF;
}
}
}
rep(k,0,N) {
rep(i,0,N) {
rep(j,0,N) {
dist[i][j] = MIN(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
*/
/* 優先度付きキュー
priority_queue<SINT64, vector<SINT64>, greater<SINT64>> data;
//小さいほうから取り出せる priority_queue<SINT64, vector<SINT64>> data;
//大きいほうから取り出せる
data.push(X); //X を挿入
data.top(); //先頭データ読み
data.pop(); //先頭データ削除
*/
/* SET コンテナ
set<SINT64> data;
data.insert(X); //X を挿入
data.erase(data.begin()); //先頭を削除
data.erase(--data.end()); //末尾を削除
*data.begin(); //先頭要素にアクセス
*data.rbegin(); //末尾要素にアクセス
//全表示
set<SINT64>::iterator it; //イテレータを用意
for(it = data.begin(); it != data.end(); it++) {
cout << *it << " ";
}
cout << endl;
//N番目を一部表示
set<SINT64>::iterator it; // イテレータを用意
it = data.begin();
rep (i,0,N) {
it++;
}
cout << *it << endl;
*/
/* map
map<string,SINT32> mp;
SINT32 N = 0;
SINT32 mx = 0;
cin >> N;
for (SINT32 i = 0; i < N; i++) {
string s;
cin >> s;
mp[s]++;
}
for(auto it=mp.begin();it!=mp.end();it++) {
mx=max(mx,it->second);
}
*/
/*
//順列全表示
//sortしてからでないと全列挙にならない
sort(data.begin(),data.end());
do {
cout << buf << endl;
rep(i,0,R) {
cout << data[i] << " ";
}
cout << endl;
} while (next_permutation(data.begin(),data.end()));
*/
// 桁指定表示
// ans = ans * M_PI;
// cout << setprecision(15) << ans << endl;
// 逆元 コンビネーション
/*
SINT64 modpow(SINT64 a, SINT64 p) {
if (p == 0) return 1;
if (p % 2 == 0) {
//pが偶数の時
SINT64 halfP = p / 2;
SINT64 half = modpow(a, halfP);
//a^(p/2) をhalfとして、half*halfを計算
return half * half % MOD;
} else {
//pが奇数の時は、偶数にするために1減らす
return a * modpow(a, p - 1) % MOD;
}
}
SINT64 calcComb(SINT64 a, SINT64 b) {
SINT64 Mul = 1;
SINT64 Div = 1;
SINT64 ans = 0;
if (b > a - b) {
return calcComb(a, a - b);
}
rep(i,0,b) {
Mul *= (a - i);
Div *= (i + 1);
Mul %= MOD;
Div %= MOD;
}
ans = Mul * modpow(Div, MOD - 2) % MOD;
return ans;
}
*/ | replace | 101 | 102 | 101 | 102 | 0 | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define etm \
cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<long long int, long long int>
#define vpl vector<pll>
#define vll vector<ll>
#define vi vector<int>
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i, a, b, c) for (ll i = (a); i <= (b); i += (c))
#define repb(i, a, b, c) for (ll i = (a); i >= (b); i -= (c))
#define MOD 1000000007
#define ld long double
#define inf 1e18
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>
#define vvll vector<vector<ll>>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define test \
ll T; \
cin >> T; \
while (T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w, size) \
for (ll i = 0; i < size; i++) \
cout << w[i] << " ";
#define print(a) cout << a << "\n";
#define pqll priority_queue<ll>
#define mset(dp, no) memset(dp, no, sizeof(dp))
#define umll unordered_map<ll, ll>
#define mll map<ll, ll>
#define input(a, n) \
ll I; \
rep(I, 0, n - 1, 1) cin >> a[I];
#define countbit __builtin_popcount // Number of set bits .
#define fbo(k) find_by_order // K-th element in a set (counting from zero) .
#define ook(k) order_of_key // Number of items strictly smaller than k .
#define lb lower_bound
#define up upper_bound
#define in insert
// #define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
using namespace std;
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
ll gcd(ll a, ll b) {
if (a < b)
return gcd(b, a);
else if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll isPrime(ll n) {
ll p = (ll)sqrt(n);
rep(i, 2, p, 1) if (n % i == 0) return 0;
return 1;
} // reuturn 1 if prime
ll pow(ll b, ll e) {
if (e == 0)
return 1;
else if (e % 2 == 0) {
ll a = pow(b, e / 2);
return a * a;
} else {
ll a = pow(b, e / 2);
return b * a * a;
}
}
ll powm(ll x, ll y, ll m = MOD) {
x = x % m;
ll res = 1;
while (y) {
if (y & 1)
res = res * x;
res %= m;
y = y >> 1;
x = x * x;
x %= m;
}
return res;
}
ll ceil(long double a) {
ll b = a;
if (a == b) {
return b;
} else {
return b + 1;
}
}
ll floor(long double a) {
ll b = a;
return b;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll a, ll m) { return powm(a, m - 2, m); }
bool issq(ll n) {
ll p = sqrt(n);
return p * p == n;
}
vll prime; // if i==prime[i] then prime otherwise smallest prime factor of that
// number
void sieve(ll n) {
prime.resize(n + 1, 1);
prime[0] = 0, prime[1] = 0;
for (ll i = 2; i * i <= n; i++)
if (prime[i])
for (ll j = i * i; j <= n; j += i)
prime[j] = 0;
}
ll extended_GCD(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
ll mulInv(ll a, ll mod = 26) {
ll x, y;
extended_GCD(a, mod, x, y);
if (x < 0)
x += mod;
return x;
}
ll find(ll num[], ll rem[], ll k, ll prod) {
// Compute product of all numbers
ll result = 0;
// Apply above formula
for (ll i = 0; i < k; i++) {
ll pp = prod / num[i];
result += rem[i] * mulInv(pp, num[i]) * pp;
}
return result % prod;
}
ll nCr(ll n, ll k) {
ll res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ll i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) {
if (p1.fi == p2.fi) {
return p1.se > p2.se;
}
return p1.fi > p2.fi;
}
bool isPalindrome(string s) {
ll i, j;
for (i = 0, j = s.length() - 1; i <= j; i++, j--) {
if (s[i] != s[j]) {
return 0;
}
}
return 1;
}
ll ToInt(char ch) { return ch - '0'; }
char ToChar(ll a) { return a + '0'; }
bool isSubSeq(string str1, string str2, ll m, ll n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters of two strings are matching
if (str1[m - 1] == str2[n - 1])
return isSubSeq(str1, str2, m - 1, n - 1);
// If last characters are not matching
return isSubSeq(str1, str2, m, n - 1);
}
void printVectorPair(vpll v) {
for (ll i = 0; i < v.size(); i++) {
cout << v[i].fi << " " << v[i].se << "\n";
}
}
void modBigNumber(string num, ll m) {
// Store the modulus of big number
vector<int> vec;
ll mod = 0;
// Do step by step division
for (int i = 0; i < num.size(); i++) {
int digit = num[i] - '0';
mod = mod * 10 + digit;
int quo = mod / m;
if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros
vec.push_back(quo);
mod = mod % m;
}
// cout << "\nRemainder : " << mod << "\n";
// cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
return;
}
struct SegmentTree {
ll n;
vll v;
SegmentTree(ll size) {
n = 4 * size + 1;
v.resize(n, 0);
}
void build(ll ar[], ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] = ar[ipos];
else {
ll mid = (ipos + fpos) / 2;
build(ar, ipos, mid, pos * 2);
build(ar, mid + 1, fpos, pos * 2 + 1);
v[pos] = v[pos * 2] + v[pos * 2 + 1];
}
}
void update(ll index, ll val, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] += val;
else {
v[pos] += val;
ll mid = (ipos + fpos) / 2;
if (mid >= index)
update(index, val, ipos, mid, pos * 2);
else
update(index, val, mid + 1, fpos, pos * 2 + 1);
}
}
ll get_sum(ll l, ll r, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return 0;
if (l > r)
return 0;
ll mid = (ipos + fpos) / 2;
if ((l == ipos) && (r == fpos))
return v[pos];
else
return get_sum(l, min(mid, r), ipos, mid, pos * 2) +
get_sum(max(mid + 1, l), r, mid + 1, fpos, pos * 2 + 1);
}
};
struct BIT {
vector<ll> bitree;
ll n;
BIT(ll n) {
this->n = n;
bitree.resize(n + 1, 0);
}
void update(ll idx, ll val) {
idx++;
while (idx <= n) {
bitree[idx] += val;
idx += idx & (-idx);
}
}
ll Sum(ll idx) {
ll sum = 0;
idx++;
while (idx > 0) {
sum += bitree[idx];
idx -= idx & (-idx);
}
return sum;
}
};
ll sumofdigits(ll a) {
ll val = 0;
while (a > 0) {
val += a % 10;
a /= 10;
}
return val;
}
class DSU {
public:
vll parent, size;
public:
DSU(ll n) {
parent.resize(n + 1);
size.resize(n + 1);
for (ll i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public:
ll find_set(ll x) {
if (parent[x] == x) {
return x;
}
return parent[x] = find_set(parent[x]);
}
public:
void union_set(ll x, ll y) {
x = find_set(x);
y = find_set(y);
if (x != y) {
if (size[y] < size[x])
swap(x, y);
parent[y] = x;
size[x] += size[y];
}
}
};
int main() {
fastio;
ll n, m;
cin >> n >> m;
DSU d(n);
vll u(n + 1), v(n + 1), ans(m + 1, 0);
rep(i, 0, m - 1, 1) cin >> u[i] >> v[i];
ans[m] = (n * (n - 1)) / 2;
repb(i, m - 1, 0, 1) {
ll x = d.find_set(u[i]), y = d.find_set(v[i]);
if (x == y)
ans[i] = ans[i + 1];
else
ans[i] = ans[i + 1] - d.size[x] * d.size[y];
d.union_set(u[i], v[i]);
}
rep(i, 1, m, 1) cout << ans[i] << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define etm \
cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<long long int, long long int>
#define vpl vector<pll>
#define vll vector<ll>
#define vi vector<int>
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i, a, b, c) for (ll i = (a); i <= (b); i += (c))
#define repb(i, a, b, c) for (ll i = (a); i >= (b); i -= (c))
#define MOD 1000000007
#define ld long double
#define inf 1e18
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>
#define vvll vector<vector<ll>>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define test \
ll T; \
cin >> T; \
while (T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w, size) \
for (ll i = 0; i < size; i++) \
cout << w[i] << " ";
#define print(a) cout << a << "\n";
#define pqll priority_queue<ll>
#define mset(dp, no) memset(dp, no, sizeof(dp))
#define umll unordered_map<ll, ll>
#define mll map<ll, ll>
#define input(a, n) \
ll I; \
rep(I, 0, n - 1, 1) cin >> a[I];
#define countbit __builtin_popcount // Number of set bits .
#define fbo(k) find_by_order // K-th element in a set (counting from zero) .
#define ook(k) order_of_key // Number of items strictly smaller than k .
#define lb lower_bound
#define up upper_bound
#define in insert
// #define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
using namespace std;
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
ll gcd(ll a, ll b) {
if (a < b)
return gcd(b, a);
else if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll isPrime(ll n) {
ll p = (ll)sqrt(n);
rep(i, 2, p, 1) if (n % i == 0) return 0;
return 1;
} // reuturn 1 if prime
ll pow(ll b, ll e) {
if (e == 0)
return 1;
else if (e % 2 == 0) {
ll a = pow(b, e / 2);
return a * a;
} else {
ll a = pow(b, e / 2);
return b * a * a;
}
}
ll powm(ll x, ll y, ll m = MOD) {
x = x % m;
ll res = 1;
while (y) {
if (y & 1)
res = res * x;
res %= m;
y = y >> 1;
x = x * x;
x %= m;
}
return res;
}
ll ceil(long double a) {
ll b = a;
if (a == b) {
return b;
} else {
return b + 1;
}
}
ll floor(long double a) {
ll b = a;
return b;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll a, ll m) { return powm(a, m - 2, m); }
bool issq(ll n) {
ll p = sqrt(n);
return p * p == n;
}
vll prime; // if i==prime[i] then prime otherwise smallest prime factor of that
// number
void sieve(ll n) {
prime.resize(n + 1, 1);
prime[0] = 0, prime[1] = 0;
for (ll i = 2; i * i <= n; i++)
if (prime[i])
for (ll j = i * i; j <= n; j += i)
prime[j] = 0;
}
ll extended_GCD(ll a, ll b, ll &x, ll &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = extended_GCD(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
ll mulInv(ll a, ll mod = 26) {
ll x, y;
extended_GCD(a, mod, x, y);
if (x < 0)
x += mod;
return x;
}
ll find(ll num[], ll rem[], ll k, ll prod) {
// Compute product of all numbers
ll result = 0;
// Apply above formula
for (ll i = 0; i < k; i++) {
ll pp = prod / num[i];
result += rem[i] * mulInv(pp, num[i]) * pp;
}
return result % prod;
}
ll nCr(ll n, ll k) {
ll res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ll i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) {
if (p1.fi == p2.fi) {
return p1.se > p2.se;
}
return p1.fi > p2.fi;
}
bool isPalindrome(string s) {
ll i, j;
for (i = 0, j = s.length() - 1; i <= j; i++, j--) {
if (s[i] != s[j]) {
return 0;
}
}
return 1;
}
ll ToInt(char ch) { return ch - '0'; }
char ToChar(ll a) { return a + '0'; }
bool isSubSeq(string str1, string str2, ll m, ll n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters of two strings are matching
if (str1[m - 1] == str2[n - 1])
return isSubSeq(str1, str2, m - 1, n - 1);
// If last characters are not matching
return isSubSeq(str1, str2, m, n - 1);
}
void printVectorPair(vpll v) {
for (ll i = 0; i < v.size(); i++) {
cout << v[i].fi << " " << v[i].se << "\n";
}
}
void modBigNumber(string num, ll m) {
// Store the modulus of big number
vector<int> vec;
ll mod = 0;
// Do step by step division
for (int i = 0; i < num.size(); i++) {
int digit = num[i] - '0';
mod = mod * 10 + digit;
int quo = mod / m;
if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros
vec.push_back(quo);
mod = mod % m;
}
// cout << "\nRemainder : " << mod << "\n";
// cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
return;
}
struct SegmentTree {
ll n;
vll v;
SegmentTree(ll size) {
n = 4 * size + 1;
v.resize(n, 0);
}
void build(ll ar[], ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] = ar[ipos];
else {
ll mid = (ipos + fpos) / 2;
build(ar, ipos, mid, pos * 2);
build(ar, mid + 1, fpos, pos * 2 + 1);
v[pos] = v[pos * 2] + v[pos * 2 + 1];
}
}
void update(ll index, ll val, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return;
if (ipos == fpos)
v[pos] += val;
else {
v[pos] += val;
ll mid = (ipos + fpos) / 2;
if (mid >= index)
update(index, val, ipos, mid, pos * 2);
else
update(index, val, mid + 1, fpos, pos * 2 + 1);
}
}
ll get_sum(ll l, ll r, ll ipos, ll fpos, ll pos = 1) {
if (ipos > fpos)
return 0;
if (l > r)
return 0;
ll mid = (ipos + fpos) / 2;
if ((l == ipos) && (r == fpos))
return v[pos];
else
return get_sum(l, min(mid, r), ipos, mid, pos * 2) +
get_sum(max(mid + 1, l), r, mid + 1, fpos, pos * 2 + 1);
}
};
struct BIT {
vector<ll> bitree;
ll n;
BIT(ll n) {
this->n = n;
bitree.resize(n + 1, 0);
}
void update(ll idx, ll val) {
idx++;
while (idx <= n) {
bitree[idx] += val;
idx += idx & (-idx);
}
}
ll Sum(ll idx) {
ll sum = 0;
idx++;
while (idx > 0) {
sum += bitree[idx];
idx -= idx & (-idx);
}
return sum;
}
};
ll sumofdigits(ll a) {
ll val = 0;
while (a > 0) {
val += a % 10;
a /= 10;
}
return val;
}
class DSU {
public:
vll parent, size;
public:
DSU(ll n) {
parent.resize(n + 1);
size.resize(n + 1);
for (ll i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public:
ll find_set(ll x) {
if (parent[x] == x) {
return x;
}
return parent[x] = find_set(parent[x]);
}
public:
void union_set(ll x, ll y) {
x = find_set(x);
y = find_set(y);
if (x != y) {
if (size[y] < size[x])
swap(x, y);
parent[y] = x;
size[x] += size[y];
}
}
};
int main() {
fastio;
ll n, m;
cin >> n >> m;
DSU d(n);
vll u(m + 1), v(m + 1), ans(m + 1, 0);
rep(i, 0, m - 1, 1) cin >> u[i] >> v[i];
ans[m] = (n * (n - 1)) / 2;
repb(i, m - 1, 0, 1) {
ll x = d.find_set(u[i]), y = d.find_set(v[i]);
if (x == y)
ans[i] = ans[i + 1];
else
ans[i] = ans[i + 1] - d.size[x] * d.size[y];
d.union_set(u[i], v[i]);
}
rep(i, 1, m, 1) cout << ans[i] << endl;
return 0;
} | replace | 363 | 364 | 363 | 364 | 0 | |
p03108 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
const int maxN = 150010;
const int MAXM = 310;
const int MAXK = 17;
const int MOD = 1000000007;
const ll INF = 10000000000000000LL;
const ll inf = 5000000000000LL;
int father[maxN];
int find(int x) {
if (x == father[x])
return x;
else
return find(father[x]);
}
ll size[maxN];
int n, m;
int X[maxN], Y[maxN];
ll ans[maxN];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &X[i], &Y[i]);
}
for (int i = 1; i <= n; i++) {
father[i] = i;
size[i] = 1ll;
}
ans[m] = (ll)(n - 1) * n / 2;
for (int i = m; i > 1; i--) {
int u = find(X[i]);
int v = find(Y[i]);
if (u != v) {
ll tmp = size[u] * size[v];
ans[i - 1] = ans[i] - tmp;
father[u] = v;
size[v] += size[u];
} else
ans[i - 1] = ans[i];
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
const int maxN = 150010;
const int MAXM = 310;
const int MAXK = 17;
const int MOD = 1000000007;
const ll INF = 10000000000000000LL;
const ll inf = 5000000000000LL;
int father[maxN];
int find(int x) {
if (x == father[x])
return x;
else
return father[x] = find(father[x]);
}
ll size[maxN];
int n, m;
int X[maxN], Y[maxN];
ll ans[maxN];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &X[i], &Y[i]);
}
for (int i = 1; i <= n; i++) {
father[i] = i;
size[i] = 1ll;
}
ans[m] = (ll)(n - 1) * n / 2;
for (int i = m; i > 1; i--) {
int u = find(X[i]);
int v = find(Y[i]);
if (u != v) {
ll tmp = size[u] * size[v];
ans[i - 1] = ans[i] - tmp;
father[u] = v;
size[v] += size[u];
} else
ans[i - 1] = ans[i];
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
return 0;
} | replace | 23 | 24 | 23 | 24 | TLE | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
UnionFind(size_t size) : v(size, -1) {}
vector<int> v;
int root(int x) { return (v[x] < 0 ? x : v[x] = root(v[x])); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (v[x] > v[y])
swap(x, y);
v[x] += v[y];
v[y] = x;
}
}
int count(int x) { return -v[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(N);
vector<int64_t> ans(M);
for (int i = M - 1; i >= 0; --i) {
if (!uf.same(a[i], b[i])) {
ans[i] = 1ll * uf.count(a[i]) * uf.count(b[i]);
}
uf.unite(a[i], b[i]);
}
for (int i = 1; i < M; ++i) {
ans[i] += ans[i - 1];
}
for (auto i : ans)
cout << i << endl;
}
| #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
UnionFind(size_t size) : v(size, -1) {}
vector<int> v;
int root(int x) { return (v[x] < 0 ? x : v[x] = root(v[x])); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (v[x] > v[y])
swap(x, y);
v[x] += v[y];
v[y] = x;
}
}
int count(int x) { return -v[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(N + 1);
vector<int64_t> ans(M);
for (int i = M - 1; i >= 0; --i) {
if (!uf.same(a[i], b[i])) {
ans[i] = 1ll * uf.count(a[i]) * uf.count(b[i]);
}
uf.unite(a[i], b[i]);
}
for (int i = 1; i < M; ++i) {
ans[i] += ans[i - 1];
}
for (auto i : ans)
cout << i << endl;
}
| replace | 33 | 34 | 33 | 34 | 0 | |
p03108 | C++ | Time Limit Exceeded | #include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
using namespace std;
long N, M;
long *a, *b;
long *ans;
class nodeClass {
public:
nodeClass() {
next = NULL;
num = 0;
}
nodeClass *next;
int id;
int num;
};
nodeClass *np;
nodeClass *getNode(int i) {
nodeClass *node = &np[i];
while (node->next != NULL) {
node = node->next;
}
return node;
}
int main() {
cin >> N >> M;
a = new long[M];
b = new long[M];
ans = new long[M];
np = new nodeClass[N];
ans[M - 1] = (N * (N - 1)) / 2;
for (int i = 0; i < N; i++) {
np[i].num = 1;
np[i].id = i;
}
for (int i = 0; i < M; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int k = M - 1; k > 0; k--) {
int x = a[k], y = b[k];
nodeClass *nx = getNode(x), *ny = getNode(y);
if (nx->id == ny->id) {
ans[k - 1] = ans[k];
} else {
ans[k - 1] = ans[k] - nx->num * ny->num;
nodeClass *node = new nodeClass;
node->id = N + M - k;
node->num = nx->num + ny->num;
nx->next = node;
ny->next = node;
}
}
for (int i = 0; i < M; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
using namespace std;
long N, M;
long *a, *b;
long *ans;
class nodeClass {
public:
nodeClass() {
next = NULL;
num = 0;
}
nodeClass *next;
int id;
int num;
};
nodeClass *np;
nodeClass *getNode(int i) {
nodeClass *node = &np[i];
while (node->next != NULL) {
node = node->next;
}
return node;
}
int main() {
cin >> N >> M;
a = new long[M];
b = new long[M];
ans = new long[M];
np = new nodeClass[N];
ans[M - 1] = (N * (N - 1)) / 2;
for (int i = 0; i < N; i++) {
np[i].num = 1;
np[i].id = i;
}
for (int i = 0; i < M; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int k = M - 1; k > 0; k--) {
int x = a[k], y = b[k];
nodeClass *nx = getNode(x), *ny = getNode(y);
if (nx->id == ny->id) {
ans[k - 1] = ans[k];
} else {
ans[k - 1] = ans[k] - nx->num * ny->num;
nodeClass *node = new nodeClass;
node->id = N + M - k;
node->num = nx->num + ny->num;
nx->next = node;
ny->next = node;
np[x].next = node;
np[y].next = node;
}
}
for (int i = 0; i < M; i++) {
cout << ans[i] << endl;
}
return 0;
} | insert | 67 | 67 | 67 | 69 | TLE | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Unionfind {
public:
vector<int> oya;
Unionfind(int N) { oya = vector<int>(N, -1); }
int root(int x) {
if (oya[x] < 0)
return x;
return oya[x] = root(oya[x]);
}
int size(int x) { return -oya[root(x)]; }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (size(x) < size(y))
swap(x, y);
oya[x] += oya[y];
oya[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]--;
}
vector<long long> ans(M);
ans[M - 1] = (long long)N * (N - 1) / 2;
Unionfind island(N);
for (int i = M - 1; i >= 0; i--) {
if (island.root(a[i]) != island.root(b[i])) {
ans[i - 1] = ans[i] - (long long)island.size(a[i]) * island.size(b[i]);
island.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 <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Unionfind {
public:
vector<int> oya;
Unionfind(int N) { oya = vector<int>(N, -1); }
int root(int x) {
if (oya[x] < 0)
return x;
return oya[x] = root(oya[x]);
}
int size(int x) { return -oya[root(x)]; }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (size(x) < size(y))
swap(x, y);
oya[x] += oya[y];
oya[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]--;
}
vector<long long> ans(M);
ans[M - 1] = (long long)N * (N - 1) / 2;
Unionfind island(N);
for (int i = M - 1; i >= 1; i--) {
if (island.root(a[i]) != island.root(b[i])) {
ans[i - 1] = ans[i] - (long long)island.size(a[i]) * island.size(b[i]);
island.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 | 48 | 49 | 48 | 49 | -6 | free(): invalid pointer
|
p03108 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n, m;
int par[20005];
int num[20005];
pair<int, int> e1[10005];
long long ans[10005];
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
long long unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return 0;
long long xn = num[x], yn = num[y];
par[y] = x;
num[x] += num[y];
return xn * yn;
}
int main() {
cin >> n >> m;
long long N = (long long)n * (long long)(n - 1) / (long long)2;
for (int i = 0; i < n; i++) {
par[i] = i;
num[i] = (long long)1;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
e1[i] = make_pair(x, y);
}
ans[m] = N;
for (int i = m - 1; i >= 0; i--) {
int x = e1[i].first;
int y = e1[i].second;
ans[i] = ans[i + 1] - unite(x, y);
}
for (int i = 1; i <= m; i++)
cout << ans[i] << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n, m;
int par[200005];
int num[200005];
pair<int, int> e1[100005];
long long ans[100005];
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
long long unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return 0;
long long xn = num[x], yn = num[y];
par[y] = x;
num[x] += num[y];
return xn * yn;
}
int main() {
cin >> n >> m;
long long N = (long long)n * (long long)(n - 1) / (long long)2;
for (int i = 0; i < n; i++) {
par[i] = i;
num[i] = (long long)1;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
e1[i] = make_pair(x, y);
}
ans[m] = N;
for (int i = m - 1; i >= 0; i--) {
int x = e1[i].first;
int y = e1[i].second;
ans[i] = ans[i + 1] - unite(x, y);
}
for (int i = 1; i <= m; i++)
cout << ans[i] << endl;
return 0;
}
| replace | 23 | 27 | 23 | 27 | 0 | |
p03108 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define repl(i, x, n) \
for (long long i = (long long)(x); i < (long long)(n); i++)
#define rep(i, x, n) for (long i = long(x); i < long(n); i++)
long id[100009];
map<long, long> q;
vector<pair<long, long>> v;
void init(long n) {
rep(i, 0, n + 1) {
id[i] = i;
q[i] = 1;
}
}
long root(long x) {
while (id[x] != x)
x = id[x];
return x;
}
void Union(long x, long y) { id[root(x)] = id[root(y)]; }
long long CC(long long x) { return ((x) * (x - 1)) / 2; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long n, m, x, y;
long long c = 0, a, b;
vector<long long> ans;
cin >> n >> m;
ans.push_back(CC(n));
// cout<<"0\n";
init(n);
rep(i, 0, m) {
cin >> x >> y;
v.push_back({x, y});
}
for (long i = m - 1; i >= 0; i--) {
// cout<<v[i].first<<" "<<v[i].second<<"\n";
x = v[i].first;
y = v[i].second;
if (root(x) != root(y)) {
a = q[root(x)];
b = q[root(y)];
q[root(x)] += b;
q[root(y)] += a;
c -= CC(a);
c -= CC(b);
c += CC(a + b);
}
ans.push_back(CC(n) - c);
// cout<<c<<"\n";
Union(x, y);
// rep(j,1,n+1)
// cout<<j<<" "<<id[j]<<"\n";
// cout<<"\n";
}
// cout<<"\n";
for (long i = m - 1; i >= 0; --i)
cout << ans[i] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define repl(i, x, n) \
for (long long i = (long long)(x); i < (long long)(n); i++)
#define rep(i, x, n) for (long i = long(x); i < long(n); i++)
long id[100009];
map<long, long> q;
vector<pair<long, long>> v;
void init(long n) {
rep(i, 0, n + 1) {
id[i] = i;
q[i] = 1;
}
}
long root(long x) {
while (id[x] != x)
x = id[x];
return x;
}
void Union(long x, long y) { id[root(x)] = id[root(y)]; }
long long CC(long long x) { return ((x) * (x - 1)) / 2; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long n, m, x, y;
long long c = 0, a, b;
vector<long long> ans;
cin >> n >> m;
ans.push_back(CC(n));
// cout<<"0\n";
init(n);
rep(i, 0, m) {
cin >> x >> y;
v.push_back({x, y});
}
for (long i = m - 1; i >= 0; i--) {
// cout<<v[i].first<<" "<<v[i].second<<"\n";
x = v[i].first;
y = v[i].second;
if (root(x) != root(y)) {
a = q[root(x)];
b = q[root(y)];
q[root(x)] += b;
q[root(y)] += a;
c -= CC(a);
c -= CC(b);
c += CC(a + b);
}
ans.push_back(CC(n) - c);
// cout<<c<<"\n";
Union(x, y);
id[x] = root(x);
id[y] = root(y);
// rep(j,1,n+1)
// cout<<j<<" "<<id[j]<<"\n";
// cout<<"\n";
}
// cout<<"\n";
for (long i = m - 1; i >= 0; --i)
cout << ans[i] << "\n";
} | insert | 52 | 52 | 52 | 54 | TLE | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
// 親の番号を格納、親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 親初期化(N個の要素すべてに-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 = 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;
}
}
| #include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
// 親の番号を格納、親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 親初期化(N個の要素すべてに-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 = 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;
}
}
| replace | 54 | 55 | 54 | 55 | -6 | free(): invalid pointer
|
p03108 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010], father[100010];
long long n, m, ans[100010], sum[100010];
int find_father(int x) {
while (father[x] != x)
x = father[x];
return x;
}
int main() {
int i, x, y;
cin >> n >> m;
for (i = 1; i <= n; i++) {
father[i] = i;
sum[i] = 1;
}
for (i = 1; i <= m; i++)
cin >> a[i] >> b[i];
ans[m] = n * (n - 1) / 2;
for (i = m; i >= 2; i--) {
x = find_father(a[i]);
y = find_father(b[i]);
if (x != y) {
ans[i - 1] = ans[i] - sum[x] * sum[y];
sum[x] += sum[y];
father[y] = x;
} else
ans[i - 1] = ans[i];
}
for (i = 1; i <= m; i++)
cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010], father[100010];
long long n, m, ans[100010], sum[100010];
int find_father(int x) {
while (father[x] != x)
x = father[x];
return x;
}
int main() {
int i, x, y;
cin >> n >> m;
for (i = 1; i <= n; i++) {
father[i] = i;
sum[i] = 1;
}
for (i = 1; i <= m; i++)
cin >> a[i] >> b[i];
ans[m] = n * (n - 1) / 2;
for (i = m; i >= 2; i--) {
x = find_father(a[i]);
y = find_father(b[i]);
father[a[i]] = x;
father[b[i]] = x;
if (x != y) {
ans[i - 1] = ans[i] - sum[x] * sum[y];
sum[x] += sum[y];
father[y] = x;
} else
ans[i - 1] = ans[i];
}
for (i = 1; i <= m; i++)
cout << ans[i] << endl;
return 0;
} | insert | 22 | 22 | 22 | 24 | TLE | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct UnionFind {
vector<ll> rnk, par, num;
UnionFind(ll N) : rnk(N), par(N), num(N) { init(); }
void init() {
for (ll i = 0; i < rnk.size(); i++) {
rnk[i] = 0;
par[i] = i;
num[i] = 1;
}
}
ll find(ll x) {
if (par[x] == 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 (rnk[x] < rnk[y]) {
par[x] = y;
num[y] += num[x];
} else {
par[y] = x;
num[x] += num[y];
if (rnk[x] == rnk[y])
rnk[x]++;
}
}
bool same(ll x, ll y) { return (find(x) == find(y)); }
ll size(ll x) { return num[find(x)]; }
};
signed main() {
ll n, m;
cin >> n >> m;
vector<ll> a(m), b(m);
for (int i = 0; i < m; i++) {
scanf("ll%d%lld", &a[i], &b[i]);
--a[i];
--b[i];
}
UnionFind tree(n);
vector<ll> ans;
ll num = n * (n - 1) / 2;
ans.push_back(num);
for (ll i = m - 1; i >= 1; i--) {
ll x = a[i], y = b[i];
if (!tree.same(x, y)) {
num -= tree.size(x) * tree.size(y);
tree.unite(x, y);
}
ans.push_back(num);
}
reverse(ans.begin(), ans.end());
for (ll i : ans)
cout << i << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct UnionFind {
vector<ll> rnk, par, num;
UnionFind(ll N) : rnk(N), par(N), num(N) { init(); }
void init() {
for (ll i = 0; i < rnk.size(); i++) {
rnk[i] = 0;
par[i] = i;
num[i] = 1;
}
}
ll find(ll x) {
if (par[x] == 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 (rnk[x] < rnk[y]) {
par[x] = y;
num[y] += num[x];
} else {
par[y] = x;
num[x] += num[y];
if (rnk[x] == rnk[y])
rnk[x]++;
}
}
bool same(ll x, ll y) { return (find(x) == find(y)); }
ll size(ll x) { return num[find(x)]; }
};
signed main() {
ll n, m;
cin >> n >> m;
vector<ll> a(m), b(m);
for (int i = 0; i < m; i++) {
scanf("%lld%lld", &a[i], &b[i]);
--a[i];
--b[i];
}
UnionFind tree(n);
vector<ll> ans;
ll num = n * (n - 1) / 2;
ans.push_back(num);
for (ll i = m - 1; i >= 1; i--) {
ll x = a[i], y = b[i];
if (!tree.same(x, y)) {
num -= tree.size(x) * tree.size(y);
tree.unite(x, y);
}
ans.push_back(num);
}
reverse(ans.begin(), ans.end());
for (ll i : ans)
cout << i << '\n';
} | replace | 43 | 44 | 43 | 44 | -6 | free(): invalid pointer
|
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
struct Unionfind {
// tree number
vector<int> par;
// tree rank
vector<int> treerank;
// constructor
Unionfind(int n = 1) { stree(n + 1); }
// make and initialization
void stree(int n = 1) {
par.resize(n);
treerank.resize(n);
for (int i = 0; i < n; ++i) {
par[i] = -1;
treerank[i] = 0;
}
}
// search root
int root(int x) {
if (par[x] < 0)
return x;
return par[x] = root(par[x]);
}
// is same?
bool issame(int x, int y) { return root(x) == root(y); }
// add
// already added, return 0
bool uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return 0;
if (treerank[x] < treerank[y]) {
par[root(y)] += size(x);
par[x] = y;
} else if (treerank[y] < treerank[x]) {
par[root(x)] += size(y);
par[y] = x;
} else {
par[root(x)] += size(y);
par[y] = x;
++treerank[x];
}
return 1;
}
int size(int x) { return -par[root(x)]; }
};
long long n, m;
long long cnt[100005] = {0};
vector<pair<int, int>> v;
Unionfind uf;
vector<long long> ans;
void solve();
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
v.push_back(make_pair(a, b));
}
reverse(v.begin(), v.end());
ans.assign(m, 0);
solve();
for (int i = 0; i < m; ++i)
cout << ans[m - 1 - i] << endl;
return 0;
}
void solve() {
ans[0] = n * (n - 1) / 2;
uf = Unionfind(n + 1);
for (int i = 0; i < n; ++i)
cnt[i] = 1;
for (int i = 0; i < m - 1; ++i) {
long long now = 0;
if (uf.issame(v[i].fi, v[i].se)) {
ans[i + 1] = ans[i];
continue;
}
now = uf.size(v[i].fi);
now *= uf.size(v[i].se);
uf.uni(v[i].fi, v[i].se);
ans[i + 1] = ans[i] - now;
}
} | #include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
struct Unionfind {
// tree number
vector<int> par;
// tree rank
vector<int> treerank;
// constructor
Unionfind(int n = 1) { stree(n + 1); }
// make and initialization
void stree(int n = 1) {
par.resize(n);
treerank.resize(n);
for (int i = 0; i < n; ++i) {
par[i] = -1;
treerank[i] = 0;
}
}
// search root
int root(int x) {
if (par[x] < 0)
return x;
return par[x] = root(par[x]);
}
// is same?
bool issame(int x, int y) { return root(x) == root(y); }
// add
// already added, return 0
bool uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return 0;
if (treerank[x] > treerank[y])
swap(x, y);
if (treerank[x] == treerank[y])
++treerank[y];
par[y] -= size(x);
par[x] = y;
return 1;
}
int size(int x) { return -par[root(x)]; }
};
long long n, m;
long long cnt[100005] = {0};
vector<pair<int, int>> v;
Unionfind uf;
vector<long long> ans;
void solve();
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
v.push_back(make_pair(a, b));
}
reverse(v.begin(), v.end());
ans.assign(m, 0);
solve();
for (int i = 0; i < m; ++i)
cout << ans[m - 1 - i] << endl;
return 0;
}
void solve() {
ans[0] = n * (n - 1) / 2;
uf = Unionfind(n + 1);
for (int i = 0; i < n; ++i)
cnt[i] = 1;
for (int i = 0; i < m - 1; ++i) {
long long now = 0;
if (uf.issame(v[i].fi, v[i].se)) {
ans[i + 1] = ans[i];
continue;
}
now = uf.size(v[i].fi);
now *= uf.size(v[i].se);
uf.uni(v[i].fi, v[i].se);
ans[i + 1] = ans[i] - now;
}
} | replace | 37 | 48 | 37 | 43 | -11 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
// clang-format off
#define FOR(i, n) for (int i=0; i<(int)n; ++i)
#define RFOR(i, n) for (int i=n-1; i>=0; --i)
#define OUTV(x) FOR(j,x.size()) cerr<<x[j]<<(x.size()-j-1?' ':'\n');
#define OUTM(x) FOR(i,x.size()) {OUTV(x[i])}
#define BLE cerr<<"BLE"<<endl;
#define DEBUG(x) cerr<<"DEBUG: "<<#x<<" = "<<x<<endl;
#define PB push_back
#define MP make_pair
// clang-format on
using namespace std;
int N, M;
vector<vector<int>> graph;
vector<int> p;
vector<long long> ans;
vector<int> component;
void merge(int a, int b) {
if (p[a] > p[b])
merge(b, a);
if (p[a] == p[b])
return;
queue<int> Q;
Q.push(b);
component[p[a]] += component[p[b]];
component[p[b]] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
p[u] = p[a];
for (int v : graph[u]) {
if (p[v] != p[a]) {
Q.push(v);
}
}
}
}
long long solve(long long res, int a, int b) {
long long ret = res;
if (p[a] != p[b])
ret -= 1LL * component[p[a]] * component[p[b]];
merge(a, b);
// OUTV(component);
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin >> N >> M;
graph.resize(N);
p.resize(N);
component.resize(N);
for (int i = 0; i < N; ++i) {
p[i] = i;
component[i] = 1;
}
ans.resize(M + 1);
ans[M] = (1LL * N * (N - 1)) / 2;
vector<int> A(N), B(N);
for (int i = 0; i < M; ++i) {
cin >> A[i];
A[i]--;
cin >> B[i];
B[i]--;
}
for (int i = 0; i < M; ++i) {
graph[A[M - 1 - i]].push_back(B[M - 1 - i]);
graph[B[M - 1 - i]].push_back(A[M - 1 - i]);
ans[M - 1 - i] = solve(ans[M - i], A[M - 1 - i], B[M - 1 - i]);
}
for (int i = 1; i <= M; ++i) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
// clang-format off
#define FOR(i, n) for (int i=0; i<(int)n; ++i)
#define RFOR(i, n) for (int i=n-1; i>=0; --i)
#define OUTV(x) FOR(j,x.size()) cerr<<x[j]<<(x.size()-j-1?' ':'\n');
#define OUTM(x) FOR(i,x.size()) {OUTV(x[i])}
#define BLE cerr<<"BLE"<<endl;
#define DEBUG(x) cerr<<"DEBUG: "<<#x<<" = "<<x<<endl;
#define PB push_back
#define MP make_pair
// clang-format on
using namespace std;
int N, M;
vector<vector<int>> graph;
vector<int> p;
vector<long long> ans;
vector<int> component;
void merge(int a, int b) {
if (p[a] > p[b])
merge(b, a);
if (p[a] == p[b])
return;
queue<int> Q;
Q.push(b);
component[p[a]] += component[p[b]];
component[p[b]] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
p[u] = p[a];
for (int v : graph[u]) {
if (p[v] != p[a]) {
Q.push(v);
}
}
}
}
long long solve(long long res, int a, int b) {
long long ret = res;
if (p[a] != p[b])
ret -= 1LL * component[p[a]] * component[p[b]];
merge(a, b);
// OUTV(component);
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin >> N >> M;
graph.resize(N);
p.resize(N);
component.resize(N);
for (int i = 0; i < N; ++i) {
p[i] = i;
component[i] = 1;
}
ans.resize(M + 1);
ans[M] = (1LL * N * (N - 1)) / 2;
vector<int> A(M), B(M);
for (int i = 0; i < M; ++i) {
cin >> A[i];
A[i]--;
cin >> B[i];
B[i]--;
}
for (int i = 0; i < M; ++i) {
graph[A[M - 1 - i]].push_back(B[M - 1 - i]);
graph[B[M - 1 - i]].push_back(A[M - 1 - i]);
ans[M - 1 - i] = solve(ans[M - i], A[M - 1 - i], B[M - 1 - i]);
}
for (int i = 1; i <= M; ++i) {
cout << ans[i] << endl;
}
return 0;
} | replace | 67 | 68 | 67 | 68 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define foor(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, n) for (int i = (n); i--;)
#define roof(i, b, a) for (int i = (b); i >= (a); --i)
#define elsif else if
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
#define popcount __builtin_popcount
using namespace std;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef vector<vu> vvu;
typedef double dbl;
typedef vector<dbl> vd;
typedef vector<vd> vvd;
typedef string str;
typedef vector<str> vs;
typedef vector<vs> vvs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
typedef pair<dbl, dbl> pdd;
typedef vector<pdd> vpdd;
typedef map<dbl, dbl> mdd;
typedef pair<str, str> pss;
typedef vector<pss> vpss;
typedef map<str, str> mss;
typedef pair<int, ll> pil;
typedef vector<pil> vpil;
typedef map<int, ll> mil;
typedef pair<ll, int> pli;
typedef vector<pli> vpli;
typedef map<ll, int> mli;
typedef pair<dbl, int> pdi;
typedef vector<pdi> vpdi;
typedef map<dbl, int> mdi;
template <typename T> vector<T> &operator<<(vector<T> &v, const T t) {
v.push_back(t);
return v;
}
template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) {
m.insert(t);
return m;
}
template <typename T> set<T> &operator<<(set<T> &s, const T t) {
s.insert(t);
return s;
}
template <typename T> stack<T> &operator<<(stack<T> &s, const T t) {
s.push(t);
return s;
}
template <typename T> stack<T> &operator>>(stack<T> &s, T &t) {
t = s.top();
s.pop();
return s;
}
template <typename T> queue<T> &operator<<(queue<T> &q, const T t) {
q.push(t);
return q;
}
template <typename T> queue<T> &operator>>(queue<T> &q, T &t) {
t = q.front();
q.pop();
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) {
t = q.top();
q.pop();
return q;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &p) {
return s >> p.first >> p.second;
}
template <typename T> istream &operator>>(istream &s, vector<T> &v) {
fr(i, v.size()) { s >> v[i]; }
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> p) {
return s << p.first << " " << p.second;
}
// template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto
// a:v){s<<a<<endl;}return s;}
template <typename T> ostream &operator<<(ostream &s, const vector<T> v) {
fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; }
return s;
}
template <typename T> ostream &operator<<(ostream &s, const deque<T> d) {
fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; }
return s;
}
template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) {
return b = b & t;
}
template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) {
return b = b ^ t;
}
template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) {
return b = b | t;
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
void print(void) { cout << endl; }
template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... U> void print(T &&t, U &&...u) {
cout << t << " ";
print(forward<U>(u)...);
}
bool YN(bool b) {
print(b ? "YES" : "NO");
return b;
}
bool PI(bool b) {
print(b ? "POSSIBLE" : "IMPOSSIBLE");
return b;
}
bool Yn(bool b) {
print(b ? "Yes" : "No");
return b;
}
bool Pi(bool b) {
print(b ? "Possible" : "Impossible");
return b;
}
bool yn(bool b) {
print(b ? "yes" : "no");
return b;
}
bool pi(bool b) {
print(b ? "possible" : "impossible");
return b;
}
const int MD = 1e9 + 7;
template <typename T> str to_string(const T &n) {
ostringstream s;
s << n;
return s.str();
}
template <typename T> T &chmax(T &a, T b) { return a = max(a, b); }
template <typename T> T &chmin(T &a, T b) { return a = min(a, b); }
template <typename T, typename U>
vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s,
const T inf) {
using P = pair<T, U>;
vector<P> d;
fr(i, E.size()) { d << P{inf, i}; }
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T, typename U>
map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
map<U, P> d;
for (pair<U, vector<P>> e : E) {
d[e.first] = P{inf, e.first};
}
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
ll maxflow(vector<mil> &E, int s, int t) {
ll z = 0;
vi b(E.size(), -1);
for (int i = 0;; ++i) {
static auto dfs = [&](int v, ll f, auto &dfs) -> ll {
if (v == t)
return f;
b[v] = i;
for (auto &p : E[v]) {
if (b[p.first] < i && p.second) {
if (ll r = dfs(p.first, min(f, p.second), dfs)) {
p.second -= r;
E[p.first][v] += r;
return r;
}
}
}
return 0;
};
ll x = dfs(s, ll(1e18), dfs);
z += x;
if (x == 0)
return z;
}
}
template <typename T> T distsq(pair<T, T> a, pair<T, T> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
template <typename T> T max(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = max(m, e);
}
return m;
}
template <typename T> T min(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = min(m, e);
}
return m;
}
template <typename T> T gcd(const T a, const T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T> T gcd(const vector<T> a) {
T g = a[0];
for (T e : a) {
g = gcd(g, e);
}
return g;
}
template <typename T> vector<T> LIS(const vector<T> A) {
vector<T> B;
for (T a : A) {
auto it = lower_bound(all(B), a);
if (it == B.end()) {
B << a;
} else {
*it = a;
}
}
return B;
}
template <typename T> vector<T> LCS(vector<T> A, vector<T> B) {
int N = A.size(), M = B.size();
vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1));
fr(i, N) {
fr(j, M) {
if (A[i] == B[j]) {
d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}};
} else {
d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]);
}
}
}
vector<T> r;
for (pii p = {N, M}; d[p.first][p.second].first;
p = d[p.first][p.second].second) {
r << A[d[p.first][p.second].second.first];
}
Reverse(r);
return r;
}
str LCS(str S, str T) {
vector<char> s =
LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end()));
return str(s.begin(), s.end());
}
template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) {
if (V.size() <= 3) {
return V;
}
Sort(V);
rf(i, V.size() - 1) V << V[i];
vector<pair<T, T>> r;
for (pair<T, T> p : V) {
int s = r.size();
while (s >= 2 &&
(p.second - r[s - 1].second) * (p.first - r[s - 2].first) <
(p.second - r[s - 2].second) * (p.first - r[s - 1].first)) {
r.pop_back();
--s;
}
r << p;
}
r.pop_back();
return r;
}
class UnionFind {
vi p, r, s;
public:
UnionFind(int N) {
p = r = vi(N);
s = vi(N, 1);
fr(i, N) { p[i] = i; }
}
int find(int i) { return p[i] = p[i] == i ? i : find(p[i]); }
void unite(int a, int b) {
if ((a = find(a)) != (b = find(b))) {
if (r[a] > r[b]) {
swap(a, b);
}
s[b] += s[a];
r[p[a] = b] += r[a] == r[b];
}
}
bool same(int a, int b) { return find(a) == find(b); }
int size(int x) { return s[find(x)]; }
};
ll strmod(const str &s, const int m) {
ll x = 0;
fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; }
return x;
}
vvl mul(const vvl &A, const vvl &B, const int m) {
vvl C;
fr(y, A.size()) { C << vl(B[y].size()); }
fr(y, C.size()) {
fr(x, C[y].size()) {
fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; }
}
}
return C;
}
vvl pow(const vvl &A, const ll n, const int m) {
vvl B;
fr(y, A.size()) { B << vl(A.size()); }
if (n == 0) {
fr(i, B.size()) { B[i][i] = 1; }
}
elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); }
else {
vvl C = pow(A, n / 2, m);
B = mul(C, C, m);
}
return B;
}
ll pow(const ll a, const ll n, const int m) {
ll t;
return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) *
(t = pow(a, n >> 1, m), t * t % m) % m
: !!a;
}
ll inv(const ll x, const int p) { return pow(x, p - 2, p); }
ll inv(const ll x) { return inv(x, MD); }
vpll fact(const int n, const int p) {
vpll v(n + 1);
v[0].first = 1;
foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; }
v[n].second = inv(v[n].first, p);
roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; }
return v;
}
class Combination {
const vpll f;
const int M;
public:
Combination(int n, int m) : f(fact(n, m)), M(m) {}
Combination(int n) : Combination(n, MD) {}
ll P(int n, int k) {
return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M;
}
ll C(int n, int k) { return P(n, k) * f[k].second % M; }
ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); }
};
ll C2(const int n) { return (ll)n * ~-n / 2; }
ll sum(const vi a) {
ll s = 0;
for (int e : a) {
s += e;
}
return s;
}
ll sum(const vl a) {
ll s = 0;
for (ll e : a) {
s += e;
}
return s;
}
template <typename T> int MSB(T N) {
int r = -1;
for (; N > 0; N /= 2) {
++r;
}
return r;
}
template <typename T> class SegmentTree {
vector<T> S;
T (*const op)(T a, T b);
const T zero;
const int B;
public:
SegmentTree(int N, T (*f)(T a, T b), const T zero)
: S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero),
B(1 << MSB(N - 1) + 1) {}
SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero)
: SegmentTree(v.size(), f, zero) {
fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; }
roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); }
}
T calc(int l, int r) {
l += B;
r += B;
if (l > r) {
return zero;
}
if (l == r) {
return S[l];
}
T L = S[l], R = S[r];
for (; l / 2 < r / 2; l /= 2, r /= 2) {
if (l % 2 == 0) {
L = op(L, S[l + 1]);
}
if (r % 2 == 1) {
R = op(S[r - 1], R);
}
}
return op(L, R);
}
void replace(int i, T x) {
for (S[i += B] = x; i != 1; i /= 2) {
if (i % 2) {
S[i / 2] = op(S[i - 1], S[i]);
} else {
S[i / 2] = op(S[i], S[i + 1]);
}
}
}
void add(int i, T x) { replace(i, op(S[B + i], x)); }
T top() { return S[1]; }
};
ll BITsum(vl &B, int i) {
ll z = 0;
while (i > 0) {
z += B[i];
i -= i & -i;
}
return z;
}
void BITadd(vl &B, int i, ll x) {
while (i < B.size()) {
B[i] += x;
i += i & -i;
}
}
ll fib(const ll n, const int m) {
ll a, b, c, d, A, B, C, D;
a = 1;
b = 0;
c = 0;
d = 1;
rf(i, 63) {
A = a * a + b * c;
B = a * b + b * d;
C = c * a + d * c;
D = c * b + d * d;
if (n >> i & 1) {
a = A;
b = B;
c = C;
d = D;
A = a + b;
B = a;
C = c + d;
D = c;
}
a = A % m;
b = B % m;
c = C % m;
d = D % m;
}
return b;
}
vi primes(int n) {
vb b(n + 1);
vi p;
foor(i, 2, n) {
if (!b[i]) {
p << i;
for (int j = 2 * i; j <= n; j += i) {
b[j] = true;
}
}
}
return p;
}
vb isprime(const int n) {
vb v(n + 1, true);
v[0] = v[1] = false;
foor(i, 2, n) {
if (v[i]) {
for (int j = 2 * i; j <= n; j += i) {
v[j] = false;
}
}
}
return v;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vpii AB(M);
cin >> AB;
vl o(N);
UnionFind uf(N + 1);
ll z = C2(N);
rf(i, M) {
o[i] = z;
if (!uf.same(AB[i].first, AB[i].second)) {
z -= C2(uf.size(AB[i].first) + uf.size(AB[i].second));
z += C2(uf.size(AB[i].first)) + C2(uf.size(AB[i].second));
uf.unite(AB[i].first, AB[i].second);
}
}
fr(i, M) print(o[i]);
return 0;
}
| #include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define foor(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, n) for (int i = (n); i--;)
#define roof(i, b, a) for (int i = (b); i >= (a); --i)
#define elsif else if
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
#define popcount __builtin_popcount
using namespace std;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef vector<vu> vvu;
typedef double dbl;
typedef vector<dbl> vd;
typedef vector<vd> vvd;
typedef string str;
typedef vector<str> vs;
typedef vector<vs> vvs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
typedef pair<dbl, dbl> pdd;
typedef vector<pdd> vpdd;
typedef map<dbl, dbl> mdd;
typedef pair<str, str> pss;
typedef vector<pss> vpss;
typedef map<str, str> mss;
typedef pair<int, ll> pil;
typedef vector<pil> vpil;
typedef map<int, ll> mil;
typedef pair<ll, int> pli;
typedef vector<pli> vpli;
typedef map<ll, int> mli;
typedef pair<dbl, int> pdi;
typedef vector<pdi> vpdi;
typedef map<dbl, int> mdi;
template <typename T> vector<T> &operator<<(vector<T> &v, const T t) {
v.push_back(t);
return v;
}
template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) {
m.insert(t);
return m;
}
template <typename T> set<T> &operator<<(set<T> &s, const T t) {
s.insert(t);
return s;
}
template <typename T> stack<T> &operator<<(stack<T> &s, const T t) {
s.push(t);
return s;
}
template <typename T> stack<T> &operator>>(stack<T> &s, T &t) {
t = s.top();
s.pop();
return s;
}
template <typename T> queue<T> &operator<<(queue<T> &q, const T t) {
q.push(t);
return q;
}
template <typename T> queue<T> &operator>>(queue<T> &q, T &t) {
t = q.front();
q.pop();
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) {
t = q.top();
q.pop();
return q;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &p) {
return s >> p.first >> p.second;
}
template <typename T> istream &operator>>(istream &s, vector<T> &v) {
fr(i, v.size()) { s >> v[i]; }
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> p) {
return s << p.first << " " << p.second;
}
// template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto
// a:v){s<<a<<endl;}return s;}
template <typename T> ostream &operator<<(ostream &s, const vector<T> v) {
fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; }
return s;
}
template <typename T> ostream &operator<<(ostream &s, const deque<T> d) {
fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; }
return s;
}
template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) {
return b = b & t;
}
template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) {
return b = b ^ t;
}
template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) {
return b = b | t;
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
void print(void) { cout << endl; }
template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... U> void print(T &&t, U &&...u) {
cout << t << " ";
print(forward<U>(u)...);
}
bool YN(bool b) {
print(b ? "YES" : "NO");
return b;
}
bool PI(bool b) {
print(b ? "POSSIBLE" : "IMPOSSIBLE");
return b;
}
bool Yn(bool b) {
print(b ? "Yes" : "No");
return b;
}
bool Pi(bool b) {
print(b ? "Possible" : "Impossible");
return b;
}
bool yn(bool b) {
print(b ? "yes" : "no");
return b;
}
bool pi(bool b) {
print(b ? "possible" : "impossible");
return b;
}
const int MD = 1e9 + 7;
template <typename T> str to_string(const T &n) {
ostringstream s;
s << n;
return s.str();
}
template <typename T> T &chmax(T &a, T b) { return a = max(a, b); }
template <typename T> T &chmin(T &a, T b) { return a = min(a, b); }
template <typename T, typename U>
vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s,
const T inf) {
using P = pair<T, U>;
vector<P> d;
fr(i, E.size()) { d << P{inf, i}; }
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T, typename U>
map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
map<U, P> d;
for (pair<U, vector<P>> e : E) {
d[e.first] = P{inf, e.first};
}
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
ll maxflow(vector<mil> &E, int s, int t) {
ll z = 0;
vi b(E.size(), -1);
for (int i = 0;; ++i) {
static auto dfs = [&](int v, ll f, auto &dfs) -> ll {
if (v == t)
return f;
b[v] = i;
for (auto &p : E[v]) {
if (b[p.first] < i && p.second) {
if (ll r = dfs(p.first, min(f, p.second), dfs)) {
p.second -= r;
E[p.first][v] += r;
return r;
}
}
}
return 0;
};
ll x = dfs(s, ll(1e18), dfs);
z += x;
if (x == 0)
return z;
}
}
template <typename T> T distsq(pair<T, T> a, pair<T, T> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
template <typename T> T max(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = max(m, e);
}
return m;
}
template <typename T> T min(const vector<T> a) {
T m = a[0];
for (T e : a) {
m = min(m, e);
}
return m;
}
template <typename T> T gcd(const T a, const T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T> T gcd(const vector<T> a) {
T g = a[0];
for (T e : a) {
g = gcd(g, e);
}
return g;
}
template <typename T> vector<T> LIS(const vector<T> A) {
vector<T> B;
for (T a : A) {
auto it = lower_bound(all(B), a);
if (it == B.end()) {
B << a;
} else {
*it = a;
}
}
return B;
}
template <typename T> vector<T> LCS(vector<T> A, vector<T> B) {
int N = A.size(), M = B.size();
vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1));
fr(i, N) {
fr(j, M) {
if (A[i] == B[j]) {
d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}};
} else {
d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]);
}
}
}
vector<T> r;
for (pii p = {N, M}; d[p.first][p.second].first;
p = d[p.first][p.second].second) {
r << A[d[p.first][p.second].second.first];
}
Reverse(r);
return r;
}
str LCS(str S, str T) {
vector<char> s =
LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end()));
return str(s.begin(), s.end());
}
template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) {
if (V.size() <= 3) {
return V;
}
Sort(V);
rf(i, V.size() - 1) V << V[i];
vector<pair<T, T>> r;
for (pair<T, T> p : V) {
int s = r.size();
while (s >= 2 &&
(p.second - r[s - 1].second) * (p.first - r[s - 2].first) <
(p.second - r[s - 2].second) * (p.first - r[s - 1].first)) {
r.pop_back();
--s;
}
r << p;
}
r.pop_back();
return r;
}
class UnionFind {
vi p, r, s;
public:
UnionFind(int N) {
p = r = vi(N);
s = vi(N, 1);
fr(i, N) { p[i] = i; }
}
int find(int i) { return p[i] = p[i] == i ? i : find(p[i]); }
void unite(int a, int b) {
if ((a = find(a)) != (b = find(b))) {
if (r[a] > r[b]) {
swap(a, b);
}
s[b] += s[a];
r[p[a] = b] += r[a] == r[b];
}
}
bool same(int a, int b) { return find(a) == find(b); }
int size(int x) { return s[find(x)]; }
};
ll strmod(const str &s, const int m) {
ll x = 0;
fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; }
return x;
}
vvl mul(const vvl &A, const vvl &B, const int m) {
vvl C;
fr(y, A.size()) { C << vl(B[y].size()); }
fr(y, C.size()) {
fr(x, C[y].size()) {
fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; }
}
}
return C;
}
vvl pow(const vvl &A, const ll n, const int m) {
vvl B;
fr(y, A.size()) { B << vl(A.size()); }
if (n == 0) {
fr(i, B.size()) { B[i][i] = 1; }
}
elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); }
else {
vvl C = pow(A, n / 2, m);
B = mul(C, C, m);
}
return B;
}
ll pow(const ll a, const ll n, const int m) {
ll t;
return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) *
(t = pow(a, n >> 1, m), t * t % m) % m
: !!a;
}
ll inv(const ll x, const int p) { return pow(x, p - 2, p); }
ll inv(const ll x) { return inv(x, MD); }
vpll fact(const int n, const int p) {
vpll v(n + 1);
v[0].first = 1;
foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; }
v[n].second = inv(v[n].first, p);
roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; }
return v;
}
class Combination {
const vpll f;
const int M;
public:
Combination(int n, int m) : f(fact(n, m)), M(m) {}
Combination(int n) : Combination(n, MD) {}
ll P(int n, int k) {
return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M;
}
ll C(int n, int k) { return P(n, k) * f[k].second % M; }
ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); }
};
ll C2(const int n) { return (ll)n * ~-n / 2; }
ll sum(const vi a) {
ll s = 0;
for (int e : a) {
s += e;
}
return s;
}
ll sum(const vl a) {
ll s = 0;
for (ll e : a) {
s += e;
}
return s;
}
template <typename T> int MSB(T N) {
int r = -1;
for (; N > 0; N /= 2) {
++r;
}
return r;
}
template <typename T> class SegmentTree {
vector<T> S;
T (*const op)(T a, T b);
const T zero;
const int B;
public:
SegmentTree(int N, T (*f)(T a, T b), const T zero)
: S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero),
B(1 << MSB(N - 1) + 1) {}
SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero)
: SegmentTree(v.size(), f, zero) {
fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; }
roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); }
}
T calc(int l, int r) {
l += B;
r += B;
if (l > r) {
return zero;
}
if (l == r) {
return S[l];
}
T L = S[l], R = S[r];
for (; l / 2 < r / 2; l /= 2, r /= 2) {
if (l % 2 == 0) {
L = op(L, S[l + 1]);
}
if (r % 2 == 1) {
R = op(S[r - 1], R);
}
}
return op(L, R);
}
void replace(int i, T x) {
for (S[i += B] = x; i != 1; i /= 2) {
if (i % 2) {
S[i / 2] = op(S[i - 1], S[i]);
} else {
S[i / 2] = op(S[i], S[i + 1]);
}
}
}
void add(int i, T x) { replace(i, op(S[B + i], x)); }
T top() { return S[1]; }
};
ll BITsum(vl &B, int i) {
ll z = 0;
while (i > 0) {
z += B[i];
i -= i & -i;
}
return z;
}
void BITadd(vl &B, int i, ll x) {
while (i < B.size()) {
B[i] += x;
i += i & -i;
}
}
ll fib(const ll n, const int m) {
ll a, b, c, d, A, B, C, D;
a = 1;
b = 0;
c = 0;
d = 1;
rf(i, 63) {
A = a * a + b * c;
B = a * b + b * d;
C = c * a + d * c;
D = c * b + d * d;
if (n >> i & 1) {
a = A;
b = B;
c = C;
d = D;
A = a + b;
B = a;
C = c + d;
D = c;
}
a = A % m;
b = B % m;
c = C % m;
d = D % m;
}
return b;
}
vi primes(int n) {
vb b(n + 1);
vi p;
foor(i, 2, n) {
if (!b[i]) {
p << i;
for (int j = 2 * i; j <= n; j += i) {
b[j] = true;
}
}
}
return p;
}
vb isprime(const int n) {
vb v(n + 1, true);
v[0] = v[1] = false;
foor(i, 2, n) {
if (v[i]) {
for (int j = 2 * i; j <= n; j += i) {
v[j] = false;
}
}
}
return v;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vpii AB(M);
cin >> AB;
vl o(M);
UnionFind uf(N + 1);
ll z = C2(N);
rf(i, M) {
o[i] = z;
if (!uf.same(AB[i].first, AB[i].second)) {
z -= C2(uf.size(AB[i].first) + uf.size(AB[i].second));
z += C2(uf.size(AB[i].first)) + C2(uf.size(AB[i].second));
uf.unite(AB[i].first, AB[i].second);
}
}
fr(i, M) print(o[i]);
return 0;
}
| replace | 550 | 551 | 550 | 551 | 0 | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
const int MAX = 510000;
const int MOD = 1000000007;
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;
}
// 大きい方(A)に小さい方(B)をくっつけたい
// 大小が逆だったらひっくり返しちゃう
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
int main() {
ll N, M;
cin >> N >> M;
vector<P> a(M);
rep(i, M) cin >> a[i].fi >> a[i].sc;
rep(i, M) {
a[i].fi--;
a[i].sc--;
}
UnionFind Uni(N);
vector<ll> ans(N);
ans[M - 1] = N * (N - 1) / 2;
for (ll i = M - 1; i > 0; i--) {
// 後ろからつなげていく
if (Uni.root(a[i].fi) == Uni.root(a[i].sc)) {
Uni.connect(a[i].fi, a[i].sc);
ans[i - 1] = ans[i];
} else {
ans[i - 1] = ans[i] - Uni.size(a[i].fi) * Uni.size(a[i].sc);
Uni.connect(a[i].fi, a[i].sc);
}
}
rep(i, M) cout << ans[i] << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
const int MAX = 510000;
const int MOD = 1000000007;
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;
}
// 大きい方(A)に小さい方(B)をくっつけたい
// 大小が逆だったらひっくり返しちゃう
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
int main() {
ll N, M;
cin >> N >> M;
vector<P> a(M);
rep(i, M) cin >> a[i].fi >> a[i].sc;
rep(i, M) {
a[i].fi--;
a[i].sc--;
}
UnionFind Uni(N);
vector<ll> ans(M);
ans[M - 1] = N * (N - 1) / 2;
for (ll i = M - 1; i > 0; i--) {
// 後ろからつなげていく
if (Uni.root(a[i].fi) == Uni.root(a[i].sc)) {
Uni.connect(a[i].fi, a[i].sc);
ans[i - 1] = ans[i];
} else {
ans[i - 1] = ans[i] - Uni.size(a[i].fi) * Uni.size(a[i].sc);
Uni.connect(a[i].fi, a[i].sc);
}
}
rep(i, M) cout << ans[i] << endl;
}
| replace | 88 | 89 | 88 | 89 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MD 1000000007
typedef long long int ll;
typedef pair<ll, ll> P;
template <typename T> std::string tostr(const T &t) {
std::ostringstream os;
os << t;
return os.str();
}
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// 親の場合parには,-(集合のサイズ)を格納
vector<ll> par(1e6, -1), dep(1e6, 0);
// 木の根を求める
ll find(ll x) {
if (par[x] < 0) {
return x;
} else {
// return find(par[x])でも可能だが
// 根をつなぎなおして計算量を落としている
return par[x] = find(par[x]);
}
}
// xとyが同じ集合に属するか否か
bool same(ll x, ll y) { return find(x) == find(y); }
// xとyの属する集合を併合
void unite(ll x, ll y) {
// 同じ集合なら意味なし
if (same(x, y))
return;
// rankの大小でどちらにくっつけるかを判定
if (dep[x] < dep[y]) {
par[y] += par[x];
par[x] = y;
} else {
par[x] += par[y];
par[y] = x;
// 同じdepのときは,yをxにくっつけ
// xのdepをプラスする
if (dep[x] == dep[y])
dep[x]++;
}
}
// xの属する集合のサイズを求める
ll size(ll x) { return -par[find(x)]; }
int main() {
ll n, m;
cin >> n >> m;
vector<P> pa(m + 2);
vector<ll> ans(m + 2);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
pa[i] = {a, b};
}
// m番目の辺から順につなげていく
// UnionFind利用
for (int i = m - 1; i >= 0; i--) {
// 全ての橋が壊れている場合
if (i == m - 1) {
ans[i] = n * (n - 1) / 2;
} else {
if (ans[i + 1] == 0 || same(pa[i + 1].first, pa[i + 1].second)) {
ans[i] = ans[i + 1];
} else {
ll tmp = size(pa[i + 1].first) * size(pa[i + 1].second);
ans[i] = ans[i + 1] - tmp;
unite(pa[i + 1].first, pa[i + 1].second);
}
}
}
// 答え出力
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MD 1000000007
typedef long long int ll;
typedef pair<ll, ll> P;
template <typename T> std::string tostr(const T &t) {
std::ostringstream os;
os << t;
return os.str();
}
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// 親の場合parには,-(集合のサイズ)を格納
vector<ll> par(1e6, -1), dep(1e6, 0);
// 木の根を求める
ll find(ll x) {
if (par[x] < 0) {
return x;
} else {
// return find(par[x])でも可能だが
// 根をつなぎなおして計算量を落としている
return par[x] = find(par[x]);
}
}
// xとyが同じ集合に属するか否か
bool same(ll x, ll y) { return find(x) == find(y); }
// xとyの属する集合を併合
void unite(ll x, ll y) {
// 同じ集合なら意味なし
x = find(x);
y = find(y);
if (x == y)
return;
// rankの大小でどちらにくっつけるかを判定
if (dep[x] < dep[y]) {
par[y] += par[x];
par[x] = y;
} else {
par[x] += par[y];
par[y] = x;
// 同じdepのときは,yをxにくっつけ
// xのdepをプラスする
if (dep[x] == dep[y])
dep[x]++;
}
}
// xの属する集合のサイズを求める
ll size(ll x) { return -par[find(x)]; }
int main() {
ll n, m;
cin >> n >> m;
vector<P> pa(m + 2);
vector<ll> ans(m + 2);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
pa[i] = {a, b};
}
// m番目の辺から順につなげていく
// UnionFind利用
for (int i = m - 1; i >= 0; i--) {
// 全ての橋が壊れている場合
if (i == m - 1) {
ans[i] = n * (n - 1) / 2;
} else {
if (ans[i + 1] == 0 || same(pa[i + 1].first, pa[i + 1].second)) {
ans[i] = ans[i + 1];
} else {
ll tmp = size(pa[i + 1].first) * size(pa[i + 1].second);
ans[i] = ans[i + 1] - tmp;
unite(pa[i + 1].first, pa[i + 1].second);
}
}
}
// 答え出力
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
} | replace | 32 | 33 | 32 | 35 | 0 | |
p03108 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
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;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
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];
}
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;
}
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
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;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
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;
}
}
| insert | 58 | 58 | 58 | 60 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;
class UnionFind {
private:
vector<ll> data;
public:
UnionFind(ll n) : data(n, -1) {}
ll size(ll i) { return -data[find(i)]; }
bool root(ll i) { return data[i] < 0; }
bool same(ll i, ll j) { return find(i) == find(j); }
ll find(ll i) { return root(i) ? i : (data[i] = find(data[i])); }
bool unite(ll i, ll j) {
if (same(i, j))
return false;
data[find(i)] += data[find(j)];
data[find(j)] = find(i);
return true;
}
};
int main(void) {
ll N, M;
cin >> N >> M;
vector<ll> A(M), B(M);
REP(i, 0, M) cin >> A[i] >> B[i];
UnionFind uf(N);
vector<ll> ans(M);
ll x = N * (N - 1) / 2;
for (ll i = M - 1; i >= 0; i--) {
ans[i] = x;
if (!uf.same(A[i], B[i])) {
x -= uf.size(A[i]) * uf.size(B[i]);
uf.unite(A[i], B[i]);
}
}
REP(i, 0, M) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;
class UnionFind {
private:
vector<ll> data;
public:
UnionFind(ll n) : data(n, -1) {}
ll size(ll i) { return -data[find(i)]; }
bool root(ll i) { return data[i] < 0; }
bool same(ll i, ll j) { return find(i) == find(j); }
ll find(ll i) { return root(i) ? i : (data[i] = find(data[i])); }
bool unite(ll i, ll j) {
if (same(i, j))
return false;
data[find(i)] += data[find(j)];
data[find(j)] = find(i);
return true;
}
};
int main(void) {
ll N, M;
cin >> N >> M;
vector<ll> A(M), B(M);
REP(i, 0, M) cin >> A[i] >> B[i], A[i]--, B[i]--;
UnionFind uf(N);
vector<ll> ans(M);
ll x = N * (N - 1) / 2;
for (ll i = M - 1; i >= 0; i--) {
ans[i] = x;
if (!uf.same(A[i], B[i])) {
x -= uf.size(A[i]) * uf.size(B[i]);
uf.unite(A[i], B[i]);
}
}
REP(i, 0, M) cout << ans[i] << endl;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (ll i = (begin)-1; i >= (end); i--)
#define IREP(i, n) IFOR(i, n, 0)
#define SORT(a) sort(a.begin(), a.end())
#define ISORT(a) sort(a.begin(), a.end(), greater<ll>())
#define REVERSE(a) reverse(a.begin(), a.end())
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) \
cout << #v << "=(" << ; \
REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \
cout << ")" << endl;
#define mdebug(m) \
cout << #m << "=" << endl; \
REP(i_debug, m.size()) { \
REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \
cout << endl; \
}
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define ll long long
#define f first
#define s second
#define pb push_back
#define INF 1000000000000000000
using vec = vector<ll>;
using mat = vector<vec>;
using Pii = pair<ll, ll>;
using PiP = pair<ll, Pii>;
using PPi = pair<Pii, ll>;
using bvec = vector<bool>;
using Pvec = vector<Pii>;
ll mod = 1000000007;
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<ll> memnum;
UnionFind(ll N) { // 最初は全てが根であるとして初期化
init(N);
}
void init(ll N) {
par.resize(N);
memnum.resize(N);
for (ll i = 0; i < N; i++) {
par[i] = i;
memnum[i] = 1;
}
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); // xの根をrx
ll ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
//
memnum[ry] += memnum[rx];
}
bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
//////////////
bool is_root(ll x) { return par[x] == x; }
ll num(ll x) { return memnum[root(x)]; }
};
ll combi(ll n, ll k) {
if (n == k || k == 0)
return 1;
else {
return combi(n - 1, k - 1) + combi(n - 1, k);
}
}
int main() {
// 入力
ll N, M;
cin >> N >> M;
vec A(M), B(M);
UnionFind tree(N);
REP(i, M) { cin >> A[i] >> B[i]; }
vec ans(M, 0);
ans[M - 1] = N * (N - 1) / 2;
IFOR(i, M, 1) {
if (!tree.same(A[i], B[i])) {
ans[i - 1] = ans[i] - tree.num(A[i]) * tree.num(B[i]);
} else
ans[i - 1] = ans[i];
// debug(tree.num(A[i], N));
tree.unite(A[i], B[i]);
}
REP(i, M) { cout << ans[i] << endl; }
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (ll i = (begin)-1; i >= (end); i--)
#define IREP(i, n) IFOR(i, n, 0)
#define SORT(a) sort(a.begin(), a.end())
#define ISORT(a) sort(a.begin(), a.end(), greater<ll>())
#define REVERSE(a) reverse(a.begin(), a.end())
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) \
cout << #v << "=(" << ; \
REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \
cout << ")" << endl;
#define mdebug(m) \
cout << #m << "=" << endl; \
REP(i_debug, m.size()) { \
REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \
cout << endl; \
}
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define ll long long
#define f first
#define s second
#define pb push_back
#define INF 1000000000000000000
using vec = vector<ll>;
using mat = vector<vec>;
using Pii = pair<ll, ll>;
using PiP = pair<ll, Pii>;
using PPi = pair<Pii, ll>;
using bvec = vector<bool>;
using Pvec = vector<Pii>;
ll mod = 1000000007;
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<ll> memnum;
UnionFind(ll N) { // 最初は全てが根であるとして初期化
init(N);
}
void init(ll N) {
par.resize(N);
memnum.resize(N);
for (ll i = 0; i < N; i++) {
par[i] = i;
memnum[i] = 1;
}
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); // xの根をrx
ll ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
//
memnum[ry] += memnum[rx];
}
bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
//////////////
bool is_root(ll x) { return par[x] == x; }
ll num(ll x) { return memnum[root(x)]; }
};
ll combi(ll n, ll k) {
if (n == k || k == 0)
return 1;
else {
return combi(n - 1, k - 1) + combi(n - 1, k);
}
}
int main() {
// 入力
ll N, M;
cin >> N >> M;
vec A(M), B(M);
UnionFind tree(N);
REP(i, M) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
vec ans(M, 0);
ans[M - 1] = N * (N - 1) / 2;
IFOR(i, M, 1) {
if (!tree.same(A[i], B[i])) {
ans[i - 1] = ans[i] - tree.num(A[i]) * tree.num(B[i]);
} else
ans[i - 1] = ans[i];
// debug(tree.num(A[i], N));
tree.unite(A[i], B[i]);
}
REP(i, M) { cout << ans[i] << endl; }
return 0;
} | replace | 98 | 99 | 98 | 103 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = {0, -1, 0, 1};
void chmin(int &a, int b) {
if (a > b)
a = b;
}
ll GCD(ll a, ll b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
struct UnionFind {
// parent[a] = b: "aの親がbである"
// 親の場合は-(その集合のサイズ)を返す。
vector<int> parent;
// 全て根であるとみなす。
// parent[x] == -1のとき、そのノードは根である。
UnionFind(int n = 0) : parent(n, -1) {}
// xがどのグループに属しているかを返す。
int find(int x) {
if (parent[x] < 0)
return x; // 今の頂点が根ならその値を返す
return parent[x] = find(parent[x]); // 経路縮約(メモ化)
}
// AとBをつなげる。AとBを直接つなぐのではなく、root(A)とroot(B)をつなげる。
bool unite(int x,
int y) { // Minimum Spanning Treeのためにboolを返す実装にしている。
x = find(x); // それぞれのノードの親を取得する。
y = find(y);
if (x == y)
return false; // すでにくっついているのでくっつけない。
// 大きい方に小さい方をくっつけたい。
if (size(x) < size(y))
swap(
x,
y); // マイナスサイズなので不等号が逆。xのサイズの方が大きいようにする。
parent[x] += parent[y]; // 連結成分のサイズを管理するためのコード
parent[y] = x; // xにyをくっつける。yの値をxの根とする。
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -parent[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n), 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 uni(n); // 頂点数を渡す。
for (int i = m - 1; i >= 1; --i) {
ans[i - 1] = ans[i];
// つながっていなかったものがつながったとき
if (uni.find(a[i]) != uni.find(b[i])) {
ans[i - 1] -= (ll)uni.size(a[i]) * uni.size(b[i]);
uni.unite(a[i], b[i]);
}
}
rep(i, m) { cout << ans[i] << endl; }
} | #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = {0, -1, 0, 1};
void chmin(int &a, int b) {
if (a > b)
a = b;
}
ll GCD(ll a, ll b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
struct UnionFind {
// parent[a] = b: "aの親がbである"
// 親の場合は-(その集合のサイズ)を返す。
vector<int> parent;
// 全て根であるとみなす。
// parent[x] == -1のとき、そのノードは根である。
UnionFind(int n = 0) : parent(n, -1) {}
// xがどのグループに属しているかを返す。
int find(int x) {
if (parent[x] < 0)
return x; // 今の頂点が根ならその値を返す
return parent[x] = find(parent[x]); // 経路縮約(メモ化)
}
// AとBをつなげる。AとBを直接つなぐのではなく、root(A)とroot(B)をつなげる。
bool unite(int x,
int y) { // Minimum Spanning Treeのためにboolを返す実装にしている。
x = find(x); // それぞれのノードの親を取得する。
y = find(y);
if (x == y)
return false; // すでにくっついているのでくっつけない。
// 大きい方に小さい方をくっつけたい。
if (size(x) < size(y))
swap(
x,
y); // マイナスサイズなので不等号が逆。xのサイズの方が大きいようにする。
parent[x] += parent[y]; // 連結成分のサイズを管理するためのコード
parent[y] = x; // xにyをくっつける。yの値をxの根とする。
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -parent[find(x)]; }
};
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 uni(n); // 頂点数を渡す。
for (int i = m - 1; i >= 1; --i) {
ans[i - 1] = ans[i];
// つながっていなかったものがつながったとき
if (uni.find(a[i]) != uni.find(b[i])) {
ans[i - 1] -= (ll)uni.size(a[i]) * uni.size(b[i]);
uni.unite(a[i], b[i]);
}
}
rep(i, m) { cout << ans[i] << endl; }
} | replace | 68 | 69 | 68 | 69 | 0 | |
p03108 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m;
ll a[11111];
ll b[11111];
ll res[11111];
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 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);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
int main() {
cin >> n >> m;
for (ll i = 0; i < m; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
UnionFind tree(n);
res[m] = n * (n - 1) / 2;
for (ll i = m - 1; i >= 0; i--) {
if (tree.issame(a[i], b[i])) {
res[i] = res[i + 1];
} else {
res[i] = res[i + 1] - (tree.size(a[i]) * tree.size(b[i]));
}
tree.merge(a[i], b[i]);
}
for (ll i = 1; i <= m; i++) {
cout << res[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m;
ll a[111111];
ll b[111111];
ll res[111111];
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 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);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
int main() {
cin >> n >> m;
for (ll i = 0; i < m; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
UnionFind tree(n);
res[m] = n * (n - 1) / 2;
for (ll i = m - 1; i >= 0; i--) {
if (tree.issame(a[i], b[i])) {
res[i] = res[i + 1];
} else {
res[i] = res[i + 1] - (tree.size(a[i]) * tree.size(b[i]));
}
tree.merge(a[i], b[i]);
}
for (ll i = 1; i <= m; i++) {
cout << res[i] << endl;
}
return 0;
}
| replace | 5 | 8 | 5 | 8 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.