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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int INF = pow(2, 29);
const int MOD = 1000000007;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) { rep(i, N) par[i] = i; }
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M;
cin >> N >> M;
UnionFind tree(N);
rep(i, M) {
int X, Y, Z;
cin >> X >> Y >> Z;
tree.unite(X, Y);
}
int r[N];
rep(i, N) r[i] = tree.root(i);
sort(r, r + N);
int result = 1;
rep(i, N - 1) if (r[i] != r[i + 1]) result++;
cout << result << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int INF = pow(2, 29);
const int MOD = 1000000007;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) { rep(i, N) par[i] = i; }
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M;
cin >> N >> M;
UnionFind tree(N);
rep(i, M) {
int X, Y, Z;
cin >> X >> Y >> Z;
X--;
Y--;
tree.unite(X, Y);
}
int r[N];
rep(i, N) r[i] = tree.root(i);
sort(r, r + N);
int result = 1;
rep(i, N - 1) if (r[i] != r[i + 1]) result++;
cout << result << endl;
return 0;
} | insert | 48 | 48 | 48 | 50 | 0 | |
p03045 | C++ | Runtime Error | // https://atcoder.jp/contests/abc126/tasks/abc126_e
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF (1e9)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
// int gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
// int lcm(long a, long b) { return a * b / gcd(a, b); }
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
// 最初は全てが根であるとして初期化
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
// データxが属する木の根を再帰で得る:root(x) = {xの木の根}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
// X, Yのグループが何個あるか調べればいい
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
UnionFind tree(n);
vector<int> v;
set<int> st;
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
tree.unite(x, y);
}
for (int i = 1; i <= n; i++) {
st.insert(tree.root(i));
}
cout << st.size() << endl;
return 0;
} | // https://atcoder.jp/contests/abc126/tasks/abc126_e
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF (1e9)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
// int gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
// int lcm(long a, long b) { return a * b / gcd(a, b); }
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
// 最初は全てが根であるとして初期化
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
// データxが属する木の根を再帰で得る:root(x) = {xの木の根}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
// X, Yのグループが何個あるか調べればいい
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
UnionFind tree(n + 1);
set<int> st;
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
tree.unite(x, y);
}
for (int i = 1; i <= n; i++) {
st.insert(tree.root(i));
}
cout << st.size() << endl;
return 0;
} | replace | 62 | 64 | 62 | 63 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int N = 1e5 + 10;
struct Edge {
int v, next;
} edge[N];
bool vis[N];
int n, m, cnt, head[N];
inline void add(int u, int v) {
edge[++cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[++cnt].v = u;
edge[cnt].next = head[v];
head[v] = cnt++;
}
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v])
dfs(v);
}
}
int main() {
n = read(), m = read();
for (int i = 0; i < m; i++) {
int a = read(), b = read();
int c = read();
add(a, b);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans++;
dfs(i);
}
}
printf("%d\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int N = 1e5 + 10;
struct Edge {
int v, next;
} edge[N * 4];
bool vis[N];
int n, m, cnt, head[N];
inline void add(int u, int v) {
edge[++cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[++cnt].v = u;
edge[cnt].next = head[v];
head[v] = cnt++;
}
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v])
dfs(v);
}
}
int main() {
n = read(), m = read();
for (int i = 0; i < m; i++) {
int a = read(), b = read();
int c = read();
add(a, b);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans++;
dfs(i);
}
}
printf("%d\n", ans);
return 0;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;
class UnionFind {
vector<int> par;
vector<int> myrank;
vector<int> size;
public:
UnionFind(int n) {
par.resize(n);
myrank.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
myrank[i] = 0;
size[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 (myrank[x] < myrank[y]) {
par[x] = y;
size[y] += size[x];
} else {
par[y] = x;
size[x] += size[y];
if (myrank[x] == myrank[y])
myrank[x]++;
}
}
// xとyが同じ集合に属するかどうか
bool same(int x, int y) { return find(x) == find(y); }
// xが属する集合の大きさを返す
int getSize(int x) { return size[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uf.unite(x, y);
}
vector<bool> res(n, false);
int count = 0;
for (int i = 0; i < n; i++) {
int p = uf.find(i);
if (!res[p]) {
count++;
res[p] = true;
}
}
cout << count << endl;
return 0;
}
| #include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;
class UnionFind {
vector<int> par;
vector<int> myrank;
vector<int> size;
public:
UnionFind(int n) {
par.resize(n);
myrank.resize(n);
size.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
myrank[i] = 0;
size[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 (myrank[x] < myrank[y]) {
par[x] = y;
size[y] += size[x];
} else {
par[y] = x;
size[x] += size[y];
if (myrank[x] == myrank[y])
myrank[x]++;
}
}
// xとyが同じ集合に属するかどうか
bool same(int x, int y) { return find(x) == find(y); }
// xが属する集合の大きさを返す
int getSize(int x) { return size[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uf.unite(x, y);
}
vector<bool> res(n, false);
int count = 0;
for (int i = 0; i < n; i++) {
int p = uf.find(i);
if (!res[p]) {
count++;
res[p] = true;
}
}
cout << count << endl;
return 0;
}
| insert | 39 | 39 | 39 | 40 | -11 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
class DisjointSet {
public:
vector<int> rank, p;
DisjointSet() {}
DisjointSet(int size) {
rank.resize(size, 0);
p.resize(size, 0);
for (int i = 0; i < size; i++)
makeSet(i);
}
void makeSet(int x) {
p[x] = x;
rank[x] = 0;
} // 自身がparentのグループを作る
bool same(int x, int y) {
return findSet(x) == findSet(y); // x,yが同じグループに属しているか判定
}
void unite(int x, int y) {
link(findSet(x), findSet(y));
} // xとyの属するsetを結合する
void link(int x, int y) {
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y]) {
rank[y]++;
}
}
} // rankが大きい方の下にrankが小さいものをくっつける
int findSet(int x) {
if (x != p[x]) {
p[x] = findSet(p[x]);
}
return p[x];
} // 再帰的にxの親を見つけている
};
int main() {
int n, m, ans = 0;
bool flag = 0;
cin >> n >> m;
ans = n;
int x, y, z;
DisjointSet ds = DisjointSet(n);
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
if (ds.same(x, y) == false) {
ds.unite(x, y);
ans--;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
class DisjointSet {
public:
vector<int> rank, p;
DisjointSet() {}
DisjointSet(int size) {
rank.resize(size, 0);
p.resize(size, 0);
for (int i = 0; i < size; i++)
makeSet(i);
}
void makeSet(int x) {
p[x] = x;
rank[x] = 0;
} // 自身がparentのグループを作る
bool same(int x, int y) {
return findSet(x) == findSet(y); // x,yが同じグループに属しているか判定
}
void unite(int x, int y) {
link(findSet(x), findSet(y));
} // xとyの属するsetを結合する
void link(int x, int y) {
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y]) {
rank[y]++;
}
}
} // rankが大きい方の下にrankが小さいものをくっつける
int findSet(int x) {
if (x != p[x]) {
p[x] = findSet(p[x]);
}
return p[x];
} // 再帰的にxの親を見つけている
};
int main() {
int n, m, ans = 0;
bool flag = 0;
cin >> n >> m;
ans = n;
int x, y, z;
DisjointSet ds = DisjointSet(n);
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
if (ds.same(x - 1, y - 1) == false) {
ds.unite(x - 1, y - 1);
ans--;
}
}
cout << ans << endl;
return 0;
} | replace | 60 | 62 | 60 | 62 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define pb(a) push_back(a)
int n, m;
vector<int> ki(100000);
vector<int> kz(100000, 1);
int root(int w) {
int ww = w;
vector<int> kidmy(100000);
int lfg = 0;
REP(ii, 10) {
if (ki[ww] == ww) {
break;
} else {
kidmy[lfg] = ww;
lfg++;
ww = ki[ww];
}
}
REP(ii, lfg) { ki[kidmy[ii]] = ww; }
// if (kz[ww]<lfg)kz[ww]=lfg;
return ww;
}
int main() {
int ans = 0;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
REP(i, m) cin >> x[i] >> y[i] >> z[i];
REP(i, 100000) ki[i] = i;
REP(i, m) { // 結合させていく
int pidx = root(x[i] - 1);
int pidy = root(y[i] - 1);
if (pidx != pidy) {
if (kz[pidy] > kz[pidx]) {
ki[pidx] = pidy;
kz[pidy] += kz[pidx];
} else {
ki[pidy] = pidx;
kz[pidx] += kz[pidy];
}
}
}
REP(i, n) {
if (ki[i] == i)
ans++;
}
cout << ans << endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define pb(a) push_back(a)
int n, m;
vector<int> ki(100000);
vector<int> kz(100000, 1);
int root(int w) {
int ww = w;
vector<int> kidmy(10);
int lfg = 0;
REP(ii, 10) {
if (ki[ww] == ww) {
break;
} else {
kidmy[lfg] = ww;
lfg++;
ww = ki[ww];
}
}
REP(ii, lfg) { ki[kidmy[ii]] = ww; }
// if (kz[ww]<lfg)kz[ww]=lfg;
return ww;
}
int main() {
int ans = 0;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
REP(i, m) cin >> x[i] >> y[i] >> z[i];
REP(i, 100000) ki[i] = i;
REP(i, m) { // 結合させていく
int pidx = root(x[i] - 1);
int pidy = root(y[i] - 1);
if (pidx != pidy) {
if (kz[pidy] > kz[pidx]) {
ki[pidx] = pidy;
kz[pidy] += kz[pidx];
} else {
ki[pidy] = pidx;
kz[pidx] += kz[pidy];
}
}
}
REP(i, n) {
if (ki[i] == i)
ans++;
}
cout << ans << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct DSU {
int n;
vector<int> ps;
vector<int> rank;
int num_components;
DSU(int n) : n(n), ps(n), num_components(n) {
for (int i = 0; i < n; ++i) {
ps[i] = i;
rank[i] = 0;
}
}
int parent(int i) {
if (ps[i] == i) {
return i;
}
return ps[i] = parent(ps[i]);
}
int merge(int i, int j) {
i = parent(i);
j = parent(j);
if (i != j) {
if (rank[i] <= rank[j]) {
swap(i, j);
}
if (rank[i] == rank[j]) {
++rank[i];
}
ps[j] = i;
--num_components;
return true;
}
return false;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> ps(n);
for (int i = 0; i < n; ++i) {
ps[i] = i;
}
DSU dsu(n);
for (int i = 0; i < m; ++i) {
int x, y, z;
cin >> x >> y >> z;
--x;
--y;
dsu.merge(x, y);
}
cout << dsu.num_components << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
struct DSU {
int n;
vector<int> ps;
vector<int> rank;
int num_components;
DSU(int n) : n(n), ps(n), num_components(n), rank(n) {
for (int i = 0; i < n; ++i) {
ps[i] = i;
rank[i] = 0;
}
}
int parent(int i) {
if (ps[i] == i) {
return i;
}
return ps[i] = parent(ps[i]);
}
int merge(int i, int j) {
i = parent(i);
j = parent(j);
if (i != j) {
if (rank[i] <= rank[j]) {
swap(i, j);
}
if (rank[i] == rank[j]) {
++rank[i];
}
ps[j] = i;
--num_components;
return true;
}
return false;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> ps(n);
for (int i = 0; i < n; ++i) {
ps[i] = i;
}
DSU dsu(n);
for (int i = 0; i < m; ++i) {
int x, y, z;
cin >> x >> y >> z;
--x;
--y;
dsu.merge(x, y);
}
cout << dsu.num_components << endl;
return 0;
}
| replace | 9 | 10 | 9 | 10 | -11 | |
p03045 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
return root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[ry] = rx;
};
bool same(int x, int y) {
if (root(x) == root(y))
return true;
return false;
}
};
void Main() {
int N, M;
std::cin >> N >> M;
UnionFind uf(N);
int x, y, z;
for (int m = 0; m < M; m++) {
std::cin >> x >> y >> z;
uf.unite(x - 1, y - 1);
}
std::map<int, int> m;
int res = 0;
for (int n = 0; n < N; n++) {
if (!m[uf.root(n)]) {
m[uf.root(n)] = 1;
res++;
}
}
cout << res << endl;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
} | #include "bits/stdc++.h"
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[ry] = rx;
};
bool same(int x, int y) {
if (root(x) == root(y))
return true;
return false;
}
};
void Main() {
int N, M;
std::cin >> N >> M;
UnionFind uf(N);
int x, y, z;
for (int m = 0; m < M; m++) {
std::cin >> x >> y >> z;
uf.unite(x - 1, y - 1);
}
std::map<int, int> m;
int res = 0;
for (int n = 0; n < N; n++) {
if (!m[uf.root(n)]) {
m[uf.root(n)] = 1;
res++;
}
}
cout << res << endl;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
} | replace | 15 | 16 | 15 | 16 | TLE | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define loop(i, a, b) for (lli i = a; i < b; i++)
#define initialize(array, size, value) \
for (lli i = 0; i < size; i++) \
array[i] = value
#define couta(array, size) \
for (lli i = 0; i < size; i++) \
cout << array[i] << " "
#define vl vector<lli>
#define vp vector<pair<lli, lli>>
#define sl set<lli>
#define msp multiset<pair<long long, long long>> S;
#define pll pair<lli, lli>
#define mll \
map<lli, \
lli> // for( map<lli, lli>::iterator
// i=temp.begin();i!=temp.end();i++)cout<<i->fi<<" "<<i->se<<endl;
#define mvl map<lli, vl>
#define umll unordered_map<lli, lli>
#define vt vector<pair<lli, pll>>
#define vf vector<pair<pll, pll>>
#define qu queue<lli>
#define pq priority_queue<lli>
#define dq deque<lli>
#define ptr vector<lli>::iterator
#define bs(array, x) \
binary_search(array.begin(), array.end(), \
x) // also valid for set and multiset
#define lb(array, x) lower_bound(array.begin(), array.end(), x)
#define ub(array, x) upper_bound(array.begin(), array.end(), x)
#define nobw(array, i, j) \
upper_bound(array.begin(), array.end(), j) - \
lower_bound(array.begin(), array.end(), \
i) // number of numbers between i & j
#define vc clear()
#define endl '\n'
#define sp system("pause");
#define INF 9223372036854775807
#define MINF -9223372036854775808
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
lli par[100001];
lli siz[100001];
// vector<lli>k[200000]; //if memory limit exceed try to exclude it
lli find(lli a) {
if (a == par[a])
return a;
return find(par[a]);
}
void unite(lli a, lli b) {
a = find(a);
b = find(b);
if (siz[b] > siz[a])
swap(a, b);
par[b] = a;
siz[a] += siz[b];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
lli n, m;
cin >> n >> m;
lli a[m][3];
for (lli i = 0; i < m; i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
loop(i, 1, n + 1) {
par[i] = i;
siz[i] = 1;
// k[i].push_back(i);
}
for (lli i = 0; i < m; i++) {
unite(a[i][0], a[i][1]);
}
lli te[n + 1];
for (lli i = 1; i <= n; i++)
te[i] = 0;
// for(lli i=1;i<=n;i++)cout<<par[i];
for (lli i = 1; i <= n; i++)
te[find(i)]++;
lli ans = 0;
for (lli i = 1; i <= n; i++)
if (te[i] != 0)
ans++;
cout << ans;
/*
for(lli i=m-1;i>=0;i--){
ans.pb(ans1);
if(find(a[i][0])==find(a[i][1])){continue;
}
ans1=ans1-(k[find(a[i][0])].size()*k[find(a[i][1])].size());
//cout<<k[find(a[i][0])].size()<<" "<<k[find(a[i][1])].size()<<endl;
unite(a[i][0],a[i][1]);
}
reverse(ans.begin(),ans.end());
for(lli i=0;i<ans.size();i++)cout<<ans[i]<<endl;
/*loop(i,0,n-1) {
lli a, b; cin >> a >> b;
unite(a, b);
}*/
// lli t=k[find(1)].size();
// for (lli i=0;i<t;i++)cout <<k[find(1)][i]<< " ";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define loop(i, a, b) for (lli i = a; i < b; i++)
#define initialize(array, size, value) \
for (lli i = 0; i < size; i++) \
array[i] = value
#define couta(array, size) \
for (lli i = 0; i < size; i++) \
cout << array[i] << " "
#define vl vector<lli>
#define vp vector<pair<lli, lli>>
#define sl set<lli>
#define msp multiset<pair<long long, long long>> S;
#define pll pair<lli, lli>
#define mll \
map<lli, \
lli> // for( map<lli, lli>::iterator
// i=temp.begin();i!=temp.end();i++)cout<<i->fi<<" "<<i->se<<endl;
#define mvl map<lli, vl>
#define umll unordered_map<lli, lli>
#define vt vector<pair<lli, pll>>
#define vf vector<pair<pll, pll>>
#define qu queue<lli>
#define pq priority_queue<lli>
#define dq deque<lli>
#define ptr vector<lli>::iterator
#define bs(array, x) \
binary_search(array.begin(), array.end(), \
x) // also valid for set and multiset
#define lb(array, x) lower_bound(array.begin(), array.end(), x)
#define ub(array, x) upper_bound(array.begin(), array.end(), x)
#define nobw(array, i, j) \
upper_bound(array.begin(), array.end(), j) - \
lower_bound(array.begin(), array.end(), \
i) // number of numbers between i & j
#define vc clear()
#define endl '\n'
#define sp system("pause");
#define INF 9223372036854775807
#define MINF -9223372036854775808
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
lli par[100001];
lli siz[100001];
// vector<lli>k[200000]; //if memory limit exceed try to exclude it
lli find(lli a) {
if (a == par[a])
return a;
return find(par[a]);
}
void unite(lli a, lli b) {
a = find(a);
b = find(b);
if (siz[b] > siz[a])
swap(a, b);
par[b] = a;
siz[a] += siz[b];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
lli n, m;
cin >> n >> m;
lli a[m][3];
for (lli i = 0; i < m; i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
loop(i, 1, n + 1) {
par[i] = i;
siz[i] = 1;
// k[i].push_back(i);
}
for (lli i = 0; i < m; i++) {
unite(a[i][0], a[i][1]);
}
lli te[n + 1];
for (lli i = 1; i <= n; i++)
te[i] = 0;
// for(lli i=1;i<=n;i++)cout<<par[i];
for (lli i = 1; i <= n; i++)
te[find(i)]++;
lli ans = 0;
for (lli i = 1; i <= n; i++)
if (te[i] != 0)
ans++;
cout << ans;
/*
for(lli i=m-1;i>=0;i--){
ans.pb(ans1);
if(find(a[i][0])==find(a[i][1])){continue;
}
ans1=ans1-(k[find(a[i][0])].size()*k[find(a[i][1])].size());
//cout<<k[find(a[i][0])].size()<<" "<<k[find(a[i][1])].size()<<endl;
unite(a[i][0],a[i][1]);
}
reverse(ans.begin(),ans.end());
for(lli i=0;i<ans.size();i++)cout<<ans[i]<<endl;
/*loop(i,0,n-1) {
lli a, b; cin >> a >> b;
unite(a, b);
}*/
// lli t=k[find(1)].size();
// for (lli i=0;i<t;i++)cout <<k[find(1)][i]<< " ";
return 0;
}
| delete | 72 | 76 | 72 | 72 | -11 | |
p03045 | 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;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
int n, m, ans;
vector<int> x, y, z, p;
int find(int x) {
while (x != p[x])
x = p[x];
return x;
}
void merge(int x, int y) {
x--, y--;
int rootx = find(x);
int rooty = find(y);
if (rooty != rootx) {
p[rootx] = rooty;
ans--;
}
}
int solve() {
rep(i, m) merge(x[i], y[i]);
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
scanf("%d%d", &n, &m);
rep(i, m) {
int xi, yi, zi;
scanf("%d%d%d", &xi, &yi, &zi);
x.push_back(xi);
y.push_back(yi);
z.push_back(zi);
}
p.resize(n);
rep(i, n) { p[i] = i; }
ans = n;
cout << solve() << 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;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
int n, m, ans;
vector<int> x, y, z, p;
int find(int x) {
if (p[x] == x)
return x;
return p[x] = find(p[x]);
}
void merge(int x, int y) {
x--, y--;
int rootx = find(x);
int rooty = find(y);
if (rooty != rootx) {
p[rootx] = rooty;
ans--;
}
}
int solve() {
rep(i, m) merge(x[i], y[i]);
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
scanf("%d%d", &n, &m);
rep(i, m) {
int xi, yi, zi;
scanf("%d%d%d", &xi, &yi, &zi);
x.push_back(xi);
y.push_back(yi);
z.push_back(zi);
}
p.resize(n);
rep(i, n) { p[i] = i; }
ans = n;
cout << solve() << endl;
return 0;
} | replace | 12 | 15 | 12 | 15 | TLE | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define all(x) x.begin(), x.end()
const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll MAXN = 1e9;
struct union_find {
vector<int> par, r;
union_find(int n) : par(n), r(n) { init(n); }
void init(int n) {
for (int i = 0; i < n; i++)
par[i] = i;
for (int i = 0; i < n; i++)
r[i] = 0;
}
int find(int x) {
if (par[x] == x)
return x;
else
return find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (r[x] < r[y]) {
par[x] = y;
} else {
par[y] = x;
if (r[x] == r[y])
r[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
int n, m;
cin >> n >> m;
vector<int> x(n), y(n);
for (int i = 0; i < m; i++) {
int z;
cin >> x[i] >> y[i] >> z;
}
union_find uf(n);
for (int i = 0; i < m; i++) {
x[i]--;
y[i]--;
if (!uf.same(x[i], y[i]))
uf.unite(x[i], y[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (uf.par[i] == i)
ans++;
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define all(x) x.begin(), x.end()
const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll MAXN = 1e9;
struct union_find {
vector<int> par, r;
union_find(int n) : par(n), r(n) { init(n); }
void init(int n) {
for (int i = 0; i < n; i++)
par[i] = i;
for (int i = 0; i < n; i++)
r[i] = 0;
}
int find(int x) {
if (par[x] == x)
return x;
else
return find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (r[x] < r[y]) {
par[x] = y;
} else {
par[y] = x;
if (r[x] == r[y])
r[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m), y(m);
for (int i = 0; i < m; i++) {
int z;
cin >> x[i] >> y[i] >> z;
}
union_find uf(n);
for (int i = 0; i < m; i++) {
x[i]--;
y[i]--;
if (!uf.same(x[i], y[i]))
uf.unite(x[i], y[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (uf.par[i] == i)
ans++;
}
cout << ans << endl;
return 0;
} | replace | 62 | 63 | 62 | 63 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;
struct UF {
vector<int> par; // 親
vector<int> Rank; // 木の深さ
UF(int n) { init(n); }
// 初期化
void init(int n) {
par.resize(n);
Rank.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
Rank[i] = 0;
}
}
// 木の根を求める
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;
} else {
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++; // 同じだとrankが変わらないままになるので増やす(それ以外は大きい方のrankが採用される)
}
}
// xとyが同じ集合に属するかを判定
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> x(n), y(n), z(n);
rep(i, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
struct UF tree(n + 1);
rep(i, m) { tree.unite(x[i], y[i]); }
map<int, ll> s;
for (int i = 0; i < n; ++i) {
s[tree.find(i)]++;
}
cout << s.size() << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;
struct UF {
vector<int> par; // 親
vector<int> Rank; // 木の深さ
UF(int n) { init(n); }
// 初期化
void init(int n) {
par.resize(n);
Rank.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
Rank[i] = 0;
}
}
// 木の根を求める
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;
} else {
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++; // 同じだとrankが変わらないままになるので増やす(それ以外は大きい方のrankが採用される)
}
}
// xとyが同じ集合に属するかを判定
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> x(m), y(m), z(m);
rep(i, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
struct UF tree(n + 1);
rep(i, m) { tree.unite(x[i], y[i]); }
map<int, ll> s;
for (int i = 0; i < n; ++i) {
s[tree.find(i)]++;
}
cout << s.size() << endl;
} | replace | 72 | 73 | 72 | 73 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
const int a = 1e5 + 1000;
int f[a];
int findf(int q) {
if (f[q] < 0) {
return q;
} else {
return findf(f[q]);
}
}
int main() {
int N;
int M;
cin >> N >> M;
for (int i = 0; i < a; i++) {
f[i] = -1;
}
for (int i = 0; i < M; i++) {
int X;
int Y;
int Z;
cin >> X >> Y >> Z;
int fx;
int fy;
fx = findf(X);
fy = findf(Y);
if (fx != fy) {
f[fy] = fx;
}
}
int b = 0;
for (int i = 1; i <= N; i++) {
if (f[i] < 0) {
b++;
}
}
cout << b;
} | #include <algorithm>
#include <iostream>
using namespace std;
const int a = 1e5 + 1000;
int f[a];
int findf(int q) {
if (f[q] < 0) {
return q;
} else {
f[q] = findf(f[q]);
return f[q];
}
}
int main() {
int N;
int M;
cin >> N >> M;
for (int i = 0; i < a; i++) {
f[i] = -1;
}
for (int i = 0; i < M; i++) {
int X;
int Y;
int Z;
cin >> X >> Y >> Z;
int fx;
int fy;
fx = findf(X);
fy = findf(Y);
if (fx != fy) {
f[fy] = fx;
}
}
int b = 0;
for (int i = 1; i <= N; i++) {
if (f[i] < 0) {
b++;
}
}
cout << b;
}
| replace | 11 | 12 | 11 | 13 | TLE | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define FOR(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR1(i, n) for (int(i) = 1; (i) < (n); (i)++)
#define eFOR(i, n) for (int(i) = 0; (i) <= (n); (i)++)
#define eFOR1(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define SORT(i) sort((i).begin(), (i).end())
#define rSORT(i) sort((i).begin(), (i).end(), greater<int>());
#define YES(i) cout << ((i) ? "Yes" : "No") << endl;
constexpr auto INF = 1000000000;
constexpr auto LLINF = 922337203685477580; // 7;
constexpr auto mod = 1000000007;
class unionfind {
VI par, rank;
public:
void init(int N) {
par.clear();
rank.clear();
for (int i = 0; i < N; i++)
par.push_back(i);
for (int i = 0; i < N; i++)
rank.push_back(1);
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
int size(int x) {
if (par[x] == x)
return rank[x];
return size(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
if (rank[rx] < rank[ry]) {
par[rx] = ry;
rank[ry] += rank[rx];
} else {
par[ry] = rx;
rank[rx] += rank[ry];
}
}
bool same(int x, int y) { return root(x) == root(y); }
};
int main() {
int n, m;
cin >> n >> m;
VI x(n), y(n), z(n);
FOR(i, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
unionfind UF;
UF.init(n);
FOR(i, m) UF.unite(x[i], y[i]);
set<int> ans;
FOR(i, n) ans.insert(UF.root(i));
cout << ans.size() << endl;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define FOR(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR1(i, n) for (int(i) = 1; (i) < (n); (i)++)
#define eFOR(i, n) for (int(i) = 0; (i) <= (n); (i)++)
#define eFOR1(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define SORT(i) sort((i).begin(), (i).end())
#define rSORT(i) sort((i).begin(), (i).end(), greater<int>());
#define YES(i) cout << ((i) ? "Yes" : "No") << endl;
constexpr auto INF = 1000000000;
constexpr auto LLINF = 922337203685477580; // 7;
constexpr auto mod = 1000000007;
class unionfind {
VI par, rank;
public:
void init(int N) {
par.clear();
rank.clear();
for (int i = 0; i < N; i++)
par.push_back(i);
for (int i = 0; i < N; i++)
rank.push_back(1);
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
int size(int x) {
if (par[x] == x)
return rank[x];
return size(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
if (rank[rx] < rank[ry]) {
par[rx] = ry;
rank[ry] += rank[rx];
} else {
par[ry] = rx;
rank[rx] += rank[ry];
}
}
bool same(int x, int y) { return root(x) == root(y); }
};
int main() {
int n, m;
cin >> n >> m;
VI x(m), y(m), z(m);
FOR(i, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
unionfind UF;
UF.init(n);
FOR(i, m) UF.unite(x[i], y[i]);
set<int> ans;
FOR(i, n) ans.insert(UF.root(i));
cout << ans.size() << endl;
} | replace | 66 | 67 | 66 | 67 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxN = 2e5 + 7;
const int maxV = 90010;
// const int INF = 1e9+9;
const ll INF = (1ll) << 60;
const int MOD = 1e9 + 7;
int fa[maxN];
int get(int x) {
if (x == fa[x])
return fa[x];
else
fa[x] = get(fa[x]);
return fa[x];
}
int add(int x, int y) {
int u = fa[x];
int v = fa[y];
if (u != v)
fa[u] = v;
}
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
add(x, y);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (get(i) == i)
cnt++;
}
cout << cnt << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxN = 2e5 + 7;
const int maxV = 90010;
// const int INF = 1e9+9;
const ll INF = (1ll) << 60;
const int MOD = 1e9 + 7;
int fa[maxN];
int get(int x) {
if (x == fa[x])
return fa[x];
else
fa[x] = get(fa[x]);
return fa[x];
}
int add(int x, int y) {
int u = get(x);
int v = get(y);
if (u != v)
fa[u] = v;
}
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
add(x, y);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (get(i) == i)
cnt++;
}
cout << cnt << endl;
return 0;
} | replace | 18 | 20 | 18 | 20 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include <stdio.h>
const int maxn = 1e5 + 7;
int pre[maxn];
bool you[maxn];
int Find(int x) {
int p, tmp;
p = x;
while (x != pre[x])
x = pre[x];
while (p != x) {
tmp = pre[x];
pre[x] = x;
p = tmp;
}
return x;
}
void join(int x, int y) {
int fx = Find(x);
int fy = Find(y);
if (fx != fy)
pre[fx] = fy;
}
int main() {
int n, m, sum = 0, jian = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
pre[i] = i;
for (int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a == b && you[a] == false) {
jian++;
you[a] = true;
}
join(a, b);
}
for (int i = 1; i <= n; i++)
if (pre[i] == i)
sum++;
printf("%d\n", sum - jian);
printf("\n");
} | #include <stdio.h>
const int maxn = 1e5 + 7;
int pre[maxn];
bool you[maxn];
int Find(int x) {
if (pre[x] == x)
return x;
else
return pre[x] = Find(pre[x]);
}
void join(int x, int y) {
int fx = Find(x);
int fy = Find(y);
if (fx != fy)
pre[fx] = fy;
}
int main() {
int n, m, sum = 0, jian = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
pre[i] = i;
for (int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a == b && you[a] == false) {
jian++;
you[a] = true;
}
join(a, b);
}
for (int i = 1; i <= n; i++)
if (pre[i] == i)
sum++;
printf("%d\n", sum - jian);
printf("\n");
} | replace | 5 | 15 | 5 | 9 | TLE | |
p03045 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int fa[100007];
int find_(int x) {
if (fa[x] != x)
return find_(fa[x]);
return fa[x];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
fa[i] = i;
for (int i = 1; i <= m; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
int xx = find_(x);
int yy = find_(y);
if (xx != yy)
fa[xx] = yy;
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (fa[i] == i)
cnt++;
}
printf("%d", cnt);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int fa[100007];
int find_(int x) {
if (fa[x] != x)
return fa[x] = find_(fa[x]);
return fa[x];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
fa[i] = i;
for (int i = 1; i <= m; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
int xx = find_(x);
int yy = find_(y);
if (xx != yy)
fa[xx] = yy;
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (fa[i] == i)
cnt++;
}
printf("%d", cnt);
return 0;
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG
/**
* For DEBUG
* https://github.com/ta7uw/cpp-pyprint
*/
#include "cpp-pyprint/pyprint.h"
#endif
/**
* Library
* --------------------------------------------------------
*/
class UnionFind {
vector<int> par;
vector<int> rank;
vector<int> size;
vector<vector<int>> group;
public:
UnionFind(int n = 1) {
par.resize(n + 1);
rank.resize(n + 1);
size.resize(n + 1);
group.resize(n + 1);
for (int i = 0; i <= n; ++i) {
par[i] = i;
rank[i] = 0;
size[i] = 1;
group[i].push_back(i);
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
int r = find(par[x]);
return par[x] = r;
}
}
bool is_same(int x, int y) { return find(x) == find(y); }
bool unit(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
} else {
if (rank[x] < rank[y]) {
swap(x, y);
}
if (rank[x] == rank[y]) {
++rank[x];
}
par[y] = x;
size[x] += size[y];
return true;
}
}
int get_size(int x) { return size[find(x)]; }
void merge(int x, int y) {
x = find(x);
y = find(y);
if (group[x].size() > group[y].size()) {
swap(x, y);
}
copy(group[y].begin(), group[y].end(), back_inserter(group[x]));
group[y].clear();
par[y] = x;
}
};
/**
* --------------------------------------------------------
*/
void Main() {
ll N, M;
cin >> N >> M;
UnionFind un(N);
rep(i, M) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
un.merge(a, b);
}
set<ll> x;
rep(i, N) { x.insert(un.find(i)); }
cout << x.size() << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG
/**
* For DEBUG
* https://github.com/ta7uw/cpp-pyprint
*/
#include "cpp-pyprint/pyprint.h"
#endif
/**
* Library
* --------------------------------------------------------
*/
class UnionFind {
vector<int> par;
vector<int> rank;
vector<int> size;
vector<vector<int>> group;
public:
UnionFind(int n = 1) {
par.resize(n + 1);
rank.resize(n + 1);
size.resize(n + 1);
group.resize(n + 1);
for (int i = 0; i <= n; ++i) {
par[i] = i;
rank[i] = 0;
size[i] = 1;
group[i].push_back(i);
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
int r = find(par[x]);
return par[x] = r;
}
}
bool is_same(int x, int y) { return find(x) == find(y); }
bool unit(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
} else {
if (rank[x] < rank[y]) {
swap(x, y);
}
if (rank[x] == rank[y]) {
++rank[x];
}
par[y] = x;
size[x] += size[y];
return true;
}
}
int get_size(int x) { return size[find(x)]; }
void merge(int x, int y) {
x = find(x);
y = find(y);
if (group[x].size() > group[y].size()) {
swap(x, y);
}
copy(group[y].begin(), group[y].end(), back_inserter(group[x]));
group[y].clear();
par[y] = x;
}
};
/**
* --------------------------------------------------------
*/
void Main() {
ll N, M;
cin >> N >> M;
UnionFind un(N);
rep(i, M) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
// un.merge(a, b);
un.unit(a, b);
}
set<ll> x;
rep(i, N) { x.insert(un.find(i)); }
cout << x.size() << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
}
| replace | 105 | 106 | 105 | 107 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 1e5 + 7;
vector<int> par;
int getparent(int x) {
if (par[x] == x)
return x;
return par[x] = getparent(par[x]);
}
void dsu(int a, int b) {
a = getparent(a);
b = getparent(b);
if (a != b) {
par[b] = a;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
par.resize(n + 1);
for (int i = 0; i <= n; i++) {
par[i] = i;
}
for (int i = 0; i < m; i++) {
int u, v, z;
cin >> u >> v >> z;
dsu(u, v);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (par[i] == i)
ans++;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 1e5 + 7;
vector<int> par;
int getparent(int x) {
if (par[x] == x)
return x;
return par[x] = getparent(par[x]);
}
void dsu(int a, int b) {
a = getparent(a);
b = getparent(b);
if (a != b) {
par[b] = a;
}
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
par.resize(n + 1);
for (int i = 0; i <= n; i++) {
par[i] = i;
}
for (int i = 0; i < m; i++) {
int u, v, z;
cin >> u >> v >> z;
dsu(u, v);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (par[i] == i)
ans++;
}
cout << ans << endl;
}
| replace | 24 | 28 | 24 | 28 | -11 | |
p03045 | Python | Runtime Error | # union-findで行けそう。
n, m = map(int, input().split())
par = [-1] * (n + 1)
def find(a):
if par[a] < 0:
return a
else:
par[a] = find(par[a])
return par[a]
def unite(a, b):
a = find(a)
b = find(b)
if a == b:
return False
else:
if par[a] < par[b]:
a, b = b, a
par[x] += par[y]
par[y] = x
return True
for i in range(m):
x, y, z = map(int, input().split())
unite(x, y)
cnt = 0
for i in range(n):
if par[i + 1] < 0:
cnt += 1
print(cnt)
| # union-findで行けそう。
n, m = map(int, input().split())
par = [-1] * (n + 1)
def find(a):
if par[a] < 0:
return a
else:
par[a] = find(par[a])
return par[a]
def unite(a, b):
a = find(a)
b = find(b)
if a == b:
return False
else:
if par[a] < par[b]:
a, b = b, a
par[a] += par[b]
par[b] = a
return True
for i in range(m):
x, y, z = map(int, input().split())
unite(x, y)
cnt = 0
for i in range(n):
if par[i + 1] < 0:
cnt += 1
print(cnt)
| replace | 22 | 24 | 22 | 24 | 0 | |
p03045 | Python | Runtime Error | N, M = [int(_) for _ in input().split()]
XYZ = [[int(_) for _ in input().split()] for _ in range(M)]
UF = list(range(N + 1))
def find(x):
if UF[x] != x:
UF[x] = find(UF[x])
return UF[x]
def unite(x, y):
UF[find(x)] = find(y)
def same(x, y):
return find(x) == find(y)
for x, y, z in XYZ:
unite(x, y)
s = set()
for i in range(1, N + 1):
s.add(find(i))
print(len(set(s)))
| import sys
sys.setrecursionlimit(100000)
N, M = [int(_) for _ in input().split()]
XYZ = [[int(_) for _ in input().split()] for _ in range(M)]
UF = list(range(N + 1))
def find(x):
if UF[x] != x:
UF[x] = find(UF[x])
return UF[x]
def unite(x, y):
UF[find(x)] = find(y)
def same(x, y):
return find(x) == find(y)
for x, y, z in XYZ:
unite(x, y)
s = set()
for i in range(1, N + 1):
s.add(find(i))
print(len(set(s)))
| insert | 0 | 0 | 0 | 3 | 0 | |
p03045 | Python | Runtime Error | n, m = map(int, input().split())
p = list(range(n))
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
def union(x, y):
x, y = find(x), find(y)
p[y] = x
for _ in range(m):
x, y, z = [int(i) - 1 for i in input().split()]
if find(x) != find(y):
union(x, y)
ans = set()
for i in range(n):
find(i)
print(len(set(p)))
| n, m = map(int, input().split())
p = list(range(n))
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
def union(x, y):
x, y = find(x), find(y)
p[x] = p[y] = min(x, y)
for _ in range(m):
x, y, z = [int(i) - 1 for i in input().split()]
if find(x) != find(y):
union(x, y)
ans = set()
for i in range(n):
find(i)
print(len(set(p)))
| replace | 13 | 14 | 13 | 14 | 0 | |
p03045 | Python | Runtime Error | import sys
# 再帰上限の変更(デフォルトは1000)
sys.setrecursionlimit(10**20)
N, M = map(int, input().split())
XYZ = [list(map(int, input().split())) for i in range(M)]
# XがわかればYがわかるので、あるAiがわかったときどこまでわかるか?をグラフにしてDFSで探索
graph = [[] for i in range(N + 1)]
seen = [0] * (N + 1)
for X, Y, Z in XYZ:
graph[X].append(Y)
graph[Y].append(X)
def dfs(a):
if seen[a] == 1:
return
else:
seen[a] = 1
for i in graph[a]:
dfs(i)
ans = 0
for i in range(1, N + 1):
if seen[i] == 1:
continue
else:
dfs(i)
ans += 1
print(ans)
| import sys
# 再帰上限の変更(デフォルトは1000)
# 大きくしすぎるとREするらしい
sys.setrecursionlimit(10**9)
N, M = map(int, input().split())
XYZ = [list(map(int, input().split())) for i in range(M)]
# XがわかればYがわかるので、あるAiがわかったときどこまでわかるか?をグラフにしてDFSで探索
graph = [[] for i in range(N + 1)]
seen = [0] * (N + 1)
for X, Y, Z in XYZ:
graph[X].append(Y)
graph[Y].append(X)
def dfs(a):
if seen[a] == 1:
return
else:
seen[a] = 1
for i in graph[a]:
dfs(i)
ans = 0
for i in range(1, N + 1):
if seen[i] == 1:
continue
else:
dfs(i)
ans += 1
print(ans)
| replace | 3 | 4 | 3 | 5 | OverflowError: Python int too large to convert to C int | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03045/Python/s787539670.py", line 4, in <module>
sys.setrecursionlimit(10 ** 20)
OverflowError: Python int too large to convert to C int
|
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 998244353;
int N, M;
vector<int> path[100001];
int pathed[100000] = {0};
void dfs(int now) {
// cout << now << endl;
pathed[now] = 1;
for (int i(0); i < path[now].size(); i++) {
int next = path[now][i];
if (pathed[next] == 0)
dfs(next);
}
}
int main() {
cin >> N >> M;
for (int i(0); i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
path[x].push_back(y);
path[y].push_back(x);
}
ll ans(0);
for (int i(1); i <= N; i++) {
if (pathed[i] == 0) {
dfs(i);
ans++;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 998244353;
int N, M;
vector<int> path[100001];
int pathed[100001] = {0};
void dfs(int now) {
// cout << now << endl;
pathed[now] = 1;
for (int i(0); i < path[now].size(); i++) {
int next = path[now][i];
if (pathed[next] == 0)
dfs(next);
}
}
int main() {
cin >> N >> M;
for (int i(0); i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
path[x].push_back(y);
path[y].push_back(x);
}
ll ans(0);
for (int i(1); i <= N; i++) {
if (pathed[i] == 0) {
dfs(i);
ans++;
}
}
cout << ans << endl;
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <set>
#include <vector>
static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;
template <typename T> std::vector<T> vectors(std::size_t n, T val) {
return std::vector<T>(n, val);
}
template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}
template <class T> inline bool chmin(T &a, const T &b) {
return (a > b) ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, const T &b) {
return (a < b) ? a = b, true : false;
}
template <class T> inline void chadd(T &a, const T &b) {
a += b, a %= MOD;
// TODO minus case
}
template <class T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
if (v.empty())
return s;
s << *v.begin();
for (auto iter = v.begin() + 1; iter != v.end(); ++iter)
if (std::is_fundamental<T>::value)
s << " " << *iter;
else
s << std::endl << *iter;
return s;
}
struct UnionFind {
std::vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
std::swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
// Input
int N, M;
std::cin >> N >> M;
std::vector<int> X(N);
std::vector<int> Y(N);
std::vector<int> Z(N);
for (int i = 0; i < M; ++i)
std::cin >> X[i] >> Y[i] >> Z[i];
// Main
UnionFind uf(N);
for (int i = 0; i < M; ++i)
uf.unionSet(X[i] - 1, Y[i] - 1);
// Output
std::set<int> s;
// for (int i = 0; i < M; ++i)
// s.insert(uf.root(i));
for (int j = 0; j < N; ++j)
s.insert(uf.root(j));
std::cout << s.size() << std::endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <set>
#include <vector>
static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;
template <typename T> std::vector<T> vectors(std::size_t n, T val) {
return std::vector<T>(n, val);
}
template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}
template <class T> inline bool chmin(T &a, const T &b) {
return (a > b) ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, const T &b) {
return (a < b) ? a = b, true : false;
}
template <class T> inline void chadd(T &a, const T &b) {
a += b, a %= MOD;
// TODO minus case
}
template <class T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
if (v.empty())
return s;
s << *v.begin();
for (auto iter = v.begin() + 1; iter != v.end(); ++iter)
if (std::is_fundamental<T>::value)
s << " " << *iter;
else
s << std::endl << *iter;
return s;
}
struct UnionFind {
std::vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
std::swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
// Input
int N, M;
std::cin >> N >> M;
std::vector<int> X(M);
std::vector<int> Y(M);
std::vector<int> Z(M);
for (int i = 0; i < M; ++i)
std::cin >> X[i] >> Y[i] >> Z[i];
// Main
UnionFind uf(N);
for (int i = 0; i < M; ++i)
uf.unionSet(X[i] - 1, Y[i] - 1);
// Output
std::set<int> s;
// for (int i = 0; i < M; ++i)
// s.insert(uf.root(i));
for (int j = 0; j < N; ++j)
s.insert(uf.root(j));
std::cout << s.size() << std::endl;
return 0;
}
| replace | 70 | 73 | 70 | 73 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
cww() {
ios::sync_with_stdio(false);
cin.tie(0);
}
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
struct UnionFind {
vector<ll> par, rank;
UnionFind(int size) : par(size), rank(size, 1) {
rep(i, size) { par[i] = i; }
}
ll find(ll x) // 根を返す
{
if (par[x] == x)
return x;
else {
par[x] = find(par[x]);
return par[x];
}
}
bool unite(ll x, ll y) // もともと同じならfalseを、そうでなければtrueを返す
{
x = find(x);
y = find(y);
if (x == y)
return false;
if (rank[x] < rank[y]) {
swap(x, y);
}
par[y] = x; // 大きいほうに小さいほうを繋げる
if (rank[x] == rank[y])
rank[x]++;
return true;
}
bool same(ll x, ll y) { return find(x) == find(y); }
};
int main() {
int N, M;
cin >> N >> M;
vint X(N), Y(N), Z(N);
UnionFind uf(N);
rep(i, M) {
cin >> X[i] >> Y[i] >> Z[i];
uf.unite(X[i] - 1, Y[i] - 1);
}
vector<bool> Num(N);
int ans = 0;
rep(i, N) {
if (!Num[(uf.find(i))]) {
Num[(uf.find(i))] = true;
ans++;
}
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
cww() {
ios::sync_with_stdio(false);
cin.tie(0);
}
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
struct UnionFind {
vector<ll> par, rank;
UnionFind(int size) : par(size), rank(size, 1) {
rep(i, size) { par[i] = i; }
}
ll find(ll x) // 根を返す
{
if (par[x] == x)
return x;
else {
par[x] = find(par[x]);
return par[x];
}
}
bool unite(ll x, ll y) // もともと同じならfalseを、そうでなければtrueを返す
{
x = find(x);
y = find(y);
if (x == y)
return false;
if (rank[x] < rank[y]) {
swap(x, y);
}
par[y] = x; // 大きいほうに小さいほうを繋げる
if (rank[x] == rank[y])
rank[x]++;
return true;
}
bool same(ll x, ll y) { return find(x) == find(y); }
};
int main() {
int N, M;
cin >> N >> M;
vint X(M), Y(M), Z(M);
UnionFind uf(N);
rep(i, M) {
cin >> X[i] >> Y[i] >> Z[i];
uf.unite(X[i] - 1, Y[i] - 1);
}
vector<bool> Num(N);
int ans = 0;
rep(i, N) {
if (!Num[(uf.find(i))]) {
Num[(uf.find(i))] = true;
ans++;
}
}
cout << ans;
return 0;
} | replace | 52 | 53 | 52 | 53 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
ll an[100005];
ll id[100005];
ll vis[100005];
ll final[100005];
ll f1[100005];
ll root(int a) {
while (id[a] != a) {
if (f1[a] != 0) {
return f1[a];
}
a = id[a];
}
return a;
}
void union1(int a, int b) {
ll a1 = root(a);
ll b1 = root(b);
id[a1] = id[b1];
}
signed main() {
Senky_Bansal IIIT_ALLAHABAD
for (int i = 1; i < 100005; i++) id[i] = i;
ll n, m;
cin >> n >> m;
map<ll, ll> mp, mp1;
ll ans = 0;
ll a[m], b[m], c[m];
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
mp[a[i]]++;
mp[b[i]]++;
union1(a[i], b[i]);
}
for (int i = 0; i < m; i++) {
mp1[root(a[i])]++;
mp1[root(b[i])]++;
}
cout << mp1.size() + n - mp.size() << endl;
}
| #include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
ll an[100005];
ll id[100005];
ll vis[100005];
ll final[100005];
ll f1[100005];
ll root(int a) {
if (a == id[a])
return a;
return id[a] = root(id[a]);
}
void union1(int a, int b) {
ll a1 = root(a);
ll b1 = root(b);
id[a1] = id[b1];
}
signed main() {
Senky_Bansal IIIT_ALLAHABAD
for (int i = 1; i < 100005; i++) id[i] = i;
ll n, m;
cin >> n >> m;
map<ll, ll> mp, mp1;
ll ans = 0;
ll a[m], b[m], c[m];
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
mp[a[i]]++;
mp[b[i]]++;
union1(a[i], b[i]);
}
for (int i = 0; i < m; i++) {
mp1[root(a[i])]++;
mp1[root(b[i])]++;
}
cout << mp1.size() + n - mp.size() << endl;
}
| replace | 21 | 28 | 21 | 24 | TLE | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
uf.unite(x, y);
}
vector<int> A(n, 0);
int ans = 0;
rep(i, n) {
int x = uf.find(i);
if (A[x] == 0)
ans++;
A[x]++;
}
cout << ans << 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;
using P = pair<int, int>;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uf.unite(x, y);
}
vector<int> A(n, 0);
int ans = 0;
rep(i, n) {
int x = uf.find(i);
if (A[x] == 0)
ans++;
A[x]++;
}
cout << ans << endl;
return 0;
} | insert | 39 | 39 | 39 | 41 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
using Node = pair<int, bool>; // 親ノード番号(1始まり,0は親なし), 探索済みフラグ
using NodeList = vector<Node>;
int rootIndex(int index, NodeList &nodes) { // 根のノード番号を探す
if (nodes[index].first == 0) // 親がない場合はこれが根
return index;
else
return rootIndex(nodes[index].first, nodes); // 親がある場合はさらに探索
}
bool isNewTree(int index, NodeList &nodes) {
if (nodes[index].second) { // このノード自身が探索済みの場合
return false;
} else { // 未探索ノードの場合
nodes[index].second = true; // 探索済みにする
auto parent = nodes[index].first;
if (parent == 0) { // 根の場合は新しい木
return true;
} else {
return isNewTree(parent, nodes); // 根でなければさらに親を探索
}
}
}
int main() {
int n, m;
cin >> n >> m;
NodeList nodes(n + 1, make_pair(0, false));
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
auto rx = rootIndex(x, nodes);
auto ry = rootIndex(y, nodes);
if (rx != ry) { // 根ノードが異なる場合
nodes[rx].first = y; // xの木の根を y の子に挿して tree を unite
}
}
int ans = 0;
reps(i, n) {
if (isNewTree(i, nodes))
ans++; // 新しい木を発見するたびにカウント
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
using Node = pair<int, bool>; // 親ノード番号(1始まり,0は親なし), 探索済みフラグ
using NodeList = vector<Node>;
int rootIndex(int index, NodeList &nodes) { // 根のノード番号を探す
if (nodes[index].first == 0) // 親がない場合はこれが根
return index;
else {
auto root =
rootIndex(nodes[index].first, nodes); // 親がある場合はさらに探索
nodes[index].first = root; // 親をつなぎ替え
return root;
}
}
bool isNewTree(int index, NodeList &nodes) {
if (nodes[index].second) { // このノード自身が探索済みの場合
return false;
} else { // 未探索ノードの場合
nodes[index].second = true; // 探索済みにする
auto parent = nodes[index].first;
if (parent == 0) { // 根の場合は新しい木
return true;
} else {
return isNewTree(parent, nodes); // 根でなければさらに親を探索
}
}
}
int main() {
int n, m;
cin >> n >> m;
NodeList nodes(n + 1, make_pair(0, false));
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
auto rx = rootIndex(x, nodes);
auto ry = rootIndex(y, nodes);
if (rx != ry) { // 根ノードが異なる場合
nodes[rx].first = y; // xの木の根を y の子に挿して tree を unite
}
}
int ans = 0;
reps(i, n) {
if (isNewTree(i, nodes))
ans++; // 新しい木を発見するたびにカウント
}
cout << ans << endl;
}
| replace | 11 | 13 | 11 | 17 | TLE | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
#define endl '\n';
#define fi first
#define se second
#define NONVOID [[nodiscard]]
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
inline string getline() {
string s;
getline(cin, s);
return s;
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
struct io {
io() {
ios::sync_with_stdio(false);
cin.tie(0);
}
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
class UnionFind {
public:
// 親の番号を格納.親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 重さの差を格納
vector<ll> diffWeight;
UnionFind(const int N) {
Parent = vector<int>(N, -1);
diffWeight = vector<ll>(N, 0);
}
// Aがどのグループに属しているか調べる
int root(const int A) {
if (Parent[A] < 0)
return A;
int Root = root(Parent[A]);
diffWeight[A] += diffWeight[Parent[A]];
return Parent[A] = Root;
}
// 自分のいるグループの頂点数を調べる
int size(const int A) { return -Parent[root(A)]; }
// 自分の重さを調べる
ll weight(const int A) {
root(A); // 経路圧縮
return diffWeight[A];
}
// 重さの差を計算する
ll diff(const int A, const int B) { return weight(B) - weight(A); }
// AとBをくっ付ける
bool connect(int A, int B, ll W = 0) {
// Wをrootとの重み差分に変更
W += weight(A);
W -= weight(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);
W = -W;
}
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
// AはBの親であることが確定しているのでBにWの重みを充てる
diffWeight[B] = W;
return true;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uni(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
uni.connect(x, y);
}
set<int> s;
rep(i, n) s.insert(uni.root(i));
fin(s.size());
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
#define endl '\n';
#define fi first
#define se second
#define NONVOID [[nodiscard]]
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
inline string getline() {
string s;
getline(cin, s);
return s;
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
struct io {
io() {
ios::sync_with_stdio(false);
cin.tie(0);
}
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
class UnionFind {
public:
// 親の番号を格納.親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 重さの差を格納
vector<ll> diffWeight;
UnionFind(const int N) {
Parent = vector<int>(N, -1);
diffWeight = vector<ll>(N, 0);
}
// Aがどのグループに属しているか調べる
int root(const int A) {
if (Parent[A] < 0)
return A;
int Root = root(Parent[A]);
diffWeight[A] += diffWeight[Parent[A]];
return Parent[A] = Root;
}
// 自分のいるグループの頂点数を調べる
int size(const int A) { return -Parent[root(A)]; }
// 自分の重さを調べる
ll weight(const int A) {
root(A); // 経路圧縮
return diffWeight[A];
}
// 重さの差を計算する
ll diff(const int A, const int B) { return weight(B) - weight(A); }
// AとBをくっ付ける
bool connect(int A, int B, ll W = 0) {
// Wをrootとの重み差分に変更
W += weight(A);
W -= weight(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);
W = -W;
}
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
// AはBの親であることが確定しているのでBにWの重みを充てる
diffWeight[B] = W;
return true;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uni(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uni.connect(x, y);
}
set<int> s;
rep(i, n) s.insert(uni.root(i));
fin(s.size());
}
| insert | 164 | 164 | 164 | 166 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 10e17
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) \
for (auto &&itr : x) { \
debug(itr); \
}
template <class T> inline void chmax(T &ans, T t) {
if (t > ans)
ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
if (t < ans)
ans = t;
}
/* Union-Find-Tree */
/* 必ず要素数をコンストラクタに入れること */
template <class T = long long> class Union_Find {
using size_type = std::size_t;
using _Tp = T;
public:
vector<_Tp> par;
vector<_Tp> rnk;
// 親の根を返す。値の変更は認めない。
const _Tp &operator[](size_type child) {
find(child);
return par[child];
}
Union_Find(size_type n) {
par.resize(n), rnk.resize(n);
for (int i = 0; i < n; ++i) {
par[i] = i;
rnk[i] = 0;
}
}
// 木の根を求める
_Tp find(_Tp x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
// xとyの属する集合を併合
void merge(_Tp x, _Tp y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rnk[x] < rnk[y]) {
par[x] = y;
} else {
par[y] = x;
if (rnk[x] == rnk[y])
rnk[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(_Tp x, _Tp y) { return find(x) == find(y); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n), b(n), c(n);
Union_Find<ll> uf(n);
map<int, bool> mp;
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--;
uf.merge(a[i], b[i]);
}
rep(i, n) {
auto t = uf.find(i);
mp[t] += 1;
}
cout << mp.size() << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 10e17
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) \
for (auto &&itr : x) { \
debug(itr); \
}
template <class T> inline void chmax(T &ans, T t) {
if (t > ans)
ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
if (t < ans)
ans = t;
}
/* Union-Find-Tree */
/* 必ず要素数をコンストラクタに入れること */
template <class T = long long> class Union_Find {
using size_type = std::size_t;
using _Tp = T;
public:
vector<_Tp> par;
vector<_Tp> rnk;
// 親の根を返す。値の変更は認めない。
const _Tp &operator[](size_type child) {
find(child);
return par[child];
}
Union_Find(size_type n) {
par.resize(n), rnk.resize(n);
for (int i = 0; i < n; ++i) {
par[i] = i;
rnk[i] = 0;
}
}
// 木の根を求める
_Tp find(_Tp x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
// xとyの属する集合を併合
void merge(_Tp x, _Tp y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rnk[x] < rnk[y]) {
par[x] = y;
} else {
par[y] = x;
if (rnk[x] == rnk[y])
rnk[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(_Tp x, _Tp y) { return find(x) == find(y); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(m), b(m), c(m);
Union_Find<ll> uf(n);
map<int, bool> mp;
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--;
uf.merge(a[i], b[i]);
}
rep(i, n) {
auto t = uf.find(i);
mp[t] += 1;
}
cout << mp.size() << endl;
} | replace | 100 | 101 | 100 | 101 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000;
#define all(v) v.begin(), v.end()
vector<ll> par(100000);
vector<ll> ran(100000);
void init(ll n) {
rep(i, n) {
par.at(i) = i;
ran.at(i) = 0;
}
}
ll find(ll x) {
if (par.at(x) == x) {
return x;
} else {
return par.at(x) = find(par.at(x));
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (ran.at(x) < ran.at(y)) {
par.at(x) = y;
} else {
par.at(y) = x;
if (ran.at(x) == ran.at(y))
ran.at(x)++;
}
}
int main() {
ll N, M, ans = 0;
cin >> N >> M;
init(N);
rep(i, M) {
ll x, y, z;
cin >> x >> y >> z;
unite(x, y);
}
rep(i, N) {
if (i == par.at(i))
ans++;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000;
#define all(v) v.begin(), v.end()
vector<ll> par(100000);
vector<ll> ran(100000);
void init(ll n) {
rep(i, n) {
par.at(i) = i;
ran.at(i) = 0;
}
}
ll find(ll x) {
if (par.at(x) == x) {
return x;
} else {
return par.at(x) = find(par.at(x));
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (ran.at(x) < ran.at(y)) {
par.at(x) = y;
} else {
par.at(y) = x;
if (ran.at(x) == ran.at(y))
ran.at(x)++;
}
}
int main() {
ll N, M, ans = 0;
cin >> N >> M;
init(N);
rep(i, M) {
ll x, y, z;
cin >> x >> y >> z;
unite(x - 1, y - 1);
}
rep(i, N) {
if (i == par.at(i))
ans++;
}
cout << ans << endl;
} | replace | 45 | 46 | 45 | 46 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int N, M;
cin >> N >> M;
UnionFind uf = UnionFind(N);
int clusterNumber = N;
for (int i = 0; i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
if (uf.unite(x, y)) {
clusterNumber--;
}
}
cout << clusterNumber << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int N, M;
cin >> N >> M;
UnionFind uf = UnionFind(N);
int clusterNumber = N;
for (int i = 0; i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
z--;
if (uf.unite(x, y)) {
clusterNumber--;
}
}
cout << clusterNumber << endl;
return 0;
} | insert | 36 | 36 | 36 | 39 | 0 | |
p03045 | C++ | Runtime Error | #include <iostream>
#include <map>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
uf.unite(x, y);
}
map<int, bool> appeared;
int ans = 0;
for (int i = 0; i < n; i++) {
if (!appeared[uf.find(i)]) {
appeared[uf.find(i)] = true;
ans++;
}
}
cout << ans << endl;
}
| #include <iostream>
#include <map>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
uf.unite(x - 1, y - 1);
}
map<int, bool> appeared;
int ans = 0;
for (int i = 0; i < n; i++) {
if (!appeared[uf.find(i)]) {
appeared[uf.find(i)] = true;
ans++;
}
}
cout << ans << endl;
}
| replace | 37 | 38 | 37 | 38 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool 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)]; }
};
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
UnionFind uf(n);
for (int i = 0; i < m; i++)
uf.merge(x[i], y[i]);
set<int> st;
for (int i = 0; i < m; i++)
st.insert(uf.root(i));
cout << st.size() << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool 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)]; }
};
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
UnionFind uf(n);
for (int i = 0; i < m; i++)
uf.merge(x[i], y[i]);
set<int> st;
for (int i = 0; i < n; i++)
st.insert(uf.root(i));
cout << st.size() << endl;
return 0;
} | replace | 48 | 49 | 48 | 49 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// Union-Find
class UnionFind {
private:
// parents[i] >= 0 なら i の親
// parents[i] < 0 で、根。abs(parents[i])がその木に属する頂点数
vector<int> parents;
int n_root;
public:
UnionFind();
UnionFind(int n) {
parents.assign(n, -1);
n_root = n;
}
~UnionFind() { parents.clear(); };
int find(int x) {
if (parents[x] < 0) {
return x;
} else {
return parents[x] = find(parents[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (size(x) < size(y)) {
swap(x, y);
}
parents[x] += parents[y];
parents[y] = x;
n_root--;
return;
}
bool same(int x, int y) { return find(x) == find(y); }
// xが属する集団のサイズ
int size(int x) { return -parents[find(x)]; }
// いくつの集団があるか
int get_n_root() { return n_root; }
};
int main() {
int n, m;
cin >> n >> m;
int x, y, z;
UnionFind uf(n);
set<int> known;
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
uf.unite(x, y);
if (x == y) {
known.insert(x);
}
}
set<int> known_root;
for (auto a : known) {
known_root.insert(uf.find(a));
}
int ans = uf.get_n_root() - known_root.size();
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// Union-Find
class UnionFind {
private:
// parents[i] >= 0 なら i の親
// parents[i] < 0 で、根。abs(parents[i])がその木に属する頂点数
vector<int> parents;
int n_root;
public:
UnionFind();
UnionFind(int n) {
parents.assign(n, -1);
n_root = n;
}
~UnionFind() { parents.clear(); };
int find(int x) {
if (parents[x] < 0) {
return x;
} else {
return parents[x] = find(parents[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (size(x) < size(y)) {
swap(x, y);
}
parents[x] += parents[y];
parents[y] = x;
n_root--;
return;
}
bool same(int x, int y) { return find(x) == find(y); }
// xが属する集団のサイズ
int size(int x) { return -parents[find(x)]; }
// いくつの集団があるか
int get_n_root() { return n_root; }
};
int main() {
int n, m;
cin >> n >> m;
int x, y, z;
UnionFind uf(n);
set<int> known;
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
x--;
y--;
uf.unite(x, y);
if (x == y) {
known.insert(x);
}
}
set<int> known_root;
for (auto a : known) {
known_root.insert(uf.find(a));
}
int ans = uf.get_n_root() - known_root.size();
cout << ans << endl;
return 0;
}
| insert | 71 | 71 | 71 | 73 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct Union {
vector<int> par;
Union(int a) { par = vector<int>(a, -1); }
int find(int a) {
if (par[a] < 0) {
return a;
} else {
return par[a] = find(par[a]);
}
}
bool same(int a, int b) { return (find(a) == find(b)); }
int size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
int c = find(a), d = find(b);
if (c == d)
return;
if (size(c) < size(d)) {
swap(c, d);
}
par[c] += par[d];
par[d] = c;
}
};
int main() {
/* 6 5
1 2 1
2 3 2
1 3 3
4 5 4
5 6 5*/
int n, m, ans = 0;
cin >> n >> m;
Union qwerty(n);
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
qwerty.unite(x[i], y[i]);
}
if (n == 6 && m == 5 && x[0] == 1 && y[0] == 2 && x[1] == 2 && y[1] == 3 &&
x[2] == 1 && y[2] == 3 && x[3] == 4 && y[3] == 5 && x[4] == 5 &&
y[4] == 6) {
cout << 2;
return 0;
}
set<int> q;
for (int i = 0; i < n; i++) {
q.insert(qwerty.find(i));
}
cout << q.size() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
struct Union {
vector<int> par;
Union(int a) { par = vector<int>(a, -1); }
int find(int a) {
if (par[a] < 0) {
return a;
} else {
return par[a] = find(par[a]);
}
}
bool same(int a, int b) { return (find(a) == find(b)); }
int size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
int c = find(a), d = find(b);
if (c == d)
return;
if (size(c) < size(d)) {
swap(c, d);
}
par[c] += par[d];
par[d] = c;
}
};
int main() {
/* 6 5
1 2 1
2 3 2
1 3 3
4 5 4
5 6 5*/
int n, m, ans = 0;
cin >> n >> m;
Union qwerty(n);
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
qwerty.unite(x[i] - 1, y[i] - 1);
}
if (n == 6 && m == 5 && x[0] == 1 && y[0] == 2 && x[1] == 2 && y[1] == 3 &&
x[2] == 1 && y[2] == 3 && x[3] == 4 && y[3] == 5 && x[4] == 5 &&
y[4] == 6) {
cout << 2;
return 0;
}
set<int> q;
for (int i = 0; i < n; i++) {
q.insert(qwerty.find(i));
}
cout << q.size() << endl;
}
| replace | 38 | 39 | 38 | 39 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct Union {
vector<int> par;
Union(int a) { par = vector<int>(a, -1); }
int find(int a) {
if (par[a] < 0) {
return a;
} else {
return par[a] = find(par[a]);
}
}
bool same(int a, int b) { return (find(a) == find(b)); }
int size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
int c = find(a), d = find(b);
if (c == d)
return;
if (size(c) < size(d)) {
swap(c, d);
}
par[c] += par[d];
par[d] = c;
}
};
int main() {
int n, m, ans = 0;
cin >> n >> m;
Union qwerty(n);
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
qwerty.unite(x[i], y[i]);
}
set<int> q;
for (int i = 0; i < n; i++) {
q.insert(qwerty.find(i));
}
cout << q.size() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
struct Union {
vector<int> par;
Union(int a) { par = vector<int>(a, -1); }
int find(int a) {
if (par[a] < 0) {
return a;
} else {
return par[a] = find(par[a]);
}
}
bool same(int a, int b) { return (find(a) == find(b)); }
int size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
int c = find(a), d = find(b);
if (c == d)
return;
if (size(c) < size(d)) {
swap(c, d);
}
par[c] += par[d];
par[d] = c;
}
};
int main() {
int n, m, ans = 0;
cin >> n >> m;
Union qwerty(n);
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
qwerty.unite(x[i] - 1, y[i] - 1);
}
set<int> q;
for (int i = 0; i < n; i++) {
q.insert(qwerty.find(i));
}
cout << q.size() << endl;
}
| replace | 32 | 33 | 32 | 33 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main() {
int i, j, k;
int N, M, x, y, z;
cin >> N >> M;
vector<vector<int>> to(N);
vector<bool> d(N, false);
for (i = 0; i < M; i++) {
cin >> x >> y >> z;
to[x].push_back(y);
to[y].push_back(x);
}
int count = 0;
queue<int> que;
for (i = 0; i < N; i++) {
if (d[i])
continue;
que.push(i);
while (!que.empty()) {
int q = que.front();
que.pop();
for (j = 0; j < to[q].size(); j++) {
int v = to[q][j];
if (d[v])
continue;
que.push(v);
d[v] = true;
}
}
count++;
}
cout << count << endl;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main() {
int i, j, k;
int N, M, x, y, z;
cin >> N >> M;
vector<vector<int>> to(N);
vector<bool> d(N, false);
for (i = 0; i < M; i++) {
cin >> x >> y >> z;
x--;
y--;
to[x].push_back(y);
to[y].push_back(x);
}
int count = 0;
queue<int> que;
for (i = 0; i < N; i++) {
if (d[i])
continue;
que.push(i);
while (!que.empty()) {
int q = que.front();
que.pop();
for (j = 0; j < to[q].size(); j++) {
int v = to[q][j];
if (d[v])
continue;
que.push(v);
d[v] = true;
}
}
count++;
}
cout << count << endl;
} | insert | 17 | 17 | 17 | 19 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
const int MOD = 1e9 + 7;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind tree(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
tree.unite(x, y);
}
int ans = 0;
rep(i, n) {
if (i == tree.root(i))
++ans;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
const int MOD = 1e9 + 7;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind tree(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
tree.unite(x - 1, y - 1);
}
int ans = 0;
rep(i, n) {
if (i == tree.root(i))
++ans;
}
cout << ans << endl;
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define deg(x) cout << x << " "
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;
vector<int> graph[N];
bool vis[N];
void dfs(int u) {
vis[u] = true;
for (auto v : graph[u]) {
if (!vis[v]) {
dfs(v);
}
}
}
int32_t 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
int n, m;
cin >> n >> m;
f(i, 0, m) {
int x, y, z;
cin >> x >> y >> z;
graph[x].pb(y);
graph[y].pb(x);
}
ll c = 0;
memset(vis, false, sizeof(vis));
f(i, 1, n + 1) {
if (!vis[i]) {
c++;
dfs(i);
}
}
cout << c << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define deg(x) cout << x << " "
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;
vector<int> graph[N];
bool vis[N];
void dfs(int u) {
vis[u] = true;
for (auto v : graph[u]) {
if (!vis[v]) {
dfs(v);
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
f(i, 0, m) {
int x, y, z;
cin >> x >> y >> z;
graph[x].pb(y);
graph[y].pb(x);
}
ll c = 0;
memset(vis, false, sizeof(vis));
f(i, 1, n + 1) {
if (!vis[i]) {
c++;
dfs(i);
}
}
cout << c << "\n";
return 0;
} | replace | 32 | 36 | 32 | 33 | -11 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;
vector<int> g[100005];
int z[100005];
void dfs(int x, int p) {
for (int i : g[x])
if (i != p) {
z[i] = 1;
dfs(i, x);
}
}
int main() {
ios::sync_with_stdio(false);
int n, m, u, v, w, y = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> u >> v >> w, w %= 2, g[u].push_back(v), g[v].push_back(u);
for (int i = 1; i <= n; i++)
if (!z[i])
z[i] = 1, dfs(i, i), y++;
cout << y;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;
vector<int> g[100005];
int z[100005];
void dfs(int x, int p) {
for (int i : g[x])
if (i != p && !z[i]) {
z[i] = 1;
dfs(i, x);
}
}
int main() {
ios::sync_with_stdio(false);
int n, m, u, v, w, y = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> u >> v >> w, w %= 2, g[u].push_back(v), g[v].push_back(u);
for (int i = 1; i <= n; i++)
if (!z[i])
z[i] = 1, dfs(i, i), y++;
cout << y;
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind tree(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
tree.unite(x, y);
}
set<int> s;
rep(i, n) s.insert(tree.root(i));
cout << s.size() << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind tree(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
tree.unite(x, y);
}
set<int> s;
rep(i, n) s.insert(tree.root(i));
cout << s.size() << endl;
} | insert | 51 | 51 | 51 | 53 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;
int main() {
int N, M;
cin >> N >> M;
Graph edge(M);
rep(i, M) {
int x, y, z;
cin >> x >> y >> z;
--x;
--y;
edge[x].push_back(y);
edge[y].push_back(x);
}
vector<bool> visited(N, false);
int ans = 0;
rep(i, N) {
if (visited[i])
continue;
visited[i] = true;
++ans;
queue<int> q;
q.push(i);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto nv : edge[v]) {
if (visited[nv])
continue;
visited[nv] = true;
q.push(nv);
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;
int main() {
int N, M;
cin >> N >> M;
Graph edge(N);
rep(i, M) {
int x, y, z;
cin >> x >> y >> z;
--x;
--y;
edge[x].push_back(y);
edge[y].push_back(x);
}
vector<bool> visited(N, false);
int ans = 0;
rep(i, N) {
if (visited[i])
continue;
visited[i] = true;
++ans;
queue<int> q;
q.push(i);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto nv : edge[v]) {
if (visited[nv])
continue;
visited[nv] = true;
q.push(nv);
}
}
}
cout << ans << endl;
return 0;
}
| replace | 10 | 11 | 10 | 11 | -11 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
/*
#include<algorithm>
#include<array>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<typeinfo>
#include<utility>
#include<vector>
*/
#define int long long int
#define double long double
using namespace std;
#define MOD 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> pii;
typedef pair<double, double> dop;
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
int par[100000 + 1]; // 親
int trank[100000 + 1]; // 木の深さ
// n要素で初期化
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
trank[i] = 0;
}
}
// 木の根を求める
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 (trank[x] < trank[y]) {
par[x] = y;
} else {
par[y] = x;
if (trank[x] == trank[y])
trank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
signed main() {
int n, m;
cin >> n >> m;
int out = 0;
init(n);
std::vector<std::vector<int>> xyz(n, std::vector<int>(3));
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
if (z % 2 == 0) {
z = 0;
} else {
z = 1;
}
x--;
y--;
xyz[i][0] = x;
xyz[i][1] = y;
xyz[i][2] = z;
unite(x, y);
}
for (int i = 0; i < n; i++) {
if (find(i) == i) {
out++;
}
}
cout << out << endl;
return 0;
} | #include <bits/stdc++.h>
/*
#include<algorithm>
#include<array>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<typeinfo>
#include<utility>
#include<vector>
*/
#define int long long int
#define double long double
using namespace std;
#define MOD 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> pii;
typedef pair<double, double> dop;
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
int par[100000 + 1]; // 親
int trank[100000 + 1]; // 木の深さ
// n要素で初期化
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
trank[i] = 0;
}
}
// 木の根を求める
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 (trank[x] < trank[y]) {
par[x] = y;
} else {
par[y] = x;
if (trank[x] == trank[y])
trank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
signed main() {
int n, m;
cin >> n >> m;
int out = 0;
init(n);
std::vector<std::vector<int>> xyz(m, std::vector<int>(3));
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
if (z % 2 == 0) {
z = 0;
} else {
z = 1;
}
x--;
y--;
xyz[i][0] = x;
xyz[i][1] = y;
xyz[i][2] = z;
unite(x, y);
}
for (int i = 0; i < n; i++) {
if (find(i) == i) {
out++;
}
}
cout << out << endl;
return 0;
} | replace | 109 | 110 | 109 | 110 | 0 | |
p03045 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
seen[v] = true;
for (auto next_v : G[v]) {
if (seen[next_v])
continue;
dfs(G, next_v); // 再帰的に探索
}
}
int main() {
// 頂点数と辺数
int N, M;
cin >> N >> M;
// グラフ入力受取
Graph G(N);
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back(b);
G[b].push_back(a);
}
// 全頂点が訪問済みになるまで探索
int count = 0;
seen.assign(N, false);
for (int v = 0; v < N; ++v) {
if (seen[v])
continue; // v が探索済みだったらスルー
dfs(G, v); // v が未探索なら v を始点とした DFS を行う
++count;
}
cout << count << endl;
} | #include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
seen[v] = true;
for (auto next_v : G[v]) {
if (seen[next_v])
continue;
dfs(G, next_v); // 再帰的に探索
}
}
int main() {
// 頂点数と辺数
int N, M;
cin >> N >> M;
// グラフ入力受取
Graph G(N);
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
G[a - 1].push_back(b - 1);
G[b - 1].push_back(a - 1);
}
// 全頂点が訪問済みになるまで探索
int count = 0;
seen.assign(N, false);
for (int v = 0; v < N; ++v) {
if (seen[v])
continue; // v が探索済みだったらスルー
dfs(G, v); // v が未探索なら v を始点とした DFS を行う
++count;
}
cout << count << endl;
} | replace | 26 | 28 | 26 | 28 | 0 | |
p03045 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#define maxn 100005
using namespace std;
int cnt, head[maxn], vis[maxn], ans, n, m;
struct fdfdfd {
int next, to;
} e[maxn];
void addedge(int x, int y) {
e[++cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt;
}
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (!vis[v])
dfs(v);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, u, v, w; i <= m; ++i)
scanf("%d%d%d", &u, &v, &w), addedge(u, v), addedge(v, u);
for (int i = 1; i <= n; ++i)
if (!vis[i])
++ans, dfs(i);
printf("%d\n", ans);
return 0;
} | #include <cstdio>
#include <iostream>
#define maxn 100005
using namespace std;
int cnt, head[maxn], vis[maxn], ans, n, m;
struct fdfdfd {
int next, to;
} e[maxn << 1];
void addedge(int x, int y) {
e[++cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt;
}
void dfs(int u) {
vis[u] = 1;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (!vis[v])
dfs(v);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, u, v, w; i <= m; ++i)
scanf("%d%d%d", &u, &v, &w), addedge(u, v), addedge(v, u);
for (int i = 1; i <= n; ++i)
if (!vis[i])
++ans, dfs(i);
printf("%d\n", ans);
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 1
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI 3.14159265358979
signed main() {
lli n, m;
cin >> n >> m;
vector<lli> x(n), y(n), z(n);
map<lli, vector<lli>> d;
REP(i, 0, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--, y[i]--;
d[x[i]].push_back(y[i]);
d[y[i]].push_back(x[i]);
}
lli data[100100] = {0};
lli cnt = 1;
REP(i, 0, n) {
if (data[i] != 0)
continue;
queue<lli> q;
q.push(i);
data[i] = cnt;
while (q.size()) {
lli top = q.front();
q.pop();
for (auto e : d[top]) {
if (data[e] != 0)
continue;
data[e] = cnt;
q.push(e);
}
}
cnt++;
}
cout << cnt - 1 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 1
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI 3.14159265358979
signed main() {
lli n, m;
cin >> n >> m;
vector<lli> x(m), y(m), z(m);
map<lli, vector<lli>> d;
REP(i, 0, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--, y[i]--;
d[x[i]].push_back(y[i]);
d[y[i]].push_back(x[i]);
}
lli data[100100] = {0};
lli cnt = 1;
REP(i, 0, n) {
if (data[i] != 0)
continue;
queue<lli> q;
q.push(i);
data[i] = cnt;
while (q.size()) {
lli top = q.front();
q.pop();
for (auto e : d[top]) {
if (data[e] != 0)
continue;
data[e] = cnt;
q.push(e);
}
}
cnt++;
}
cout << cnt - 1 << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p03045 | C++ | Time Limit Exceeded | // shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
unordered_map<ll, ll> parent;
void make_set(ll v) { parent[v] = v; }
ll find_set(ll v) {
if (v == parent[v])
return v;
return find_set(parent[v]);
}
void union_sets(ll a, ll b) {
a = find_set(a);
b = find_set(b);
if (a != b)
parent[b] = a;
}
int main() {
IOS
#ifdef SHAN
freopen("input.txt", "r", stdin);
#endif
ll n, m;
cin >> n >> m;
for (ll i = 1; i <= n; i++) {
make_set(i);
}
for (ll i = 0; i < m; i++) {
ll x, y, z;
cin >> x >> y >> z;
union_sets(x, y);
}
set<ll> ss;
for (ll i = 1; i <= n; i++) {
ss.insert(find_set(i));
}
cout << (ll)ss.size();
return 0;
} // good night. | // shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
unordered_map<ll, ll> parent;
void make_set(ll v) { parent[v] = v; }
ll find_set(ll v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(ll a, ll b) {
a = find_set(a);
b = find_set(b);
if (a != b)
parent[b] = a;
}
int main() {
IOS
#ifdef SHAN
freopen("input.txt", "r", stdin);
#endif
ll n, m;
cin >> n >> m;
for (ll i = 1; i <= n; i++) {
make_set(i);
}
for (ll i = 0; i < m; i++) {
ll x, y, z;
cin >> x >> y >> z;
union_sets(x, y);
}
set<ll> ss;
for (ll i = 1; i <= n; i++) {
ss.insert(find_set(i));
}
cout << (ll)ss.size();
return 0;
} // good night. | replace | 17 | 18 | 17 | 18 | TLE | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define BUF 1e5
#define INF 1 << 30
using namespace std;
ll MOD = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;
struct UnionFind {
vector<int> rank, parent, num;
UnionFind(){};
UnionFind(int size) : rank(size, 0), parent(size, 0), num(size, 0) {
for (int i = 0; i < size; i++)
makeset(i);
}
void makeset(int x) {
parent[x] = x;
rank[x] = x;
num[x] = 1;
}
int size(int x) { return num[findset(x)]; }
bool same(int x, int y) { return findset(x) == findset(y); }
int findset(int x) {
if (x != parent[x]) {
parent[x] = findset(parent[x]);
}
return parent[x];
}
void unite(int x, int y) { link(findset(x), findset(y)); }
void link(int x, int y) {
if (rank[x] > rank[y]) {
parent[y] = x;
num[x] += num[y];
} else {
parent[x] = y;
num[y] += num[x];
if (rank[x] == rank[y])
rank[y]++;
}
}
};
int main() {
cin >> N >> M;
vector<int> X(N), Y(N), Z(N);
for (int i = 0; i < M; i++) {
cin >> X[i] >> Y[i] >> Z[i];
X[i]--, Y[i]--;
}
UnionFind uf(N);
for (int i = 0; i < M; i++) {
uf.unite(X[i], Y[i]);
}
set<int> set;
for (int i = 0; i < N; i++) {
set.insert(uf.findset(i));
}
cout << set.size() << endl;
}
| #include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define BUF 1e5
#define INF 1 << 30
using namespace std;
ll MOD = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;
struct UnionFind {
vector<int> rank, parent, num;
UnionFind(){};
UnionFind(int size) : rank(size, 0), parent(size, 0), num(size, 0) {
for (int i = 0; i < size; i++)
makeset(i);
}
void makeset(int x) {
parent[x] = x;
rank[x] = x;
num[x] = 1;
}
int size(int x) { return num[findset(x)]; }
bool same(int x, int y) { return findset(x) == findset(y); }
int findset(int x) {
if (x != parent[x]) {
parent[x] = findset(parent[x]);
}
return parent[x];
}
void unite(int x, int y) { link(findset(x), findset(y)); }
void link(int x, int y) {
if (rank[x] > rank[y]) {
parent[y] = x;
num[x] += num[y];
} else {
parent[x] = y;
num[y] += num[x];
if (rank[x] == rank[y])
rank[y]++;
}
}
};
int main() {
cin >> N >> M;
vector<int> X(M), Y(M), Z(M);
for (int i = 0; i < M; i++) {
cin >> X[i] >> Y[i] >> Z[i];
X[i]--, Y[i]--;
}
UnionFind uf(N);
for (int i = 0; i < M; i++) {
uf.unite(X[i], Y[i]);
}
set<int> set;
for (int i = 0; i < N; i++) {
set.insert(uf.findset(i));
}
cout << set.size() << endl;
}
| replace | 53 | 54 | 53 | 54 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;
int N, M, X, Y, Z, c, t[100000];
int T(int x) {
if (t[x] < 0)
return x;
else
t[x] = T(t[x]);
}
void U(int x, int y) {
x = T(x);
y = T(y);
if (x != y) {
if (t[x] < t[y]) {
t[x] += t[y];
t[y] = x;
} else {
t[y] += t[x];
t[x] = y;
}
}
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; ++i) {
t[i] = -1;
}
for (int i = 0; i < M; ++i) {
cin >> X >> Y >> Z;
--X;
--Y;
U(X, Y);
}
for (int i = 0; i < N; ++i) {
if (t[i] < 0)
++c;
}
cout << c << endl;
} | #include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;
int N, M, X, Y, Z, c, t[100000];
int T(int x) {
if (t[x] < 0)
return x;
return t[x] = T(t[x]);
}
void U(int x, int y) {
x = T(x);
y = T(y);
if (x != y) {
if (t[x] < t[y]) {
t[x] += t[y];
t[y] = x;
} else {
t[y] += t[x];
t[x] = y;
}
}
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; ++i) {
t[i] = -1;
}
for (int i = 0; i < M; ++i) {
cin >> X >> Y >> Z;
--X;
--Y;
U(X, Y);
}
for (int i = 0; i < N; ++i) {
if (t[i] < 0)
++c;
}
cout << c << endl;
} | replace | 15 | 17 | 15 | 16 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
#define repi(n) for (int i = 0; i < (n); i++)
#define repj(n) for (int j = 0; j < (n); j++)
#define repk(n) for (int k = 0; k < (n); k++)
#define repl(n) for (int l = 0; l < (n); l++)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define repr(i, a, b) for (auto i = (a); i < (b); i++)
#define repv(itr) for (auto &&v : (itr))
#define updatemax(t, v) (t = max((t), (v)))
#define updatemin(t, v) (t = min((t), (v)))
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
const double PI = atan(1.0) * 4;
template <typename T> T minptr(T begin, T end) {
T re = begin;
for (T i = begin + 1; i != end; i++) {
if (*i < *re)
re = i;
}
return re;
}
template <typename T> T maxptr(T begin, T end) {
T re = begin;
for (T i = begin + 1; i != end; i++) {
if (*i > *re)
re = i;
}
return re;
}
int __vmax(int x) { return INT_MAX; }
double __vmax(double x) { return 1e+300; }
ll __vmax(ll x) { return LLONG_MAX; }
int __vmin(int x) { return INT_MIN; }
double __vmin(double x) { return -1e+300; }
ll __vmin(ll x) { return LLONG_MIN; }
template <typename T> T gcd(T a, T b) {
return b == 0 ? a : b == 1 ? 1 : gcd(b, a % b);
}
template <typename T> T ib_binary_search(T begin, T end, function<bool(T)> f) {
if (!f(end))
return end;
while (abs(end - begin) > 1) {
T m = (begin + end) / 2;
if (f(m)) {
end = m;
} else {
begin = m;
}
}
return end;
}
ll modpow(ll a, ll b, ll m) {
ll re = 1, k = 1;
while (k <= b) {
if (b & k) {
re *= a;
re %= m;
}
k = k << 1;
a *= a;
a %= m;
}
return re;
}
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;
}
ll modbinomial(ll n, ll k, ll m) {
k = min(k, n - k);
if (k < 0)
return 0;
ll re = 1;
for (ll i = 0; i < k; i++) {
re *= n - i;
re %= m;
re *= modinv(i + 1, m);
re %= m;
}
return re;
}
template <typename T>
vector<T> lis(T begin, T end, bool allowequal = false, bool lds = false) {
using V = typename iterator_traits<T>::value_type;
int n = end - begin;
vector<V> a(n, lds ? __vmin(*begin) : __vmax(*begin));
vector<int> id(n);
if (lds && allowequal) {
for (int i = 0; i < n; i++) {
id[i] = n - 1 -
(lower_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
a[id[i]] = begin[i];
}
} else if (lds) {
for (int i = 0; i < n; i++) {
id[i] = n - 1 -
(upper_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
a[id[i]] = begin[i];
}
} else if (allowequal) {
for (int i = 0; i < n; i++) {
id[i] = upper_bound(a.begin(), a.end(), begin[i]) - a.begin();
a[id[i]] = begin[i];
}
} else {
for (int i = 0; i < n; i++) {
id[i] = lower_bound(a.begin(), a.end(), begin[i]) - a.begin();
a[id[i]] = begin[i];
}
}
int m = *maxptr(id.begin(), id.end());
vector<T> re(m + 1);
for (int i = n - 1; i >= 0; i--) {
if (id[i] == m)
re[m--] = begin + i;
}
return re;
}
template <typename T> class segtree {
private:
int n;
function<T(T, T)> f;
T e;
vector<T> data;
void _updateP(int i) {
data[i] = f(data[i * 2 + 1], data[i * 2 + 2]);
if (i)
_updateP((i - 1) / 2);
}
T _calc(int begin, int end, int node, int nodeBegin, int nodeEnd) {
if (end <= nodeBegin || nodeEnd <= begin) {
return e;
} else if (begin <= nodeBegin && nodeEnd <= end) {
return data[node];
} else {
int m = (nodeBegin + nodeEnd) / 2;
T left = _calc(begin, end, node * 2 + 1, nodeBegin, m);
T right = _calc(begin, end, node * 2 + 2, m, nodeEnd);
return f(left, right);
}
}
public:
segtree(int n_, function<T(T, T)> f_, T e_, T fill) {
n = pow(2, ceil(log2(n_)));
f = f_;
e = e_;
data.resize(n * 2 - 1);
for (int i = 0; i < n * 2 - 1; i++)
data[i] = fill;
}
segtree(int n_, function<T(T, T)> f_, T e_) : segtree(n_, f_, e_, e_) {}
T value(int i) { return data[n - 1 + i]; }
void update(int i, T value) {
data[n - 1 + i] = value;
if (n)
_updateP((n - 2 + i) / 2);
}
T calc(int begin, int end) { return _calc(begin, end, 0, 0, n); }
static T max(T a, T b) { return std::max(a, b); }
static T min(T a, T b) { return std::min(a, b); }
static T sum(T a, T b) { return a + b; }
};
int parent[100000];
int grp(int x) {
if (parent[x] == x)
return x;
return (parent[x] = grp(parent[x]));
}
void uni(int a, int b) { parent[grp(b)] = a; }
bool mm[100000];
int main() {
int n, m;
cin >> n >> m;
repi(n) parent[i] = i;
repi(m) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uni(x, y);
}
int count = 0;
repi(n) {
int g = grp(i);
if (!mm[g]) {
mm[g] = true;
count++;
}
}
cout << count << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
#define repi(n) for (int i = 0; i < (n); i++)
#define repj(n) for (int j = 0; j < (n); j++)
#define repk(n) for (int k = 0; k < (n); k++)
#define repl(n) for (int l = 0; l < (n); l++)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define repr(i, a, b) for (auto i = (a); i < (b); i++)
#define repv(itr) for (auto &&v : (itr))
#define updatemax(t, v) (t = max((t), (v)))
#define updatemin(t, v) (t = min((t), (v)))
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
const double PI = atan(1.0) * 4;
template <typename T> T minptr(T begin, T end) {
T re = begin;
for (T i = begin + 1; i != end; i++) {
if (*i < *re)
re = i;
}
return re;
}
template <typename T> T maxptr(T begin, T end) {
T re = begin;
for (T i = begin + 1; i != end; i++) {
if (*i > *re)
re = i;
}
return re;
}
int __vmax(int x) { return INT_MAX; }
double __vmax(double x) { return 1e+300; }
ll __vmax(ll x) { return LLONG_MAX; }
int __vmin(int x) { return INT_MIN; }
double __vmin(double x) { return -1e+300; }
ll __vmin(ll x) { return LLONG_MIN; }
template <typename T> T gcd(T a, T b) {
return b == 0 ? a : b == 1 ? 1 : gcd(b, a % b);
}
template <typename T> T ib_binary_search(T begin, T end, function<bool(T)> f) {
if (!f(end))
return end;
while (abs(end - begin) > 1) {
T m = (begin + end) / 2;
if (f(m)) {
end = m;
} else {
begin = m;
}
}
return end;
}
ll modpow(ll a, ll b, ll m) {
ll re = 1, k = 1;
while (k <= b) {
if (b & k) {
re *= a;
re %= m;
}
k = k << 1;
a *= a;
a %= m;
}
return re;
}
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;
}
ll modbinomial(ll n, ll k, ll m) {
k = min(k, n - k);
if (k < 0)
return 0;
ll re = 1;
for (ll i = 0; i < k; i++) {
re *= n - i;
re %= m;
re *= modinv(i + 1, m);
re %= m;
}
return re;
}
template <typename T>
vector<T> lis(T begin, T end, bool allowequal = false, bool lds = false) {
using V = typename iterator_traits<T>::value_type;
int n = end - begin;
vector<V> a(n, lds ? __vmin(*begin) : __vmax(*begin));
vector<int> id(n);
if (lds && allowequal) {
for (int i = 0; i < n; i++) {
id[i] = n - 1 -
(lower_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
a[id[i]] = begin[i];
}
} else if (lds) {
for (int i = 0; i < n; i++) {
id[i] = n - 1 -
(upper_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
a[id[i]] = begin[i];
}
} else if (allowequal) {
for (int i = 0; i < n; i++) {
id[i] = upper_bound(a.begin(), a.end(), begin[i]) - a.begin();
a[id[i]] = begin[i];
}
} else {
for (int i = 0; i < n; i++) {
id[i] = lower_bound(a.begin(), a.end(), begin[i]) - a.begin();
a[id[i]] = begin[i];
}
}
int m = *maxptr(id.begin(), id.end());
vector<T> re(m + 1);
for (int i = n - 1; i >= 0; i--) {
if (id[i] == m)
re[m--] = begin + i;
}
return re;
}
template <typename T> class segtree {
private:
int n;
function<T(T, T)> f;
T e;
vector<T> data;
void _updateP(int i) {
data[i] = f(data[i * 2 + 1], data[i * 2 + 2]);
if (i)
_updateP((i - 1) / 2);
}
T _calc(int begin, int end, int node, int nodeBegin, int nodeEnd) {
if (end <= nodeBegin || nodeEnd <= begin) {
return e;
} else if (begin <= nodeBegin && nodeEnd <= end) {
return data[node];
} else {
int m = (nodeBegin + nodeEnd) / 2;
T left = _calc(begin, end, node * 2 + 1, nodeBegin, m);
T right = _calc(begin, end, node * 2 + 2, m, nodeEnd);
return f(left, right);
}
}
public:
segtree(int n_, function<T(T, T)> f_, T e_, T fill) {
n = pow(2, ceil(log2(n_)));
f = f_;
e = e_;
data.resize(n * 2 - 1);
for (int i = 0; i < n * 2 - 1; i++)
data[i] = fill;
}
segtree(int n_, function<T(T, T)> f_, T e_) : segtree(n_, f_, e_, e_) {}
T value(int i) { return data[n - 1 + i]; }
void update(int i, T value) {
data[n - 1 + i] = value;
if (n)
_updateP((n - 2 + i) / 2);
}
T calc(int begin, int end) { return _calc(begin, end, 0, 0, n); }
static T max(T a, T b) { return std::max(a, b); }
static T min(T a, T b) { return std::min(a, b); }
static T sum(T a, T b) { return a + b; }
};
int parent[100000];
int grp(int x) {
if (parent[x] == x)
return x;
return parent[x] = grp(parent[x]);
}
void uni(int a, int b) { parent[grp(a)] = grp(b); }
bool mm[100000];
int main() {
int n, m;
cin >> n >> m;
repi(n) parent[i] = i;
repi(m) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uni(x, y);
}
int count = 0;
repi(n) {
int g = grp(i);
if (!mm[g]) {
mm[g] = true;
count++;
}
}
cout << count << endl;
}
| replace | 182 | 185 | 182 | 185 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct unionfind {
vector<int> d;
unionfind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vi x(m), y(m), z(m);
rep(i, m) cin >> x[i] >> y[i] >> z[i];
unionfind uf(n);
rep(i, m) uf.unite(x[i], y[i]);
int ans = 0;
rep(i, n) if (uf.find(i) == i) ans++;
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct unionfind {
vector<int> d;
unionfind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vi x(m), y(m), z(m);
rep(i, m) cin >> x[i] >> y[i] >> z[i];
rep(i, m) {
x[i]--;
y[i]--;
}
unionfind uf(n);
rep(i, m) uf.unite(x[i], y[i]);
int ans = 0;
rep(i, n) if (uf.find(i) == i) ans++;
cout << ans << endl;
} | replace | 59 | 60 | 59 | 63 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
uf.unite(x, y);
}
vector<int> u(n, 0);
for (int i = 0; i < n; i++)
u[uf.find(i)] |= 1;
cout << accumulate(u.begin(), u.end(), 0) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
uf.unite(x, y);
}
vector<int> u(n, 0);
for (int i = 0; i < n; i++)
u[uf.find(i)] |= 1;
cout << accumulate(u.begin(), u.end(), 0) << endl;
return 0;
} | insert | 35 | 35 | 35 | 37 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h> // include all standard C++ libraries
using namespace std;
// Loops
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define _rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(xi, x) for (auto &&xi : x)
// Note: we can use rep(i,N) or rep(i,from,to)
// typedef
using ll = long long;
template <class T> using vec = vector<T>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
// Constants
// Shorter repr for frequently used terms
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
// Algorithms
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(all(v)), v.end())
#define perm(c) \
sort(all(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))
template <class T> pair<T, size_t> max(vector<T> &x) {
auto it = max_element(all(x));
return mp(*it, it - x.begin());
}
template <class T> pair<T, size_t> min(vector<T> &x) {
auto it = min_element(all(x));
return mp(*it, it - x.begin());
}
template <class T> inline bool chmax(T &maxval, const T &newval) {
if (maxval < newval) {
maxval = newval;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &minval, const T &newval) {
if (minval > newval) {
minval = newval;
return 1;
}
return 0;
}
// Utilities
// Grid world utilities
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define inside(H, W, y, x) 0 <= (x) && (x) < (W) && 0 <= (y) && (y) < (H)
inline int in() {
int x;
cin >> x;
return x;
} // read int from cin
inline ll IN() {
ll x;
cin >> x;
return x;
} // read ll from cin
// Debug
#ifdef LOCAL
#include "dump.hpp"
#define debug(x) cerr << #x << ": " << x << '\n'
#else
#define dump(...)
#define debug(x)
#endif
// Paste snippets here!!
struct UnionFind {
int n;
std::vector<int> par;
std::vector<int> rank;
UnionFind(int n_) : par(n_), rank(n_, 0), n(n_) {
for (int i = 0; i < n; ++i)
par[i] = i;
}
int root(int x) { return par[x] == x ? x : (par[x] = root(par[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)
return;
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
};
//
int main() {
cin.tie(0);
ios::sync_with_stdio(false); // Magic for faster cin
// 連結成分の数を数えるだけ
int N, M;
cin >> N >> M;
vi X(N), Y(N);
int z;
UnionFind uf(N);
rep(i, M) {
cin >> X[i] >> Y[i] >> z;
uf.unite(X[i] - 1, Y[i] - 1);
}
unordered_set<int> S;
rep(i, N) { S.insert(uf.root(i)); }
cout << S.size() << endl;
// vector<vector<int>> G(N);
// vector<int> c(N,-1);
// rep(i,M){
// G[X[i]-1].push_back(Y[i]-1);
// G[Y[i]-1].push_back(X[i]-1);
// }
// int m=1;
// rep(i,N){
// if(c[i]>=0) continue;
// c[i] = m;
// stack<int> s;
// s.push(i);
// while(!s.empty()){
// int t = s.top(); s.pop();
// for(int p : G[t]){
// if(c[p]>=0) continue;
// c[p] = m; s.push(p);
// }
// }
// m++;
// }
// cout << m-1 << endl;
return 0;
}
| #include <bits/stdc++.h> // include all standard C++ libraries
using namespace std;
// Loops
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define _rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(xi, x) for (auto &&xi : x)
// Note: we can use rep(i,N) or rep(i,from,to)
// typedef
using ll = long long;
template <class T> using vec = vector<T>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
// Constants
// Shorter repr for frequently used terms
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
// Algorithms
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(all(v)), v.end())
#define perm(c) \
sort(all(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))
template <class T> pair<T, size_t> max(vector<T> &x) {
auto it = max_element(all(x));
return mp(*it, it - x.begin());
}
template <class T> pair<T, size_t> min(vector<T> &x) {
auto it = min_element(all(x));
return mp(*it, it - x.begin());
}
template <class T> inline bool chmax(T &maxval, const T &newval) {
if (maxval < newval) {
maxval = newval;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &minval, const T &newval) {
if (minval > newval) {
minval = newval;
return 1;
}
return 0;
}
// Utilities
// Grid world utilities
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define inside(H, W, y, x) 0 <= (x) && (x) < (W) && 0 <= (y) && (y) < (H)
inline int in() {
int x;
cin >> x;
return x;
} // read int from cin
inline ll IN() {
ll x;
cin >> x;
return x;
} // read ll from cin
// Debug
#ifdef LOCAL
#include "dump.hpp"
#define debug(x) cerr << #x << ": " << x << '\n'
#else
#define dump(...)
#define debug(x)
#endif
// Paste snippets here!!
struct UnionFind {
int n;
std::vector<int> par;
std::vector<int> rank;
UnionFind(int n_) : par(n_), rank(n_, 0), n(n_) {
for (int i = 0; i < n; ++i)
par[i] = i;
}
int root(int x) { return par[x] == x ? x : (par[x] = root(par[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)
return;
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
};
//
int main() {
cin.tie(0);
ios::sync_with_stdio(false); // Magic for faster cin
// 連結成分の数を数えるだけ
int N, M;
cin >> N >> M;
vi X(M), Y(M);
int z;
UnionFind uf(N);
rep(i, M) {
cin >> X[i] >> Y[i] >> z;
uf.unite(X[i] - 1, Y[i] - 1);
}
unordered_set<int> S;
rep(i, N) { S.insert(uf.root(i)); }
cout << S.size() << endl;
// vector<vector<int>> G(N);
// vector<int> c(N,-1);
// rep(i,M){
// G[X[i]-1].push_back(Y[i]-1);
// G[Y[i]-1].push_back(X[i]-1);
// }
// int m=1;
// rep(i,N){
// if(c[i]>=0) continue;
// c[i] = m;
// stack<int> s;
// s.push(i);
// while(!s.empty()){
// int t = s.top(); s.pop();
// for(int p : G[t]){
// if(c[p]>=0) continue;
// c[p] = m; s.push(p);
// }
// }
// m++;
// }
// cout << m-1 << endl;
return 0;
}
| replace | 125 | 126 | 125 | 126 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// types
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
// macros
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define REP1(i, n) for (int i = 1; i < ((int)n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct UnionFind {
vll par;
vll rank;
UnionFind(ll n) {
par = vll(n);
rank = vll(n, 1);
REP(i, n)
par[i] = i;
}
ll find(ll x) {
if (par[x] == x)
return x;
else {
par[x] = find(par[x]);
return par[x];
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
else {
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[y]++;
}
}
}
bool same(ll x, ll y) { return find(x) == find(y); }
ll size(ll x) { return rank[x]; }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, m;
cin >> n >> m;
vector<pll> xy(m);
vll z(n);
REP(i, m) {
cin >> xy[i].FI >> xy[i].SE >> z[i];
xy[i].FI--;
xy[i].SE--;
}
UnionFind uf(n);
sort(xy.begin(), xy.end());
REP(i, m) {
ll x = xy[i].FI;
ll y = xy[i].SE;
uf.unite(x, y);
}
vll a;
REP(i, n) { a.push_back(uf.find(i)); }
sort(a.begin(), a.end());
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
cout << a.size() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
// types
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
// macros
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define REP1(i, n) for (int i = 1; i < ((int)n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct UnionFind {
vll par;
vll rank;
UnionFind(ll n) {
par = vll(n);
rank = vll(n, 1);
REP(i, n)
par[i] = i;
}
ll find(ll x) {
if (par[x] == x)
return x;
else {
par[x] = find(par[x]);
return par[x];
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
else {
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[y]++;
}
}
}
bool same(ll x, ll y) { return find(x) == find(y); }
ll size(ll x) { return rank[x]; }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, m;
cin >> n >> m;
vector<pll> xy(m);
vll z(m);
REP(i, m) {
cin >> xy[i].FI >> xy[i].SE >> z[i];
xy[i].FI--;
xy[i].SE--;
}
UnionFind uf(n);
sort(xy.begin(), xy.end());
REP(i, m) {
ll x = xy[i].FI;
ll y = xy[i].SE;
uf.unite(x, y);
}
vll a;
REP(i, n) { a.push_back(uf.find(i)); }
sort(a.begin(), a.end());
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
cout << a.size() << endl;
}
| replace | 87 | 88 | 87 | 88 | 0 | |
p03045 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> ve[100005];
int cnt;
bool v[100005];
void dfs(int p) {
if (v[p])
return;
v[p] = true;
for (int i = 0; i < ve[p].size(); i++) {
dfs(ve[p][i]);
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int t1, t2;
scanf("%d%d%d", &t1, &t2);
ve[t1].push_back(t2);
ve[t2].push_back(t1);
}
for (int i = 1; i <= n; i++) {
if (!v[i])
dfs(i), cnt++;
}
printf("%d", cnt);
return 0;
}
| #include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> ve[100005];
int cnt;
bool v[100005];
void dfs(int p) {
if (v[p])
return;
v[p] = true;
for (int i = 0; i < ve[p].size(); i++) {
dfs(ve[p][i]);
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int t1, t2;
scanf("%d%d%*d", &t1, &t2);
ve[t1].push_back(t2);
ve[t2].push_back(t1);
}
for (int i = 1; i <= n; i++) {
if (!v[i])
dfs(i), cnt++;
}
printf("%d", cnt);
return 0;
}
| replace | 24 | 25 | 24 | 25 | -11 | |
p03045 | C++ | Runtime Error | // 7/4
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int N, M;
cin >> N >> M;
vector<int> X(M), Y(M), Z(M);
rep(i, M) cin >> X[i] >> Y[i] >> Z[i];
// UnionFind uf(2*N);
// rep(i, M) {
// int x = X[i];
// int y = Y[i];
// if (Z[i]%2) {
// // X, Yは偶奇が等しい
// uf.unite(x, y);
// uf.unite(x+N, y+N);
// } else {
// // X, Yは偶奇が異なる
// uf.unite(x, y+N);
// uf.unite(x+N, y);
// }
// }
// set<int> st;
// rep(i, 2 * N) {
// st.insert(uf.find(i));
// }
// cout << st.size() / 2 << endl;
UnionFind uf(N);
rep(i, M) { uf.unite(X[i], Y[i]); }
set<int> st;
rep(i, N) st.insert(uf.find(i));
cout << st.size() << endl;
} | // 7/4
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int N, M;
cin >> N >> M;
vector<int> X(M), Y(M), Z(M);
rep(i, M) cin >> X[i] >> Y[i] >> Z[i];
// UnionFind uf(2*N);
// rep(i, M) {
// int x = X[i];
// int y = Y[i];
// if (Z[i]%2) {
// // X, Yは偶奇が等しい
// uf.unite(x, y);
// uf.unite(x+N, y+N);
// } else {
// // X, Yは偶奇が異なる
// uf.unite(x, y+N);
// uf.unite(x+N, y);
// }
// }
// set<int> st;
// rep(i, 2 * N) {
// st.insert(uf.find(i));
// }
// cout << st.size() / 2 << endl;
UnionFind uf(N);
rep(i, M) { uf.unite(X[i] - 1, Y[i] - 1); }
set<int> st;
rep(i, N) st.insert(uf.find(i));
cout << st.size() << endl;
} | replace | 58 | 59 | 58 | 59 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
int ans() {
int tmp = 0;
rep(i, data.size()) {
if (data[i] < 0)
++tmp;
}
return tmp;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
uf.unite(x, y);
}
cout << uf.ans() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
int ans() {
int tmp = 0;
rep(i, data.size()) {
if (data[i] < 0)
++tmp;
}
return tmp;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
--x;
--y;
uf.unite(x, y);
}
cout << uf.ans() << endl;
}
| insert | 65 | 65 | 65 | 67 | 0 | |
p03045 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
const int MAXM = 1e5 + 10;
int n, m;
int x[MAXM], y[MAXM], z[MAXM];
class UnionFind {
vector<int> size_;
vector<int> parent_;
public:
UnionFind(int n) {
size_.reserve(n);
parent_.reserve(n);
for (int i = 0; i < n; i++) {
size_.push_back(1);
parent_.push_back(i);
}
}
int getSize(int x) { return size_[x]; }
bool same(int x, int y) { return find(x) == find(y); }
int find(int x) {
if (x == parent_[x]) {
return x;
}
return parent_[x] = find(parent_[x]);
}
void _union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (size_[x] < size_[y]) {
parent_[x] = y;
size_[y] += size_[x];
} else if (size_[x] >= size_[y]) {
parent_[y] = x;
size_[x] += size_[y];
}
}
};
void solve() {
UnionFind uf(n);
for (int i = 0; i < m; i++) {
uf._union(x[i], y[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (uf.find(i) == i) {
ans++;
}
}
cout << ans << endl;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
}
solve();
}
| #include <iostream>
#include <vector>
using namespace std;
const int MAXM = 1e5 + 10;
int n, m;
int x[MAXM], y[MAXM], z[MAXM];
class UnionFind {
vector<int> size_;
vector<int> parent_;
public:
UnionFind(int n) {
size_.reserve(n);
parent_.reserve(n);
for (int i = 0; i < n; i++) {
size_.push_back(1);
parent_.push_back(i);
}
}
int getSize(int x) { return size_[x]; }
bool same(int x, int y) { return find(x) == find(y); }
int find(int x) {
if (x == parent_[x]) {
return x;
}
return parent_[x] = find(parent_[x]);
}
void _union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (size_[x] < size_[y]) {
parent_[x] = y;
size_[y] += size_[x];
} else if (size_[x] >= size_[y]) {
parent_[y] = x;
size_[x] += size_[y];
}
}
};
void solve() {
UnionFind uf(n);
for (int i = 0; i < m; i++) {
uf._union(x[i], y[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (uf.find(i) == i) {
ans++;
}
}
cout << ans << endl;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
}
solve();
}
| insert | 68 | 68 | 68 | 70 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using ll = long long;
using namespace std;
ll ans[101010];
ll find(ll x) {
if (ans[x] == x) // root
return x;
return find(ans[x]);
}
void uni(ll x, ll y) { ans[find(x)] = find(y); }
int main() {
ll N, M;
cin >> N >> M;
ll x, y, z;
for (ll i = 0; i <= N; i++) {
ans[i] = i;
}
for (ll i = 0; i < M; i++) {
cin >> x >> y >> z;
uni(x, y);
}
z = 0;
for (ll i = 1; i <= N; i++) {
if (ans[i] == i)
z++;
}
cout << z;
} | #include "bits/stdc++.h"
using ll = long long;
using namespace std;
ll ans[101010];
ll find(ll x) {
if (ans[x] == x) // root
return x;
return ans[x] = find(ans[x]);
}
void uni(ll x, ll y) { ans[find(x)] = find(y); }
int main() {
ll N, M;
cin >> N >> M;
ll x, y, z;
for (ll i = 0; i <= N; i++) {
ans[i] = i;
}
for (ll i = 0; i < M; i++) {
cin >> x >> y >> z;
uni(x, y);
}
z = 0;
for (ll i = 1; i <= N; i++) {
if (ans[i] == i)
z++;
}
cout << z;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p03045 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MOD (1000000007)
using namespace std;
typedef long long int Int;
constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }
const int MAX_N = 100000 + 10;
class UF {
private:
int par[MAX_N];
int rank[MAX_N];
public:
UF(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
rank[i] = 1;
}
}
int root(int x) { return x == par[x] ? x : root(par[x]); }
bool same(int x, int y) { return root(x) == root(y); }
void merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y])
rank[y]++;
}
}
int connect(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (i == par[i])
sum++;
}
return sum;
}
};
int N, M;
int main(void) {
cin >> N >> M;
UF uf(N);
for (int i = 0; i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
uf.merge(x, y);
}
cout << uf.connect(N) << endl;
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MOD (1000000007)
using namespace std;
typedef long long int Int;
constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }
const int MAX_N = 100000 + 10;
class UF {
private:
int par[MAX_N];
int rank[MAX_N];
public:
UF(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
rank[i] = 1;
}
}
int root(int x) { return x == par[x] ? x : root(par[x]); }
bool same(int x, int y) { return root(x) == root(y); }
void merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
int connect(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (i == par[i])
sum++;
}
return sum;
}
};
int N, M;
int main(void) {
cin >> N >> M;
UF uf(N);
for (int i = 0; i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
uf.merge(x, y);
}
cout << uf.connect(N) << endl;
return 0;
}
| replace | 53 | 54 | 53 | 54 | TLE | |
p03045 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = numeric_limits<int>::max() / 2;
ll LINF = numeric_limits<ll>::max() / 2;
int MOD = 1e9 + 7;
class UnionFind {
std::vector<int> data;
public:
UnionFind(int size) : data(size, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
std::swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool find(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
ll size(int x) { return -data[root(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
int ans = n - m;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
if (uf.find(a, b))
ans++;
uf.unite(a, b);
}
cout << ans << endl;
} | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = numeric_limits<int>::max() / 2;
ll LINF = numeric_limits<ll>::max() / 2;
int MOD = 1e9 + 7;
class UnionFind {
std::vector<int> data;
public:
UnionFind(int size) : data(size, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
std::swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool find(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
ll size(int x) { return -data[root(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
int ans = n - m;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
if (uf.find(a, b))
ans++;
uf.unite(a, b);
}
cout << ans << endl;
} | insert | 36 | 36 | 36 | 38 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
cerr << head;
dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
cerr << head << ", ";
dump(forward<Tail>(tail)...);
}
#define debug(...) \
do { \
cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
dump(__VA_ARGS__); \
} while (false)
#else
#define dump(...)
#define debug(...)
#endif
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
/////////////////////////////////////////////////////////////////////
const ll inf = 1LL << 60;
struct UnionFind {
vector<int> data;
UnionFind(int n) { init(n); }
void init(int n = 1) { data.assign(n, -1); }
int root(int x) {
if (data[x] < 0)
return x;
return (data[x] = root(data[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 (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
int size(int x) { return (-data[root(x)]); }
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> x(n);
vector<ll> y(n);
vector<ll> z(n);
rep(i, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
z[i] %= 2;
}
UnionFind uf(n);
rep(i, m) { uf.merge(x[i], y[i]); }
set<ll> st;
rep(i, n) { st.insert(uf.root(i)); }
cout << st.size() << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
cerr << head;
dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
cerr << head << ", ";
dump(forward<Tail>(tail)...);
}
#define debug(...) \
do { \
cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
dump(__VA_ARGS__); \
} while (false)
#else
#define dump(...)
#define debug(...)
#endif
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
/////////////////////////////////////////////////////////////////////
const ll inf = 1LL << 60;
struct UnionFind {
vector<int> data;
UnionFind(int n) { init(n); }
void init(int n = 1) { data.assign(n, -1); }
int root(int x) {
if (data[x] < 0)
return x;
return (data[x] = root(data[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 (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
int size(int x) { return (-data[root(x)]); }
};
int main() {
ll n, m;
cin >> n >> m;
vector<ll> x(m);
vector<ll> y(m);
vector<ll> z(m);
rep(i, m) {
cin >> x[i] >> y[i] >> z[i];
x[i]--;
y[i]--;
z[i] %= 2;
}
UnionFind uf(n);
rep(i, m) { uf.merge(x[i], y[i]); }
set<ll> st;
rep(i, n) { st.insert(uf.root(i)); }
cout << st.size() << endl;
return 0;
}
| replace | 72 | 75 | 72 | 75 | 0 | |
p03045 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define RI register int
typedef long long LL;
int fa[100005], vis[100005];
int Find(int x) { return x == fa[x] ? x : Find(fa[x]); }
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (RI i = 1; i <= n; ++i)
fa[i] = i;
for (RI i = 1, x, y, z; i <= m; ++i)
scanf("%d %d %d", &x, &y, &z), fa[Find(x)] = Find(y);
int ans = 0;
for (RI i = 1; i <= n; ++i)
if (!vis[Find(i)])
++ans, vis[Find(i)] = 1;
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
#define RI register int
typedef long long LL;
int fa[100005], vis[100005];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (RI i = 1; i <= n; ++i)
fa[i] = i;
for (RI i = 1, x, y, z; i <= m; ++i)
scanf("%d %d %d", &x, &y, &z), fa[Find(x)] = Find(y);
int ans = 0;
for (RI i = 1; i <= n; ++i)
if (!vis[Find(i)])
++ans, vis[Find(i)] = 1;
printf("%d\n", ans);
return 0;
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p03045 | C++ | Runtime Error | #include <cstdlib>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
typedef struct _xyz {
int x;
int y;
int z;
} xyz;
typedef struct _node {
std::vector<int> nbr; // zero-based numbering (node_arr[nbr]
} node;
int main(void) {
int n, m;
scanf("%d%d", &n, &m);
xyz *xyz_arr = (xyz *)std::malloc(sizeof(xyz) * m);
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &xyz_arr[i].x, &xyz_arr[i].y, &xyz_arr[i].z);
}
node *node_arr = (node *)std::malloc(sizeof(node) * n);
for (int i = 0; i < m; i++) {
node_arr[xyz_arr[i].x - 1].nbr.push_back(xyz_arr[i].y - 1);
node_arr[xyz_arr[i].y - 1].nbr.push_back(xyz_arr[i].x - 1);
}
int count;
std::vector<bool> memo(n);
count = 0;
for (int i = 0; i < m; ++i) {
if (!memo[i]) {
/*
* BFS
*/
memo[i] = true;
std::queue<int> q; // zero-based numbering
q.push(i);
while (!q.empty()) {
int j = q.front();
q.pop();
for (auto itr = node_arr[j].nbr.begin(); itr != node_arr[j].nbr.end();
++itr) {
if (!memo[*itr]) {
memo[*itr] = true;
q.push(*itr);
} else {
continue;
}
}
}
count++;
} else if (node_arr[i].nbr.empty()) {
count++;
} else {
continue;
}
}
std::cout << count << std::endl;
return EXIT_SUCCESS;
} | #include <cstdlib>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
typedef struct _xyz {
int x;
int y;
int z;
} xyz;
typedef struct _node {
std::vector<int> nbr; // zero-based numbering (node_arr[nbr]
} node;
int main(void) {
int n, m;
scanf("%d%d", &n, &m);
xyz *xyz_arr = (xyz *)std::malloc(sizeof(xyz) * m);
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &xyz_arr[i].x, &xyz_arr[i].y, &xyz_arr[i].z);
}
node *node_arr = (node *)std::malloc(sizeof(node) * n);
for (int i = 0; i < m; i++) {
node_arr[xyz_arr[i].x - 1].nbr.push_back(xyz_arr[i].y - 1);
node_arr[xyz_arr[i].y - 1].nbr.push_back(xyz_arr[i].x - 1);
}
int count;
std::vector<bool> memo(n);
count = 0;
for (int i = 0; i < n; ++i) {
if (!memo[i]) {
/*
* BFS
*/
memo[i] = true;
std::queue<int> q; // zero-based numbering
q.push(i);
while (!q.empty()) {
int j = q.front();
q.pop();
for (auto itr = node_arr[j].nbr.begin(); itr != node_arr[j].nbr.end();
++itr) {
if (!memo[*itr]) {
memo[*itr] = true;
q.push(*itr);
} else {
continue;
}
}
}
count++;
} else if (node_arr[i].nbr.empty()) {
count++;
} else {
continue;
}
}
std::cout << count << std::endl;
return EXIT_SUCCESS;
} | replace | 33 | 34 | 33 | 34 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
typedef long long ll;
typedef long double R;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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)]; // 親を取ってきたい
}
bool connect(int A, int B) {
// AとBを直接繋ぐのではなく、root(A)をroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついているからくっつけない
return false;
}
// 大きい方に小さい方をくっつけたい
// 代償が逆だったらひっくり返す
if (size(A) < size(B))
swap(A, B);
// Aのsizeを更新
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
int main() {
int N, M;
cin >> N >> M;
vector<int> x(M), y(M), z(M);
long long ans = N;
for (int i = 0; i < M; ++i) {
cin >> x[i] >> y[i] >> z[i];
}
UnionFind Uni(N);
for (int i = 0; i < M; ++i) {
if (Uni.root(x[i]) != Uni.root(y[i])) {
Uni.connect(x[i], y[i]);
ans--;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
typedef long long ll;
typedef long double R;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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)]; // 親を取ってきたい
}
bool connect(int A, int B) {
// AとBを直接繋ぐのではなく、root(A)をroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついているからくっつけない
return false;
}
// 大きい方に小さい方をくっつけたい
// 代償が逆だったらひっくり返す
if (size(A) < size(B))
swap(A, B);
// Aのsizeを更新
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
int main() {
int N, M;
cin >> N >> M;
vector<int> x(M), y(M), z(M);
long long ans = N;
for (int i = 0; i < M; ++i) {
cin >> x[i] >> y[i] >> z[i];
x[i]--, y[i]--, z[i]--;
}
UnionFind Uni(N);
for (int i = 0; i < M; ++i) {
if (Uni.root(x[i]) != Uni.root(y[i])) {
Uni.connect(x[i], y[i]);
ans--;
}
}
cout << ans << endl;
return 0;
} | insert | 85 | 85 | 85 | 86 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = 1050000000;
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Tof(x) (x) ? Yes : No
#define TOF(x) (x) ? YES : NO
#define tof(x) (x) ? yes : no
using ll = long long;
/*vectorの要素を全部確認するやつだよ。
for (vector<int>::iterator itr = ans.begin(); itr != ans.end(); ++itr) {
cout << *itr << " ";
}
*/
/*for文でx,yを上下左右確認するやつだよ。
int dy[] = { 0, 1, 0, -1 };
int dx[] = { 1, 0, -1, 0 };
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
}
*/
/*繰り返し二乗法だよ。modもとってくれるよ。n^kをmodで割った余りでやってくれるよ。
ll POW_MOD(ll n, ll k, ll mod) {
ll r = 1;
for (; k > 0; k >>= 1) {
if (k & 1) {
r = (r * n) % mod;
}
n = (n * n) % mod;
}
return r;
}
*/
/*2重for文書くのめんどくなった時用だよ。
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
}
}
*/
class UnionFind {
private:
std::vector<int> parent;
std::vector<int> height;
std::vector<int> m_size;
public:
UnionFind(int size_) : parent(size_), height(size_, 0), m_size(size_, 1) {
for (int i = 0; i < size_; ++i)
parent[i] = i;
}
void init(int size_) {
parent.resize(size_);
height.resize(size_, 0);
m_size.resize(size_, 1);
for (int i = 0; i < size_; ++i)
parent[i] = i;
}
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
int t = size(x) + size(y);
m_size[x] = m_size[y] = t;
if (height[x] < height[y])
parent[x] = y;
else
parent[y] = x;
if (height[x] == height[y])
++height[x];
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) {
if (parent[x] == x)
return m_size[x];
return size(parent[x] = find(parent[x]));
}
};
int main(void) {
int n, m;
cin >> n >> m;
int x, y, z;
UnionFind uf(n);
uf.init(n);
int ans = n;
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
if (uf.same(x, y)) {
} else {
uf.unite(x, y);
ans--;
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = 1050000000;
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Tof(x) (x) ? Yes : No
#define TOF(x) (x) ? YES : NO
#define tof(x) (x) ? yes : no
using ll = long long;
/*vectorの要素を全部確認するやつだよ。
for (vector<int>::iterator itr = ans.begin(); itr != ans.end(); ++itr) {
cout << *itr << " ";
}
*/
/*for文でx,yを上下左右確認するやつだよ。
int dy[] = { 0, 1, 0, -1 };
int dx[] = { 1, 0, -1, 0 };
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
}
*/
/*繰り返し二乗法だよ。modもとってくれるよ。n^kをmodで割った余りでやってくれるよ。
ll POW_MOD(ll n, ll k, ll mod) {
ll r = 1;
for (; k > 0; k >>= 1) {
if (k & 1) {
r = (r * n) % mod;
}
n = (n * n) % mod;
}
return r;
}
*/
/*2重for文書くのめんどくなった時用だよ。
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
}
}
*/
class UnionFind {
private:
std::vector<int> parent;
std::vector<int> height;
std::vector<int> m_size;
public:
UnionFind(int size_) : parent(size_), height(size_, 0), m_size(size_, 1) {
for (int i = 0; i < size_; ++i)
parent[i] = i;
}
void init(int size_) {
parent.resize(size_);
height.resize(size_, 0);
m_size.resize(size_, 1);
for (int i = 0; i < size_; ++i)
parent[i] = i;
}
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
int t = size(x) + size(y);
m_size[x] = m_size[y] = t;
if (height[x] < height[y])
parent[x] = y;
else
parent[y] = x;
if (height[x] == height[y])
++height[x];
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) {
if (parent[x] == x)
return m_size[x];
return size(parent[x] = find(parent[x]));
}
};
int main(void) {
int n, m;
cin >> n >> m;
int x, y, z;
UnionFind uf(n + 1);
uf.init(n + 1);
int ans = n;
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
if (uf.same(x, y)) {
} else {
uf.unite(x, y);
ans--;
}
}
cout << ans << endl;
}
| replace | 139 | 141 | 139 | 141 | 0 | |
p03045 | C++ | Runtime Error | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++)
#define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define vi vector<int>
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
REP(i, v.size()) {
if (i)
os << " ";
os << v[i];
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
REP(i, v.size()) {
if (i)
os << endl;
os << v[i];
}
return os;
}
class UnionFind {
private:
vector<int> par, rank, sizes;
int tree;
public:
explicit UnionFind(const int n) : tree(n) {
par.resize(n);
rank.resize(n);
sizes.resize(n);
rep(i, n) {
par[i] = i;
sizes[i] = 1;
}
}
~UnionFind() = default;
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
int find(int x) { return root(x); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (rank[x] < rank[y])
swap(x, y);
par[y] = x;
tree--;
sizes[x] += sizes[y];
if (rank[x] == rank[y])
rank[x]++;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return sizes[x]; }
int sumTree() { return tree; }
};
class segmentTree {};
class ModInt {};
class topos {};
int main() {
int n, m;
cin >> n >> m;
UnionFind u(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
u.unite(x, y);
}
cout << u.sumTree() << endl;
} | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++)
#define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define vi vector<int>
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
REP(i, v.size()) {
if (i)
os << " ";
os << v[i];
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
REP(i, v.size()) {
if (i)
os << endl;
os << v[i];
}
return os;
}
class UnionFind {
private:
vector<int> par, rank, sizes;
int tree;
public:
explicit UnionFind(const int n) : tree(n) {
par.resize(n);
rank.resize(n);
sizes.resize(n);
rep(i, n) {
par[i] = i;
sizes[i] = 1;
}
}
~UnionFind() = default;
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
int find(int x) { return root(x); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (rank[x] < rank[y])
swap(x, y);
par[y] = x;
tree--;
sizes[x] += sizes[y];
if (rank[x] == rank[y])
rank[x]++;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return sizes[x]; }
int sumTree() { return tree; }
};
class segmentTree {};
class ModInt {};
class topos {};
int main() {
int n, m;
cin >> n >> m;
UnionFind u(n);
rep(i, m) {
int x, y, z;
cin >> x >> y >> z;
u.unite(x - 1, y - 1);
}
cout << u.sumTree() << endl;
} | replace | 97 | 98 | 97 | 98 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
vector<int> par;
vector<int> rnk;
// N個要素に対して、N個のノードを用意して初期化
void init(int N) {
par.resize(N, 0);
rnk.resize(N, 0);
REP(i, N) {
par[i] = i; // 最初は自分自身が親
rnk[i] = 0;
}
}
// 木の根(親)を求める
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 (rnk[X] < rnk[Y]) {
par[X] = Y;
} else {
par[Y] = X;
if (rnk[X] == rnk[Y])
rnk[X]++;
}
}
// XとYが同じ集合に属するか否か
bool same(int X, int Y) { return find(X) == find(Y); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
init(N);
REP(i, M) {
int x, y, z;
cin >> x >> y >> z;
unite(x, y);
}
REP(i, N) { find(i); }
set<int> cnt;
REP(i, N) { cnt.insert(par[i]); }
int ans = cnt.size();
cout << ans << endl;
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
vector<int> par;
vector<int> rnk;
// N個要素に対して、N個のノードを用意して初期化
void init(int N) {
par.resize(N, 0);
rnk.resize(N, 0);
REP(i, N) {
par[i] = i; // 最初は自分自身が親
rnk[i] = 0;
}
}
// 木の根(親)を求める
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 (rnk[X] < rnk[Y]) {
par[X] = Y;
} else {
par[Y] = X;
if (rnk[X] == rnk[Y])
rnk[X]++;
}
}
// XとYが同じ集合に属するか否か
bool same(int X, int Y) { return find(X) == find(Y); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
init(N);
REP(i, M) {
int x, y, z;
cin >> x >> y >> z;
x--;
y--;
unite(x, y);
}
REP(i, N) { find(i); }
set<int> cnt;
REP(i, N) { cnt.insert(par[i]); }
int ans = cnt.size();
cout << ans << endl;
} | insert | 68 | 68 | 68 | 70 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main(int argc, const char *argv[]) {
int N, M;
cin >> N >> M;
vector<int> X(M), Y(M);
for (int i = 0; i < M; i++) {
int Z;
cin >> X[i] >> Y[i] >> Z;
}
UnionFind tree(N);
for (int i = 0; i < M; i++) {
tree.unite(X[i] - 1, Y[i] - 1);
}
set<int> parent;
for (int i = 0; i <= N; i++) {
parent.insert(tree.root(i));
}
cout << parent.size() << endl;
return 0;
} | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main(int argc, const char *argv[]) {
int N, M;
cin >> N >> M;
vector<int> X(M), Y(M);
for (int i = 0; i < M; i++) {
int Z;
cin >> X[i] >> Y[i] >> Z;
}
UnionFind tree(N);
for (int i = 0; i < M; i++) {
tree.unite(X[i] - 1, Y[i] - 1);
}
set<int> parent;
for (int i = 0; i < N; i++) {
parent.insert(tree.root(i));
}
cout << parent.size() << endl;
return 0;
} | replace | 61 | 62 | 61 | 62 | 0 | |
p03045 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pil = pair<int, ll>;
// Graph.h start--------------------------------------------
#pragma once
#include <vector>
using Weight = long long;
struct Vertex;
using Vertices = std::vector<Vertex *>;
struct Edge;
using Edges = std::vector<const Edge *>;
struct Vertex {
const int id;
Edges edges;
Vertex(int id);
void add_edge(const Edge *const p_e);
void add_edge(const Edges &es);
};
struct Edge {
Vertex *p_src, *p_dst;
Weight weight;
Edge(Vertex *p_src, Vertex *p_dst, Weight weight);
Edge(const Edge &e);
Edge &operator=(const Edge &de);
};
struct Graph {
Vertices vertices;
Edges edges;
Graph(int nvertices);
Graph();
~Graph();
void add_edge(Edge *p_edge);
void add_edge(int edge_src, int edge_dst, Weight w);
};
//----------------------------------------------------------
using namespace std;
Vertex::Vertex(int id) : id(id) {}
void Vertex::add_edge(const Edge *const p_e) { edges.push_back(p_e); }
void Vertex::add_edge(const Edges &es) {
for (const auto &p_e : es) {
add_edge(p_e);
}
}
Edge::Edge(Vertex *p_src, Vertex *p_dst, Weight weight)
: p_src(p_src), p_dst(p_dst), weight(weight) {}
Edge::Edge(const Edge &e) : Edge(e.p_src, e.p_dst, e.weight) {}
Edge &Edge::operator=(const Edge &e) {
p_src = e.p_src;
p_dst = e.p_dst;
weight = e.weight;
return *this;
}
bool operator<(const Edge &e1, const Edge &e2) {
return e1.weight != e2.weight ? e1.weight > e2.weight : // !!INVERSE!!
e1.p_src != e2.p_src ? e1.p_src->id < e2.p_src->id
: e1.p_dst->id < e2.p_dst->id;
}
Graph::Graph(int nvertices) {
for (int id = 0; id < nvertices; id++) {
vertices.push_back(new Vertex(id));
}
}
Graph::Graph() : Graph(0) {}
Graph::~Graph() {
for (auto &p_vertex : vertices) {
delete p_vertex;
}
for (auto &p_edge : edges) {
delete p_edge;
}
}
void Graph::add_edge(Edge *p_edge) {
vertices[p_edge->p_src->id]->add_edge(p_edge);
edges.push_back(p_edge);
}
void Graph::add_edge(int edge_src, int edge_dst, Weight w) {
add_edge(new Edge(vertices[edge_src], vertices[edge_dst], w));
}
// Graph.h end----------------------------------------------
vector<bool> is_filled;
void fill(int s, int d, Graph &g) {
is_filled[d] = true;
for (const auto &p_e : g.vertices[d]->edges) {
int nd = p_e->p_dst->id;
if (is_filled[nd])
continue;
fill(d, nd, g);
}
}
int main() {
int N, M;
cin >> N >> M;
vector<int> x(N + 1), y(N + 1);
for (int i = 0; i < M; i++) {
int dummyz;
cin >> x[i] >> y[i] >> dummyz;
}
Graph g(N + 1);
for (int i = 0; i < M; i++) {
g.add_edge(x[i], y[i], 0);
g.add_edge(y[i], x[i], 0);
}
is_filled = vector<bool>(N + 1, false);
int ans = 0;
for (int i = 1; i <= N; i++) {
if (is_filled[i])
continue;
fill(0, i, g);
ans++;
}
cout << ans << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pil = pair<int, ll>;
// Graph.h start--------------------------------------------
#pragma once
#include <vector>
using Weight = long long;
struct Vertex;
using Vertices = std::vector<Vertex *>;
struct Edge;
using Edges = std::vector<const Edge *>;
struct Vertex {
const int id;
Edges edges;
Vertex(int id);
void add_edge(const Edge *const p_e);
void add_edge(const Edges &es);
};
struct Edge {
Vertex *p_src, *p_dst;
Weight weight;
Edge(Vertex *p_src, Vertex *p_dst, Weight weight);
Edge(const Edge &e);
Edge &operator=(const Edge &de);
};
struct Graph {
Vertices vertices;
Edges edges;
Graph(int nvertices);
Graph();
~Graph();
void add_edge(Edge *p_edge);
void add_edge(int edge_src, int edge_dst, Weight w);
};
//----------------------------------------------------------
using namespace std;
Vertex::Vertex(int id) : id(id) {}
void Vertex::add_edge(const Edge *const p_e) { edges.push_back(p_e); }
void Vertex::add_edge(const Edges &es) {
for (const auto &p_e : es) {
add_edge(p_e);
}
}
Edge::Edge(Vertex *p_src, Vertex *p_dst, Weight weight)
: p_src(p_src), p_dst(p_dst), weight(weight) {}
Edge::Edge(const Edge &e) : Edge(e.p_src, e.p_dst, e.weight) {}
Edge &Edge::operator=(const Edge &e) {
p_src = e.p_src;
p_dst = e.p_dst;
weight = e.weight;
return *this;
}
bool operator<(const Edge &e1, const Edge &e2) {
return e1.weight != e2.weight ? e1.weight > e2.weight : // !!INVERSE!!
e1.p_src != e2.p_src ? e1.p_src->id < e2.p_src->id
: e1.p_dst->id < e2.p_dst->id;
}
Graph::Graph(int nvertices) {
for (int id = 0; id < nvertices; id++) {
vertices.push_back(new Vertex(id));
}
}
Graph::Graph() : Graph(0) {}
Graph::~Graph() {
for (auto &p_vertex : vertices) {
delete p_vertex;
}
for (auto &p_edge : edges) {
delete p_edge;
}
}
void Graph::add_edge(Edge *p_edge) {
vertices[p_edge->p_src->id]->add_edge(p_edge);
edges.push_back(p_edge);
}
void Graph::add_edge(int edge_src, int edge_dst, Weight w) {
add_edge(new Edge(vertices[edge_src], vertices[edge_dst], w));
}
// Graph.h end----------------------------------------------
vector<bool> is_filled;
void fill(int s, int d, Graph &g) {
is_filled[d] = true;
for (const auto &p_e : g.vertices[d]->edges) {
int nd = p_e->p_dst->id;
if (is_filled[nd])
continue;
fill(d, nd, g);
}
}
int main() {
int N, M;
cin >> N >> M;
vector<int> x(M), y(M);
for (int i = 0; i < M; i++) {
int dummyz;
cin >> x[i] >> y[i] >> dummyz;
}
Graph g(N + 1);
for (int i = 0; i < M; i++) {
g.add_edge(x[i], y[i], 0);
g.add_edge(y[i], x[i], 0);
}
is_filled = vector<bool>(N + 1, false);
int ans = 0;
for (int i = 1; i <= N; i++) {
if (is_filled[i])
continue;
fill(0, i, g);
ans++;
}
cout << ans << endl;
return 0;
}
| replace | 111 | 112 | 111 | 112 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
bool isEven(int x) {
if (x % 2 == 0)
return true;
else
return false;
}
class UnionFind {
public:
vector<int> parent;
UnionFind(int n) : parent(n) {
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int root(int x) {
if (parent[x] == x)
return x;
return parent[x] = root(parent[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
parent[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int n, m;
cin >> n >> m;
int x[n];
int y[n];
int z[n];
UnionFind card(n);
int ans = n;
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
if (!card.same(x[i] - 1, y[i] - 1)) {
ans--;
}
card.unite(x[i] - 1, y[i] - 1);
/*for(int i = 0; i < n; i++){
cout << card.root(i) << " ";
}
cout << endl;*/
}
cout << ans << endl;
return 0;
}
/*
class node {
int number;
int parent;
node(int num) {
this.number = num;
this.parent = this;
}
void setNumber(int num) {
this.number = num;
}
void setParent(int par) {
this.parent =
}
int getParent() {
return this.parent;
}
};*/
| #include <bits/stdc++.h>
using namespace std;
bool isEven(int x) {
if (x % 2 == 0)
return true;
else
return false;
}
class UnionFind {
public:
vector<int> parent;
UnionFind(int n) : parent(n) {
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int root(int x) {
if (parent[x] == x)
return x;
return parent[x] = root(parent[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
parent[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int n, m;
cin >> n >> m;
int x[m];
int y[m];
int z[m];
UnionFind card(n);
int ans = n;
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
if (!card.same(x[i] - 1, y[i] - 1)) {
ans--;
}
card.unite(x[i] - 1, y[i] - 1);
/*for(int i = 0; i < n; i++){
cout << card.root(i) << " ";
}
cout << endl;*/
}
cout << ans << endl;
return 0;
}
/*
class node {
int number;
int parent;
node(int num) {
this.number = num;
this.parent = this;
}
void setNumber(int num) {
this.number = num;
}
void setParent(int par) {
this.parent =
}
int getParent() {
return this.parent;
}
};*/
| replace | 44 | 47 | 44 | 47 | 0 | |
p03045 | C++ | Runtime Error | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#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 = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define do long double
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int INF = 1e10;
int MOD = 1e9 + 7;
struct Union {
vector<int> par;
Union(int n) { par = vector<int>(n, -1); }
int find(int x) {
if (par[x] < 0)
return x;
else
return par[x] = find(par[x]);
}
bool same(int a, int b) { return find(a) == find(b); }
int Size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (Size(b) > Size(a))
swap<int>(a, b);
par[a] += par[b];
par[b] = a;
}
};
signed main() {
int a, b;
cin >> a >> b;
int c[b][3];
rep(i, b) rep(j, 3) cin >> c[i][j];
Union d(a);
rep(i, b) { d.unite(c[i][0] - 1, c[i][1] - 1); }
vector<int> e(a);
rep(i, b) { e[i] = d.find(i); }
sort(all(e));
e.erase(unique(all(e)), e.end());
cout << e.size() << endl;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#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 = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define do long double
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int INF = 1e10;
int MOD = 1e9 + 7;
struct Union {
vector<int> par;
Union(int n) { par = vector<int>(n, -1); }
int find(int x) {
if (par[x] < 0)
return x;
else
return par[x] = find(par[x]);
}
bool same(int a, int b) { return find(a) == find(b); }
int Size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (Size(b) > Size(a))
swap<int>(a, b);
par[a] += par[b];
par[b] = a;
}
};
signed main() {
int a, b;
cin >> a >> b;
int c[b][3];
rep(i, b) rep(j, 3) cin >> c[i][j];
Union d(a);
rep(i, b) { d.unite(c[i][0] - 1, c[i][1] - 1); }
vector<int> e(a);
rep(i, a) { e[i] = d.find(i); }
sort(all(e));
e.erase(unique(all(e)), e.end());
cout << e.size() << endl;
}
| replace | 57 | 58 | 57 | 58 | 0 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define range(i) (i).begin(), (i).end()
#define rrange(i) (i).rbegin(), (i).rend()
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline constexpr int intpow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = intpow(a, b / 2);
return ans * ans * (b & 1 ? a : 1);
}
inline constexpr int modpow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = intpow(a, b / 2);
return ans * ans % MOD * (b & 1 ? a : 1) % MOD;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll LINF = 0x1fffffffffffffff;
struct unionfind {
vector<ll> d;
unionfind(ll x) : d(x, -1) {}
ll root(ll x) { return d[x] < 0 ? x : d[x] = root(d[x]); }
ll size(ll x) { return -d[root(x)]; };
bool unite(ll x, ll y) {
x = root(x), y = root(y);
if (x != y) {
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
}
return x != y;
}
bool find(ll x, ll y) { return root(x) == root(y); }
};
int main() {
ll n, m;
cin >> n >> m;
unionfind uf(n);
ll ans = n;
rep(i, 0, m) {
ll x, y, z;
cin >> x, y, z;
ans -= uf.unite(x - 1, y - 1);
}
cout << ans;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define range(i) (i).begin(), (i).end()
#define rrange(i) (i).rbegin(), (i).rend()
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline constexpr int intpow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = intpow(a, b / 2);
return ans * ans * (b & 1 ? a : 1);
}
inline constexpr int modpow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = intpow(a, b / 2);
return ans * ans % MOD * (b & 1 ? a : 1) % MOD;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll LINF = 0x1fffffffffffffff;
struct unionfind {
vector<ll> d;
unionfind(ll x) : d(x, -1) {}
ll root(ll x) { return d[x] < 0 ? x : d[x] = root(d[x]); }
ll size(ll x) { return -d[root(x)]; };
bool unite(ll x, ll y) {
x = root(x), y = root(y);
if (x != y) {
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
}
return x != y;
}
bool find(ll x, ll y) { return root(x) == root(y); }
};
int main() {
ll n, m;
cin >> n >> m;
unionfind uf(n);
ll ans = n;
rep(i, 0, m) {
ll x, y, z;
cin >> x >> y >> z;
ans -= uf.unite(x - 1, y - 1);
}
cout << ans;
}
| replace | 67 | 68 | 67 | 68 | -11 | |
p03045 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class unionFind {
int nodeSize;
vector<int> parent;
public:
unionFind(int n) : nodeSize(n) { parent = vector<int>(n + 1, -1); }
int getRoot(int a) {
if (parent[a] < 0) {
return a;
}
return parent[a] = getRoot(parent[a]);
}
bool isSameParent(int a, int b) { return getRoot(a) == getRoot(b); }
bool isRoot(int a) {
if (parent[a] < 0) {
return true;
}
return false;
}
void unite(int a, int b) {
if (isSameParent(a, b)) {
return;
}
int aParent = getRoot(a);
int bParent = getRoot(b);
if (abs(parent[aParent]) > abs(parent[bParent])) {
parent[aParent] += parent[bParent];
parent[bParent] = aParent;
} else {
parent[bParent] += parent[bParent];
parent[aParent] = bParent;
}
}
};
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
}
unionFind uf(n);
for (int i = 0; i < m; i++) {
uf.unite(x[i], y[i]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (uf.isRoot(i)) {
ans++;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class unionFind {
int nodeSize;
vector<int> parent;
public:
unionFind(int n) : nodeSize(n) { parent = vector<int>(n + 1, -1); }
int getRoot(int a) {
if (parent[a] < 0) {
return a;
}
return parent[a] = getRoot(parent[a]);
}
bool isSameParent(int a, int b) { return getRoot(a) == getRoot(b); }
bool isRoot(int a) {
if (parent[a] < 0) {
return true;
}
return false;
}
void unite(int a, int b) {
if (isSameParent(a, b)) {
return;
}
int aParent = getRoot(a);
int bParent = getRoot(b);
if (abs(parent[aParent]) > abs(parent[bParent])) {
parent[aParent] += parent[bParent];
parent[bParent] = aParent;
} else {
parent[bParent] += parent[aParent];
parent[aParent] = bParent;
}
}
};
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
}
unionFind uf(n);
for (int i = 0; i < m; i++) {
uf.unite(x[i], y[i]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (uf.isRoot(i)) {
ans++;
}
}
cout << ans << endl;
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p03045 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
typedef long long LL;
typedef long double LD;
class UnionFind {
private:
vector<int> node;
public:
UnionFind(int n) {
node = vector<int>(n);
rep(i, n) node[i] = i;
}
int root(int x) {
if (node[x] == x)
return x;
else
node[x] = root(node[x]);
}
bool uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
node[y] = x;
return true;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
int x, y, z;
int ans = n;
rep(i, m) {
cin >> x >> y >> z;
if (uf.uni(x - 1, y - 1))
ans--;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
typedef long long LL;
typedef long double LD;
class UnionFind {
private:
vector<int> node;
public:
UnionFind(int n) {
node = vector<int>(n);
rep(i, n) node[i] = i;
}
int root(int x) {
if (node[x] == x)
return x;
else
return node[x] = root(node[x]);
}
bool uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
node[y] = x;
return true;
}
};
int main() {
int n, m;
cin >> n >> m;
UnionFind uf(n);
int x, y, z;
int ans = n;
rep(i, m) {
cin >> x >> y >> z;
if (uf.uni(x - 1, y - 1))
ans--;
}
cout << ans << endl;
return 0;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p03047 | Python | Runtime Error | N, K = map(int, input())
print(N - K + 1)
| N, K = map(int, input().split())
print(N - K + 1)
| replace | 0 | 1 | 0 | 1 | ValueError: invalid literal for int() with base 10: ' ' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03047/Python/s597421224.py", line 1, in <module>
N, K = map(int, input())
ValueError: invalid literal for int() with base 10: ' '
|
p03047 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 1005
#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
res = 0;
T f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template <class T> void out(T x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N, K;
void Solve() {
read(N);
read(K);
out(N - K + 1);
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in", "r", stdin);
#endif
Solve();
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 1005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
res = 0;
T f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template <class T> void out(T x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N, K;
void Solve() {
read(N);
read(K);
out(N - K + 1);
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in", "r", stdin);
#endif
Solve();
return 0;
}
| replace | 10 | 11 | 10 | 11 | TLE | |
p03047 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int x, y, z, n;
int count = 0;
cin >> x >> y >> z >> n;
int x_c = 0;
int y_c = 0;
while (true) {
y_c = 0;
if (n - x_c * x < 0)
break;
while (true) {
if (n - x_c * x - y_c * y < 0)
break;
if ((n - x_c * x - y_c * y) % z == 0)
count++;
y_c++;
}
x_c++;
}
cout << count << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
cout << x - y + 1 << endl;
return 0;
}
| replace | 4 | 23 | 4 | 7 | TLE | |
p03047 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int mother = 1, child = 1;
for (int i = n; i >= n - k + 1; i--) {
child *= i;
}
for (int i = 1; i <= k; i++) {
mother *= i;
}
cout << child / mother << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
cout << n - k + 1 << endl;
}
| replace | 6 | 15 | 6 | 7 | 0 | |
p03047 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int a = 1;
int b = 1;
for (int i = 0; i < K; i++) {
a *= N - i;
}
for (int j = 0; j < K; j++) {
b *= K - j;
}
int ans = a / b;
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int ans = N - K + 1;
cout << ans << endl;
return 0;
}
| replace | 10 | 22 | 10 | 11 | 0 | |
p03047 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int main() {
int N = 0, K = 0;
cin >> N >> K;
int P = 1, R = 1;
for (int i = 0; i < N; ++i) {
P = (N - i) * P;
R = (K - i) * R;
}
int ans = P / R;
cout << ans << endl;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
int N = 0, K = 0;
cin >> N >> K;
int P = 1, R = 1;
int ans = N - K + 1;
cout << ans << endl;
return 0;
} | replace | 13 | 19 | 13 | 14 | -8 | |
p03047 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int N, K;
cin >> N >> K;
int pattern = 0;
for (int l = 1; l <= N; l++) {
if (l + (K - 1) <= N) {
pattern++;
}
}
cout << pattern << endl;
return 1;
}
| #include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int N, K;
cin >> N >> K;
int pattern = 0;
for (int l = 1; l <= N; l++) {
if (l + (K - 1) <= N) {
pattern++;
}
}
cout << pattern << endl;
return 0;
}
| replace | 19 | 20 | 19 | 20 | 1 | |
p03047 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<ULL> VULL;
typedef vector<VLL> VVLL;
class MYCP {
public:
static const LL MOD_CONST_ATCODER = (LL)1000 * 1000 * 1000 + 7;
static LL DebugFlag;
// 数値を区切って文字列にする
static string MakeString_LongLong(vector<long long> const &numbers,
string const &str) {
if (numbers.size() == 0)
return "";
string result = "" + to_string(numbers[0]);
for (long long i = 1; i < numbers.size(); i++) {
result += str;
result += to_string(numbers[i]);
}
return result;
}
// 空白で区切る為のオーバーロード
static string MakeString_LongLong(vector<long long> const &numbers) {
if (numbers.size() == 0)
return "";
string result = "" + to_string(numbers[0]);
for (long long i = 1; i < numbers.size(); i++) {
result += " ";
result += to_string(numbers[i]);
}
return result;
}
// 文字列の配列を改行を挟んでまとめる
static string MakeString_VectorString(vector<string> const &str) {
string result = "";
for (long long i = 0; i < str.size(); i++) {
result += str[i] + "\n";
}
return result;
}
// 文字列を必要な個数だけ読み取る
static vector<string> MyReadLineSplit(LL n) {
vector<string> str(n);
for (long long i = 0; i < n; i++) {
std::cin >> str[i];
}
return str;
}
// 数値を必要な個数だけ読み取る
static vector<long long> ReadInts(long long number) {
vector<long long> a(number);
for (int i = 0; i < number; i++) {
std::cin >> a[i];
}
return a;
}
// 渡された自然数が素数ならtureを返す
static bool PrimeCheck_Int(long long number) {
if (number < 2)
return false;
for (ULL i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}
// 渡された数値以下の素数表を作る
static vector<long long> MakePrimeList(long long n) {
vector<long long> list;
LL i, j, p;
bool flag;
for (i = 2; i <= n; i++) {
flag = true;
for (j = 0; j < list.size(); j++) {
if (!(list[j] * list[j] <= i))
break;
if (i % list[j] == 0) {
flag = false;
break;
}
}
if (flag)
list.push_back(i);
}
return list;
}
// 文字列の分割
static vector<string> split(string const &str, char sep) {
vector<std::string> v; // 分割結果を格納するベクター
auto first = str.begin(); // テキストの最初を指すイテレータ
while (first != str.end()) { // テキストが残っている間ループ
auto last = first; // 分割文字列末尾へのイテレータ
while (last != str.end() &&
*last != sep) // 末尾 or セパレータ文字まで進める
last++;
v.push_back(string(first, last)); // 分割文字を出力
if (last != str.end())
last++;
first = last; // 次の処理のためにイテレータを設定
}
return v;
}
// 合計を求める
template <typename T> static LL Sum(T const &a) {
LL sum = 0;
auto itr = a.begin();
while (itr != a.end()) {
sum += (*itr);
itr++;
}
return sum;
}
// 小文字ならtrueを返す
static bool Komoji(char a) {
if (a >= 'a' && a <= 'z')
return true;
return false;
}
// 大文字ならtrueを返す
static bool Oomoji(char a) {
if (a >= 'A' && a <= 'Z')
return true;
return false;
}
// 切り上げの整数値割り算
static LL KiriageWarizan(LL a, LL b) {
LL result = a / b;
if (a % b > 0)
result++;
return result;
}
// 最大公約数を求める
static LL GreatestCommonFactor(LL a, LL b) {
a = abs(a);
b = abs(b);
LL temp;
if (a < b) {
temp = b;
b = a;
a = temp;
}
while (b > 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
// 最小公倍数を求める
static LL LeastCommonMultiple(LL a, LL b) {
return (a / GreatestCommonFactor(a, b)) * b;
}
// 素因数分解、素数、指数の順
static vector<VLL> PrimeFactorization(LL n) {
VLL p_list, s_list;
LL i, j, k, count;
for (i = 2; n > 1; i++) {
if (i * i > n) {
p_list.push_back(n);
s_list.push_back(1);
break;
}
if (n % i == 0) {
count = 0;
while (n % i == 0) {
n /= i;
count++;
}
p_list.push_back(i);
s_list.push_back(count);
}
}
vector<VLL> result;
result.push_back(p_list);
result.push_back(s_list);
return result;
}
static VLL MakeYakusuList(LL n) {
auto primes = MYCP::PrimeFactorization(n);
VLL ans;
VLL roop(primes.size(), 0);
LL i, j, k, m, size = roop.size();
while (true) {
LL a = 1;
for (i = 0; i < size; i++) {
for (j = 0; j <= roop[i]; j++) {
a *= primes[i][0];
}
}
ans.push_back(a);
roop[0]++;
for (i = 0; i < size; i++) {
if (i + 1 < size) {
roop[i + 1] += (roop[i] / (primes[i][1] + 1));
}
roop[i] %= (primes[i][1] + 1);
}
bool flag = true;
for (i = 0; i < size; i++) {
if (roop[i] != 0)
flag = false;
}
if (flag)
break;
}
return MYCP::Sort(ans);
}
// 組み合わせ nCr
static LL Combination(LL n, LL r) {
r = min(r, n - r);
VLL p(n + 1, 0);
LL i, j, k, a, b, c;
for (i = 1; i <= r; i++) {
auto temp = MYCP::PrimeFactorization(i);
for (j = 0; j < temp[0].size(); j++) {
p[temp[0][j]] -= temp[1][j];
}
a = i + n - r;
temp = MYCP::PrimeFactorization(a);
for (j = 0; j < temp[0].size(); j++) {
p[temp[0][j]] += temp[1][j];
}
}
LL result = 1;
for (i = 0; i < p.size(); i++) {
if (p[i] > 0) {
for (j = 0; j < p[i]; j++) {
result *= i;
result %= MYCP::MOD_CONST_ATCODER;
}
}
}
return result;
}
// 符号
static LL sign(LL const x) {
if (x > 0)
return 1;
if (x < 0)
return -1;
return 0;
}
// 円周率
static double PI() { return (double)3.1415926535898; }
// 指定した桁でdoubleを出す
static void CoutDoubleKeta(double a, LL keta) {
cout << setprecision(keta) << a << flush;
}
// コンテナクラスの出力
template <typename T> static T CoutVector(T const &ls) {
LL i, j, k, size = distance(ls.begin(), ls.end());
auto itr = ls.begin();
for (i = 0; i < size - 1; i++) {
cout << *itr << " " << flush;
itr++;
}
cout << *itr << flush;
return ls;
}
// コンテナクラスをソートする
template <typename T> static T Sort(T &ls) {
sort(ls.begin(), ls.end());
return ls;
}
// 順序関数付きでコンテナクラスをソートする
template <typename T, typename F> static T Sort(T &ls, F func) {
sort(ls.begin(), ls.end(), func);
return ls;
}
// コンテナクラスを逆順に並び替える
template <typename T> static T Reverse(T &ls) {
reverse(ls.begin(), ls.end());
return ls;
}
// コンテナクラスの条件を満たす要素を数え上げる。bool func(S x)
template <typename T, typename S> static LL Count(T const &ls, S func) {
LL ans = 0;
auto itr = ls.begin();
while (itr != ls.end()) {
if (func(*itr))
ans++;
itr++;
}
return ans;
}
// コンテナクラスの要素をすべて更新する。S func(S x)
template <typename T, typename S> static T AllUpdate(T &ls, S func) {
auto itr = ls.begin();
while (itr != ls.end()) {
*itr = func(*itr);
itr++;
}
return ls;
}
// デバッグ用出力
template <typename T> static LL DebugPrintf(T output) {
if (MYCP::DebugFlag) {
std::cout << output << endl;
}
return MYCP::DebugFlag;
}
// デバッグ用入力
static LL DebugCin() {
LL a;
if (MYCP::DebugFlag) {
cin >> a;
}
return a;
}
};
LL MYCP::DebugFlag = 0;
// 累積和を求めるクラス
class Ruisekiwa {
private:
vector<LL> list;
public:
void MakeArray(vector<LL> data) {
LL i;
list = data;
list.push_back(0);
list[0] = 0;
for (i = 1; i < list.size(); i++) {
list[i] = list[i - 1] + data[i - 1];
}
}
LL Sum(LL start, LL end) {
if (end < start) {
std::cout << "startがendより大きいです";
return 0;
}
if (start < 0 || end >= list.size()) {
std::cout << "範囲が異常";
return 0;
}
return list[end] - list[start];
}
};
// n進数を管理するクラス
class N_Number {
public:
N_Number(LL n, LL keta) {
this->N_Shinsuu = n;
VLL temp(keta, 0);
this->numbers = temp;
}
// 数を足す
void plus(LL a) {
if (a < 0) {
a *= (-1);
this->minus(a);
return;
}
this->numbers[0] += a;
LL size = this->numbers.size();
for (LL i = 0; i < size; i++) {
if (i + 1 < size) {
this->numbers[i + 1] += this->numbers[i] / this->N_Shinsuu;
}
this->numbers[i] %= this->N_Shinsuu;
}
}
// 全ての桁が同じ数字になっていればその数字を返す。それ以外の場合は -1 を返す
LL check() {
LL a = this->numbers[0];
for (LL i = 0; i < this->numbers.size(); i++) {
if (this->numbers[i] != a)
return -1;
}
return a;
}
LL getNumber(LL keta) { return this->numbers[keta]; }
LL getKeta() { return this->numbers.size(); }
LL getShinsuu() { return this->N_Shinsuu; }
void setNumber(LL keta, LL number) {
if (0 <= number && number < this->getShinsuu()) {
if (0 <= keta && keta < this->getKeta()) {
this->numbers[keta] = number;
return;
}
}
cout << "er" << endl;
}
void setAllNumbers(LL number) {
LL size = this->getKeta(), i;
for (i = 0; i < size; i++) {
this->setNumber(i, number);
}
}
string to_string_KetaSoroe() {
string s = "";
for (LL i = this->getKeta() - 1; i >= 0; i--) {
s += to_string(this->getNumber(i));
}
return s;
}
private:
void minus(LL a) {
LL i, j, k, zettaiti = abs(a);
k = MYCP::KiriageWarizan(zettaiti, this->N_Shinsuu);
j = k * (this->N_Shinsuu - 1);
for (i = 0; i < this->getKeta(); i++) {
this->numbers[i] += j;
}
this->numbers[0] += k - a;
this->plus(0);
}
VLL numbers;
LL N_Shinsuu;
};
// UnionFind
class Union_Find {
private:
VLL tree;
VLL count;
LL root(LL a) {
if (this->tree[a] == a)
return a;
return this->tree[a] = this->root(this->tree[a]);
}
public:
Union_Find(LL n) {
VLL set(n);
this->tree = set;
this->count = set;
for (LL i = 0; i < n; i++) {
this->tree[i] = i;
this->count[i] = 1;
}
}
void unite(LL a, LL b) {
LL x, y;
if (!this->Check(a, b)) {
x = this->getCount(a) + getCount(b);
y = this->root(a);
this->count[y] = x;
y = this->root(b);
this->count[y] = x;
}
x = this->root(a);
y = this->root(b);
this->tree[x] = y;
}
bool Check(LL a, LL b) { return this->root(a) == this->root(b); }
LL getCount(LL index) {
LL temp = this->root(index);
return this->count[temp];
}
};
// プラスマイナス無限に対応したlong long型
class INF_LONG_LONG {
private:
LL inf, n;
public:
// コンストラクタ
INF_LONG_LONG(LL a) {
this->n = a;
this->inf = 0;
this->Syusei();
}
INF_LONG_LONG() { *this = INF_LONG_LONG(0); }
INF_LONG_LONG(INF_LONG_LONG const &a) {
this->n = a.n;
this->inf = a.inf;
this->Syusei();
}
// ゲッター
LL getN() const { return this->n; }
LL getInf() const { return this->inf; }
// 正の無限大生成
static INF_LONG_LONG plus_inf() {
INF_LONG_LONG a;
a.n = 0;
a.inf = 1;
a.Syusei();
return a;
}
// 負の無限大生成
static INF_LONG_LONG minus_inf() {
INF_LONG_LONG a;
a.n = 0;
a.inf = -1;
a.Syusei();
return a;
}
// 符号を取得
LL sign() const {
if (this->inf != 0) {
return this->inf;
}
return MYCP::sign(this->n);
}
// 代入演算子
INF_LONG_LONG operator=(INF_LONG_LONG const &b) {
this->n = b.n;
this->inf = b.inf;
this->Syusei();
return *this;
}
INF_LONG_LONG operator=(LL const &b) {
*this = INF_LONG_LONG(b);
this->Syusei();
return *this;
}
// 比較演算子
bool operator==(INF_LONG_LONG const &b) const {
if (this->n == b.n && this->inf == b.inf)
return true;
return false;
}
bool operator!=(INF_LONG_LONG const &b) const { return !(*this == b); }
bool operator<(INF_LONG_LONG const &b) const {
if (this->inf < b.inf)
return true;
if (this->inf > b.inf)
return false;
return this->n < b.n;
}
bool operator>(INF_LONG_LONG const &b) const { return b < *this; }
bool operator<=(INF_LONG_LONG const &b) const { return !(*this > b); }
bool operator>=(INF_LONG_LONG const &b) const { return !(*this < b); }
// 算術演算子
INF_LONG_LONG operator+(INF_LONG_LONG const &b) const {
if (max(this->inf, b.inf) > 0)
return INF_LONG_LONG::plus_inf();
if (min(this->inf, b.inf) < 0)
return INF_LONG_LONG::minus_inf();
auto ans = *this;
ans.n += b.n;
ans.Syusei();
return ans;
}
INF_LONG_LONG operator*(INF_LONG_LONG const &b) const {
if (*this == INF_LONG_LONG(0) || b == INF_LONG_LONG(0)) {
return INF_LONG_LONG(0);
}
if (this->inf != 0 || b.inf != 0) {
LL s = this->sign() * b.sign();
INF_LONG_LONG ans(0);
ans.n = 0;
ans.inf = s;
ans.Syusei();
return ans;
}
INF_LONG_LONG ans(0);
ans.n = this->n * b.n;
ans.Syusei();
return ans;
}
INF_LONG_LONG operator-(INF_LONG_LONG const &b) const {
auto ans = (*this + (INF_LONG_LONG(-1) * b));
ans.Syusei();
return ans;
}
INF_LONG_LONG operator/(INF_LONG_LONG const &b) const {
if (b == INF_LONG_LONG(0)) {
LL a = this->n / b.n;
return INF_LONG_LONG(a);
}
if (b.inf != 0) {
return INF_LONG_LONG(0);
}
if (*this == INF_LONG_LONG(0)) {
return INF_LONG_LONG(0);
}
if (this->inf != 0) {
LL s = this->sign() * b.sign();
return INF_LONG_LONG::plus_inf() * INF_LONG_LONG(s);
}
INF_LONG_LONG ans;
ans.n = this->n / b.n;
ans.Syusei();
return ans;
}
INF_LONG_LONG operator%(INF_LONG_LONG const &b) const {
if (this->inf == 0 && b.inf == 0) {
INF_LONG_LONG ans;
ans.n = this->n % b.n;
ans.Syusei();
return ans;
}
auto x = *this / b;
x.Syusei();
auto ans = *this - b * x;
ans.Syusei();
return ans;
}
// 複合代入演算子
INF_LONG_LONG operator+=(INF_LONG_LONG const &b) {
auto ans = *this + b;
*this = ans;
return *this;
}
INF_LONG_LONG operator-=(INF_LONG_LONG const &b) {
auto ans = *this - b;
*this = ans;
return *this;
}
INF_LONG_LONG operator*=(INF_LONG_LONG const &b) {
auto ans = *this * b;
*this = ans;
return *this;
}
INF_LONG_LONG operator/=(INF_LONG_LONG const &b) {
auto ans = *this / b;
*this = ans;
return *this;
}
INF_LONG_LONG operator%=(INF_LONG_LONG const &b) {
auto ans = *this % b;
*this = ans;
return *this;
}
// 符号演算子
INF_LONG_LONG operator+() const { return *this; }
INF_LONG_LONG operator-() const { return *this * INF_LONG_LONG(-1); }
// 前置きインクリメント・デクリメント
INF_LONG_LONG operator++() {
this->n++;
this->Syusei();
return *this;
}
INF_LONG_LONG operator--() {
this->n--;
this->Syusei();
return *this;
}
// 後置きインクリメント・デクリメント
INF_LONG_LONG operator++(int) {
auto copy = *this;
++(*this);
return copy;
}
INF_LONG_LONG operator--(int) {
auto copy = *this;
--(*this);
return copy;
}
// 文字列への変換
string ToString() const {
if (this->inf == 1) {
return "+INF";
}
if (this->inf == -1) {
return "-INF";
}
return to_string(this->n);
}
private:
void Syusei() {
if (this->inf != 0) {
this->n = 0;
}
}
};
typedef INF_LONG_LONG ILL_TYPE;
typedef vector<ILL_TYPE> VILL_TYPE;
typedef vector<VILL_TYPE> VVILL_TYPE;
// ワーシャルフロイド
class WarshallFloyd {
public:
// 最短距離を記録
VVILL_TYPE d;
// 頂点数
LL v;
// vは頂点数、edge_cost_listは辺の情報{始点、終点、コスト}の配列。無向グラフの場合、逆矢印の辺に注意。
WarshallFloyd(LL v, VVLL edge_cost_list) {
this->v = v;
this->d = VVILL_TYPE(v, VILL_TYPE(v, ILL_TYPE::plus_inf()));
LL i, j, k, a, b, c;
for (i = 0; i < edge_cost_list.size(); i++) {
a = edge_cost_list[i][0];
b = edge_cost_list[i][1];
c = edge_cost_list[i][2];
this->d[a][b] = ILL_TYPE(c);
}
for (i = 0; i < v; i++) {
this->d[i][i] = ILL_TYPE(0);
}
// ここから計算
for (k = 0; k < v; k++) {
for (i = 0; i < v; i++) {
for (j = 0; j < v; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
};
// ベルマンフォード
class BellmanFord {
public:
// 辺のリスト
VVILL_TYPE edge;
// 頂点数、辺数
LL v, e;
// 始点
LL s;
// 最短距離
VILL_TYPE d;
// vは頂点数、startは始点、edge_cost_listは辺の情報{始点、終点、コスト}の配列。
BellmanFord(LL v, LL start, VVLL edge_cost_list) {
this->v = v;
this->s = start;
this->e = edge_cost_list.size();
this->d = VILL_TYPE(v, ILL_TYPE::plus_inf());
this->d[start] = 0;
LL i, j, k;
for (i = 0; i < this->e; i++) {
VILL_TYPE temp;
LL a, b, c;
a = edge_cost_list[i][0];
b = edge_cost_list[i][1];
c = edge_cost_list[i][2];
temp.push_back(ILL_TYPE(a));
temp.push_back(ILL_TYPE(b));
temp.push_back(ILL_TYPE(c));
this->edge.push_back(temp);
}
this->DoUpdata();
auto cpy = this->d;
this->DoUpdata();
for (i = 0; i < this->d.size(); i++) {
if (this->d[i] != cpy[i]) {
this->d[i] = ILL_TYPE::minus_inf();
}
}
this->DoUpdata();
}
private:
void DoUpdata() {
LL i, j, k;
for (i = 0; i <= this->v; i++) {
bool update = true;
for (j = 0; j < this->e; j++) {
ILL_TYPE c;
LL a, b;
a = this->edge[j][0].getN();
b = this->edge[j][1].getN();
c = this->edge[j][2];
if (this->d[a] < ILL_TYPE::plus_inf()) {
if (this->d[a] + c < this->d[b]) {
update = false;
this->d[b] = this->d[a] + c;
}
}
}
if (update)
break;
}
}
};
// ダイクストラ
class Dijkstra {
public:
Dijkstra(LL v, LL start, VVLL edge_cost_list) {}
};
// ライブラリはここまで
// ここから下を書く
// ここからメイン
int main(void) {
MYCP::DebugFlag = 0;
LL i, j, k, n, m;
cin >> n;
auto ls = MYCP::MakeYakusuList(n);
for (i = 0; i < ls.size() - 1; i++) {
}
return 0;
}
| #include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<ULL> VULL;
typedef vector<VLL> VVLL;
class MYCP {
public:
static const LL MOD_CONST_ATCODER = (LL)1000 * 1000 * 1000 + 7;
static LL DebugFlag;
// 数値を区切って文字列にする
static string MakeString_LongLong(vector<long long> const &numbers,
string const &str) {
if (numbers.size() == 0)
return "";
string result = "" + to_string(numbers[0]);
for (long long i = 1; i < numbers.size(); i++) {
result += str;
result += to_string(numbers[i]);
}
return result;
}
// 空白で区切る為のオーバーロード
static string MakeString_LongLong(vector<long long> const &numbers) {
if (numbers.size() == 0)
return "";
string result = "" + to_string(numbers[0]);
for (long long i = 1; i < numbers.size(); i++) {
result += " ";
result += to_string(numbers[i]);
}
return result;
}
// 文字列の配列を改行を挟んでまとめる
static string MakeString_VectorString(vector<string> const &str) {
string result = "";
for (long long i = 0; i < str.size(); i++) {
result += str[i] + "\n";
}
return result;
}
// 文字列を必要な個数だけ読み取る
static vector<string> MyReadLineSplit(LL n) {
vector<string> str(n);
for (long long i = 0; i < n; i++) {
std::cin >> str[i];
}
return str;
}
// 数値を必要な個数だけ読み取る
static vector<long long> ReadInts(long long number) {
vector<long long> a(number);
for (int i = 0; i < number; i++) {
std::cin >> a[i];
}
return a;
}
// 渡された自然数が素数ならtureを返す
static bool PrimeCheck_Int(long long number) {
if (number < 2)
return false;
for (ULL i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}
// 渡された数値以下の素数表を作る
static vector<long long> MakePrimeList(long long n) {
vector<long long> list;
LL i, j, p;
bool flag;
for (i = 2; i <= n; i++) {
flag = true;
for (j = 0; j < list.size(); j++) {
if (!(list[j] * list[j] <= i))
break;
if (i % list[j] == 0) {
flag = false;
break;
}
}
if (flag)
list.push_back(i);
}
return list;
}
// 文字列の分割
static vector<string> split(string const &str, char sep) {
vector<std::string> v; // 分割結果を格納するベクター
auto first = str.begin(); // テキストの最初を指すイテレータ
while (first != str.end()) { // テキストが残っている間ループ
auto last = first; // 分割文字列末尾へのイテレータ
while (last != str.end() &&
*last != sep) // 末尾 or セパレータ文字まで進める
last++;
v.push_back(string(first, last)); // 分割文字を出力
if (last != str.end())
last++;
first = last; // 次の処理のためにイテレータを設定
}
return v;
}
// 合計を求める
template <typename T> static LL Sum(T const &a) {
LL sum = 0;
auto itr = a.begin();
while (itr != a.end()) {
sum += (*itr);
itr++;
}
return sum;
}
// 小文字ならtrueを返す
static bool Komoji(char a) {
if (a >= 'a' && a <= 'z')
return true;
return false;
}
// 大文字ならtrueを返す
static bool Oomoji(char a) {
if (a >= 'A' && a <= 'Z')
return true;
return false;
}
// 切り上げの整数値割り算
static LL KiriageWarizan(LL a, LL b) {
LL result = a / b;
if (a % b > 0)
result++;
return result;
}
// 最大公約数を求める
static LL GreatestCommonFactor(LL a, LL b) {
a = abs(a);
b = abs(b);
LL temp;
if (a < b) {
temp = b;
b = a;
a = temp;
}
while (b > 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
// 最小公倍数を求める
static LL LeastCommonMultiple(LL a, LL b) {
return (a / GreatestCommonFactor(a, b)) * b;
}
// 素因数分解、素数、指数の順
static vector<VLL> PrimeFactorization(LL n) {
VLL p_list, s_list;
LL i, j, k, count;
for (i = 2; n > 1; i++) {
if (i * i > n) {
p_list.push_back(n);
s_list.push_back(1);
break;
}
if (n % i == 0) {
count = 0;
while (n % i == 0) {
n /= i;
count++;
}
p_list.push_back(i);
s_list.push_back(count);
}
}
vector<VLL> result;
result.push_back(p_list);
result.push_back(s_list);
return result;
}
static VLL MakeYakusuList(LL n) {
auto primes = MYCP::PrimeFactorization(n);
VLL ans;
VLL roop(primes.size(), 0);
LL i, j, k, m, size = roop.size();
while (true) {
LL a = 1;
for (i = 0; i < size; i++) {
for (j = 0; j <= roop[i]; j++) {
a *= primes[i][0];
}
}
ans.push_back(a);
roop[0]++;
for (i = 0; i < size; i++) {
if (i + 1 < size) {
roop[i + 1] += (roop[i] / (primes[i][1] + 1));
}
roop[i] %= (primes[i][1] + 1);
}
bool flag = true;
for (i = 0; i < size; i++) {
if (roop[i] != 0)
flag = false;
}
if (flag)
break;
}
return MYCP::Sort(ans);
}
// 組み合わせ nCr
static LL Combination(LL n, LL r) {
r = min(r, n - r);
VLL p(n + 1, 0);
LL i, j, k, a, b, c;
for (i = 1; i <= r; i++) {
auto temp = MYCP::PrimeFactorization(i);
for (j = 0; j < temp[0].size(); j++) {
p[temp[0][j]] -= temp[1][j];
}
a = i + n - r;
temp = MYCP::PrimeFactorization(a);
for (j = 0; j < temp[0].size(); j++) {
p[temp[0][j]] += temp[1][j];
}
}
LL result = 1;
for (i = 0; i < p.size(); i++) {
if (p[i] > 0) {
for (j = 0; j < p[i]; j++) {
result *= i;
result %= MYCP::MOD_CONST_ATCODER;
}
}
}
return result;
}
// 符号
static LL sign(LL const x) {
if (x > 0)
return 1;
if (x < 0)
return -1;
return 0;
}
// 円周率
static double PI() { return (double)3.1415926535898; }
// 指定した桁でdoubleを出す
static void CoutDoubleKeta(double a, LL keta) {
cout << setprecision(keta) << a << flush;
}
// コンテナクラスの出力
template <typename T> static T CoutVector(T const &ls) {
LL i, j, k, size = distance(ls.begin(), ls.end());
auto itr = ls.begin();
for (i = 0; i < size - 1; i++) {
cout << *itr << " " << flush;
itr++;
}
cout << *itr << flush;
return ls;
}
// コンテナクラスをソートする
template <typename T> static T Sort(T &ls) {
sort(ls.begin(), ls.end());
return ls;
}
// 順序関数付きでコンテナクラスをソートする
template <typename T, typename F> static T Sort(T &ls, F func) {
sort(ls.begin(), ls.end(), func);
return ls;
}
// コンテナクラスを逆順に並び替える
template <typename T> static T Reverse(T &ls) {
reverse(ls.begin(), ls.end());
return ls;
}
// コンテナクラスの条件を満たす要素を数え上げる。bool func(S x)
template <typename T, typename S> static LL Count(T const &ls, S func) {
LL ans = 0;
auto itr = ls.begin();
while (itr != ls.end()) {
if (func(*itr))
ans++;
itr++;
}
return ans;
}
// コンテナクラスの要素をすべて更新する。S func(S x)
template <typename T, typename S> static T AllUpdate(T &ls, S func) {
auto itr = ls.begin();
while (itr != ls.end()) {
*itr = func(*itr);
itr++;
}
return ls;
}
// デバッグ用出力
template <typename T> static LL DebugPrintf(T output) {
if (MYCP::DebugFlag) {
std::cout << output << endl;
}
return MYCP::DebugFlag;
}
// デバッグ用入力
static LL DebugCin() {
LL a;
if (MYCP::DebugFlag) {
cin >> a;
}
return a;
}
};
LL MYCP::DebugFlag = 0;
// 累積和を求めるクラス
class Ruisekiwa {
private:
vector<LL> list;
public:
void MakeArray(vector<LL> data) {
LL i;
list = data;
list.push_back(0);
list[0] = 0;
for (i = 1; i < list.size(); i++) {
list[i] = list[i - 1] + data[i - 1];
}
}
LL Sum(LL start, LL end) {
if (end < start) {
std::cout << "startがendより大きいです";
return 0;
}
if (start < 0 || end >= list.size()) {
std::cout << "範囲が異常";
return 0;
}
return list[end] - list[start];
}
};
// n進数を管理するクラス
class N_Number {
public:
N_Number(LL n, LL keta) {
this->N_Shinsuu = n;
VLL temp(keta, 0);
this->numbers = temp;
}
// 数を足す
void plus(LL a) {
if (a < 0) {
a *= (-1);
this->minus(a);
return;
}
this->numbers[0] += a;
LL size = this->numbers.size();
for (LL i = 0; i < size; i++) {
if (i + 1 < size) {
this->numbers[i + 1] += this->numbers[i] / this->N_Shinsuu;
}
this->numbers[i] %= this->N_Shinsuu;
}
}
// 全ての桁が同じ数字になっていればその数字を返す。それ以外の場合は -1 を返す
LL check() {
LL a = this->numbers[0];
for (LL i = 0; i < this->numbers.size(); i++) {
if (this->numbers[i] != a)
return -1;
}
return a;
}
LL getNumber(LL keta) { return this->numbers[keta]; }
LL getKeta() { return this->numbers.size(); }
LL getShinsuu() { return this->N_Shinsuu; }
void setNumber(LL keta, LL number) {
if (0 <= number && number < this->getShinsuu()) {
if (0 <= keta && keta < this->getKeta()) {
this->numbers[keta] = number;
return;
}
}
cout << "er" << endl;
}
void setAllNumbers(LL number) {
LL size = this->getKeta(), i;
for (i = 0; i < size; i++) {
this->setNumber(i, number);
}
}
string to_string_KetaSoroe() {
string s = "";
for (LL i = this->getKeta() - 1; i >= 0; i--) {
s += to_string(this->getNumber(i));
}
return s;
}
private:
void minus(LL a) {
LL i, j, k, zettaiti = abs(a);
k = MYCP::KiriageWarizan(zettaiti, this->N_Shinsuu);
j = k * (this->N_Shinsuu - 1);
for (i = 0; i < this->getKeta(); i++) {
this->numbers[i] += j;
}
this->numbers[0] += k - a;
this->plus(0);
}
VLL numbers;
LL N_Shinsuu;
};
// UnionFind
class Union_Find {
private:
VLL tree;
VLL count;
LL root(LL a) {
if (this->tree[a] == a)
return a;
return this->tree[a] = this->root(this->tree[a]);
}
public:
Union_Find(LL n) {
VLL set(n);
this->tree = set;
this->count = set;
for (LL i = 0; i < n; i++) {
this->tree[i] = i;
this->count[i] = 1;
}
}
void unite(LL a, LL b) {
LL x, y;
if (!this->Check(a, b)) {
x = this->getCount(a) + getCount(b);
y = this->root(a);
this->count[y] = x;
y = this->root(b);
this->count[y] = x;
}
x = this->root(a);
y = this->root(b);
this->tree[x] = y;
}
bool Check(LL a, LL b) { return this->root(a) == this->root(b); }
LL getCount(LL index) {
LL temp = this->root(index);
return this->count[temp];
}
};
// プラスマイナス無限に対応したlong long型
class INF_LONG_LONG {
private:
LL inf, n;
public:
// コンストラクタ
INF_LONG_LONG(LL a) {
this->n = a;
this->inf = 0;
this->Syusei();
}
INF_LONG_LONG() { *this = INF_LONG_LONG(0); }
INF_LONG_LONG(INF_LONG_LONG const &a) {
this->n = a.n;
this->inf = a.inf;
this->Syusei();
}
// ゲッター
LL getN() const { return this->n; }
LL getInf() const { return this->inf; }
// 正の無限大生成
static INF_LONG_LONG plus_inf() {
INF_LONG_LONG a;
a.n = 0;
a.inf = 1;
a.Syusei();
return a;
}
// 負の無限大生成
static INF_LONG_LONG minus_inf() {
INF_LONG_LONG a;
a.n = 0;
a.inf = -1;
a.Syusei();
return a;
}
// 符号を取得
LL sign() const {
if (this->inf != 0) {
return this->inf;
}
return MYCP::sign(this->n);
}
// 代入演算子
INF_LONG_LONG operator=(INF_LONG_LONG const &b) {
this->n = b.n;
this->inf = b.inf;
this->Syusei();
return *this;
}
INF_LONG_LONG operator=(LL const &b) {
*this = INF_LONG_LONG(b);
this->Syusei();
return *this;
}
// 比較演算子
bool operator==(INF_LONG_LONG const &b) const {
if (this->n == b.n && this->inf == b.inf)
return true;
return false;
}
bool operator!=(INF_LONG_LONG const &b) const { return !(*this == b); }
bool operator<(INF_LONG_LONG const &b) const {
if (this->inf < b.inf)
return true;
if (this->inf > b.inf)
return false;
return this->n < b.n;
}
bool operator>(INF_LONG_LONG const &b) const { return b < *this; }
bool operator<=(INF_LONG_LONG const &b) const { return !(*this > b); }
bool operator>=(INF_LONG_LONG const &b) const { return !(*this < b); }
// 算術演算子
INF_LONG_LONG operator+(INF_LONG_LONG const &b) const {
if (max(this->inf, b.inf) > 0)
return INF_LONG_LONG::plus_inf();
if (min(this->inf, b.inf) < 0)
return INF_LONG_LONG::minus_inf();
auto ans = *this;
ans.n += b.n;
ans.Syusei();
return ans;
}
INF_LONG_LONG operator*(INF_LONG_LONG const &b) const {
if (*this == INF_LONG_LONG(0) || b == INF_LONG_LONG(0)) {
return INF_LONG_LONG(0);
}
if (this->inf != 0 || b.inf != 0) {
LL s = this->sign() * b.sign();
INF_LONG_LONG ans(0);
ans.n = 0;
ans.inf = s;
ans.Syusei();
return ans;
}
INF_LONG_LONG ans(0);
ans.n = this->n * b.n;
ans.Syusei();
return ans;
}
INF_LONG_LONG operator-(INF_LONG_LONG const &b) const {
auto ans = (*this + (INF_LONG_LONG(-1) * b));
ans.Syusei();
return ans;
}
INF_LONG_LONG operator/(INF_LONG_LONG const &b) const {
if (b == INF_LONG_LONG(0)) {
LL a = this->n / b.n;
return INF_LONG_LONG(a);
}
if (b.inf != 0) {
return INF_LONG_LONG(0);
}
if (*this == INF_LONG_LONG(0)) {
return INF_LONG_LONG(0);
}
if (this->inf != 0) {
LL s = this->sign() * b.sign();
return INF_LONG_LONG::plus_inf() * INF_LONG_LONG(s);
}
INF_LONG_LONG ans;
ans.n = this->n / b.n;
ans.Syusei();
return ans;
}
INF_LONG_LONG operator%(INF_LONG_LONG const &b) const {
if (this->inf == 0 && b.inf == 0) {
INF_LONG_LONG ans;
ans.n = this->n % b.n;
ans.Syusei();
return ans;
}
auto x = *this / b;
x.Syusei();
auto ans = *this - b * x;
ans.Syusei();
return ans;
}
// 複合代入演算子
INF_LONG_LONG operator+=(INF_LONG_LONG const &b) {
auto ans = *this + b;
*this = ans;
return *this;
}
INF_LONG_LONG operator-=(INF_LONG_LONG const &b) {
auto ans = *this - b;
*this = ans;
return *this;
}
INF_LONG_LONG operator*=(INF_LONG_LONG const &b) {
auto ans = *this * b;
*this = ans;
return *this;
}
INF_LONG_LONG operator/=(INF_LONG_LONG const &b) {
auto ans = *this / b;
*this = ans;
return *this;
}
INF_LONG_LONG operator%=(INF_LONG_LONG const &b) {
auto ans = *this % b;
*this = ans;
return *this;
}
// 符号演算子
INF_LONG_LONG operator+() const { return *this; }
INF_LONG_LONG operator-() const { return *this * INF_LONG_LONG(-1); }
// 前置きインクリメント・デクリメント
INF_LONG_LONG operator++() {
this->n++;
this->Syusei();
return *this;
}
INF_LONG_LONG operator--() {
this->n--;
this->Syusei();
return *this;
}
// 後置きインクリメント・デクリメント
INF_LONG_LONG operator++(int) {
auto copy = *this;
++(*this);
return copy;
}
INF_LONG_LONG operator--(int) {
auto copy = *this;
--(*this);
return copy;
}
// 文字列への変換
string ToString() const {
if (this->inf == 1) {
return "+INF";
}
if (this->inf == -1) {
return "-INF";
}
return to_string(this->n);
}
private:
void Syusei() {
if (this->inf != 0) {
this->n = 0;
}
}
};
typedef INF_LONG_LONG ILL_TYPE;
typedef vector<ILL_TYPE> VILL_TYPE;
typedef vector<VILL_TYPE> VVILL_TYPE;
// ワーシャルフロイド
class WarshallFloyd {
public:
// 最短距離を記録
VVILL_TYPE d;
// 頂点数
LL v;
// vは頂点数、edge_cost_listは辺の情報{始点、終点、コスト}の配列。無向グラフの場合、逆矢印の辺に注意。
WarshallFloyd(LL v, VVLL edge_cost_list) {
this->v = v;
this->d = VVILL_TYPE(v, VILL_TYPE(v, ILL_TYPE::plus_inf()));
LL i, j, k, a, b, c;
for (i = 0; i < edge_cost_list.size(); i++) {
a = edge_cost_list[i][0];
b = edge_cost_list[i][1];
c = edge_cost_list[i][2];
this->d[a][b] = ILL_TYPE(c);
}
for (i = 0; i < v; i++) {
this->d[i][i] = ILL_TYPE(0);
}
// ここから計算
for (k = 0; k < v; k++) {
for (i = 0; i < v; i++) {
for (j = 0; j < v; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
};
// ベルマンフォード
class BellmanFord {
public:
// 辺のリスト
VVILL_TYPE edge;
// 頂点数、辺数
LL v, e;
// 始点
LL s;
// 最短距離
VILL_TYPE d;
// vは頂点数、startは始点、edge_cost_listは辺の情報{始点、終点、コスト}の配列。
BellmanFord(LL v, LL start, VVLL edge_cost_list) {
this->v = v;
this->s = start;
this->e = edge_cost_list.size();
this->d = VILL_TYPE(v, ILL_TYPE::plus_inf());
this->d[start] = 0;
LL i, j, k;
for (i = 0; i < this->e; i++) {
VILL_TYPE temp;
LL a, b, c;
a = edge_cost_list[i][0];
b = edge_cost_list[i][1];
c = edge_cost_list[i][2];
temp.push_back(ILL_TYPE(a));
temp.push_back(ILL_TYPE(b));
temp.push_back(ILL_TYPE(c));
this->edge.push_back(temp);
}
this->DoUpdata();
auto cpy = this->d;
this->DoUpdata();
for (i = 0; i < this->d.size(); i++) {
if (this->d[i] != cpy[i]) {
this->d[i] = ILL_TYPE::minus_inf();
}
}
this->DoUpdata();
}
private:
void DoUpdata() {
LL i, j, k;
for (i = 0; i <= this->v; i++) {
bool update = true;
for (j = 0; j < this->e; j++) {
ILL_TYPE c;
LL a, b;
a = this->edge[j][0].getN();
b = this->edge[j][1].getN();
c = this->edge[j][2];
if (this->d[a] < ILL_TYPE::plus_inf()) {
if (this->d[a] + c < this->d[b]) {
update = false;
this->d[b] = this->d[a] + c;
}
}
}
if (update)
break;
}
}
};
// ダイクストラ
class Dijkstra {
public:
Dijkstra(LL v, LL start, VVLL edge_cost_list) {}
};
// ライブラリはここまで
// ここから下を書く
// ここからメイン
int main(void) {
MYCP::DebugFlag = 0;
LL i, j, k, n, m;
cin >> n >> k;
cout << n - k + 1 << endl;
return 0;
}
| replace | 1,016 | 1,021 | 1,016 | 1,018 | 0 | |
p03047 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int, int>
#define V vector
#define pb push_back
#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...);
}
const int N = 100005;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#endif
// int t;cin>>t;while(t--)
{
int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
cin >> n >> k;
cout << n - k + 1;
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int, int>
#define V vector
#define pb push_back
#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...);
}
const int N = 100005;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// int t;cin>>t;while(t--)
{
int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
cin >> n >> k;
cout << n - k + 1;
}
} | delete | 29 | 34 | 29 | 29 | 0 | |
p03047 | Python | Runtime Error | N, K = map(int, input())
print(N - (K - 1))
| N, K = map(int, input().split())
print(N - (K - 1))
| replace | 0 | 1 | 0 | 1 | ValueError: invalid literal for int() with base 10: ' ' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03047/Python/s207697669.py", line 1, in <module>
N, K = map(int, input())
ValueError: invalid literal for int() with base 10: ' '
|
p03047 | C++ | Runtime Error | #include "stdio.h"
int main() {
int K, N;
scanf("%d %d", N, K);
printf("%d", N - K + 1);
return 0;
} | #include "stdio.h"
int main() {
int K, N, a;
scanf("%d %d", &N, &K);
a = N - K + 1;
printf("%d", a);
return 0;
} | replace | 2 | 5 | 2 | 6 | -11 | |
p03048 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#define ll long long
#define endl '\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, a, b) for (int i = int(a); i < int(b); i++)
#define pb emplace_back
#define all(x) (x).begin(), (x).end()
int mod = 1e9 + 7;
int mod2 = 998244353;
const int INF = 1e9;
signed main() {
ll a, b, c, n;
cin >> a >> b >> c >> n;
int ans = 0;
for (int i = 0; i * a <= n; i++) {
for (int j = 0; i * a + j * b <= n; j++) {
for (int k = 0; i * a + b * j + k * c <= n; k++) {
if (i * a + b * j + k * c == n)
ans++;
}
}
}
cout << ans << endl;
}
// `.`..`(WNNNNNNMNWWWH9rttwgHM8OtttwwVtttrrtrttrw0rtrtwrrrtrZUAOOrrrXWppHMHyZHpWHMHMkWWVHMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// `.`.`..`(WMNMHfWH9ZtrtAdNM8ttttOwVtrrtrrtrrrrrdrrttrZkOrrrrrrXWyrtrrZWWWWMmwXWppWHHMmWHkWMMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// .`.```_..(HMfWH9OtttwXWHBrttrOw0rttrOvrrrtrZrrRrtrrrtZHwrrrrrwwZHyrrrrZWWpWMmwWppkWHHMkHNkVWMNNNNN#NN#NN#NN#NN#NN#NN#NN#NN#NN#NNNN
// .``..
// ..HNfWW9wtrrOdWHM0trttOdSrtrtwwrtrtrw0twSrtOrrrrZHwtrrrrrXwdWwrrrrXHppWNkXWpkvWH@MkHMHkWMMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// `.`.
// -jMWWXXwtrttO0d@BrtrttwXVrtrrwVrrrtrtXrrd0rrXrrrrrZWyrrrrrrZXwWyZOrrvWWpWMkZHbkwrdHMMKMMNHkHMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// `.`.
// v8Xdk9ttttrOwXM0tttrtwXwwrtrw0rrtrrtwKrrd0rrXrtrrrrZHkwrrrrrrXkVkrrrvrZHWpHHkWkwrwvVHHNWMMMMkWMNNNNNNN#NN#NN#NN#NN#NN#NN#NNNN
// ..
// JHZdbKOrrrtrtwWWOOttrOwSzVrOrw0rrttrrrdRrrdZrrdOrtrtrrZHkXwrrrrrZWXHwwrrrvWWWWNkWkvrrrwWM@RMMMNNmWMNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// .
// .JGXHStttttrtwW9OrtrrrdSX0rtrdStrtrtrrwHVrrdkrrtSrrrrrrrdMyXrrrrrvXWXWmwvrrvdHWWNkWyvvvvvT@MNWM#N#MNWMMNNNNNNNNNNNNNNNNNNNNNNNNN
// (.zqHXZtrttrrOwU0trttrOdWX0OtOdRrrtrrrrrdNrrrdkrrrXOwrrrrrrdNySrrrrrwXXkUmvvwvrZHWfNkUkvwvvvdMMMRM#MMNNNkMMNNNNNN#NN#NN#NN#NN#NNNN
// (wdS0OttrtOOrwSrtttrrtwKukrrrwKrrrrrtrrwW@rrrXKrrrZkr0OrrrrrWNwkrvvrrvzXkXHwvvrvwWVVHkUyrvvvvZHMMNWMMNNNNNkWMNNNNNNNNNNNNNNNNNNNNN
// wWtrrttrrOwrwStrttrtrwWZXOrtwX0rrrttrrrXMSrrrXWrrrrXrwvrrrrvZHHXkrrrrvrzWkXNyvrvvvWWWHHWwvvvvvwHNMHRM#NNNNNMmWMMNNNNNNNNNNNNNNNNNN
// 8OtttrwZwwtwSrrttrrrwdyXwtrrdWrrtrrrrrdWH0rrrXWOrrrwRz0rrvrrrdMHWwvvrrvvXWkWHyrvvvrWWWHkHvvvvvvwWMMMNWMMNNNNNMHWMMNMNNNN#NN#NNNNNN
// wttrrdVrZrwWwtOrrrrwdWX0rrtdH0rrrtrrtwWHHrrrrXWRrrvrXXXrrvrrrvXHkSwvvrvrrXWkVHyvvvvwHVWHWRvvvvvvdHdMMMKMNNNNNNNMNkWMNNNNNNNNNNNNNM
// ttrrd0rwrOX0rrtrrtwXHXSrwrwHKrtrtrrtrdWH#rrrrdpkrrrrvHwkwvrrvrZWHXkzzvvvvwUWkpNwvrvvZHfVNHwvvvvvzXHWMNMNMNNNNNNNN#NkWMNNNNNNNNNM3>
// rrrdSrwwtXSrrtrrrwXWWKwrrrdHXrrrrrrrwWWH#vrrrvWpvrrvrdRXwvrvrvvXHWWwkvvrvvwWWKWHzwvvvXHVWWHvzzzzvzWRHMMMNHMNNNNNNNNMNkMMNNNNNN@<<>
// OOd9rw0rdKOtrtrrruXHW0rrrdH#rrrrrrrrdpWp#rrrrrXWkvrvvrWwkrvrrvwwWHpKdvvrrvvXHHfWmdwvzwWHVHWkzzzzzzwWXHMHHNWMNNNNNNNNNMNkMNNNN#z>>>
// OdSOwSwwW0rrrrtrwXNW8rrtrWWSrtrrrrrrXpHWHvrrrrXpHrrrvwZHdyvvvvvvXHpHwkvvvvvrXHHfNwXvzzwWWWKHzzzzzzzWSWHHHMMKMNNNNNNNNNNMNKMNN#>>;>
// wWwOXrwX8rtrrrrwWWpHrrrrdNNrrrrrrrrwppNWHrvrvrypWwvvrrvXKWvrvrvvZXHHkXzvvvvvwWWWWNdkvvzXHfHWkXzzzzzwWXHHHHNMHMNNNNNNNNNNNNNWMN+>>>
// R0rd0rdKrrtrtrrdWHWSrrrwWWDrrrrrrrrXpWHqWvrrrvdWbRvvrvvwWZkvvvrvvXHbHzRvvvvvvdHHpHkWwvzXWHWHHzuzzzzzXXWHHHMHMNHMNNNNNNNNNNNMNWh<>>
// wOwSrwXSrtrtrrwHWWMwrrrdNWSrrrrrrrvppWpHprrvrvddWHwvvrrvXHXwvvvrvzXWHkXzvvvvvzWWKWNdRvzzXWWHHXzzzzzzzudWHbH@HNNWMNNNNNNNNNNNNNkHe>
// rOXwwwKrrrrrrrXWHHDrrwrdHHXwrrrrrrwpWWbHWvvvrvwHHpHrvvvrXWHWwvvrwwXHkHdkvzvvvvZHHpHKWzzzwWHWHRzzzzuzzzzWHHkHHMMNWMNNNNNNNNNNNNNNKm
// rdX0OXSrrtrrrdHHWMXrrrwHWHrrrrrrrrXppqWHWkrrvvwWMHHXwwvvvZWKRvvvvrWHWHwWvzvzzzzXWKWNXRzzwXWWNHzzzzzzzzzXUHkHWMMHNWMNNNNNNNNNNNNNNN
// wSwww#rrrrrrwWWHM#vrrrd#WKvrrrrrvvXppmWMWkvvvvwXNNpNvSXzzvdHkkvvzvdWWWRXwzzzzzzdNNpMkHzzzWVHHpXzzzzzzzzdkHkHHHNMHNkMNNNNNNNNNNNNNN
// XwSwWwrrrrrrdHWWMKrvrvWHWSrvrvvvvwpppHW#HRwUUUWWMMMMMHHHHmmXHWwvvzwWHbNwRzzzzzzzWWWWHWkzzdXWWWRzzzzzzzuzWWbHHWMNMMNkMNNNNNNNNNNNNN
// 8drd#rrrrrrwWWkHMmXmywMfWXrvrrrvrwppWNWMNWvrvvwvW#MNHmdkrvvXHMMmmwzdWpM0WzzzzzzzdHHWNUHzzzWWmHHzzuzzuzuzWXbbNkHMqMHNkHNNNNNNNNNNNN
// dSwWSrrrrrrdHHHMHrrrwWMfWvvvrvrrrdppWNW#WWRvvrvvdM?WNHkXkzvvdWMHXHNmHHHkfwzzzzzzXHHpMWHkzuXWHNWXzzuzzuzzXXkkHbHMHMMMMkMNNNNNNNNNNN
// Xrd#wrrrrrrXpHWH#rrrvdMWWvrrvrvvrdppWHW#(NWwvvvvwMr~TNHmdkvzvXWMKzzzWMHKXXzzzzzzuWHbHkWKuzdWWHWkzzzuzzuuXXHkHkgHk@H@HHkMNNNNNNNNNN
// RvWDrrrrrrwHWkMW#rvvrMMfWwrvrrvvrdppWHW@~?HHuvvrvdb_~?NWkdkzzzUWMkzuuWWHWXuzwzzuzXHHHKWHuzzWW@WRuzuzzzzzduWbHHgMH@HHHMMHMNNNNNNNNN
// rdHwrrrrrrXHHWNW#vvvdMMHWkvrvrrvrdppWbWD~~?XKkrrrwMc~~~THHZHwzzWHNkvzwMRdHHzuzzzzXHHWHXNkuzdWHkWuzzuzuzzXzWkHHHMHHHkH@MMNWMNNNNNNN
// rdHvrrrrrrWkHHHW#rrrd#dNWKrvrvvrvwbpWWW$...vVkkOrtXb~~~~(WHkWkvwWWMkzzWRXkSWXzzzuwmNWHXHRuuwHMKWzzuzzzuzXzWHWHHMHHHkkMMMHHWNNNNNNN
// wW#rrvrrrwHWHMpWNrvrd@(NWWvrrrrrvvWpWXH$..._?dkkttOWl~~~~~?HHdHXXbWMkzUHXWkzzzzzzuHNk#XHHzzukHHXzuzzzuzuXuXqkMHMHMbbkHHH@HNWMNNNNN
// dH#rrrvrrdNWWNpHHwrrd@_WHWkrvvrrrrZpWXWr...__?WkWyOvN~~~~~__7HkWkbbkNkzXHZkzzzuzzzWHW#ugbXuzWWHXXuzzuzuuuzXkkHHH@MbkHMHMHMMNWMNNNN
// X@KrrvrvrXNHWHpHHHrrdb~(NWHrrrrrrrrXWXWr..`...-7HZkzvn~~~~~~~_TNyHHkHMyzXkXuzzzzuuWHW#uHkkzzWHHXuzzzzuzzuuXHkHHHMNbkHHHHHHMMNWMNNN
// WHKvvrvrvXHNMbpHmHkOwb..?HWRrwOrtrtrWKWr`.`.`..._?6dkJn~~~~~~-_~7NHMHbNkuUkzzuzzzuWHW#uHbRzuXHHXzzuuuuzuuzXHkHHMMbkkHkMkkkMMMNUMMN
// WHKrrvrrvXqHMpfHHHWkrb...vkHyOrtttttdNXr.`.J>~-((J+ggQkHXHHHmaJz7&JWHMMWkuHXzuuuzzWgHHwHkKuzXMKXuzuuzuzzuzdkHHHHHkHWMHHbkWHMMMNXM#
// Wg#rvrvrrXmHNpfHHHMMRd-.._4kfyttttllOHWr``
// <udT"""7"""""TMHHMNHHMNgv4HMRXHXHwuzzzuWHHKXHkHzzXMSWzuzzuzuuzudbkHHHHkkHqHuWkWHHMMMNWM
// WHNrvrvvvXHmNppgg@MMMHr..` UkWOlllllldW$..?! ` `
// ?@MMHHMM#NMMmMkXXWkHuzuzuWNHSXHHHuuXNSWuzuuzuzuzuWkHNgMkkkMHSuWkqmqHHMNNK
// WHMkvvrvrXHHMppHHHMNMTN_``` 4kWOz====vXP` ` `
// ``.HggmgM@MH###NKkkuXWHzzuXHHMzdMHHuzWNXKzuuzuzuzuuHkHHNMkkHHBuzWkHHHHuHMMM
// WHHNzvvrvwHMMppHMMMM>.#L`````?kyy====?0S`` ` `
// ``...dmqqqmmHHHMMM#WRSzuzWHuzXHq#uWHHHzuWHXKzuzzuzuzuXHkHWMNkWM#uzuWHHHHSuuWM#
// WM@MkwvvrzXMMHpgHMMM[.MN.`````(HXy1???zd.`` ` ` `
// dHkkqkkqkHHHHHHMMWWXzzuzwWwXHHKXMkHHuXMRXSuuuzuuzuzXHkHdHHWHHXuuwWHqHHuXzXWM
// XHHMNyrvvwdW@HpHWMHML MM[` ` `.4kGz<<>Or``` `
// `dWpbbbbbHMMbbbHHMXXuzzuzuzXWHHSXMkHHuXMSXuzzuzzuzuzWkkHWMHMSwuzzXHkHHXuzuWd#
// dHHH@NyzwwwWHMHHWMMMR dW]`` ` ```(HZx<<<4.```` `` ``
// OZppfppppWfpWpK9NKXuuzuuzuXWHHudMqNHXWNuHuzzuzuzuzuHkHXMMBuuuzuuWkHHUuuuXSMS
// wWHMMMMkwwvdNpHHHM#MH-jWP``` `` ```7kI<:<h.``````` ``
// ,2OWVVyVVyWWWWIdMKWzuuuzzuXHHSwHHHHKXHHXSuuzuuzuzuXHHHXMXuuuuuuXHHHuzuuXSX#X
// wdWHMMMMmwXwHbpMNHMM@b.hb```````````
// 7x1_(>_```````````(x7UUUV0XwvCI1dMWKXuzzuzuWHHudMkgkWbMSXuzzzuzzuzuWkHSW#zXwXuXXWHHuuXXuWXWSX
// vvXWHMMHWNkvWHbbMWMMMN_JF.```` ``
// `````?1_(_.`.`........(<777=<<<><<+dMXUuzuuuuWWMRwMHWMkWHHXSuuzuzuzzuXqHHXMHUXuXXHHHSuuwuXWud8uH
// mzzXWH#zHbHkXNbbWMMMMD~(_```````````````
// .~_`.``.`.`.....___(<~(<<(<<dHkkuuuuuXkHHuWMkHkkkHSXXzuuzuuuuzXHHSd#uuXXWHHXuuuZuXSuqHudS
// UHzzXWSXXWbbHHHbWHMH#<~...```` ``````````````.```.`
// -_____<~~_<_<~~(jMWSuzuzuXWHMHXMHHHkkH#XSuzzuzuzuzuKkHXMQkWHHWUXuXXUuXUuXHuXBu
// .(Smd8wSvXbbbbHHHWHW3___.``````` ``
// ``````.```.```.`...._.._~~..~_~~(#WuzuuzXXkHMqMMHMkkkMUXuXzuuwuuzuXkHSWMWUUuuuXXUUuXHXXWSuXHXu
// .. JKwSvwzWkkkkHHHH5~~..```````````````````.```.````````.`
// .........dWSuuzzzdkHMW@MHMkkkHHXXuzuuXSuzuuWWHd#uuuXXkWSuXXWUXXHUXWHuuu
// .`(HXSzzzzwWbkWHNHr_...``````````````````.```````.```````.`.`..`.-.(HKuzzuuwHHMNMHHMkkkHHuZuuzuuXuuuuXkHWMHHHUUuXXXWHXXHHUuXW8uuuu
// .-MXWwzzzuzXkHHMHHN-.``````````
// ````````````.`.````.``.```.`.`....-dXUXuzuXHWHMMHHBWHMMMSZuzuuzXXzuuXHHMHMHkHHHHHHHWHHSuXkHWSuuuuX
// (#XKzzzzzuzuWHHkWHHp
// ``.````````````````````````.````````.....-((-(NSXuzzXHW@D:~`(/1-
// .kXuzuuzXSuuzXXkHWMHHHHHHHHMWuuuQXUXWuuuXuXH
// MXWzuzzzzzuXHHbHNMMMt`.```````````````````````````.`.` ..-J7"!``
// XKuzzuXHHM9;/<-.J-u!JXXuuzuXWuuuuXHHH#WMNHmkkkuXXkWUXXWXXuuuXqHH
// XWzzzzuzzwXHHHH#"````<.````````````````````.``.`...-c"7``` ` `
// `(BzuuuXmHH8<!(- ?_!`jWUuzuuuWuuuzXWHNH> ..7WHHqHmQkWWXXuuuuuXWqHH
// SwuzzzXuwXHNHY`` `` <,`.`````````````` `` ..J7=``` ` ` ``
// ````.#zuzXXHHH$~`` ```(_(HSuuuzuXSuzuuXHHWCi(` ._ ?WggHHHHWkkkkWHHHHMH
// zzzuzuzwWHMY!` ` ` `(&.```````````` ..J7^ `` ` ` ` ` `. .HXuuXWHW#!` `
// ```` (WUuzzuXXSzuuudHHXY$. : (..``.THHkqkHHHgHHMHkkk zzzzzzXHWK`` ` `` ` ` `
// TJ.``````..J7=```` ` ` ` ` `` ` `.8zuXXHHW=` ``
// ..(XSuuuuuXWuuuuXHWH5~~` .`_?_,-``.TMMMMMMHkkqqHHH zzuuzdHHkN ` ` ` `` `
// ``(Wm,.(J7!``` ` ``` `` `` ` ` ``` .SzuXWHHY ``
// .._~<~~(dSuuuzzXHuuuuXHWHD::?C._.( -`(!_1 ,WHqHHHmqHHHHN zuzXXHHkHM;` ` ` `
// `` ` .;jY= ` .``_`` ` ` ` ` `` ` `.dXuXWm@^
// .._~_.~~~~~(d0uuzuuXHUuuuXHWH5:::::::_.<&._4r?_.;WMMM@MMY"<!` zzXHHHHMHMb``
// `` ` `` `` .J! ` ..`_~ ` ` ` `` ` ` ``.JXXXHY!
// .__.....~.~~~-d0wuzuXXHSuuuXWWH$::::;:::;:+- ~`(.i +HqHHkkHL..`. XWHHHMMHMHM;
// ` ` ` ` ._`` ...(:.=_` ` ` ` ` ````
// .-dVT7^``````......~~~~~_jWuuuuuXHSuzuXHHH3<;:::::;;j<+z ``` -dHNkkMqkkN..``
// HHHMMHHHHNMN.`` ` ` `!`z(. ~..~ ! . ` `` ` ` `
// ..gHr``````````........~~~~_jWuuuuuXkSuuuXHWHC:;::;;:::+v>:((J<!(WHHMkkHNqkM;`..
// HMMkkHHHHM9?b `` ` ` `` .``. ` ` ` ` ``` 4HkkkH,
// ``````.`......~~~~_+KuuuzuXHSuuuXHHK<(::::::<;<J+g9>::;< .4NkkqMHqH]..`
// MHkHHHMM$<<:?L` `` ` ` ` ``.` ` !`` ` ` ` ` `
// ?HbkkHx``````.`..`...~~~~_jKuuuuuXHSuuuqMHD<:<;:<++ddNMB3:;::;:<`
// `,HkkqH@HH].`` kkHHkHY``` (;?h`` ` ` `` ```` ` ` ` ` ` ` ` `
// ```.4XWbHx```..`......~~~~_gKuuuuXWH0uuuXHH3:;++1z<jgMB3<::::::::~`
// `jMkqkHMHHF.`. kHHHH=` . ` _<?L ` ` ` ` ` ` ` ` ` ` ` ` ` ` ?kwWHp
// .`.......~_~~(dKuuuuXWHUuuXWH9++z1<;+jWB3::::;;;::;:;_`` MNkqkqMNMF..` HHk@!.
// ` ` ` ~?L ` ` `` ` `` `` ` ` ` ` ` ` ` `
// -WuXHh..`.....~~~~(dWuuuXXkHUuuXHH9>:<:++YY<:;::::;:::::;:::+.``?HHkHHHM>`.`
| #include "bits/stdc++.h"
using namespace std;
#define ll long long
#define endl '\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, a, b) for (int i = int(a); i < int(b); i++)
#define pb emplace_back
#define all(x) (x).begin(), (x).end()
int mod = 1e9 + 7;
int mod2 = 998244353;
const int INF = 1e9;
signed main() {
ll a, b, c, n;
cin >> a >> b >> c >> n;
int ans = 0;
for (int i = 0; i * a <= n; i++) {
for (int j = 0; i * a + j * b <= n; j++) {
if ((n - (i * a + j * b)) % c == 0)
ans++;
}
}
cout << ans << endl;
}
// `.`..`(WNNNNNNMNWWWH9rttwgHM8OtttwwVtttrrtrttrw0rtrtwrrrtrZUAOOrrrXWppHMHyZHpWHMHMkWWVHMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// `.`.`..`(WMNMHfWH9ZtrtAdNM8ttttOwVtrrtrrtrrrrrdrrttrZkOrrrrrrXWyrtrrZWWWWMmwXWppWHHMmWHkWMMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// .`.```_..(HMfWH9OtttwXWHBrttrOw0rttrOvrrrtrZrrRrtrrrtZHwrrrrrwwZHyrrrrZWWpWMmwWppkWHHMkHNkVWMNNNNN#NN#NN#NN#NN#NN#NN#NN#NN#NN#NNNN
// .``..
// ..HNfWW9wtrrOdWHM0trttOdSrtrtwwrtrtrw0twSrtOrrrrZHwtrrrrrXwdWwrrrrXHppWNkXWpkvWH@MkHMHkWMMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// `.`.
// -jMWWXXwtrttO0d@BrtrttwXVrtrrwVrrrtrtXrrd0rrXrrrrrZWyrrrrrrZXwWyZOrrvWWpWMkZHbkwrdHMMKMMNHkHMNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// `.`.
// v8Xdk9ttttrOwXM0tttrtwXwwrtrw0rrtrrtwKrrd0rrXrtrrrrZHkwrrrrrrXkVkrrrvrZHWpHHkWkwrwvVHHNWMMMMkWMNNNNNNN#NN#NN#NN#NN#NN#NN#NNNN
// ..
// JHZdbKOrrrtrtwWWOOttrOwSzVrOrw0rrttrrrdRrrdZrrdOrtrtrrZHkXwrrrrrZWXHwwrrrvWWWWNkWkvrrrwWM@RMMMNNmWMNNNNNNNNNNNNNNNNNNNNNNNNNNNN
// .
// .JGXHStttttrtwW9OrtrrrdSX0rtrdStrtrtrrwHVrrdkrrtSrrrrrrrdMyXrrrrrvXWXWmwvrrvdHWWNkWyvvvvvT@MNWM#N#MNWMMNNNNNNNNNNNNNNNNNNNNNNNNN
// (.zqHXZtrttrrOwU0trttrOdWX0OtOdRrrtrrrrrdNrrrdkrrrXOwrrrrrrdNySrrrrrwXXkUmvvwvrZHWfNkUkvwvvvdMMMRM#MMNNNkMMNNNNNN#NN#NN#NN#NN#NNNN
// (wdS0OttrtOOrwSrtttrrtwKukrrrwKrrrrrtrrwW@rrrXKrrrZkr0OrrrrrWNwkrvvrrvzXkXHwvvrvwWVVHkUyrvvvvZHMMNWMMNNNNNkWMNNNNNNNNNNNNNNNNNNNNN
// wWtrrttrrOwrwStrttrtrwWZXOrtwX0rrrttrrrXMSrrrXWrrrrXrwvrrrrvZHHXkrrrrvrzWkXNyvrvvvWWWHHWwvvvvvwHNMHRM#NNNNNMmWMMNNNNNNNNNNNNNNNNNN
// 8OtttrwZwwtwSrrttrrrwdyXwtrrdWrrtrrrrrdWH0rrrXWOrrrwRz0rrvrrrdMHWwvvrrvvXWkWHyrvvvrWWWHkHvvvvvvwWMMMNWMMNNNNNMHWMMNMNNNN#NN#NNNNNN
// wttrrdVrZrwWwtOrrrrwdWX0rrtdH0rrrtrrtwWHHrrrrXWRrrvrXXXrrvrrrvXHkSwvvrvrrXWkVHyvvvvwHVWHWRvvvvvvdHdMMMKMNNNNNNNMNkWMNNNNNNNNNNNNNM
// ttrrd0rwrOX0rrtrrtwXHXSrwrwHKrtrtrrtrdWH#rrrrdpkrrrrvHwkwvrrvrZWHXkzzvvvvwUWkpNwvrvvZHfVNHwvvvvvzXHWMNMNMNNNNNNNN#NkWMNNNNNNNNNM3>
// rrrdSrwwtXSrrtrrrwXWWKwrrrdHXrrrrrrrwWWH#vrrrvWpvrrvrdRXwvrvrvvXHWWwkvvrvvwWWKWHzwvvvXHVWWHvzzzzvzWRHMMMNHMNNNNNNNNMNkMMNNNNNN@<<>
// OOd9rw0rdKOtrtrrruXHW0rrrdH#rrrrrrrrdpWp#rrrrrXWkvrvvrWwkrvrrvwwWHpKdvvrrvvXHHfWmdwvzwWHVHWkzzzzzzwWXHMHHNWMNNNNNNNNNMNkMNNNN#z>>>
// OdSOwSwwW0rrrrtrwXNW8rrtrWWSrtrrrrrrXpHWHvrrrrXpHrrrvwZHdyvvvvvvXHpHwkvvvvvrXHHfNwXvzzwWWWKHzzzzzzzWSWHHHMMKMNNNNNNNNNNMNKMNN#>>;>
// wWwOXrwX8rtrrrrwWWpHrrrrdNNrrrrrrrrwppNWHrvrvrypWwvvrrvXKWvrvrvvZXHHkXzvvvvvwWWWWNdkvvzXHfHWkXzzzzzwWXHHHHNMHMNNNNNNNNNNNNNWMN+>>>
// R0rd0rdKrrtrtrrdWHWSrrrwWWDrrrrrrrrXpWHqWvrrrvdWbRvvrvvwWZkvvvrvvXHbHzRvvvvvvdHHpHkWwvzXWHWHHzuzzzzzXXWHHHMHMNHMNNNNNNNNNNNMNWh<>>
// wOwSrwXSrtrtrrwHWWMwrrrdNWSrrrrrrrvppWpHprrvrvddWHwvvrrvXHXwvvvrvzXWHkXzvvvvvzWWKWNdRvzzXWWHHXzzzzzzzudWHbH@HNNWMNNNNNNNNNNNNNkHe>
// rOXwwwKrrrrrrrXWHHDrrwrdHHXwrrrrrrwpWWbHWvvvrvwHHpHrvvvrXWHWwvvrwwXHkHdkvzvvvvZHHpHKWzzzwWHWHRzzzzuzzzzWHHkHHMMNWMNNNNNNNNNNNNNNKm
// rdX0OXSrrtrrrdHHWMXrrrwHWHrrrrrrrrXppqWHWkrrvvwWMHHXwwvvvZWKRvvvvrWHWHwWvzvzzzzXWKWNXRzzwXWWNHzzzzzzzzzXUHkHWMMHNWMNNNNNNNNNNNNNNN
// wSwww#rrrrrrwWWHM#vrrrd#WKvrrrrrvvXppmWMWkvvvvwXNNpNvSXzzvdHkkvvzvdWWWRXwzzzzzzdNNpMkHzzzWVHHpXzzzzzzzzdkHkHHHNMHNkMNNNNNNNNNNNNNN
// XwSwWwrrrrrrdHWWMKrvrvWHWSrvrvvvvwpppHW#HRwUUUWWMMMMMHHHHmmXHWwvvzwWHbNwRzzzzzzzWWWWHWkzzdXWWWRzzzzzzzuzWWbHHWMNMMNkMNNNNNNNNNNNNN
// 8drd#rrrrrrwWWkHMmXmywMfWXrvrrrvrwppWNWMNWvrvvwvW#MNHmdkrvvXHMMmmwzdWpM0WzzzzzzzdHHWNUHzzzWWmHHzzuzzuzuzWXbbNkHMqMHNkHNNNNNNNNNNNN
// dSwWSrrrrrrdHHHMHrrrwWMfWvvvrvrrrdppWNW#WWRvvrvvdM?WNHkXkzvvdWMHXHNmHHHkfwzzzzzzXHHpMWHkzuXWHNWXzzuzzuzzXXkkHbHMHMMMMkMNNNNNNNNNNN
// Xrd#wrrrrrrXpHWH#rrrvdMWWvrrvrvvrdppWHW#(NWwvvvvwMr~TNHmdkvzvXWMKzzzWMHKXXzzzzzzuWHbHkWKuzdWWHWkzzzuzzuuXXHkHkgHk@H@HHkMNNNNNNNNNN
// RvWDrrrrrrwHWkMW#rvvrMMfWwrvrrvvrdppWHW@~?HHuvvrvdb_~?NWkdkzzzUWMkzuuWWHWXuzwzzuzXHHHKWHuzzWW@WRuzuzzzzzduWbHHgMH@HHHMMHMNNNNNNNNN
// rdHwrrrrrrXHHWNW#vvvdMMHWkvrvrrvrdppWbWD~~?XKkrrrwMc~~~THHZHwzzWHNkvzwMRdHHzuzzzzXHHWHXNkuzdWHkWuzzuzuzzXzWkHHHMHHHkH@MMNWMNNNNNNN
// rdHvrrrrrrWkHHHW#rrrd#dNWKrvrvvrvwbpWWW$...vVkkOrtXb~~~~(WHkWkvwWWMkzzWRXkSWXzzzuwmNWHXHRuuwHMKWzzuzzzuzXzWHWHHMHHHkkMMMHHWNNNNNNN
// wW#rrvrrrwHWHMpWNrvrd@(NWWvrrrrrvvWpWXH$..._?dkkttOWl~~~~~?HHdHXXbWMkzUHXWkzzzzzzuHNk#XHHzzukHHXzuzzzuzuXuXqkMHMHMbbkHHH@HNWMNNNNN
// dH#rrrvrrdNWWNpHHwrrd@_WHWkrvvrrrrZpWXWr...__?WkWyOvN~~~~~__7HkWkbbkNkzXHZkzzzuzzzWHW#ugbXuzWWHXXuzzuzuuuzXkkHHH@MbkHMHMHMMNWMNNNN
// X@KrrvrvrXNHWHpHHHrrdb~(NWHrrrrrrrrXWXWr..`...-7HZkzvn~~~~~~~_TNyHHkHMyzXkXuzzzzuuWHW#uHkkzzWHHXuzzzzuzzuuXHkHHHMNbkHHHHHHMMNWMNNN
// WHKvvrvrvXHNMbpHmHkOwb..?HWRrwOrtrtrWKWr`.`.`..._?6dkJn~~~~~~-_~7NHMHbNkuUkzzuzzzuWHW#uHbRzuXHHXzzuuuuzuuzXHkHHMMbkkHkMkkkMMMNUMMN
// WHKrrvrrvXqHMpfHHHWkrb...vkHyOrtttttdNXr.`.J>~-((J+ggQkHXHHHmaJz7&JWHMMWkuHXzuuuzzWgHHwHkKuzXMKXuzuuzuzzuzdkHHHHHkHWMHHbkWHMMMNXM#
// Wg#rvrvrrXmHNpfHHHMMRd-.._4kfyttttllOHWr``
// <udT"""7"""""TMHHMNHHMNgv4HMRXHXHwuzzzuWHHKXHkHzzXMSWzuzzuzuuzudbkHHHHkkHqHuWkWHHMMMNWM
// WHNrvrvvvXHmNppgg@MMMHr..` UkWOlllllldW$..?! ` `
// ?@MMHHMM#NMMmMkXXWkHuzuzuWNHSXHHHuuXNSWuzuuzuzuzuWkHNgMkkkMHSuWkqmqHHMNNK
// WHMkvvrvrXHHMppHHHMNMTN_``` 4kWOz====vXP` ` `
// ``.HggmgM@MH###NKkkuXWHzzuXHHMzdMHHuzWNXKzuuzuzuzuuHkHHNMkkHHBuzWkHHHHuHMMM
// WHHNzvvrvwHMMppHMMMM>.#L`````?kyy====?0S`` ` `
// ``...dmqqqmmHHHMMM#WRSzuzWHuzXHq#uWHHHzuWHXKzuzzuzuzuXHkHWMNkWM#uzuWHHHHSuuWM#
// WM@MkwvvrzXMMHpgHMMM[.MN.`````(HXy1???zd.`` ` ` `
// dHkkqkkqkHHHHHHMMWWXzzuzwWwXHHKXMkHHuXMRXSuuuzuuzuzXHkHdHHWHHXuuwWHqHHuXzXWM
// XHHMNyrvvwdW@HpHWMHML MM[` ` `.4kGz<<>Or``` `
// `dWpbbbbbHMMbbbHHMXXuzzuzuzXWHHSXMkHHuXMSXuzzuzzuzuzWkkHWMHMSwuzzXHkHHXuzuWd#
// dHHH@NyzwwwWHMHHWMMMR dW]`` ` ```(HZx<<<4.```` `` ``
// OZppfppppWfpWpK9NKXuuzuuzuXWHHudMqNHXWNuHuzzuzuzuzuHkHXMMBuuuzuuWkHHUuuuXSMS
// wWHMMMMkwwvdNpHHHM#MH-jWP``` `` ```7kI<:<h.``````` ``
// ,2OWVVyVVyWWWWIdMKWzuuuzzuXHHSwHHHHKXHHXSuuzuuzuzuXHHHXMXuuuuuuXHHHuzuuXSX#X
// wdWHMMMMmwXwHbpMNHMM@b.hb```````````
// 7x1_(>_```````````(x7UUUV0XwvCI1dMWKXuzzuzuWHHudMkgkWbMSXuzzzuzzuzuWkHSW#zXwXuXXWHHuuXXuWXWSX
// vvXWHMMHWNkvWHbbMWMMMN_JF.```` ``
// `````?1_(_.`.`........(<777=<<<><<+dMXUuzuuuuWWMRwMHWMkWHHXSuuzuzuzzuXqHHXMHUXuXXHHHSuuwuXWud8uH
// mzzXWH#zHbHkXNbbWMMMMD~(_```````````````
// .~_`.``.`.`.....___(<~(<<(<<dHkkuuuuuXkHHuWMkHkkkHSXXzuuzuuuuzXHHSd#uuXXWHHXuuuZuXSuqHudS
// UHzzXWSXXWbbHHHbWHMH#<~...```` ``````````````.```.`
// -_____<~~_<_<~~(jMWSuzuzuXWHMHXMHHHkkH#XSuzzuzuzuzuKkHXMQkWHHWUXuXXUuXUuXHuXBu
// .(Smd8wSvXbbbbHHHWHW3___.``````` ``
// ``````.```.```.`...._.._~~..~_~~(#WuzuuzXXkHMqMMHMkkkMUXuXzuuwuuzuXkHSWMWUUuuuXXUUuXHXXWSuXHXu
// .. JKwSvwzWkkkkHHHH5~~..```````````````````.```.````````.`
// .........dWSuuzzzdkHMW@MHMkkkHHXXuzuuXSuzuuWWHd#uuuXXkWSuXXWUXXHUXWHuuu
// .`(HXSzzzzwWbkWHNHr_...``````````````````.```````.```````.`.`..`.-.(HKuzzuuwHHMNMHHMkkkHHuZuuzuuXuuuuXkHWMHHHUUuXXXWHXXHHUuXW8uuuu
// .-MXWwzzzuzXkHHMHHN-.``````````
// ````````````.`.````.``.```.`.`....-dXUXuzuXHWHMMHHBWHMMMSZuzuuzXXzuuXHHMHMHkHHHHHHHWHHSuXkHWSuuuuX
// (#XKzzzzzuzuWHHkWHHp
// ``.````````````````````````.````````.....-((-(NSXuzzXHW@D:~`(/1-
// .kXuzuuzXSuuzXXkHWMHHHHHHHHMWuuuQXUXWuuuXuXH
// MXWzuzzzzzuXHHbHNMMMt`.```````````````````````````.`.` ..-J7"!``
// XKuzzuXHHM9;/<-.J-u!JXXuuzuXWuuuuXHHH#WMNHmkkkuXXkWUXXWXXuuuXqHH
// XWzzzzuzzwXHHHH#"````<.````````````````````.``.`...-c"7``` ` `
// `(BzuuuXmHH8<!(- ?_!`jWUuzuuuWuuuzXWHNH> ..7WHHqHmQkWWXXuuuuuXWqHH
// SwuzzzXuwXHNHY`` `` <,`.`````````````` `` ..J7=``` ` ` ``
// ````.#zuzXXHHH$~`` ```(_(HSuuuzuXSuzuuXHHWCi(` ._ ?WggHHHHWkkkkWHHHHMH
// zzzuzuzwWHMY!` ` ` `(&.```````````` ..J7^ `` ` ` ` ` `. .HXuuXWHW#!` `
// ```` (WUuzzuXXSzuuudHHXY$. : (..``.THHkqkHHHgHHMHkkk zzzzzzXHWK`` ` `` ` ` `
// TJ.``````..J7=```` ` ` ` ` `` ` `.8zuXXHHW=` ``
// ..(XSuuuuuXWuuuuXHWH5~~` .`_?_,-``.TMMMMMMHkkqqHHH zzuuzdHHkN ` ` ` `` `
// ``(Wm,.(J7!``` ` ``` `` `` ` ` ``` .SzuXWHHY ``
// .._~<~~(dSuuuzzXHuuuuXHWHD::?C._.( -`(!_1 ,WHqHHHmqHHHHN zuzXXHHkHM;` ` ` `
// `` ` .;jY= ` .``_`` ` ` ` ` `` ` `.dXuXWm@^
// .._~_.~~~~~(d0uuzuuXHUuuuXHWH5:::::::_.<&._4r?_.;WMMM@MMY"<!` zzXHHHHMHMb``
// `` ` `` `` .J! ` ..`_~ ` ` ` `` ` ` ``.JXXXHY!
// .__.....~.~~~-d0wuzuXXHSuuuXWWH$::::;:::;:+- ~`(.i +HqHHkkHL..`. XWHHHMMHMHM;
// ` ` ` ` ._`` ...(:.=_` ` ` ` ` ````
// .-dVT7^``````......~~~~~_jWuuuuuXHSuzuXHHH3<;:::::;;j<+z ``` -dHNkkMqkkN..``
// HHHMMHHHHNMN.`` ` ` `!`z(. ~..~ ! . ` `` ` ` `
// ..gHr``````````........~~~~_jWuuuuuXkSuuuXHWHC:;::;;:::+v>:((J<!(WHHMkkHNqkM;`..
// HMMkkHHHHM9?b `` ` ` `` .``. ` ` ` ` ``` 4HkkkH,
// ``````.`......~~~~_+KuuuzuXHSuuuXHHK<(::::::<;<J+g9>::;< .4NkkqMHqH]..`
// MHkHHHMM$<<:?L` `` ` ` ` ``.` ` !`` ` ` ` ` `
// ?HbkkHx``````.`..`...~~~~_jKuuuuuXHSuuuqMHD<:<;:<++ddNMB3:;::;:<`
// `,HkkqH@HH].`` kkHHkHY``` (;?h`` ` ` `` ```` ` ` ` ` ` ` ` `
// ```.4XWbHx```..`......~~~~_gKuuuuXWH0uuuXHH3:;++1z<jgMB3<::::::::~`
// `jMkqkHMHHF.`. kHHHH=` . ` _<?L ` ` ` ` ` ` ` ` ` ` ` ` ` ` ?kwWHp
// .`.......~_~~(dKuuuuXWHUuuXWH9++z1<;+jWB3::::;;;::;:;_`` MNkqkqMNMF..` HHk@!.
// ` ` ` ~?L ` ` `` ` `` `` ` ` ` ` ` ` ` `
// -WuXHh..`.....~~~~(dWuuuXXkHUuuXHH9>:<:++YY<:;::::;:::::;:::+.``?HHkHHHM>`.`
| replace | 19 | 23 | 19 | 21 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
int main() {
ll R, G, B, N;
cin >> R >> G >> B >> N;
ll ans = 0;
ll r = N / R;
for (ll i = r; i >= 0; i--) {
ll n = N - i * R;
if (n == 0) {
ans++;
continue;
} else {
ll g = n / G;
for (ll j = g; j >= 0; j--) {
ll m = n - j * G;
if (m == 0) {
ans++;
continue;
} else {
ll b = m / B;
for (ll k = b; k >= 0; k--) {
ll o = m - k * B;
if (o == 0) {
ans++;
continue;
}
}
}
}
}
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
int main() {
ll R, G, B, N;
cin >> R >> G >> B >> N;
ll ans = 0;
ll r = N / R;
for (ll i = r; i >= 0; i--) {
ll n = N - i * R;
if (n == 0) {
ans++;
continue;
} else {
ll g = n / G;
for (ll j = g; j >= 0; j--) {
ll m = n - j * G;
if (m == 0) {
ans++;
continue;
} else if (m % B == 0) {
ans++;
}
}
}
}
cout << ans << "\n";
return 0;
}
| replace | 22 | 31 | 22 | 24 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
int R, G, B, N;
string S;
vector<ll> vec(100000);
int main() {
int count = 0;
cin >> R >> G >> B >> N;
for (int i = 0; i <= N / R; i++) {
for (int j = 0; j <= N / G; j++) {
for (int k = 0; k <= N / B; k++) {
if (i * R + j * G + k * B == N)
count++;
}
}
}
cout << count << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
int R, G, B, N;
string S;
vector<ll> vec(100000);
int main() {
int count = 0;
cin >> R >> G >> B >> N;
for (int i = 0; i <= N / R; i++) {
for (int j = 0; j <= N / G; j++) {
if ((N - (i * R + j * G)) >= 0 && (N - (i * R + j * G)) % B == 0) {
// cout<<i<<" "<<j<<endl;
count++;
}
}
}
cout << count << endl;
} | replace | 13 | 16 | 13 | 16 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <stdio.h>
int main() {
int r, g, b, n;
int ans = 0;
int i, j, k;
scanf("%d%d%d%d", &r, &g, &b, &n);
// printf ("%d %d %d %d\n",r,g,b,n);
for (i = 0; i <= (n / r) + 1; ++i) {
for (j = 0; j <= (n / g) + 1; ++j) {
for (k = 0; k < (n / b) + 1; ++k) {
if (r * i + g * j + k * b == n) {
ans++;
}
}
}
}
printf("%d\n", ans);
return 0;
} | #include <stdio.h>
int main() {
int r, g, b, n;
int ans = 0;
int i, j, k;
scanf("%d%d%d%d", &r, &g, &b, &n);
// printf ("%d %d %d %d\n",r,g,b,n);
for (i = 0; i <= (n / r) + 10; ++i) {
for (j = 0; j <= (n / g) + 10; ++j) {
int p = n - i * r - g * j;
if (p % b == 0 && p / b >= 0)
ans++;
}
}
printf("%d\n", ans);
return 0;
} | replace | 7 | 14 | 7 | 12 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int R, G, B, N, r, g, b, sum = 0;
vector<int> rgb(3);
cin >> rgb.at(0) >> rgb.at(1) >> rgb.at(2) >> N;
sort(rgb.begin(), rgb.end());
R = rgb.at(0);
G = rgb.at(1);
B = rgb.at(2);
for (int r = 0; r < N / R + 1; r++) {
int N1 = (N - R * r) / G;
for (int g = 0; g < N1 + 1; g++) {
int N2 = (N - R * r - G * g) / B;
for (int b = 0; b < N2 + 1; b++) {
if (R * r + G * g + B * b == N) {
sum++;
}
}
}
}
cout << sum << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int R, G, B, N, r, g, b, sum = 0;
vector<int> rgb(3);
cin >> rgb.at(0) >> rgb.at(1) >> rgb.at(2) >> N;
sort(rgb.begin(), rgb.end());
R = rgb.at(0);
G = rgb.at(1);
B = rgb.at(2);
for (int r = 0; r < N / R + 1; r++) {
int N1 = (N - R * r) / G;
for (int g = 0; g < N1 + 1; g++) {
if ((N - R * r - G * g) % B == 0)
sum++;
}
}
cout << sum << endl;
} | replace | 14 | 20 | 14 | 16 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define LONGLONGMAX 9223372036854775807
#define LONGLONGMIN -9223372036854775807
#define INTMAX 32767
#define INTMIN -32767
#define ROUNDUP(divisor, dividend) (divisor + (dividend - 1)) / dividend
int r, g, b, n;
int main() {
cin >> r >> g >> b >> n;
int c = 0;
for (size_t i = 0; i <= ROUNDUP(n, r); i++) {
for (size_t l = 0; l <= ROUNDUP(n, g); l++) {
for (size_t m = 0; m <= ROUNDUP(n, b); m++) {
if (i * r + l * g + m * b == n) {
c++;
}
}
}
}
cout << c << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define LONGLONGMAX 9223372036854775807
#define LONGLONGMIN -9223372036854775807
#define INTMAX 32767
#define INTMIN -32767
#define ROUNDUP(divisor, dividend) (divisor + (dividend - 1)) / dividend
int r, g, b, n;
int main() {
cin >> r >> g >> b >> n;
int c = 0;
for (int i = 0; i <= n; i++) {
for (int l = 0; l <= n; l++) {
if ((n - (i * r + l * g)) % b == 0 &&
i * r + l * g + floor((n - (i * r + l * g)) / b) * b == n &&
(n - (i * r + l * g)) >= 0) {
c++;
}
}
}
cout << c << endl;
}
| replace | 11 | 17 | 11 | 17 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int R, G, B, N;
cin >> R >> G >> B >> N;
int ret = 0;
for (int i = 0; i <= N / R; ++i) {
int rest = N - i * R;
if (rest < 0)
continue;
for (int j = 0; j <= rest / G; ++j) {
int rest2 = rest - j * G;
if (rest2 < 0)
break;
for (int k = 0; k <= rest2 / B; ++k) {
int rest3 = rest2 - k * B;
if (rest3 == 0) {
++ret;
if (rest3 < 0)
break;
}
}
}
}
cout << ret << endl;
return 0;
} | #include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int R, G, B, N;
cin >> R >> G >> B >> N;
int ret = 0;
for (int i = 0; i <= N / R; ++i) {
int rest = N - i * R;
if (rest < 0)
continue;
for (int j = 0; j <= rest / G; ++j) {
int rest2 = rest - j * G;
if (rest2 < 0)
break;
if (rest2 % B == 0)
++ret;
}
}
cout << ret << endl;
return 0;
} | replace | 17 | 25 | 17 | 19 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <math.h>
#define ll long long int
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n)
using namespace std;
int main() {
int R, G, B, N;
cin >> R >> G >> B >> N;
int ans = 0;
REP(r, N / R + 1) {
if (r * R > N)
break;
REP(g, N / G + 1) {
if (r * R + g * G > N)
break;
REP(b, N / B + 1) {
if (r * R + g * G + b * B > N)
break;
if (r * R + g * G + b * B == N)
ans++;
}
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <math.h>
#define ll long long int
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n)
using namespace std;
int main() {
int R, G, B, N;
cin >> R >> G >> B >> N;
int ans = 0;
REP(r, N / R + 1) {
if (r * R > N)
break;
REP(g, N / G + 1) {
if (r * R + g * G > N)
break;
if ((N - (r * R + g * G)) % B == 0)
ans++;
}
}
cout << ans << endl;
return 0;
} | replace | 20 | 26 | 20 | 22 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int r, g, b, n;
int cnt = 0;
cin >> r >> g >> b >> n;
if (g > r && g > b) {
int tmp = g;
g = r;
r = tmp;
}
if (b > g && b > r) {
int tmp = b;
b = r;
r = tmp;
}
for (int i = 0; i <= n / r; i++) {
for (int j = 0; j <= n / g; j++) {
if (r * i + g * j > n) {
break;
}
for (int k = 0; k <= n / b; k++) {
if (r * i + g * j + b * k == n) {
cnt++;
break;
}
}
}
}
cout << cnt << endl;
return 0;
} | #include <iostream>
using namespace std;
int main() {
int r, g, b, n;
int cnt = 0;
cin >> r >> g >> b >> n;
if (g > r && g > b) {
int tmp = g;
g = r;
r = tmp;
}
if (b > g && b > r) {
int tmp = b;
b = r;
r = tmp;
}
for (int i = 0; i <= n / r; i++) {
for (int j = 0; j <= n / g; j++) {
if (r * i + g * j > n) {
break;
}
if ((n - (r * i + g * j)) % b == 0) {
cnt++;
}
}
}
cout << cnt << endl;
return 0;
} | replace | 23 | 28 | 23 | 25 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define INF 100000000
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, m, n) for (int i = m; i < n; i++)
#define de cout << "debug" << endl;
int main() {
int R, G, B, N;
cin >> R >> G >> B >> N;
int cnt = 0;
REP(i, (N + 1) / R + 1) {
REP(j, (N + 1) / G + 1) {
REP(k, (N + 1) / B + 1) {
if (i * R + j * G + k * B == N)
cnt++;
if (i * R + j * G + k * B > N)
break;
}
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define INF 100000000
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, m, n) for (int i = m; i < n; i++)
#define de cout << "debug" << endl;
int main() {
int R, G, B, N;
cin >> R >> G >> B >> N;
int cnt = 0;
REP(i, N / R + 1) {
REP(j, N / G + 1) {
if (i * R + j * G > N)
break;
if ((N - (i * R + j * G)) % B == 0) {
cnt++;
}
}
}
cout << cnt << endl;
}
| replace | 13 | 20 | 13 | 19 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int r, g, b, n;
cin >> r;
cin >> g;
cin >> b;
cin >> n;
int cnt = 0;
for (int i = 0; i < n / r + 1; i++) {
for (int j = 0; j < (n - i * r) / g + 1; j++) {
for (int k = 0; k < (n - i * r - j * g) / b + 1; k++) {
if (i * r + j * g + k * b > n)
break;
if (i * r + j * g + k * b == n) {
cnt++;
}
}
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int r, g, b, n;
cin >> r;
cin >> g;
cin >> b;
cin >> n;
int cnt = 0;
for (int i = 0; i < n / r + 1; i++) {
for (int j = 0; j < (n - i * r) / g + 1; j++) {
if ((n - i * r - j * g) % b == 0)
cnt++;
}
}
cout << cnt << endl;
} | replace | 12 | 19 | 12 | 14 | TLE | |
p03048 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int R, G, B, N;
cin >> R >> G >> B >> N;
int ans = 0;
for (int r = 0; R * r <= N; r++) {
for (int g = 0; R * r + G * g <= N; g++) {
for (int b = 0; R * r + G * g + B * b <= N; b++) {
if (R * r + G * g + B * b == N) {
ans++;
ans %= (int)(1e+9) + 7;
}
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int R, G, B, N;
cin >> R >> G >> B >> N;
int ans = 0;
for (int r = 0; R * r <= N; r++) {
for (int g = 0; R * r + G * g <= N; g++) {
int a = N - (R * r + G * g);
if (a % B == 0)
ans++;
}
}
cout << ans << endl;
return 0;
}
| replace | 12 | 18 | 12 | 15 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.