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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
struct UnionFind {
vector<int> parent;
UnionFind(int N) : parent(N) {
for (int i = 0; i < N; i++) {
parent[i] = -1;
}
}
int root(int i) {
if (parent[i] < 0) {
return i;
}
return (parent[i] = root(parent[i]));
}
bool unite(int from, int to) {
int rx = root(from);
int ry = root(to);
if (rx != ry) {
parent[ry] += parent[rx];
parent[rx] = ry;
return true;
} else {
return false;
}
}
bool same(int x, int y) { return root(x) == root(y); }
int treeSize(int x) { return -parent[root(x)]; }
};
long long int modpow(long long int base, long long int pow, long long int mod) {
if (pow == 1) {
return base;
} else if (pow == 0) {
return 1;
}
if (pow % 2 == 0) {
auto temp = modpow(base, pow / 2, mod);
return (temp * temp) % mod;
} else {
return base * modpow(base, pow - 1, mod) % mod;
}
}
long long int moddiv(long long int X, long long int Y, long long int mod) {
auto fermatDiv = modpow(Y, mod - 2, mod);
return (X * fermatDiv) % mod;
}
long long modCombination(long long left, long long right, long long int mod) {
long long answer = 1;
for (long long i = 0; i < right; i++) {
answer = (answer * (left - i)) % mod;
answer = moddiv(answer, (i + 1), mod);
}
return answer;
}
bool IsPrime(long long N) {
if (N == 1) {
return false;
}
for (long long i = 2; i * i <= N; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
vector<pair<long long, long long>> prime_factorize(long long N) {
vector<pair<long long, long long>> res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
long long ex = 0; // 指数
// 割れる限り割り続ける
while (N % a == 0) {
++ex;
N /= a;
}
// その結果を push
res.push_back({a, ex});
}
// 最後に残った数について
if (N != 1)
res.push_back({N, 1});
return res;
}
long long euclid(long long a, long long b) {
if (a > b) {
long long temp = b;
b = a;
a = temp;
}
if (b % a == 0) {
return a;
} else {
return euclid(a, b - a);
}
}
int main() {
int N;
cin >> N;
vector<vector<int>> connected(N + 1);
vector<vector<int>> distance(N + 1, vector<int>(N + 1));
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
connected[u].push_back(v);
connected[v].push_back(u);
distance[u][v] = w;
distance[v][u] = w;
}
vector<bool> isAlreadyChecked(N + 1, false);
queue<int> Q;
vector<int> answer(N + 1);
answer[1] = 0;
isAlreadyChecked[1] = true;
Q.push(1);
while (!Q.empty()) {
int target = Q.front();
Q.pop();
for (auto connect : connected[target]) {
if (!isAlreadyChecked[connect]) {
if (distance[target][connect] % 2 != 0) {
answer[connect] = 1 - answer[target];
} else {
answer[connect] = answer[target];
}
isAlreadyChecked[connect] = true;
Q.push(connect);
}
}
}
for (int i = 1; i <= N; i++) {
cout << answer[i] << endl;
}
return 0;
} | #include <algorithm>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
struct UnionFind {
vector<int> parent;
UnionFind(int N) : parent(N) {
for (int i = 0; i < N; i++) {
parent[i] = -1;
}
}
int root(int i) {
if (parent[i] < 0) {
return i;
}
return (parent[i] = root(parent[i]));
}
bool unite(int from, int to) {
int rx = root(from);
int ry = root(to);
if (rx != ry) {
parent[ry] += parent[rx];
parent[rx] = ry;
return true;
} else {
return false;
}
}
bool same(int x, int y) { return root(x) == root(y); }
int treeSize(int x) { return -parent[root(x)]; }
};
long long int modpow(long long int base, long long int pow, long long int mod) {
if (pow == 1) {
return base;
} else if (pow == 0) {
return 1;
}
if (pow % 2 == 0) {
auto temp = modpow(base, pow / 2, mod);
return (temp * temp) % mod;
} else {
return base * modpow(base, pow - 1, mod) % mod;
}
}
long long int moddiv(long long int X, long long int Y, long long int mod) {
auto fermatDiv = modpow(Y, mod - 2, mod);
return (X * fermatDiv) % mod;
}
long long modCombination(long long left, long long right, long long int mod) {
long long answer = 1;
for (long long i = 0; i < right; i++) {
answer = (answer * (left - i)) % mod;
answer = moddiv(answer, (i + 1), mod);
}
return answer;
}
bool IsPrime(long long N) {
if (N == 1) {
return false;
}
for (long long i = 2; i * i <= N; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
vector<pair<long long, long long>> prime_factorize(long long N) {
vector<pair<long long, long long>> res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
long long ex = 0; // 指数
// 割れる限り割り続ける
while (N % a == 0) {
++ex;
N /= a;
}
// その結果を push
res.push_back({a, ex});
}
// 最後に残った数について
if (N != 1)
res.push_back({N, 1});
return res;
}
long long euclid(long long a, long long b) {
if (a > b) {
long long temp = b;
b = a;
a = temp;
}
if (b % a == 0) {
return a;
} else {
return euclid(a, b - a);
}
}
int main() {
int N;
cin >> N;
vector<vector<int>> connected(N + 1);
vector<map<int, int>> distance(N + 1, map<int, int>());
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
connected[u].push_back(v);
connected[v].push_back(u);
distance[u][v] = w;
distance[v][u] = w;
}
vector<bool> isAlreadyChecked(N + 1, false);
queue<int> Q;
vector<int> answer(N + 1);
answer[1] = 0;
isAlreadyChecked[1] = true;
Q.push(1);
while (!Q.empty()) {
int target = Q.front();
Q.pop();
for (auto connect : connected[target]) {
if (!isAlreadyChecked[connect]) {
if (distance[target][connect] % 2 != 0) {
answer[connect] = 1 - answer[target];
} else {
answer[connect] = answer[target];
}
isAlreadyChecked[connect] = true;
Q.push(connect);
}
}
}
for (int i = 1; i <= N; i++) {
cout << answer[i] << endl;
}
return 0;
} | replace | 140 | 142 | 140 | 141 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef vector<vector<char>> field_t;
typedef pair<int, int> point_t;
vector<vector<pair<int, int>>> z(100010);
int ans[100010];
bool used[100010];
void dfs(int v, int w) {
if (w % 2 == 0) {
ans[v] = 1;
} else {
ans[v] = 0;
}
for (auto t : z[v]) {
if (!used[t.first]) {
used[t.first] = true;
dfs(t.first, t.second + w);
}
}
}
int main() {
int N, i;
cin >> N;
vector<int> x(N);
rep(i, N) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
z[u].push_back(make_pair(v, w));
z[v].push_back(make_pair(u, w));
}
dfs(0, 0);
rep(i, N) { cout << ans[i] << endl; }
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define drep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef vector<vector<char>> field_t;
typedef pair<int, int> point_t;
vector<vector<pair<int, int>>> z(100010);
int ans[100010];
bool used[100010];
void dfs(int v, int w) {
if (w % 2 == 0) {
ans[v] = 1;
} else {
ans[v] = 0;
}
for (auto t : z[v]) {
if (!used[t.first]) {
used[t.first] = true;
dfs(t.first, t.second + w);
}
}
}
int main() {
int N, i;
cin >> N;
vector<int> x(N);
rep(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
z[u].push_back(make_pair(v, w));
z[v].push_back(make_pair(u, w));
}
dfs(0, 0);
rep(i, N) { cout << ans[i] << endl; }
return 0;
} | replace | 39 | 40 | 39 | 40 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ln '\n'
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> VP;
typedef vector<PL> VPL;
typedef vector<string> VS;
VI ans;
void dfs(int i, bool is_black, vector<vector<P>> &edge) {
if (ans[i] != -1)
return;
ans[i] = is_black ? 0 : 1;
for (auto &adj : edge[i]) {
int cost = adj.first;
int to = adj.second;
bool next_is_black = cost % 2 == 0 ? is_black : !is_black;
dfs(to, next_is_black, edge);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
ans.resize(n, -1); // init
vector<vector<P>> edge(n); // first: cost, second: to
REP(i, n) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
edge[u].push_back(P(w, v));
edge[v].push_back(P(w, u));
}
dfs(0, true, edge);
REP(i, n) cout << ans[i] << ln;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ln '\n'
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> VP;
typedef vector<PL> VPL;
typedef vector<string> VS;
VI ans;
void dfs(int i, bool is_black, vector<vector<P>> &edge) {
if (ans[i] != -1)
return;
ans[i] = is_black ? 0 : 1;
for (auto &adj : edge[i]) {
int cost = adj.first;
int to = adj.second;
bool next_is_black = cost % 2 == 0 ? is_black : !is_black;
dfs(to, next_is_black, edge);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
ans.resize(n, -1); // init
vector<vector<P>> edge(n); // first: cost, second: to
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
edge[u].push_back(P(w, v));
edge[v].push_back(P(w, u));
}
dfs(0, true, edge);
REP(i, n) cout << ans[i] << ln;
return 0;
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p03044 | C++ | Runtime Error | /*
confirm 0LL and 1LL
confirm cornercases such as 0
confirm times of cin < 10^6
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecS = vector<string>;
using VecVec = vector<Vec>;
using Tree = vector<VecP>;
#define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for (ll(i) = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> void co(const T n) { cout << n << endl; }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void coVec(const Vec &v) {
for (ll i : v)
cosp(i);
cout << endl;
}
void sonic() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;
Tree tree;
Vec dist;
Vec color;
void dfs(ll node, ll prev) {
for (P p : tree[node]) {
if (p.fs != prev) {
dist[p.fs] = dist[node] + p.sc;
dfs(p.sc, node);
}
}
}
int main(void) {
ll n;
cin >> n;
tree.resize(n);
dist.assign(n, LINF);
color.resize(n);
rep(i, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
tree[u].pb({v, w});
tree[v].pb({u, w});
}
dist[0] = 0;
dfs(0, 0);
rep(i, n) color[i] = dist[i] % 2;
rep(i, n) co(color[i]);
return 0;
}
| /*
confirm 0LL and 1LL
confirm cornercases such as 0
confirm times of cin < 10^6
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecS = vector<string>;
using VecVec = vector<Vec>;
using Tree = vector<VecP>;
#define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for (ll(i) = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> void co(const T n) { cout << n << endl; }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void coVec(const Vec &v) {
for (ll i : v)
cosp(i);
cout << endl;
}
void sonic() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;
Tree tree;
Vec dist;
Vec color;
void dfs(ll node, ll prev) {
for (P p : tree[node]) {
if (p.fs != prev) {
dist[p.fs] = dist[node] + p.sc;
dfs(p.fs, node);
}
}
}
int main(void) {
ll n;
cin >> n;
tree.resize(n);
dist.assign(n, LINF);
color.resize(n);
rep(i, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
tree[u].pb({v, w});
tree[v].pb({u, w});
}
dist[0] = 0;
dfs(0, 0);
rep(i, n) color[i] = dist[i] % 2;
rep(i, n) co(color[i]);
return 0;
}
| replace | 72 | 73 | 72 | 73 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main(void) {
int n;
cin >> n;
vector<int> color(n, -1);
color[0] = 0;
Graph g(n, vector<int>(n));
Graph cost(n, vector<int>(n));
for (int i = 0; i < n - 1; ++i) {
int a, b, l;
cin >> a >> b >> l;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
cost[a].push_back(l);
cost[b].push_back(l);
}
queue<int> q;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
int w = cost[v][i];
if (color[u] != -1)
continue;
color[u] = (color[v] + w) % 2;
q.push(u);
}
}
for (auto ans : color) {
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main(void) {
int n;
cin >> n;
vector<int> color(n, -1);
color[0] = 0;
Graph g(n), cost(n);
for (int i = 0; i < n - 1; ++i) {
int a, b, l;
cin >> a >> b >> l;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
cost[a].push_back(l);
cost[b].push_back(l);
}
queue<int> q;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
int w = cost[v][i];
if (color[u] != -1)
continue;
color[u] = (color[v] + w) % 2;
q.push(u);
}
}
for (auto ans : color) {
cout << ans << endl;
}
} | replace | 10 | 12 | 10 | 11 | 0 | |
p03044 | Python | Time Limit Exceeded | def solve(string):
n, *uvw = map(int, string.split())
uvw = list(zip(uvw[::3], uvw[1::3], uvw[2::3]))
whites = set([1])
blacks = set([])
while len(uvw) > 0:
tmp = []
for i in range(len(uvw)):
t = uvw.pop()
if t[0] in whites:
if t[2] % 2 == 0:
whites.add(t[1])
else:
blacks.add(t[1])
elif t[1] in whites:
if t[2] % 2 == 0:
whites.add(t[0])
else:
blacks.add(t[0])
elif t[0] in blacks:
if t[2] % 2 == 0:
blacks.add(t[1])
else:
whites.add(t[1])
elif t[1] in blacks:
if t[2] % 2 == 0:
blacks.add(t[0])
else:
whites.add(t[0])
else:
tmp.append(t)
uvw = tmp
ans = ["0"] * n
for i in range(n):
if i + 1 in blacks:
ans[i] = "1"
return "\n".join(ans)
if __name__ == "__main__":
n = int(input())
print(solve("{}\n".format(n) + "\n".join([input() for _ in range(n - 1)])))
| N = int(input())
p = [[] for _ in range(N + 1)]
for _ in range(N - 1):
u, v, w = map(int, input().split())
p[u].append((v, w))
p[v].append((u, w))
s = [(1, 0)]
l = [-1] * N
while s:
a, w = s.pop()
l[a - 1] = w % 2
for b, c in p[a]:
if l[b - 1] == -1:
s.append((b, w + c))
for i in l:
print(i)
| replace | 0 | 43 | 0 | 16 | TLE | |
p03044 | Python | Runtime Error | N = int(input())
en = [[] for _ in range(N)]
on = [[] for _ in range(N)]
for i in range(N - 1):
u, v, w = map(int, input().split())
if w % 2:
on[u - 1].append(v - 1)
on[v - 1].append(u - 1)
else:
en[u - 1].append(v - 1)
en[v - 1].append(u - 1)
color = [None] * N
def dfs(node, parent, col):
if color[node] is not None:
return
color[node] = col
for k in en[node]:
if k != parent:
dfs(k, node, col)
for k in on[node]:
if k != parent:
dfs(k, node, (col + 1) % 2)
dfs(0, -1, 0)
print(*color, sep="\n")
| import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N = int(input())
en = [[] for _ in range(N)]
on = [[] for _ in range(N)]
for i in range(N - 1):
u, v, w = map(int, input().split())
if w % 2:
on[u - 1].append(v - 1)
on[v - 1].append(u - 1)
else:
en[u - 1].append(v - 1)
en[v - 1].append(u - 1)
color = [None] * N
def dfs(node, parent, col):
if color[node] is not None:
return
color[node] = col
for k in en[node]:
if k != parent:
dfs(k, node, col)
for k in on[node]:
if k != parent:
dfs(k, node, (col + 1) % 2)
dfs(0, -1, 0)
print(*color, sep="\n")
| insert | 0 | 0 | 0 | 5 | 0 | |
p03044 | Python | Runtime Error | N = int(input())
neighbors = [[] for _ in range(N + 1)]
for _ in range(N - 1):
u, v, w = map(int, input().split())
neighbors[u].append((v, w))
neighbors[v].append((u, w))
ans = [None for _ in range(N)]
def dfs(v, p, c):
ans[v - 1] = c
for next_v, distance in neighbors[v]:
if next_v == p:
continue
next_c = c if distance % 2 == 0 else 1 - c
dfs(next_v, v, next_c)
dfs(1, -1, 0)
for e in ans:
print(e)
| import sys
sys.setrecursionlimit(200000)
N = int(input())
neighbors = [[] for _ in range(N + 1)]
for _ in range(N - 1):
u, v, w = map(int, input().split())
neighbors[u].append((v, w))
neighbors[v].append((u, w))
ans = [None for _ in range(N)]
def dfs(v, p, c):
ans[v - 1] = c
for next_v, distance in neighbors[v]:
if next_v == p:
continue
next_c = c if distance % 2 == 0 else 1 - c
dfs(next_v, v, next_c)
dfs(1, -1, 0)
for e in ans:
print(e)
| insert | 0 | 0 | 0 | 4 | 0 | |
p03044 | Python | Runtime Error | import sys
from collections import defaultdict
input = sys.stdin.readline
N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
u, v, w = map(int, input().split())
G[u - 1].append((v - 1, w))
G[v - 1].append((u - 1, w))
res = [None] * N
def dfs(v, c):
res[v] = c
for u, w in G[v]:
if res[u] is not None:
continue
if w % 2 == 0:
dfs(u, c)
else:
dfs(u, 1 - c)
dfs(0, 0)
for x in res:
print(x)
| import sys
from collections import defaultdict
sys.setrecursionlimit(100000)
input = sys.stdin.readline
N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
u, v, w = map(int, input().split())
G[u - 1].append((v - 1, w))
G[v - 1].append((u - 1, w))
res = [None] * N
def dfs(v, c):
res[v] = c
for u, w in G[v]:
if res[u] is not None:
continue
if w % 2 == 0:
dfs(u, c)
else:
dfs(u, 1 - c)
dfs(0, 0)
for x in res:
print(x)
| insert | 3 | 3 | 3 | 4 | 0 | |
p03044 | Python | Runtime Error | def dfs(v):
for u, w in e[v]:
if c[u] != -1:
continue
c[u] = (c[v] + w) % 2
dfs(u)
n = int(input())
e = tuple(set() for _ in range(n))
for _ in range(n - 1):
u, v, w = map(int, input().split())
u -= 1
v -= 1
e[u].add((v, w))
e[v].add((u, w))
c = [-1] * n
c[0] = 0
dfs(0)
print(*c, sep="\n")
| import sys
sys.setrecursionlimit(10**7)
def dfs(v):
for u, w in e[v]:
if c[u] != -1:
continue
c[u] = (c[v] + w) % 2
dfs(u)
n = int(input())
e = tuple(set() for _ in range(n))
for _ in range(n - 1):
u, v, w = map(int, input().split())
u -= 1
v -= 1
e[u].add((v, w))
e[v].add((u, w))
c = [-1] * n
c[0] = 0
dfs(0)
print(*c, sep="\n")
| insert | 0 | 0 | 0 | 5 | 0 | |
p03044 | Python | Runtime Error | # -*- coding: utf-8 -*-
n = int(input())
g = [[] for i in range(n)]
c = [-1 for i in range(n)]
for i in range(n - 1):
u, v, w = map(int, input().split())
u -= 1
v -= 1
w = w % 2
g[u].append([v, w])
g[v].append([u, w])
def dfs(p, d):
if c[p] == -1:
c[p] = d
for x, w in g[p]:
dfs(x, d ^ w)
dfs(0, 0)
for i in range(len(c)):
print(c[i])
| # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(100100)
n = int(input())
g = [[] for i in range(n)]
c = [-1 for i in range(n)]
for i in range(n - 1):
u, v, w = map(int, input().split())
u -= 1
v -= 1
w = w % 2
g[u].append([v, w])
g[v].append([u, w])
def dfs(p, d):
if c[p] == -1:
c[p] = d
for x, w in g[p]:
dfs(x, d ^ w)
dfs(0, 0)
for i in range(len(c)):
print(c[i])
| insert | 1 | 1 | 1 | 5 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
struct edge {
ll dst, cost;
edge() {}
edge(ll b, ll c) : dst(b), cost(c) {}
};
vector<edge> tree[1005];
int ans[1005];
// 頂点 0 を 0(白) と決める.
// 頂点 0 から dfs
// v: 今いる頂点, p: v の親
// f: 親の色
// cost: 直前のコスト
void dfs(ll v, ll p, ll f, ll cost) {
if ((f == 0 && (cost & 1)) || (f == 1 && (cost % 2) == 0)) {
ans[v] = 1;
} else {
ans[v] = 0;
}
for (ll i = 0; i < tree[v].size(); i++) {
edge e = tree[v][i];
// 戻らないようにする処理
if (e.dst == p) {
continue;
}
dfs(e.dst, v, ans[v], e.cost);
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
tree[u].push_back(edge(v, w));
tree[v].push_back(edge(u, w));
}
dfs(0, -1, 0, 0);
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
struct edge {
ll dst, cost;
edge() {}
edge(ll b, ll c) : dst(b), cost(c) {}
};
vector<edge> tree[100005];
int ans[100005];
// 頂点 0 を 0(白) と決める.
// 頂点 0 から dfs
// v: 今いる頂点, p: v の親
// f: 親の色
// cost: 直前のコスト
void dfs(ll v, ll p, ll f, ll cost) {
if ((f == 0 && (cost & 1)) || (f == 1 && (cost % 2) == 0)) {
ans[v] = 1;
} else {
ans[v] = 0;
}
for (ll i = 0; i < tree[v].size(); i++) {
edge e = tree[v][i];
// 戻らないようにする処理
if (e.dst == p) {
continue;
}
dfs(e.dst, v, ans[v], e.cost);
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
tree[u].push_back(edge(v, w));
tree[v].push_back(edge(u, w));
}
dfs(0, -1, 0, 0);
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
} | replace | 16 | 18 | 16 | 18 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
using Edge = pair<int, ll>;
using Graphy = vector<vector<Edge>>;
int n;
Graphy g;
vector<int> ans(n);
void dfs(int v, int p, int c) {
ans[v] = c;
for (auto e : g[v]) {
if (e.first == p)
continue;
if (e.second & 1)
dfs(e.first, v, 1 - c);
else
dfs(e.first, v, c);
}
}
int main() {
cin >> n;
rep(i, n - 1) {
int u, v;
ll w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back(Edge(v, w));
g[v].push_back(Edge(u, w));
}
ans.assign(n, 0);
dfs(0, -1, 0);
for (auto d : ans)
cout << d << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
using Edge = pair<int, ll>;
using Graphy = vector<vector<Edge>>;
int n;
Graphy g;
vector<int> ans(n);
void dfs(int v, int p, int c) {
ans[v] = c;
for (auto e : g[v]) {
if (e.first == p)
continue;
if (e.second & 1)
dfs(e.first, v, 1 - c);
else
dfs(e.first, v, c);
}
}
int main() {
cin >> n;
g.assign(n, vector<Edge>());
rep(i, n - 1) {
int u, v;
ll w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back(Edge(v, w));
g[v].push_back(Edge(u, w));
}
ans.assign(n, 0);
dfs(0, -1, 0);
for (auto d : ans)
cout << d << endl;
return 0;
}
| insert | 30 | 30 | 30 | 31 | -11 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;
const double PI = 3.14159265358979323846;
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))
#define rep(i, N) for (int i = 0; i < N; ++i)
#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL
vector<vector<int>> g;
vector<vector<int>> gw;
vector<bool> done;
vs32 ans;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
g.resize(N);
gw.resize(N);
done.resize(N, false);
ans.resize(N);
int u, v, w;
rep(i, N - 1) {
cin >> u >> v >> w;
g[u - 1].push_back(v - 1);
g[w - 1].push_back(u - 1);
gw[u - 1].push_back(w);
gw[v - 1].push_back(w);
}
ans[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int root = q.front();
q.pop();
done[root] = true;
rep(i, g[root].size()) {
int leaf = g[root][i];
if (done[leaf])
continue;
int dist = gw[root][i];
if (dist & 1)
ans[leaf] = ans[root] ^ 1;
else
ans[leaf] = ans[root];
q.push(leaf);
}
}
rep(i, N) cout << ans[i] << "\n";
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;
const double PI = 3.14159265358979323846;
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))
#define rep(i, N) for (int i = 0; i < N; ++i)
#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL
vector<vector<int>> g;
vector<vector<int>> gw;
vector<bool> done;
vs32 ans;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
g.resize(N);
gw.resize(N);
done.resize(N, false);
ans.resize(N);
int u, v, w;
rep(i, N - 1) {
cin >> u >> v >> w;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
gw[u - 1].push_back(w);
gw[v - 1].push_back(w);
}
ans[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int root = q.front();
q.pop();
done[root] = true;
rep(i, g[root].size()) {
int leaf = g[root][i];
if (done[leaf])
continue;
int dist = gw[root][i];
if (dist & 1)
ans[leaf] = ans[root] ^ 1;
else
ans[leaf] = ans[root];
q.push(leaf);
}
}
rep(i, N) cout << ans[i] << "\n";
return 0;
}
| replace | 56 | 57 | 56 | 57 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> N[10009];
int v[10009], n;
void bfs(int k) {
queue<int> Q;
Q.push(k);
while (!Q.empty()) {
for (int j = 0; j < N[Q.front()].size(); j++) {
int nei_v = N[Q.front()][j].first;
int nei_w = N[Q.front()][j].second;
if (v[nei_v] == -1) {
v[nei_v] = (v[Q.front()] + nei_w) % 2;
Q.push(nei_v);
}
}
Q.pop();
}
}
void solve() {
for (int i = 0; i < n; i++) {
v[i] = -1;
}
v[0] = 1;
bfs(0);
for (int i = 0; i < n; i++) {
cout << v[i] << endl;
}
}
int main() {
cin >> n;
int tmp_u, tmp_v, tmp_w;
for (int i = 0; i < n - 1; i++) {
cin >> tmp_u >> tmp_v >> tmp_w;
tmp_u--;
tmp_v--;
N[tmp_u].push_back(make_pair(tmp_v, tmp_w));
N[tmp_v].push_back(make_pair(tmp_u, tmp_w));
}
solve();
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> N[100009];
int v[100009], n;
void bfs(int k) {
queue<int> Q;
Q.push(k);
while (!Q.empty()) {
for (int j = 0; j < N[Q.front()].size(); j++) {
int nei_v = N[Q.front()][j].first;
int nei_w = N[Q.front()][j].second;
if (v[nei_v] == -1) {
v[nei_v] = (v[Q.front()] + nei_w) % 2;
Q.push(nei_v);
}
}
Q.pop();
}
}
void solve() {
for (int i = 0; i < n; i++) {
v[i] = -1;
}
v[0] = 1;
bfs(0);
for (int i = 0; i < n; i++) {
cout << v[i] << endl;
}
}
int main() {
cin >> n;
int tmp_u, tmp_v, tmp_w;
for (int i = 0; i < n - 1; i++) {
cin >> tmp_u >> tmp_v >> tmp_w;
tmp_u--;
tmp_v--;
N[tmp_u].push_back(make_pair(tmp_v, tmp_w));
N[tmp_v].push_back(make_pair(tmp_u, tmp_w));
}
solve();
} | replace | 6 | 8 | 6 | 8 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
vector<int> result;
int N;
Graph G;
void dfs(int v, int c) {
result[v] = c;
for (auto nx : G[v]) {
if (result[nx.first] != -1) {
continue;
}
if (nx.second % 2 == 0) {
dfs(nx.first, c);
}
if (nx.second % 2 == 1) {
dfs(nx.first, 1 - c);
}
}
}
int main() {
cin >> N;
G.resize(N);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
result.assign(N, -1);
dfs(0, 0);
for (auto x : result) {
cout << x << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
vector<int> result;
int N;
Graph G;
void dfs(int v, int c) {
result[v] = c;
for (auto nx : G[v]) {
if (result[nx.first] != -1) {
continue;
}
if (nx.second % 2 == 0) {
dfs(nx.first, c);
}
if (nx.second % 2 == 1) {
dfs(nx.first, 1 - c);
}
}
}
int main() {
cin >> N;
G.resize(N);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
result.assign(N, -1);
dfs(0, 0);
for (auto x : result) {
cout << x << endl;
}
return 0;
} | insert | 31 | 31 | 31 | 33 | -11 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
struct Edge {
int to, dist;
Edge(int a, int b) : to(a), dist(b) {}
};
vector<vector<Edge>> G;
vector<int> colors;
void dfs(int v, int c) {
for (const auto &e : G[v]) {
int u = e.to;
int w = e.dist;
int nc = (w % 2 == 0) ? c : -c;
if (colors[u] == 0) {
colors[u] = nc;
dfs(u, nc);
}
}
}
int main() {
int N;
cin >> N;
G.resize(N);
colors.resize(N, 0);
for (int i = 0; i < N; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
colors[0] = 1;
dfs(0, 1);
for (int c : colors) {
cout << max(c, 0) << endl;
}
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
struct Edge {
int to, dist;
Edge(int a, int b) : to(a), dist(b) {}
};
vector<vector<Edge>> G;
vector<int> colors;
void dfs(int v, int c) {
for (const auto &e : G[v]) {
int u = e.to;
int w = e.dist;
int nc = (w % 2 == 0) ? c : -c;
if (colors[u] == 0) {
colors[u] = nc;
dfs(u, nc);
}
}
}
int main() {
int N;
cin >> N;
G.resize(N);
colors.resize(N, 0);
for (int i = 0; i < N - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
colors[0] = 1;
dfs(0, 1);
for (int c : colors) {
cout << max(c, 0) << endl;
}
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p03044 | C++ | Runtime Error | // #include "bits/stdc++.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define int long long
#define REP(i, l, r) REPEAT(i, l, r, true) // [l, r)
#define rep(i, n) REP(i, 0, n) // [0, n)
#define REPEAT(i, l, r, condition) \
for (int i = (condition) ? l : r - 1; (condition) ? i < r : i >= l; \
(condition) ? ++i : --i) // false<-[l, r)->true
#define all(e) e.begin(), e.end()
#define rall(e) e.rbegin(), e.rend()
#define pb push_back
#define fs first
#define sc second
#ifdef LOCAL
#define show(...) \
cerr << #__VA_ARGS__ << " = "; \
_DEBUG(__VA_ARGS__)
#define showlr(n, l, r) \
cerr << #n << " = "; \
for (int i = l; i < r; i++) { \
cerr << n[i] << ", "; \
} \
cerr << endl // [l, r)
#else
#define show(...)
#define showlr(n, l, r)
#endif
#define YN(condition) puts((condition) ? "YES" : "NO")
#define Yn(condition) puts((condition) ? "Yes" : "No")
#define yn(condition) puts((condition) ? "yes" : "no")
#define YES puts("YES")
#define Yes puts("Yes")
#define yes puts("yes")
#define NO puts("NO")
#define No puts("No")
#define no puts("no")
#define case(i) printf("Case #%lld: ", i)
using namespace std;
using vi = vector<int>;
using pint = pair<int, int>;
struct io {
io() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.tie(0);
cout << fixed << setprecision(20);
}
} io;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (T &e : v)
is >> e;
return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> v) {
os << "{";
for (T &e : v)
os << e << (v.size() - (int)(&e - &v[0]) > 1 ? ", " : "");
os << "}";
return os;
}
void _DEBUG() {}
template <typename H, typename... T> void _DEBUG(H a, T... b) {
cerr << a << (sizeof...(b) ? "," : "\n");
_DEBUG(b...);
}
inline void in() {}
template <typename H, typename... T> void in(H &a, T &...b) {
cin >> a;
in(b...);
}
inline void out() {}
template <typename H, typename... T> void out(H a, T... b) {
cout << a << (sizeof...(b) ? " " : "\n");
out(b...);
}
template <class T> void resz(int n, T &v) { v.resize(n); }
template <class H, class... T> void resz(int n, H &a, T &...b) {
a.resize(n);
resz(n, b...);
}
template <typename V, typename H> void resize(vector<V> &v, const H a) {
v.resize(a);
}
template <typename V, typename H, typename... T>
void resize(vector<V> &v, const H &a, const T... b) {
v.resize(a);
for (auto &v : v)
resize(v, b...);
}
const int INF = 1LL << 55;
const int MOD = 1000000007;
const double EPS = 1e-8;
/*------------end of definitions------------*/
const int MAX_V = 1e5;
vector<pint> G[MAX_V];
int V;
vi color;
vector<bool> visited;
void dfs(int v, int c) {
visited[v] = true;
if (c & 1)
color[v] = 1;
rep(i, G[v].size()) {
if (!visited[G[v][i].fs]) {
dfs(G[v][i].fs, c + G[v][i].sc);
}
}
}
signed main() {
in(V);
visited.resize(V, false);
color.resize(V);
rep(i, V) {
int u, v, w;
in(u, v, w);
u--;
v--;
G[u].pb({v, w});
G[v].pb({u, w});
}
dfs(0, 0);
for (auto e : color) {
out(e);
}
return 0;
}
| // #include "bits/stdc++.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define int long long
#define REP(i, l, r) REPEAT(i, l, r, true) // [l, r)
#define rep(i, n) REP(i, 0, n) // [0, n)
#define REPEAT(i, l, r, condition) \
for (int i = (condition) ? l : r - 1; (condition) ? i < r : i >= l; \
(condition) ? ++i : --i) // false<-[l, r)->true
#define all(e) e.begin(), e.end()
#define rall(e) e.rbegin(), e.rend()
#define pb push_back
#define fs first
#define sc second
#ifdef LOCAL
#define show(...) \
cerr << #__VA_ARGS__ << " = "; \
_DEBUG(__VA_ARGS__)
#define showlr(n, l, r) \
cerr << #n << " = "; \
for (int i = l; i < r; i++) { \
cerr << n[i] << ", "; \
} \
cerr << endl // [l, r)
#else
#define show(...)
#define showlr(n, l, r)
#endif
#define YN(condition) puts((condition) ? "YES" : "NO")
#define Yn(condition) puts((condition) ? "Yes" : "No")
#define yn(condition) puts((condition) ? "yes" : "no")
#define YES puts("YES")
#define Yes puts("Yes")
#define yes puts("yes")
#define NO puts("NO")
#define No puts("No")
#define no puts("no")
#define case(i) printf("Case #%lld: ", i)
using namespace std;
using vi = vector<int>;
using pint = pair<int, int>;
struct io {
io() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.tie(0);
cout << fixed << setprecision(20);
}
} io;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (T &e : v)
is >> e;
return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> v) {
os << "{";
for (T &e : v)
os << e << (v.size() - (int)(&e - &v[0]) > 1 ? ", " : "");
os << "}";
return os;
}
void _DEBUG() {}
template <typename H, typename... T> void _DEBUG(H a, T... b) {
cerr << a << (sizeof...(b) ? "," : "\n");
_DEBUG(b...);
}
inline void in() {}
template <typename H, typename... T> void in(H &a, T &...b) {
cin >> a;
in(b...);
}
inline void out() {}
template <typename H, typename... T> void out(H a, T... b) {
cout << a << (sizeof...(b) ? " " : "\n");
out(b...);
}
template <class T> void resz(int n, T &v) { v.resize(n); }
template <class H, class... T> void resz(int n, H &a, T &...b) {
a.resize(n);
resz(n, b...);
}
template <typename V, typename H> void resize(vector<V> &v, const H a) {
v.resize(a);
}
template <typename V, typename H, typename... T>
void resize(vector<V> &v, const H &a, const T... b) {
v.resize(a);
for (auto &v : v)
resize(v, b...);
}
const int INF = 1LL << 55;
const int MOD = 1000000007;
const double EPS = 1e-8;
/*------------end of definitions------------*/
const int MAX_V = 1e5;
vector<pint> G[MAX_V];
int V;
vi color;
vector<bool> visited;
void dfs(int v, int c) {
visited[v] = true;
if (c & 1)
color[v] = 1;
rep(i, G[v].size()) {
if (!visited[G[v][i].fs]) {
dfs(G[v][i].fs, c + G[v][i].sc);
}
}
}
signed main() {
in(V);
visited.resize(V, false);
color.resize(V);
rep(i, V - 1) {
int u, v, w;
in(u, v, w);
u--;
v--;
G[u].pb({v, w});
G[v].pb({u, w});
}
dfs(0, 0);
for (auto e : color) {
out(e);
}
return 0;
}
| replace | 148 | 149 | 148 | 149 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<int> vecint;
typedef vector<double> vecd;
typedef vector<vector<int>> vec2int;
typedef vector<vector<double>> vec2d;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<bool> vecbool;
typedef vector<vector<bool>> vec2bool;
typedef vector<bool> vecbool;
////////////////////////////////////////////////
int n;
vector<vector<pair<int, int>>>
u; // u[i].first = node from u[i], u[i].second = cost
// output
vecint ans;
////////////////////////////////////////////////
void dfs(int pos, int color) {
ans[pos] = color;
REPvec(itr, u[pos]) {
if (ans[itr->first] > 0)
continue;
dfs(itr->first, (color + (itr->second)) % 2);
}
}
////////////////////////////////////////////////
int main() {
cin >> n;
u.resize(n);
ans.resize(n);
ll a, b, c;
REP(i, n - 1) {
cin >> a >> b >> c;
a--;
b--;
u[a].push_back(make_pair(b, c));
u[b].push_back(make_pair(a, c));
}
REP(i, n) { ans[i] = -1; }
dfs(0, 0);
REP(i, n) { cout << ans[i] << endl; }
} | #include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<int> vecint;
typedef vector<double> vecd;
typedef vector<vector<int>> vec2int;
typedef vector<vector<double>> vec2d;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<bool> vecbool;
typedef vector<vector<bool>> vec2bool;
typedef vector<bool> vecbool;
////////////////////////////////////////////////
int n;
vector<vector<pair<int, int>>>
u; // u[i].first = node from u[i], u[i].second = cost
// output
vecint ans;
////////////////////////////////////////////////
void dfs(int pos, int color) {
ans[pos] = color;
REPvec(itr, u[pos]) {
if (ans[itr->first] != -1)
continue;
dfs(itr->first, (color + (itr->second)) % 2);
}
}
////////////////////////////////////////////////
int main() {
cin >> n;
u.resize(n);
ans.resize(n);
ll a, b, c;
REP(i, n - 1) {
cin >> a >> b >> c;
a--;
b--;
u[a].push_back(make_pair(b, c));
u[b].push_back(make_pair(a, c));
}
REP(i, n) { ans[i] = -1; }
dfs(0, 0);
REP(i, n) { cout << ans[i] << endl; }
}
| replace | 26 | 27 | 26 | 27 | -11 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct edge {
int next, to, v;
} e[maxn];
int h[maxn], col[maxn], n, cnt;
void addedge(int x, int y, int z) {
e[++cnt].next = h[x];
e[cnt].to = y;
e[cnt].v = z;
h[x] = cnt;
}
void dfs(int u, int fa, int now) {
// cout<<u<<' '<<fa<<' '<<now<<endl;
col[u] = now;
for (register int i = h[u]; i; i = e[i].next) {
int j = e[i].to;
if (j == fa) {
continue;
}
dfs(j, u, e[i].v % 2 == 1 ? (now ^ 1) : now);
}
}
int main() {
int x, y, z;
scanf("%d", &n);
for (register int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
addedge(y, x, z);
}
dfs(1, 0, 0);
for (register int i = 1; i <= n; i++) {
printf("%d\n", col[i]);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
struct edge {
int next, to, v;
} e[maxn];
int h[maxn], col[maxn], n, cnt;
void addedge(int x, int y, int z) {
e[++cnt].next = h[x];
e[cnt].to = y;
e[cnt].v = z;
h[x] = cnt;
}
void dfs(int u, int fa, int now) {
// cout<<u<<' '<<fa<<' '<<now<<endl;
col[u] = now;
for (register int i = h[u]; i; i = e[i].next) {
int j = e[i].to;
if (j == fa) {
continue;
}
dfs(j, u, e[i].v % 2 == 1 ? (now ^ 1) : now);
}
}
int main() {
int x, y, z;
scanf("%d", &n);
for (register int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
addedge(y, x, z);
}
dfs(1, 0, 0);
for (register int i = 1; i <= n; i++) {
printf("%d\n", col[i]);
}
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
int color[10010];
void dfs(vector<vector<pair<int, int>>> &g, int v, int p, int c) {
color[v] = c;
for (auto &e : g[v]) {
if (e.first == p)
continue;
dfs(g, e.first, v, e.second ? !c : c);
}
}
int main() {
int n;
cin >> n;
vector<vector<pair<int, int>>> g(n + 1);
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w % 2);
g[v].emplace_back(u, w % 2);
}
dfs(g, 1, -1, 0);
for (int i = 1; i <= n; ++i) {
cout << color[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
int color[100010];
void dfs(vector<vector<pair<int, int>>> &g, int v, int p, int c) {
color[v] = c;
for (auto &e : g[v]) {
if (e.first == p)
continue;
dfs(g, e.first, v, e.second ? !c : c);
}
}
int main() {
int n;
cin >> n;
vector<vector<pair<int, int>>> g(n + 1);
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w % 2);
g[v].emplace_back(u, w % 2);
}
dfs(g, 1, -1, 0);
for (int i = 1; i <= n; ++i) {
cout << color[i] << endl;
}
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
void dfs(int a, int b) {
seen[a] = b;
for (auto x : G[a]) {
int s, t;
tie(s, t) = x;
if (seen[s] == -1) {
dfs(s, (b + t) % 2);
}
}
}
int main() {
cin >> N;
G.resize(N);
seen.resize(N);
for (int i = 0; i < N; i++) {
seen[i] = -1;
}
for (int i = 0; i < N; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
G[b].push_back(make_pair(a, c));
G[a].push_back(make_pair(b, c));
}
dfs(0, 0);
for (int i = 0; i < N; i++) {
cout << seen[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
void dfs(int a, int b) {
seen[a] = b;
for (auto x : G[a]) {
int s, t;
tie(s, t) = x;
if (seen[s] == -1) {
dfs(s, (b + t) % 2);
}
}
}
int main() {
cin >> N;
G.resize(N);
seen.resize(N);
for (int i = 0; i < N; i++) {
seen[i] = -1;
}
for (int i = 1; i < N; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
G[b].push_back(make_pair(a, c));
G[a].push_back(make_pair(b, c));
}
dfs(0, 0);
for (int i = 0; i < N; i++) {
cout << seen[i] << endl;
}
} | replace | 24 | 25 | 24 | 25 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> G(n);
vector<vector<int>> b(n);
for (int i = 0; i < n - 1; i++) {
int a, s, w;
cin >> a >> s >> w;
G[a].push_back(s);
G[s].push_back(a);
b[a].push_back(w);
b[s].push_back(w);
}
vector<int> co(n, -1);
co[0] = 0;
queue<int> que;
que.push(0);
int j = 0;
while (!que.empty()) {
int v = que.front();
que.pop();
for (int nv = 0; nv < G[v].size(); nv++) {
int u = G[v][nv];
int w = b[v][nv];
if (co[u] != -1)
continue;
co[u] = (co[v] + w) % 2;
que.push(u);
}
}
for (int i = 0; i < n; i++) {
cout << co[i] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> G(n);
vector<vector<int>> b(n);
for (int i = 0; i < n - 1; i++) {
int a, s, w;
cin >> a >> s >> w;
a--;
s--;
G[a].push_back(s);
G[s].push_back(a);
b[a].push_back(w);
b[s].push_back(w);
}
vector<int> co(n, -1);
co[0] = 0;
queue<int> que;
que.push(0);
int j = 0;
while (!que.empty()) {
int v = que.front();
que.pop();
for (int nv = 0; nv < G[v].size(); nv++) {
int u = G[v][nv];
int w = b[v][nv];
if (co[u] != -1)
continue;
co[u] = (co[v] + w) % 2;
que.push(u);
}
}
for (int i = 0; i < n; i++) {
cout << co[i] << endl;
}
}
| insert | 11 | 11 | 11 | 13 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()
const int INF = 1001001001;
struct Edge {
int to, cost;
Edge(int to, int cost) : to(to), cost(cost) {}
};
vector<vector<Edge>> g(100005);
vector<int> visited(10005, 0);
void dfs(int u, int c) {
visited[u] = c;
for (Edge e : g[u]) {
int to = e.to, cost = e.cost;
if (visited[to] != 0)
continue;
if (cost % 2)
dfs(to, -c);
else
dfs(to, c);
}
}
int main() {
int n;
cin >> n;
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs(0, 1);
rep(i, n) { cout << (visited[i] == 1 ? 1 : 0) << endl; }
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()
const int INF = 1001001001;
struct Edge {
int to, cost;
Edge(int to, int cost) : to(to), cost(cost) {}
};
vector<vector<Edge>> g(100005);
vector<int> visited(100005, 0);
void dfs(int u, int c) {
visited[u] = c;
for (Edge e : g[u]) {
int to = e.to, cost = e.cost;
if (visited[to] != 0)
continue;
if (cost % 2)
dfs(to, -c);
else
dfs(to, c);
}
}
int main() {
int n;
cin >> n;
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs(0, 1);
rep(i, n) { cout << (visited[i] == 1 ? 1 : 0) << endl; }
} | replace | 20 | 21 | 20 | 21 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
// typedef
//-------------------------#include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
// conversion
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt() {
int x;
scanf("%d", &x);
return x;
}
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
class UnionFind {
public:
vector<int> par;
vector<int> siz;
UnionFind(int sz_) : par(sz_), siz(sz_, 1) {
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
void init(int sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
int root(int x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y) { return root(x) == root(y); }
int size(int x) { return siz[root(x)]; }
};
ll modPow(ll x, ll n, ll mod = MOD) {
ll res = 1;
while (n) {
if (n & 1)
res = (res * x) % mod;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
#define SIEVE_SIZE 5000000 + 10
bool sieve[SIEVE_SIZE];
void makeSieve() {
for (int i = 0; i < SIEVE_SIZE; ++i)
sieve[i] = true;
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < SIEVE_SIZE; ++i)
if (sieve[i])
for (int j = 2; i * j < SIEVE_SIZE; ++j)
sieve[i * j] = false;
}
bool isprime(ll n) {
if (n == 0 || n == 1)
return false;
for (ll i = 2; i * i <= n; ++i)
if (n % i == 0)
return false;
return true;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
long long x, y;
extGCD(a, m, x, y);
return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b) {
if (b == 0)
return a;
return GCD(b, a % b);
}
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat &A, mat &B) {
mat C(A.size(), vec((int)B[0].size()));
for (int i = 0; i < A.size(); ++i) {
for (int k = 0; k < B.size(); ++k) {
for (int j = 0; j < B[0].size(); ++j) {
C[i][j] = (C[i][j] + A[i][k] * B[k][j] % MOD) % MOD;
}
}
}
return C;
}
mat matPow(mat A, ll n) {
mat B(A.size(), vec((int)A.size()));
for (int i = 0; i < A.size(); ++i) {
B[i][i] = 1;
}
while (n > 0) {
if (n & 1)
B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
vector<vector<pair<int, int>>> g(100010);
int ans[100010];
void dfs(int v, int p, ll sum) {
if (sum % 2 == 0) {
ans[v] = 1;
} else {
ans[v] = 0;
}
for (int i = 0; i < g[v].size(); i++) {
int u = g[v][i].first;
ll cost = g[v][i].second;
if (u == p)
continue;
dfs(u, v, sum + cost);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
int N;
cin >> N;
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back({v, w});
g[v].push_back({w, v});
}
dfs(0, -1, 0);
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
// typedef
//-------------------------#include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
// conversion
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt() {
int x;
scanf("%d", &x);
return x;
}
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
class UnionFind {
public:
vector<int> par;
vector<int> siz;
UnionFind(int sz_) : par(sz_), siz(sz_, 1) {
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
void init(int sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
int root(int x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y) { return root(x) == root(y); }
int size(int x) { return siz[root(x)]; }
};
ll modPow(ll x, ll n, ll mod = MOD) {
ll res = 1;
while (n) {
if (n & 1)
res = (res * x) % mod;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
#define SIEVE_SIZE 5000000 + 10
bool sieve[SIEVE_SIZE];
void makeSieve() {
for (int i = 0; i < SIEVE_SIZE; ++i)
sieve[i] = true;
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < SIEVE_SIZE; ++i)
if (sieve[i])
for (int j = 2; i * j < SIEVE_SIZE; ++j)
sieve[i * j] = false;
}
bool isprime(ll n) {
if (n == 0 || n == 1)
return false;
for (ll i = 2; i * i <= n; ++i)
if (n % i == 0)
return false;
return true;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
long long x, y;
extGCD(a, m, x, y);
return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b) {
if (b == 0)
return a;
return GCD(b, a % b);
}
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat &A, mat &B) {
mat C(A.size(), vec((int)B[0].size()));
for (int i = 0; i < A.size(); ++i) {
for (int k = 0; k < B.size(); ++k) {
for (int j = 0; j < B[0].size(); ++j) {
C[i][j] = (C[i][j] + A[i][k] * B[k][j] % MOD) % MOD;
}
}
}
return C;
}
mat matPow(mat A, ll n) {
mat B(A.size(), vec((int)A.size()));
for (int i = 0; i < A.size(); ++i) {
B[i][i] = 1;
}
while (n > 0) {
if (n & 1)
B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
vector<vector<pair<int, int>>> g(100010);
int ans[100010];
void dfs(int v, int p, ll sum) {
if (sum % 2 == 0) {
ans[v] = 1;
} else {
ans[v] = 0;
}
for (int i = 0; i < g[v].size(); i++) {
int u = g[v][i].first;
ll cost = g[v][i].second;
if (u == p)
continue;
dfs(u, v, sum + cost);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
int N;
cin >> N;
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(0, -1, 0);
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 259 | 260 | 259 | 260 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> G;
vector<vector<int>> W;
vector<int> res;
void dfs(int u) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (res[v] != -1)
continue;
res[v] = (res[u] + W[u][i]) % 2;
dfs(v);
}
}
int main() {
cin >> N;
G.resize(N);
W.resize(N);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
W[u].push_back(w);
G[v].push_back(w);
}
res.resize(N, -1);
res[0] = 0;
dfs(0);
for (int i = 0; i < N; i++) {
cout << res[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> G;
vector<vector<int>> W;
vector<int> res;
void dfs(int u) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (res[v] != -1)
continue;
res[v] = (res[u] + W[u][i]) % 2;
dfs(v);
}
}
int main() {
cin >> N;
G.resize(N);
W.resize(N);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
W[u].push_back(w);
W[v].push_back(w);
}
res.resize(N, -1);
res[0] = 0;
dfs(0);
for (int i = 0; i < N; i++) {
cout << res[i] << endl;
}
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
using pqueg = priority_queue<int, vector<int>, greater<int>>;
using pquel = priority_queue<int, vector<int>, less<int>>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int r_int(int a, int b) { return rand() % b + a; }
const lint mod = 1e9 + 7;
lint n;
vector<vector<plint>> cnct;
vector<int> seen;
void dfs(lint node, int color) {
seen[node] = color;
for (plint next : cnct[node]) {
// 隣接していて,かつ未探索ノードを探す
if (next.second != 0 && seen[next.first] == -1) {
// 辺の長さが偶数なら同じ色で塗る
if (next.second % 2 == 0)
dfs(next.first, color);
else
dfs(next.first, (color + 1) % 2);
}
}
}
int main() {
cin >> n;
cnct.assign(n, vector<plint>(n));
REP(i, n - 1) {
lint x, y, edge;
cin >> x >> y >> edge;
x--;
y--;
cnct[x].push_back({y, edge});
cnct[y].push_back({x, edge});
}
seen.assign(n, -1);
dfs(0, 0);
REP(i, n) cout << seen[i] << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
using pqueg = priority_queue<int, vector<int>, greater<int>>;
using pquel = priority_queue<int, vector<int>, less<int>>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int r_int(int a, int b) { return rand() % b + a; }
const lint mod = 1e9 + 7;
lint n;
vector<vector<plint>> cnct;
vector<int> seen;
void dfs(lint node, int color) {
seen[node] = color;
for (plint next : cnct[node]) {
// 隣接していて,かつ未探索ノードを探す
if (next.second != 0 && seen[next.first] == -1) {
// 辺の長さが偶数なら同じ色で塗る
if (next.second % 2 == 0)
dfs(next.first, color);
else
dfs(next.first, (color + 1) % 2);
}
}
}
int main() {
cin >> n;
cnct.assign(n, vector<plint>(0));
REP(i, n - 1) {
lint x, y, edge;
cin >> x >> y >> edge;
x--;
y--;
cnct[x].push_back({y, edge});
cnct[y].push_back({x, edge});
}
seen.assign(n, -1);
dfs(0, 0);
REP(i, n) cout << seen[i] << "\n";
return 0;
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
int src, dst;
CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &rhs) const {
return cost != rhs.cost ? cost < rhs.cost
: dst != rhs.dst ? dst < rhs.dst
: src < rhs.src;
}
inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
inline bool operator>(const Edge &rhs) const {
return cost != rhs.cost ? cost > rhs.cost
: dst != rhs.dst ? dst > rhs.dst
: src > rhs.src;
}
inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};
vector<vector<Edge>> graph(10000);
int color[10000] = {};
void dfs(int par, int ver, bool black) {
for (Edge e : graph[ver])
if (e.dst != par) {
if (e.cost % 2 == 0) {
color[e.dst] = (black ? 0 : 1);
dfs(ver, e.dst, black);
} else {
color[e.dst] = (black ? 1 : 0);
dfs(ver, e.dst, !black);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
int n;
cin >> n;
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
--u;
--v;
graph[u].emplace_back(Edge(u, v, w));
graph[v].emplace_back(Edge(v, u, w));
}
dfs(-1, 0, true);
REP(i, n) cout << color[i] << '\n';
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
int src, dst;
CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &rhs) const {
return cost != rhs.cost ? cost < rhs.cost
: dst != rhs.dst ? dst < rhs.dst
: src < rhs.src;
}
inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
inline bool operator>(const Edge &rhs) const {
return cost != rhs.cost ? cost > rhs.cost
: dst != rhs.dst ? dst > rhs.dst
: src > rhs.src;
}
inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};
vector<vector<Edge>> graph(100000);
int color[100000] = {};
void dfs(int par, int ver, bool black) {
for (Edge e : graph[ver])
if (e.dst != par) {
if (e.cost % 2 == 0) {
color[e.dst] = (black ? 0 : 1);
dfs(ver, e.dst, black);
} else {
color[e.dst] = (black ? 1 : 0);
dfs(ver, e.dst, !black);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
int n;
cin >> n;
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
--u;
--v;
graph[u].emplace_back(Edge(u, v, w));
graph[v].emplace_back(Edge(v, u, w));
}
dfs(-1, 0, true);
REP(i, n) cout << color[i] << '\n';
return 0;
}
| replace | 54 | 56 | 54 | 56 | 0 | |
p03044 | C++ | Runtime Error | /**Function Template**/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for (ll i = ll(0); i < ll(n); i++)
#define Rep(i, n) for (ll i = ll(1); i < ll(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define PI 3.14159265358979323846
#define ifYN(x) cout << (x ? "Yes" : "No") << "\n"
ll fac[MAX], finv[MAX], inv[MAX];
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
bool palindrome(string s) {
bool flag = true;
rep(i, (ll)s.size()) if (s[i] != s[s.size() - 1 - i]) flag = false;
return flag;
}
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll Len(ll n) {
ll s = 0;
while (n != 0)
s++, n /= 10;
return s;
}
ll Sint(ll n) {
ll ans = 0;
while (n != 0)
ans += n % 10, n /= 10;
return ans;
}
ll Svec(vector<ll> v) {
ll n = 0;
rep(i, (ll)v.size()) n += v[i];
return n;
}
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
ll Factorial(ll n) {
ll m = 1;
while (n >= 1)
m *= n, n--;
return m;
}
void runlength(string s, vector<pair<char, ll>> &p) {
ll x = 1;
if (s.size() == 1) {
p.push_back(pair<char, ll>(s[0], 1));
}
rep(i, (ll)s.size() - 1) {
if (s[i] == s[i + 1]) {
x++;
if (i == (ll)s.size() - 2) {
p.push_back(pair<char, ll>(s[i], x));
}
} else {
p.push_back(pair<char, ll>(s[i], x));
x = 1;
if (i == (ll)s.size() - 2) {
p.push_back(pair<char, ll>(s[s.size() - 1], x));
}
}
}
}
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
string Toupper(string s) {
string ans = "";
rep(i, s.size()) {
if ('a' <= s[i] && s[i] <= 'z')
ans += (char)s[i] - 32;
else
ans += s[i];
}
return ans;
}
string Tolower(string s) {
string ans = "";
rep(i, s.size()) {
if ('A' <= s[i] && s[i] <= 'Z')
ans += (char)s[i] + 32;
else
ans += s[i];
}
return ans;
}
void dis(vector<ll> v) { rep(i, v.size()) cout << v[i] << endl; }
void dis2(vector<vector<ll>> v) {
rep(i, v.size()) {
rep(j, v[0].size()) cout << v[i][j] << ' ';
cout << endl;
}
}
const int MAX_N = 100010;
vector<bool> sieve_of_eratosthenes() {
vector<bool> isPrime(MAX_N + 1, true);
/* isPrime[1]=false; */
for (int i = 2; i <= MAX_N; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= MAX_N; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
vector<pint> prime_factorize(ll n) {
vector<pint> ans;
for (ll p = 2; p <= sqrt(n); p++) {
if (n % p != 0)
continue;
ll cnt = 0;
while (n % p == 0) {
n /= p;
cnt++;
}
ans.push_back(make_pair(p, cnt));
}
if (n != 1)
ans.push_back(make_pair(n, 1));
return ans;
}
/*bool cmp(pint a, pint b) { return a.second < b.second; }*/
struct edge {
ll to, cost;
};
vector<edge> v[100010];
vector<bool> reached(100010);
ll ans[100010];
void dfs(ll n, ll c) {
if (reached[n])
return;
reached[n] = true;
ans[n] = c;
for (auto tmp : v[n]) {
ll to = tmp.to;
ll cost = tmp.cost;
if (cost % 2 == 0) {
dfs(to, c);
} else {
dfs(to, -c);
}
}
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
IOS;
ll n;
cin >> n;
rep(i, n) reached[i] = false;
rep(i, n) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
v[a].push_back(edge{b, c});
v[b].push_back(edge{a, c});
}
dfs(0, 1);
rep(i, n) {
if (ans[i] == 1)
cout << 1 << endl;
else
cout << 0 << endl;
}
} | /**Function Template**/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for (ll i = ll(0); i < ll(n); i++)
#define Rep(i, n) for (ll i = ll(1); i < ll(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define PI 3.14159265358979323846
#define ifYN(x) cout << (x ? "Yes" : "No") << "\n"
ll fac[MAX], finv[MAX], inv[MAX];
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
bool palindrome(string s) {
bool flag = true;
rep(i, (ll)s.size()) if (s[i] != s[s.size() - 1 - i]) flag = false;
return flag;
}
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll Len(ll n) {
ll s = 0;
while (n != 0)
s++, n /= 10;
return s;
}
ll Sint(ll n) {
ll ans = 0;
while (n != 0)
ans += n % 10, n /= 10;
return ans;
}
ll Svec(vector<ll> v) {
ll n = 0;
rep(i, (ll)v.size()) n += v[i];
return n;
}
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
ll Factorial(ll n) {
ll m = 1;
while (n >= 1)
m *= n, n--;
return m;
}
void runlength(string s, vector<pair<char, ll>> &p) {
ll x = 1;
if (s.size() == 1) {
p.push_back(pair<char, ll>(s[0], 1));
}
rep(i, (ll)s.size() - 1) {
if (s[i] == s[i + 1]) {
x++;
if (i == (ll)s.size() - 2) {
p.push_back(pair<char, ll>(s[i], x));
}
} else {
p.push_back(pair<char, ll>(s[i], x));
x = 1;
if (i == (ll)s.size() - 2) {
p.push_back(pair<char, ll>(s[s.size() - 1], x));
}
}
}
}
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
string Toupper(string s) {
string ans = "";
rep(i, s.size()) {
if ('a' <= s[i] && s[i] <= 'z')
ans += (char)s[i] - 32;
else
ans += s[i];
}
return ans;
}
string Tolower(string s) {
string ans = "";
rep(i, s.size()) {
if ('A' <= s[i] && s[i] <= 'Z')
ans += (char)s[i] + 32;
else
ans += s[i];
}
return ans;
}
void dis(vector<ll> v) { rep(i, v.size()) cout << v[i] << endl; }
void dis2(vector<vector<ll>> v) {
rep(i, v.size()) {
rep(j, v[0].size()) cout << v[i][j] << ' ';
cout << endl;
}
}
const int MAX_N = 100010;
vector<bool> sieve_of_eratosthenes() {
vector<bool> isPrime(MAX_N + 1, true);
/* isPrime[1]=false; */
for (int i = 2; i <= MAX_N; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= MAX_N; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
vector<pint> prime_factorize(ll n) {
vector<pint> ans;
for (ll p = 2; p <= sqrt(n); p++) {
if (n % p != 0)
continue;
ll cnt = 0;
while (n % p == 0) {
n /= p;
cnt++;
}
ans.push_back(make_pair(p, cnt));
}
if (n != 1)
ans.push_back(make_pair(n, 1));
return ans;
}
/*bool cmp(pint a, pint b) { return a.second < b.second; }*/
struct edge {
ll to, cost;
};
vector<edge> v[100010];
vector<bool> reached(100010);
ll ans[100010];
void dfs(ll n, ll c) {
if (reached[n])
return;
reached[n] = true;
ans[n] = c;
for (auto tmp : v[n]) {
ll to = tmp.to;
ll cost = tmp.cost;
if (cost % 2 == 0) {
dfs(to, c);
} else {
dfs(to, -c);
}
}
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
IOS;
ll n;
cin >> n;
rep(i, n) reached[i] = false;
rep(i, n - 1) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
v[a].push_back(edge{b, c});
v[b].push_back(edge{a, c});
}
dfs(0, 1);
rep(i, n) {
if (ans[i] == 1)
cout << 1 << endl;
else
cout << 0 << endl;
}
} | replace | 210 | 211 | 210 | 211 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[10005];
bool use[10005];
int d[10005];
void dfs(int n, int w) {
use[n] = true;
d[n] = (w & 1);
for (int i = 0; i < v[n].size(); i++) {
if (!use[v[n][i].first])
dfs(v[n][i].first, v[n][i].second + w);
}
}
int n, mb;
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back(make_pair(b, c));
v[b].push_back(make_pair(a, c));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << d[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[100005];
bool use[100005];
int d[100005];
void dfs(int n, int w) {
use[n] = true;
d[n] = (w & 1);
for (int i = 0; i < v[n].size(); i++) {
if (!use[v[n][i].first])
dfs(v[n][i].first, v[n][i].second + w);
}
}
int n, mb;
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back(make_pair(b, c));
v[b].push_back(make_pair(a, c));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << d[i] << endl;
}
return 0;
}
| replace | 2 | 5 | 2 | 5 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// cout << << endl;
typedef long long int ll;
typedef double ld;
vector<pair<ll, ll>> p;
bool pairCompare(const pair<double, ll> &firstElof,
const pair<double, ll> &secondElof) {
return firstElof.first < secondElof.first;
}
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;
}
#define MAX_N 100010
#define MAX_H 310
#define MAX_W 310
struct edge {
ll to, cost;
};
bool x[MAX_N];
ll num[MAX_N];
ll eratosthenes(ll n) {
int p = 0;
for (ll i = 0; i <= n; ++i)
x[i] = true;
x[0] = x[1] = false;
for (int i = 2; i <= n; ++i) {
if (x[i]) {
p++;
for (int j = 2 * i; j <= n; j += i)
x[j] = false;
}
num[i] = p;
}
return p;
}
ll gcd(ll a, ll b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
vector<edge> G[MAX_N];
ll color[MAX_N];
bool dfs(ll v, ll p, ll c) {
color[v] = c;
for (auto e : G[v]) {
if (e.to == p)
continue;
if (e.cost & 1)
dfs(e.to, v, 1 - c);
else
dfs(e.to, v, c);
}
return true;
}
int main() {
ll v;
cin >> v;
ll s, t, w;
for (ll i = 0; i < v - 1; i++) {
cin >> s >> t >> w;
s--;
t--;
G[s - 1].push_back(edge{t, w});
G[t - 1].push_back(edge{s, w});
}
dfs(0LL, -1, 1);
for (ll i = 0; i < v; i++) {
cout << color[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// cout << << endl;
typedef long long int ll;
typedef double ld;
vector<pair<ll, ll>> p;
bool pairCompare(const pair<double, ll> &firstElof,
const pair<double, ll> &secondElof) {
return firstElof.first < secondElof.first;
}
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;
}
#define MAX_N 100010
#define MAX_H 310
#define MAX_W 310
struct edge {
ll to, cost;
};
bool x[MAX_N];
ll num[MAX_N];
ll eratosthenes(ll n) {
int p = 0;
for (ll i = 0; i <= n; ++i)
x[i] = true;
x[0] = x[1] = false;
for (int i = 2; i <= n; ++i) {
if (x[i]) {
p++;
for (int j = 2 * i; j <= n; j += i)
x[j] = false;
}
num[i] = p;
}
return p;
}
ll gcd(ll a, ll b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
vector<edge> G[MAX_N];
ll color[MAX_N];
bool dfs(ll v, ll p, ll c) {
color[v] = c;
for (auto e : G[v]) {
if (e.to == p)
continue;
if (e.cost & 1)
dfs(e.to, v, 1 - c);
else
dfs(e.to, v, c);
}
return true;
}
int main() {
ll v;
cin >> v;
ll s, t, w;
for (ll i = 0; i < v - 1; i++) {
cin >> s >> t >> w;
s--;
t--;
G[s].push_back(edge{t, w});
G[t].push_back(edge{s, w});
}
dfs(0LL, -1, 1);
for (ll i = 0; i < v; i++) {
cout << color[i] << endl;
}
return 0;
}
| replace | 95 | 97 | 95 | 97 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
int n;
cin >> n;
vector<int> ans(n, 0);
queue<int> q;
// set<int> S;
vector<bool> vis(n + 10, false);
vector<vector<pair<int, int>>> adj(n + 10);
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
// if (w % 2 == 0) {
// ans[u] = ans[v] = 1;
// vis[u] = vis[v] = true;
// S.insert(u);
// S.insert(v);
// }
}
// for (auto it : S) {
// q.push(it);
// }
q.push(1);
vis[1] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto &edge : adj[v]) {
int u = edge.first, w = edge.second;
if (vis[u]) {
continue;
}
if (w % 2 == 0) {
ans[u] = ans[v];
} else {
ans[u] = !ans[v];
}
q.push(u);
vis[u] = true;
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
}
| #include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
int n;
cin >> n;
vector<int> ans(n + 10, 0);
queue<int> q;
// set<int> S;
vector<bool> vis(n + 10, false);
vector<vector<pair<int, int>>> adj(n + 10);
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
// if (w % 2 == 0) {
// ans[u] = ans[v] = 1;
// vis[u] = vis[v] = true;
// S.insert(u);
// S.insert(v);
// }
}
// for (auto it : S) {
// q.push(it);
// }
q.push(1);
vis[1] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto &edge : adj[v]) {
int u = edge.first, w = edge.second;
if (vis[u]) {
continue;
}
if (w % 2 == 0) {
ans[u] = ans[v];
} else {
ans[u] = !ans[v];
}
q.push(u);
vis[u] = true;
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
}
| replace | 13 | 14 | 13 | 14 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846
using namespace std;
// conversion
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt() {
int x;
scanf("%d", &x);
return x;
}
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int n, m;
vector<vector<pair<int, int>>> G(100010);
int ans[100010];
int main() {
// cout << fixed << setprecision(15);
int N = readInt();
REP(i, 100010) ans[i] = -1;
REP(i, N) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(make_pair(v, w));
G[v].push_back({u, w});
}
queue<pair<int, int>> q;
q.push({0, 1});
ans[0] = 1;
while (q.size()) {
pair<int, int> p = q.front();
q.pop();
int v = p.first;
int bin = p.second;
for (int i = 0; i < (int)G[v].size(); ++i) {
int u = G[v][i].first;
if (ans[u] != -1)
continue;
int len = G[v][i].second;
if (len % 2 == 0) {
if (bin == 0) {
ans[u] = 0;
q.push({u, 0});
} else {
ans[u] = 1;
q.push({u, 1});
}
} else {
if (bin == 0) {
ans[u] = 1;
q.push({u, 1});
} else {
ans[u] = 0;
q.push({u, 0});
}
}
}
}
for (int i = 0; i < N; ++i)
cout << ans[i] << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846
using namespace std;
// conversion
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt() {
int x;
scanf("%d", &x);
return x;
}
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int n, m;
vector<vector<pair<int, int>>> G(100010);
int ans[100010];
int main() {
// cout << fixed << setprecision(15);
int N = readInt();
REP(i, 100010) ans[i] = -1;
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(make_pair(v, w));
G[v].push_back({u, w});
}
queue<pair<int, int>> q;
q.push({0, 1});
ans[0] = 1;
while (q.size()) {
pair<int, int> p = q.front();
q.pop();
int v = p.first;
int bin = p.second;
for (int i = 0; i < (int)G[v].size(); ++i) {
int u = G[v][i].first;
if (ans[u] != -1)
continue;
int len = G[v][i].second;
if (len % 2 == 0) {
if (bin == 0) {
ans[u] = 0;
q.push({u, 0});
} else {
ans[u] = 1;
q.push({u, 1});
}
} else {
if (bin == 0) {
ans[u] = 1;
q.push({u, 1});
} else {
ans[u] = 0;
q.push({u, 0});
}
}
}
}
for (int i = 0; i < N; ++i)
cout << ans[i] << endl;
return 0;
} | replace | 104 | 105 | 104 | 105 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
/*
Bismillahir Rahmanir Rahim
Problem :
Problem Link :
Topics :
Solver : Masud Parves
I Love Code More than Sharks Love Blood <3
*/
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define SZ(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define sf(a) scanf("%d", &a)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)
#define CIN \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define TEST_CASE(t) for (int zz = 1; zz <= t; zz++)
#define PRINT_CASE printf("Case %d: ", zz)
#define Debug(x) cout << #x " = " << (x) << endl
#define NOT_VISITED 0
#define IS_VISITED 1
#define WHITE 0
#define GRAY 1
#define BLACK 2
int fx[4] = {1, -1, 0, 0};
int fy[4] = {0, 0, 1, -1};
/*-----------------------Bitmask------------------*/
// int Set(int N,int pos){return N=N (1<<pos);}
// int Reset(int N,int pos){return N= N & ~(1<<pos);}
// bool Check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/
const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;
vector<int> g[maxn];
vector<ll> cost[maxn];
bool visited[maxn];
int color[maxn];
void bfs(int src) {
queue<int> Q;
Q.push(src);
visited[src] = 1;
color[src] = 1;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!visited[v]) {
Q.push(v);
int w = cost[u][i];
if (w % 2 == 0) {
color[v] = color[u];
} else {
if (color[u] == 0)
color[v] = 1;
else
color[v] = 0;
}
}
}
}
}
int main() {
CIN
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n;
cin >> n;
f1(i, n - 1) {
int u, v;
ll w;
cin >> u >> v >> w;
g[u].pb(v);
g[v].pb(u);
cost[u].pb(w);
cost[v].pb(w);
}
bfs(1);
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
/*
Bismillahir Rahmanir Rahim
Problem :
Problem Link :
Topics :
Solver : Masud Parves
I Love Code More than Sharks Love Blood <3
*/
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define SZ(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define sf(a) scanf("%d", &a)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)
#define CIN \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define TEST_CASE(t) for (int zz = 1; zz <= t; zz++)
#define PRINT_CASE printf("Case %d: ", zz)
#define Debug(x) cout << #x " = " << (x) << endl
#define NOT_VISITED 0
#define IS_VISITED 1
#define WHITE 0
#define GRAY 1
#define BLACK 2
int fx[4] = {1, -1, 0, 0};
int fy[4] = {0, 0, 1, -1};
/*-----------------------Bitmask------------------*/
// int Set(int N,int pos){return N=N (1<<pos);}
// int Reset(int N,int pos){return N= N & ~(1<<pos);}
// bool Check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/
const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;
vector<int> g[maxn];
vector<ll> cost[maxn];
bool visited[maxn];
int color[maxn];
void bfs(int src) {
queue<int> Q;
Q.push(src);
visited[src] = 1;
color[src] = 1;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!visited[v]) {
Q.push(v);
visited[v] = 1;
int w = cost[u][i];
if (w % 2 == 0) {
color[v] = color[u];
} else {
if (color[u] == 0)
color[v] = 1;
else
color[v] = 0;
}
}
}
}
}
int main() {
CIN
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n;
cin >> n;
f1(i, n - 1) {
int u, v;
ll w;
cin >> u >> v >> w;
g[u].pb(v);
g[v].pb(u);
cost[u].pb(w);
cost[v].pb(w);
}
bfs(1);
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
cout << endl;
return 0;
}
| insert | 85 | 85 | 85 | 86 | TLE | |
p03044 | C++ | Runtime Error | #include <iostream>
#include <utility>
#include <vector>
using namespace std;
vector<pair<int, int>> neighbour[10001];
int color[10001];
void dfs(int now, int prev) {
for (auto p : neighbour[now]) {
if (p.first != prev) {
color[p.first] = p.second % 2 ? !color[now] : color[now];
dfs(p.first, now);
}
}
}
int main() {
int n, u, v, w;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v >> w;
neighbour[u].push_back(make_pair(v, w));
neighbour[v].push_back(make_pair(u, w));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
return 0;
} | #include <iostream>
#include <utility>
#include <vector>
using namespace std;
vector<pair<int, int>> neighbour[100001];
int color[100001];
void dfs(int now, int prev) {
for (auto p : neighbour[now]) {
if (p.first != prev) {
color[p.first] = p.second % 2 ? !color[now] : color[now];
dfs(p.first, now);
}
}
}
int main() {
int n, u, v, w;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v >> w;
neighbour[u].push_back(make_pair(v, w));
neighbour[v].push_back(make_pair(u, w));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
return 0;
} | replace | 5 | 7 | 5 | 7 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define rint register int
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
struct Edge {
int vv, next, ww;
} e[maxn];
int n, u, v, w, tot;
int head[maxn], clr[maxn];
void add(int u, int v, int w) {
e[++tot].vv = v;
e[tot].ww = w;
e[tot].next = head[u];
head[u] = tot;
}
void dfs(int x, int fa) {
for (rint i = head[x]; i; i = e[i].next) {
int kx = e[i].vv;
if (fa == kx)
continue;
clr[kx] = clr[x] ^ (e[i].ww % 2);
dfs(kx, x);
}
}
int main() {
scanf("%d", &n);
for (rint i = 1; i < n; ++i)
scanf("%d%d%d", &u, &v, &w), add(u, v, w), add(v, u, w);
clr[1] = 1;
dfs(1, 0);
for (rint i = 1; i <= n; ++i)
printf("%d\n", clr[i]);
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define rint register int
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
struct Edge {
int vv, next, ww;
} e[2 * maxn];
int n, u, v, w, tot;
int head[maxn], clr[maxn];
void add(int u, int v, int w) {
e[++tot].vv = v;
e[tot].ww = w;
e[tot].next = head[u];
head[u] = tot;
}
void dfs(int x, int fa) {
for (rint i = head[x]; i; i = e[i].next) {
int kx = e[i].vv;
if (fa == kx)
continue;
clr[kx] = clr[x] ^ (e[i].ww % 2);
dfs(kx, x);
}
}
int main() {
scanf("%d", &n);
for (rint i = 1; i < n; ++i)
scanf("%d%d%d", &u, &v, &w), add(u, v, w), add(v, u, w);
clr[1] = 1;
dfs(1, 0);
for (rint i = 1; i <= n; ++i)
printf("%d\n", clr[i]);
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i, s, n) for (ll(i) = (s); (i) < (n); ++(i))
#define rep(i, n) reps(i, 0, n)
#define reptr(i, n) for (ll(i) = (n); (i) >= 0; --(i))
#define All(x) (x).begin(), (x).end()
#define Rall(a) (a).rbegin(), (a).rend()
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define mp make_pair
#define pb push_back
const ll MOD = 1e9 + 7;
const ll INF = 1e10 + 1;
using namespace std;
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
ll N;
vector<bool> used;
vector<vector<pair<ll, ll>>> node;
// vector<vector<ll> > l;
vector<bool> color;
void dfs(ll id, ll value);
int main() {
cin >> N;
used = vector<bool>(N + 1, false);
node = vector<vector<pair<ll, ll>>>(N + 1);
// l = vector<vector<ll> >(N+1,vector<ll>(N+1,INF));
color = vector<bool>(N + 1, 0);
for (ll i = 0; i < N - 1; i++) {
ll u, v, w;
cin >> u >> v >> w;
node[u].pb({v, w});
node[v].pb({u, w});
}
color[1] = 1;
dfs(1, 0);
for (ll i = 1; i <= N; i++) {
if (color[i])
putchar('1');
else
putchar('0');
putchar('\n');
}
}
void dfs(ll id, ll value) {
used[id] = true;
for (ll key = 0; key < node[id].size(); key++) {
ll nxnode = node[id][key].first;
if (used[nxnode])
continue;
ll nxvalue = value + node[id][key].second;
if (used[nxnode] == false) {
if (nxvalue % 2 == 0) {
color[nxvalue] = 1;
}
dfs(nxnode, nxvalue);
}
}
} | #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i, s, n) for (ll(i) = (s); (i) < (n); ++(i))
#define rep(i, n) reps(i, 0, n)
#define reptr(i, n) for (ll(i) = (n); (i) >= 0; --(i))
#define All(x) (x).begin(), (x).end()
#define Rall(a) (a).rbegin(), (a).rend()
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define mp make_pair
#define pb push_back
const ll MOD = 1e9 + 7;
const ll INF = 1e10 + 1;
using namespace std;
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
ll N;
vector<bool> used;
vector<vector<pair<ll, ll>>> node;
// vector<vector<ll> > l;
vector<bool> color;
void dfs(ll id, ll value);
int main() {
cin >> N;
used = vector<bool>(N + 1, false);
node = vector<vector<pair<ll, ll>>>(N + 1);
// l = vector<vector<ll> >(N+1,vector<ll>(N+1,INF));
color = vector<bool>(N + 1, 0);
for (ll i = 0; i < N - 1; i++) {
ll u, v, w;
cin >> u >> v >> w;
node[u].pb({v, w});
node[v].pb({u, w});
}
color[1] = 1;
dfs(1, 0);
for (ll i = 1; i <= N; i++) {
if (color[i])
putchar('1');
else
putchar('0');
putchar('\n');
}
}
void dfs(ll id, ll value) {
used[id] = true;
for (ll key = 0; key < node[id].size(); key++) {
ll nxnode = node[id][key].first;
if (used[nxnode])
continue;
ll nxvalue = value + node[id][key].second;
if (used[nxnode] == false) {
if (nxvalue % 2 == 0) {
color[nxnode] = 1;
}
dfs(nxnode, nxvalue);
}
}
} | replace | 65 | 66 | 65 | 66 | 0 | |
p03044 | 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>;
// vector<int> to[100005], co[100005];
int main() {
int n;
cin >> n;
vector<vector<int>> to(n), co(n);
rep(i, n - 1) {
int a, b, w;
cin >> a >> b >> w;
a--;
b--;
cin >> a >> b >> w;
to[a].push_back(b);
co[a].push_back(w);
to[b].push_back(a);
co[b].push_back(w);
}
vector<int> ans(n, -1);
queue<int> q;
ans[0] = 0;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
rep(i, to[v].size()) {
int u = to[v][i];
int w = co[v][i];
if (ans[u] != -1)
continue;
ans[u] = (ans[v] + w) % 2;
q.push(u);
}
}
rep(i, n) cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
// vector<int> to[100005], co[100005];
int main() {
int n;
cin >> n;
vector<vector<int>> to(n), co(n);
rep(i, n - 1) {
int a, b, w;
cin >> a >> b >> w;
a--;
b--;
to[a].push_back(b);
co[a].push_back(w);
to[b].push_back(a);
co[b].push_back(w);
}
vector<int> ans(n, -1);
queue<int> q;
ans[0] = 0;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
rep(i, to[v].size()) {
int u = to[v][i];
int w = co[v][i];
if (ans[u] != -1)
continue;
ans[u] = (ans[v] + w) % 2;
q.push(u);
}
}
rep(i, n) cout << ans[i] << endl;
return 0;
} | delete | 17 | 18 | 17 | 17 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
#define REP(i, n) for (int i = 0; i < n; i++)
int main() {
int N;
cin >> N;
vector<vector<int>> a(N, vector<int>(N));
vector<vector<int>> cost(N, vector<int>(N));
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
a[u].emplace_back(v);
cost[u].emplace_back(w);
a[v].emplace_back(u);
cost[v].emplace_back(w);
}
vector<int> ans(N, -1);
queue<int> q;
ans[0] = 0;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i < a[v].size(); i++) {
int u = a[v][i];
int w = cost[v][i];
if (ans[u] != -1)
continue;
ans[u] = (ans[v] + w) % 2;
q.push(u);
}
}
for (auto iter : ans) {
cout << iter << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
#define REP(i, n) for (int i = 0; i < n; i++)
int main() {
int N;
cin >> N;
vector<vector<int>> a(N);
vector<vector<int>> cost(N);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
a[u].emplace_back(v);
cost[u].emplace_back(w);
a[v].emplace_back(u);
cost[v].emplace_back(w);
}
vector<int> ans(N, -1);
queue<int> q;
ans[0] = 0;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i < a[v].size(); i++) {
int u = a[v][i];
int w = cost[v][i];
if (ans[u] != -1)
continue;
ans[u] = (ans[v] + w) % 2;
q.push(u);
}
}
for (auto iter : ans) {
cout << iter << endl;
}
} | replace | 16 | 18 | 16 | 18 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void a() {
int N, K;
cin >> N >> K;
string S;
cin >> S;
if (S[K - 1] == 'A')
S[K - 1] = 'a';
if (S[K - 1] == 'B')
S[K - 1] = 'b';
if (S[K - 1] == 'C')
S[K - 1] = 'c';
cout << S;
}
void b() {
string S;
cin >> S;
int fast = stoi(S.substr(0, 2));
int last = stoi(S.substr(2, 2));
bool may_yymm = (0 <= fast && fast <= 99) && (1 <= last && last <= 12);
bool may_mmyy = (0 <= last && last <= 99) && (1 <= fast && fast <= 12);
if (may_yymm && !may_mmyy)
cout << "YYMM";
else if (!may_yymm && may_mmyy)
cout << "MMYY";
else if (may_mmyy && may_yymm)
cout << "AMBIGUOUS";
else if (!may_mmyy && !may_yymm)
cout << "NA";
}
int pow(int x) {
if (x <= 0)
return 1;
return 2 * (pow(x - 1));
}
void c() {
int N, K;
cin >> N >> K;
vector<int> vec;
vec.push_back((N >= K) ? (N - K + 1) : 0);
int k = K;
while (k > 1) {
vec.push_back(max(0, min(k, N + 1) - ((k + 1) / 2)));
k = (k + 1) / 2;
}
long long upper = 0;
for (int i = 0; i < vec.size(); i++) {
upper += vec[i] * pow(vec.size() - 1 - i);
}
double p = static_cast<double>(upper) / (N * pow(vec.size() - 1));
cout << fixed;
cout << setprecision(12) << p;
}
char flip_color(char color) {
if (color == '0')
return '1';
else
return '0';
}
void dfs_color(int node, int parent, char required_color,
const vector<vector<pair<int, int>>> &adj,
vector<char> &color_map) {
color_map[node] = required_color;
for (auto child_weight : adj[node]) {
int child = child_weight.first;
int weigh = child_weight.second;
if (child != parent) {
if (weigh % 2 == 0) {
dfs_color(child, node, required_color, adj, color_map);
} else {
dfs_color(child, node, flip_color(required_color), adj, color_map);
}
}
}
}
void d() {
int N;
cin >> N;
vector<vector<pair<int, int>>> adj(N + 1, vector<pair<int, int>>());
for (int i = 1; i <= N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
vector<char> color(N, '0');
dfs_color(1, -1, '0', adj, color);
for (int i = 1; i <= N; i++) {
cout << color[i] << endl;
}
}
int main() {
d();
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void a() {
int N, K;
cin >> N >> K;
string S;
cin >> S;
if (S[K - 1] == 'A')
S[K - 1] = 'a';
if (S[K - 1] == 'B')
S[K - 1] = 'b';
if (S[K - 1] == 'C')
S[K - 1] = 'c';
cout << S;
}
void b() {
string S;
cin >> S;
int fast = stoi(S.substr(0, 2));
int last = stoi(S.substr(2, 2));
bool may_yymm = (0 <= fast && fast <= 99) && (1 <= last && last <= 12);
bool may_mmyy = (0 <= last && last <= 99) && (1 <= fast && fast <= 12);
if (may_yymm && !may_mmyy)
cout << "YYMM";
else if (!may_yymm && may_mmyy)
cout << "MMYY";
else if (may_mmyy && may_yymm)
cout << "AMBIGUOUS";
else if (!may_mmyy && !may_yymm)
cout << "NA";
}
int pow(int x) {
if (x <= 0)
return 1;
return 2 * (pow(x - 1));
}
void c() {
int N, K;
cin >> N >> K;
vector<int> vec;
vec.push_back((N >= K) ? (N - K + 1) : 0);
int k = K;
while (k > 1) {
vec.push_back(max(0, min(k, N + 1) - ((k + 1) / 2)));
k = (k + 1) / 2;
}
long long upper = 0;
for (int i = 0; i < vec.size(); i++) {
upper += vec[i] * pow(vec.size() - 1 - i);
}
double p = static_cast<double>(upper) / (N * pow(vec.size() - 1));
cout << fixed;
cout << setprecision(12) << p;
}
char flip_color(char color) {
if (color == '0')
return '1';
else
return '0';
}
void dfs_color(int node, int parent, char required_color,
const vector<vector<pair<int, int>>> &adj,
vector<char> &color_map) {
color_map[node] = required_color;
for (auto child_weight : adj[node]) {
int child = child_weight.first;
int weigh = child_weight.second;
if (child != parent) {
if (weigh % 2 == 0) {
dfs_color(child, node, required_color, adj, color_map);
} else {
dfs_color(child, node, flip_color(required_color), adj, color_map);
}
}
}
}
void d() {
int N;
cin >> N;
vector<vector<pair<int, int>>> adj(N + 1, vector<pair<int, int>>());
for (int i = 1; i <= N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
vector<char> color(N + 1, '0');
dfs_color(1, -1, '0', adj, color);
for (int i = 1; i <= N; i++) {
cout << color[i] << endl;
}
}
int main() {
d();
return 0;
} | replace | 104 | 105 | 104 | 105 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;
void dfs(int v, int dist) {
ans.at(v) = dist % 2;
if (accumulate(V.begin(), V.end(), 0) == V.size() - 1)
return;
for (auto g : G.at(v)) {
if (V.at(g.first))
continue;
V.at(v) = 1;
dfs(g.first, dist + g.second);
V.at(v) = 0;
}
}
int main() {
int N;
cin >> N;
G.resize(N);
V.resize(N);
ans.resize(N);
for (int i = 0, a, b, c; cin >> a >> b >> c; i++) {
G.at(--a).push_back({--b, c % 2});
G.at(b).push_back({a, c % 2});
}
V.at(0) = 1;
dfs(0, 0);
for (auto a : ans)
cout << a << "\n";
} | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;
void dfs(int v, int dist) {
ans.at(v) = dist % 2;
for (auto g : G.at(v)) {
if (V.at(g.first))
continue;
V.at(v) = 1;
dfs(g.first, dist + g.second);
V.at(v) = 0;
}
}
int main() {
int N;
cin >> N;
G.resize(N);
V.resize(N);
ans.resize(N);
for (int i = 0, a, b, c; cin >> a >> b >> c; i++) {
G.at(--a).push_back({--b, c % 2});
G.at(b).push_back({a, c % 2});
}
V.at(0) = 1;
dfs(0, 0);
for (auto a : ans)
cout << a << "\n";
} | delete | 9 | 11 | 9 | 9 | TLE | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;
void dfs(int v, int dist) {
V.at(v) = 1;
ans.at(v) = dist % 2;
if (accumulate(V.begin(), V.end(), 0L) == N)
return;
for (auto g : G.at(v)) {
if (V.at(g.first))
continue;
dfs(g.first, dist + g.second);
}
}
int main() {
cin >> N;
G.resize(N);
V.resize(N);
ans.resize(N);
for (int a, b, c; cin >> a >> b >> c;) {
G.at(--a).push_back({--b, c % 2});
G.at(b).push_back({a, c % 2});
}
dfs(0, 0);
for (auto a : ans)
cout << a << "\n";
} | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<pair<int, int>>> G;
vector<int> V;
vector<int> ans;
void dfs(int v, int dist) {
V.at(v) = 1;
ans.at(v) = dist % 2;
// if (accumulate(V.begin(), V.end(), 0L) == N) return;
for (auto g : G.at(v)) {
if (V.at(g.first))
continue;
dfs(g.first, dist + g.second);
}
}
int main() {
cin >> N;
G.resize(N);
V.resize(N);
ans.resize(N);
for (int a, b, c; cin >> a >> b >> c;) {
G.at(--a).push_back({--b, c % 2});
G.at(b).push_back({a, c % 2});
}
dfs(0, 0);
for (auto a : ans)
cout << a << "\n";
} | replace | 11 | 13 | 11 | 12 | TLE | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
vector<int> ans;
void dfs(int v, int dist) {
seen.at(v) = 1;
ans.at(v) = dist % 2;
if (accumulate(seen.begin(), seen.end(), 0) == N)
return;
for (auto to : G.at(v)) {
if (!seen.at(to.first))
dfs(to.first, dist + to.second);
}
}
int main() {
cin >> N;
G.resize(N);
seen.resize(N);
ans.resize(N);
for (int a, b, c; cin >> a >> b >> c;) {
G.at(--a).push_back({--b, c % 2});
G.at(b).push_back({a, c % 2});
}
dfs(0, 0);
for (auto a : ans)
cout << a << "\n";
} | #include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<pair<int, int>>> G;
vector<int> seen;
vector<int> ans;
void dfs(int v, int dist) {
seen.at(v) = 1;
ans.at(v) = dist % 2;
// if (accumulate(seen.begin(), seen.end(), 0) == N) return;
for (auto to : G.at(v)) {
if (!seen.at(to.first))
dfs(to.first, dist + to.second);
}
}
int main() {
cin >> N;
G.resize(N);
seen.resize(N);
ans.resize(N);
for (int a, b, c; cin >> a >> b >> c;) {
G.at(--a).push_back({--b, c % 2});
G.at(b).push_back({a, c % 2});
}
dfs(0, 0);
for (auto a : ans)
cout << a << "\n";
} | replace | 11 | 13 | 11 | 12 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> tree;
vector<int> col;
void dfs(int current, int c) {
col.at(current) = c;
for (pair<int, int> e : tree.at(current)) {
int to = e.first;
if (col.at(to) != -1)
continue;
int dist = e.second;
if (dist % 2 == 0) {
dfs(to, c);
} else {
dfs(to, 1 - c);
}
}
}
int main() {
int N;
cin >> N;
tree.resize(N);
col.resize(N, -1);
for (int i = 0; i < N; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
tree.at(u).push_back(make_pair(v, w));
tree.at(v).push_back(make_pair(u, w));
}
dfs(0, 0);
for (int i = 0; i < N; i++) {
cout << col.at(i) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> tree;
vector<int> col;
void dfs(int current, int c) {
col.at(current) = c;
for (pair<int, int> e : tree.at(current)) {
int to = e.first;
if (col.at(to) != -1)
continue;
int dist = e.second;
if (dist % 2 == 0) {
dfs(to, c);
} else {
dfs(to, 1 - c);
}
}
}
int main() {
int N;
cin >> N;
tree.resize(N);
col.resize(N, -1);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
tree.at(u).push_back(make_pair(v, w));
tree.at(v).push_back(make_pair(u, w));
}
dfs(0, 0);
for (int i = 0; i < N; i++) {
cout << col.at(i) << endl;
}
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
vector<vector<pair<ll, ll>>> g;
vector<ll> seen;
void dfs(int v, int p = -1, int c = 1) {
seen[v] = c;
for (auto x : g[v]) {
if (x.first == p)
continue;
if (x.second % 2 == 0)
dfs(x.first, v, c);
else
dfs(x.first, v, 1 - c);
}
}
int main() {
int n;
cin >> n;
g.resize(n);
seen.assign(n, -1);
REP(i, n - 1) {
ll a, b, w;
cin >> a >> b >> w;
a--;
b--;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
REP(i, n) dfs(i);
REP(i, n) cout << seen[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
vector<vector<pair<ll, ll>>> g;
vector<ll> seen;
void dfs(int v, int p = -1, int c = 1) {
seen[v] = c;
for (auto x : g[v]) {
if (x.first == p)
continue;
if (x.second % 2 == 0)
dfs(x.first, v, c);
else
dfs(x.first, v, 1 - c);
}
}
int main() {
int n;
cin >> n;
g.resize(n);
seen.assign(n, -1);
REP(i, n - 1) {
ll a, b, w;
cin >> a >> b >> w;
a--;
b--;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
dfs(0);
REP(i, n) cout << seen[i] << endl;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct pp {
ll fi;
ll se;
ll th;
};
ll n, a, b, c, d[100000];
queue<pp> q;
vector<P> v[100000];
int main(void) {
cin >> n;
d[0] = 1;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
a--, b--;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
for (auto x : v[0]) {
struct pp P1;
P1.fi = x.first, P1.se = x.second, P1.th = 0LL;
q.push(P1);
}
while (!q.empty()) {
struct pp p = q.front();
q.pop();
if (d[p.fi] == 0) {
if (p.se % 2 == 0)
d[p.fi] = d[p.th];
else
d[p.fi] = -d[p.th];
for (auto y : v[p.fi]) {
struct pp P2;
P2.fi = y.first, P2.se = y.second, P2.th = p.fi;
q.push(P2);
}
}
}
for (int i = 0; i < n; i++) {
cout << (d[i] == 1) << endl;
}
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct pp {
ll fi;
ll se;
ll th;
};
ll n, a, b, c, d[100000];
queue<pp> q;
vector<P> v[100000];
int main(void) {
cin >> n;
d[0] = 1;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b >> c;
a--, b--;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
for (auto x : v[0]) {
struct pp P1;
P1.fi = x.first, P1.se = x.second, P1.th = 0LL;
q.push(P1);
}
while (!q.empty()) {
struct pp p = q.front();
q.pop();
if (d[p.fi] == 0) {
if (p.se % 2 == 0)
d[p.fi] = d[p.th];
else
d[p.fi] = -d[p.th];
for (auto y : v[p.fi]) {
struct pp P2;
P2.fi = y.first, P2.se = y.second, P2.th = p.fi;
q.push(P2);
}
}
}
for (int i = 0; i < n; i++) {
cout << (d[i] == 1) << endl;
}
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, c) \
for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
// priority_queue<ll> max;//大きい順
// priority_queue<ll, Array, greater<ll>> min;//小さい順
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// sortは初期で昇順 greater<hoge>()で降順
// substr 文字列取り出し
// upper_bound
// ある値より大きい一番左のイテレータを返す、lowerは以上(setに対して使うとO(N)なので、setのメンバ関数を使う
// stoi
struct Edge { // グラフ
ll to, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag,
ll revCap) { // 最大フロー求める Ford-fulkerson
G[from].push_back(Edge(to, cap, (ll)G[to].size()));
if (revFlag)
G[to].push_back(Edge(
from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする
}
ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) {
if (v == t)
return f;
used[v] = true;
for (int i = 0; i < G[v].size(); ++i) {
Edge &e = G[v][i];
if (!used[e.to] && e.cap > 0) {
ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used);
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
ll max_flow(Graph &G, ll s, ll t) {
ll flow = 0;
for (;;) {
vector<bool> used(G.size());
REP(i, used.size()) used[i] = false;
ll f = max_flow_dfs(G, s, t, INF, used);
if (f == 0) {
return flow;
}
flow += f;
}
}
void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|)
d.resize(G.size());
negative.resize(G.size());
REP(i, d.size()) d[i] = INF;
REP(i, d.size()) negative[i] = false;
d[s] = 0;
REP(k, G.size() - 2) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
}
}
}
}
REP(k, G.size() - 2) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
negative[G[i][j].to] = true;
}
if (negative[i] == true)
negative[G[i][j].to] = true;
}
}
}
}
void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|)
d.resize(G.size());
REP(i, d.size()) d[i] = INF;
d[s] = 0;
priority_queue<P, vector<P>, greater<P>> q;
q.push(make_pair(0, s));
while (!q.empty()) {
P a = q.top();
q.pop();
if (d[a.second] < a.first)
continue;
REP(i, G[a.second].size()) {
Edge e = G[a.second][i];
if (d[e.to] > d[a.second] + e.cap) {
d[e.to] = d[a.second] + e.cap;
q.push(make_pair(d[e.to], e.to));
}
}
}
}
void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3)
d.resize(G.size());
REP(i, d.size()) d[i].resize(G.size());
REP(i, d.size()) {
REP(j, d[i].size()) { d[i][j] = INF; }
}
REP(i, G.size()) {
REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; }
}
REP(i, G.size()) {
REP(j, G.size()) {
REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); }
}
}
}
class UnionFind {
vector<int> data;
public:
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) { // xとyの集合を統合する
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { // xとyが同じ集合か返す
return root(x) == root(y);
}
int root(int x) { // xのルートを返す
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) { // xの集合のサイズを返す
return -data[root(x)];
}
};
class SumSegTree {
private:
// 区間[a,b)の総和。ノードk=[l,r)に着目している。
int _sum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0; // 交差しない
if (a <= l && r <= b)
return dat[k]; // a,l,r,bの順で完全に含まれる
else {
int s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
int s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
return s1 + s2;
}
}
public:
int n, height;
vector<int> dat;
// 初期化(_nは最大要素数)
SumSegTree(int _n) {
n = 1;
height = 1;
while (n < _n) {
n *= 2;
height++;
}
dat = vector<int>(2 * n - 1);
}
// 場所i(0-indexed)にxを足す
void add(int i, int x) {
i += n - 1; // i番目の葉ノードへ
dat[i] += x;
while (i > 0) { // 下から上がっていく
i = (i - 1) / 2;
dat[i] += x;
}
}
// 内部クエリ_sum()を呼び出す
int sum(int a, int b) { return _sum(a, b, 0, 0, n); }
};
// 約数求める //約数
void divisor(ll n, vector<ll> &ret) {
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
}
// nCrとか
class Combination {
public:
Array fact;
Array inv;
ll mod;
ll mod_inv(ll x) {
ll n = mod - 2LL;
ll res = 1LL;
while (n > 0) {
if (n & 1)
res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; }
ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; }
Combination(ll n, ll _mod) {
mod = _mod;
fact.resize(n + 1);
fact[0] = 1;
REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; }
inv.resize(n + 1);
REP(i, n + 1) { inv[i] = mod_inv(fact[i]); }
}
};
bool compare_by_b(pair<ll, ll> a, pair<ll, ll> b) { // 降順second
if (a.second != b.second) {
return a.second > b.second;
} else {
return a.first > b.first;
}
}
bool compare_by_a(pair<ll, ll> a, pair<ll, ll> b) { // 降順first
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second > b.second;
}
}
ll gcd(ll m, ll n) {
if (n == 0)
return m;
return gcd(n, m % n);
} // gcd
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
ll color[100010];
void dfs(Graph &graph, ll v) {
REP(i, graph[v].size()) {
if (color[graph[v][i].to] == 0) {
if (graph[v][i].cap % 2 == 0) {
color[graph[v][i].to] = color[v];
} else {
color[graph[v][i].to] = color[v] ^ 1;
}
dfs(graph, graph[v][i].to);
}
}
}
int main() {
ll n;
cin >> n;
Graph graph(n + 1);
REP(i, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
add_edge(graph, u, v, w, true, w);
}
color[1] = 1;
dfs(graph, 1);
REP(i, n) {
if (color[i + 1] == 1) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
| #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, c) \
for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
// priority_queue<ll> max;//大きい順
// priority_queue<ll, Array, greater<ll>> min;//小さい順
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// sortは初期で昇順 greater<hoge>()で降順
// substr 文字列取り出し
// upper_bound
// ある値より大きい一番左のイテレータを返す、lowerは以上(setに対して使うとO(N)なので、setのメンバ関数を使う
// stoi
struct Edge { // グラフ
ll to, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag,
ll revCap) { // 最大フロー求める Ford-fulkerson
G[from].push_back(Edge(to, cap, (ll)G[to].size()));
if (revFlag)
G[to].push_back(Edge(
from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする
}
ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) {
if (v == t)
return f;
used[v] = true;
for (int i = 0; i < G[v].size(); ++i) {
Edge &e = G[v][i];
if (!used[e.to] && e.cap > 0) {
ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used);
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
ll max_flow(Graph &G, ll s, ll t) {
ll flow = 0;
for (;;) {
vector<bool> used(G.size());
REP(i, used.size()) used[i] = false;
ll f = max_flow_dfs(G, s, t, INF, used);
if (f == 0) {
return flow;
}
flow += f;
}
}
void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|)
d.resize(G.size());
negative.resize(G.size());
REP(i, d.size()) d[i] = INF;
REP(i, d.size()) negative[i] = false;
d[s] = 0;
REP(k, G.size() - 2) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
}
}
}
}
REP(k, G.size() - 2) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
negative[G[i][j].to] = true;
}
if (negative[i] == true)
negative[G[i][j].to] = true;
}
}
}
}
void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|)
d.resize(G.size());
REP(i, d.size()) d[i] = INF;
d[s] = 0;
priority_queue<P, vector<P>, greater<P>> q;
q.push(make_pair(0, s));
while (!q.empty()) {
P a = q.top();
q.pop();
if (d[a.second] < a.first)
continue;
REP(i, G[a.second].size()) {
Edge e = G[a.second][i];
if (d[e.to] > d[a.second] + e.cap) {
d[e.to] = d[a.second] + e.cap;
q.push(make_pair(d[e.to], e.to));
}
}
}
}
void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3)
d.resize(G.size());
REP(i, d.size()) d[i].resize(G.size());
REP(i, d.size()) {
REP(j, d[i].size()) { d[i][j] = INF; }
}
REP(i, G.size()) {
REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; }
}
REP(i, G.size()) {
REP(j, G.size()) {
REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); }
}
}
}
class UnionFind {
vector<int> data;
public:
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) { // xとyの集合を統合する
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { // xとyが同じ集合か返す
return root(x) == root(y);
}
int root(int x) { // xのルートを返す
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) { // xの集合のサイズを返す
return -data[root(x)];
}
};
class SumSegTree {
private:
// 区間[a,b)の総和。ノードk=[l,r)に着目している。
int _sum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0; // 交差しない
if (a <= l && r <= b)
return dat[k]; // a,l,r,bの順で完全に含まれる
else {
int s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
int s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
return s1 + s2;
}
}
public:
int n, height;
vector<int> dat;
// 初期化(_nは最大要素数)
SumSegTree(int _n) {
n = 1;
height = 1;
while (n < _n) {
n *= 2;
height++;
}
dat = vector<int>(2 * n - 1);
}
// 場所i(0-indexed)にxを足す
void add(int i, int x) {
i += n - 1; // i番目の葉ノードへ
dat[i] += x;
while (i > 0) { // 下から上がっていく
i = (i - 1) / 2;
dat[i] += x;
}
}
// 内部クエリ_sum()を呼び出す
int sum(int a, int b) { return _sum(a, b, 0, 0, n); }
};
// 約数求める //約数
void divisor(ll n, vector<ll> &ret) {
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
}
// nCrとか
class Combination {
public:
Array fact;
Array inv;
ll mod;
ll mod_inv(ll x) {
ll n = mod - 2LL;
ll res = 1LL;
while (n > 0) {
if (n & 1)
res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; }
ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; }
Combination(ll n, ll _mod) {
mod = _mod;
fact.resize(n + 1);
fact[0] = 1;
REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; }
inv.resize(n + 1);
REP(i, n + 1) { inv[i] = mod_inv(fact[i]); }
}
};
bool compare_by_b(pair<ll, ll> a, pair<ll, ll> b) { // 降順second
if (a.second != b.second) {
return a.second > b.second;
} else {
return a.first > b.first;
}
}
bool compare_by_a(pair<ll, ll> a, pair<ll, ll> b) { // 降順first
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second > b.second;
}
}
ll gcd(ll m, ll n) {
if (n == 0)
return m;
return gcd(n, m % n);
} // gcd
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
ll color[100010];
void dfs(Graph &graph, ll v) {
REP(i, graph[v].size()) {
if (color[graph[v][i].to] == 0) {
if (graph[v][i].cap % 2 == 0) {
color[graph[v][i].to] = color[v];
} else {
if (color[v] == 1) {
color[graph[v][i].to] = -1;
} else {
color[graph[v][i].to] = 1;
}
}
dfs(graph, graph[v][i].to);
}
}
}
int main() {
ll n;
cin >> n;
Graph graph(n + 1);
REP(i, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
add_edge(graph, u, v, w, true, w);
}
color[1] = 1;
dfs(graph, 1);
REP(i, n) {
if (color[i + 1] == 1) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
| replace | 303 | 304 | 303 | 308 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;
struct Edge {
int to; // 辺の行き先
int weight; // 辺の重み
Edge(int t, int w) : to(t), weight(w) {}
};
using vve = vector<vector<Edge>>;
vi ans;
void dfs(vve graph, int v) {
for (auto n : graph[v]) {
int u = n.to;
int w = n.weight;
if (ans[u] != -1)
continue;
ans[u] = (ans[v] + w) % 2;
dfs(graph, u);
}
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
vve graph(N);
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
--u;
--v;
graph[u].push_back(Edge(v, w));
graph[v].push_back(Edge(u, w));
}
ans.assign(N, -1);
ans[0] = 0;
dfs(graph, 0);
for (auto n : ans) {
cout << n << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;
struct Edge {
int to; // 辺の行き先
int weight; // 辺の重み
Edge(int t, int w) : to(t), weight(w) {}
};
using vve = vector<vector<Edge>>;
vi ans;
void dfs(vve &graph, int v) {
for (auto n : graph[v]) {
int u = n.to;
int w = n.weight;
if (ans[u] != -1)
continue;
ans[u] = (ans[v] + w) % 2;
dfs(graph, u);
}
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
vve graph(N);
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
--u;
--v;
graph[u].push_back(Edge(v, w));
graph[v].push_back(Edge(u, w));
}
ans.assign(N, -1);
ans[0] = 0;
dfs(graph, 0);
for (auto n : ans) {
cout << n << endl;
}
return 0;
} | replace | 20 | 21 | 20 | 21 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<vector<pair<long, long>>> e(
N + 1, vector<pair<long, long>>{}); // dest, weight
for (int i = 0; i < N - 1; ++i) {
long u, v, w;
cin >> u >> v >> w;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
vector<int> c(N);
int count = 0;
function<void(int, int, int, int)> solve =
[&](int current, int prev_node, int prev_color, int prev_dist) {
c[current] = (prev_color + prev_dist) % 2;
++count;
if (count >= N)
return;
for (auto p : e[current]) {
if (p.first != prev_node) {
solve(p.first, current, c[current], p.second);
}
}
};
solve(1, 0, 0, 0);
for (int i = 1; i <= N; ++i) {
cout << c[i] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<vector<pair<long, long>>> e(
N + 1, vector<pair<long, long>>{}); // dest, weight
for (int i = 0; i < N - 1; ++i) {
long u, v, w;
cin >> u >> v >> w;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
vector<int> c(N + 1);
int count = 0;
function<void(int, int, int, int)> solve =
[&](int current, int prev_node, int prev_color, int prev_dist) {
c[current] = (prev_color + prev_dist) % 2;
++count;
if (count >= N)
return;
for (auto p : e[current]) {
if (p.first != prev_node) {
solve(p.first, current, c[current], p.second);
}
}
};
solve(1, 0, 0, 0);
for (int i = 1; i <= N; ++i) {
cout << c[i] << endl;
}
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<VV> VVV;
vector<pair<int, int>> G[10000];
/*
辺に属性がある場合
struct edge{int to,cost;};
vector<edge> G[10000];
*/
long long int co[1000000], t[1000000];
void Representing_graph(long long int s, long long int t, long long int c) {
G[s].push_back(make_pair(t, c));
G[t].push_back(make_pair(s, c));
}
void dfs(long long int n, long long int r) {
t[n] = 1;
for (long long int i = 0; i < G[n].size(); i++) {
long long int len = G[n][i].second, po = G[n][i].first;
if ((r + len) % 2 == 0) {
co[po] = 1;
}
if (t[po] == 0)
dfs(po, r + len);
}
return;
};
int main() {
long long int n, a, b, c;
cin >> n;
vector<pair<long long int, long long int>> vec(n);
for (long long int i = 0; i < n - 1; i++) {
cin >> a >> b >> c;
Representing_graph(a - 1, b - 1, c);
}
co[0] = 1;
dfs(0, 0);
for (long long int i = 0; i < n; i++) {
cout << co[i] << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<VV> VVV;
vector<pair<int, int>> G[1000000];
/*
辺に属性がある場合
struct edge{int to,cost;};
vector<edge> G[10000];
*/
long long int co[1000000], t[1000000];
void Representing_graph(long long int s, long long int t, long long int c) {
G[s].push_back(make_pair(t, c));
G[t].push_back(make_pair(s, c));
}
void dfs(long long int n, long long int r) {
t[n] = 1;
for (long long int i = 0; i < G[n].size(); i++) {
long long int len = G[n][i].second, po = G[n][i].first;
if ((r + len) % 2 == 0) {
co[po] = 1;
}
if (t[po] == 0)
dfs(po, r + len);
}
return;
};
int main() {
long long int n, a, b, c;
cin >> n;
vector<pair<long long int, long long int>> vec(n);
for (long long int i = 0; i < n - 1; i++) {
cin >> a >> b >> c;
Representing_graph(a - 1, b - 1, c);
}
co[0] = 1;
dfs(0, 0);
for (long long int i = 0; i < n; i++) {
cout << co[i] << endl;
}
return 0;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 0x7fffffff;
const ll LINF = 0x7fffffffffffffff;
const int maxn = 1e5;
const int modn = 1e9 + 7;
struct node {
int p, w;
};
vector<node> tree[maxn + 10];
int color[maxn + 10];
void dfs(int o) {
int tmp = color[o];
for (auto it = tree[o].begin(); it != tree[o].end(); ++it) {
if (!color[(*it).p]) {
if (!((*it).w % 2))
color[(*it).p] = tmp;
else
color[(*it).p] = 3 - tmp;
dfs((*it).p);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, u, v, w;
cin >> n;
for (int i = 1; i < n; ++i) {
cin >> u >> v >> w;
tree[u].push_back({v, w});
tree[w].push_back({u, w});
}
color[1] = 1;
dfs(1);
for (int i = 1; i <= n; ++i) {
cout << color[i] % 2 << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 0x7fffffff;
const ll LINF = 0x7fffffffffffffff;
const int maxn = 1e5;
const int modn = 1e9 + 7;
struct node {
int p, w;
};
vector<node> tree[maxn + 10];
int color[maxn + 10];
void dfs(int o) {
int tmp = color[o];
for (auto it = tree[o].begin(); it != tree[o].end(); ++it) {
if (!color[(*it).p]) {
if (!((*it).w % 2))
color[(*it).p] = tmp;
else
color[(*it).p] = 3 - tmp;
dfs((*it).p);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, u, v, w;
cin >> n;
for (int i = 1; i < n; ++i) {
cin >> u >> v >> w;
tree[u].push_back({v, w});
tree[v].push_back({u, w});
}
color[1] = 1;
dfs(1);
for (int i = 1; i <= n; ++i) {
cout << color[i] % 2 << "\n";
}
return 0;
}
| replace | 36 | 37 | 36 | 37 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) REP(i, 0, n)
#define REP(i, l, r) for (long long i = l; i < r; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
class dijkstra {
int V;
int d[100002];
priority_queue<P, vector<P>, greater<P>> que;
void update(int s, vector<edge> G[]) {
fill(d, d + V + 1, 100000000000ll);
d[s] = 0;
que.push(make_pair(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
rep(i, G[v].size()) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(make_pair(d[e.to], e.to));
}
}
}
}
public:
dijkstra(int v, int s, vector<edge> G[]) {
V = v;
update(s, G);
}
int find(int e) { return d[e]; }
};
int gcd(int a, int b) {
if (a % b == 0)
return b;
else
return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool prime(int a) {
if (a == 1)
return false;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
class Union_Find {
private:
vector<int> t, s;
public:
Union_Find(int max_length) {
rep(i, max_length + 1) {
t.push_back(i);
s.push_back(1);
}
}
void Union(int x, int y) {
if (same(x, y))
return;
int tx = Find(x), ty = Find(y);
if (s[tx] < s[ty]) {
s[ty] += s[tx];
t[tx] = ty;
} else if (s[tx] > s[ty]) {
s[tx] += s[ty];
t[ty] = tx;
} else if (tx > ty) {
t[tx] = ty;
s[ty] += s[tx];
} else {
t[ty] = tx;
s[tx] += s[ty];
}
}
int Find(int n) {
if (t[n] == n)
return n;
else
return t[n] = Find(t[n]);
}
bool same(int x, int y) { return Find(x) == Find(y); }
int get_Size(int a) { return s[a]; }
int get_tree(int size) {
int cnt = 1;
vector<int> vec;
rep(i, size) {
if (vec.size() == 0)
vec.push_back(Find(i + 1));
else {
rep(j, vec.size()) {
if (vec[j] == Find(i + 1))
goto ioi;
}
vec.push_back(Find(i + 1));
cnt++;
ioi:;
}
}
return cnt;
}
};
signed main() {
int n, ans[100000];
vector<P> vec[100000];
cin >> n;
fill(ans, ans + n, 2);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
vec[u].push_back({v, w});
vec[v].push_back({u, w});
}
queue<int> que;
ans[0] = 0;
que.push(0);
while (!que.empty()) {
int a = que.front();
que.pop();
rep(i, vec[a + 1].size()) {
if (ans[vec[a + 1][i].first - 1] != 2)
continue;
if (vec[a + 1][i].second % 2 == 0 && ans[a] == 0)
ans[vec[a + 1][i].first - 1] = 0;
if (vec[a + 1][i].second % 2 == 0 && ans[a] == 1)
ans[vec[a + 1][i].first - 1] = 1;
if (vec[a + 1][i].second % 2 == 1 && ans[a] == 0)
ans[vec[a + 1][i].first - 1] = 1;
if (vec[a + 1][i].second % 2 == 1 && ans[a] == 1)
ans[vec[a + 1][i].first - 1] = 0;
que.push(vec[a + 1][i].first - 1);
}
}
rep(i, n) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) REP(i, 0, n)
#define REP(i, l, r) for (long long i = l; i < r; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
class dijkstra {
int V;
int d[100002];
priority_queue<P, vector<P>, greater<P>> que;
void update(int s, vector<edge> G[]) {
fill(d, d + V + 1, 100000000000ll);
d[s] = 0;
que.push(make_pair(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
rep(i, G[v].size()) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(make_pair(d[e.to], e.to));
}
}
}
}
public:
dijkstra(int v, int s, vector<edge> G[]) {
V = v;
update(s, G);
}
int find(int e) { return d[e]; }
};
int gcd(int a, int b) {
if (a % b == 0)
return b;
else
return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool prime(int a) {
if (a == 1)
return false;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
class Union_Find {
private:
vector<int> t, s;
public:
Union_Find(int max_length) {
rep(i, max_length + 1) {
t.push_back(i);
s.push_back(1);
}
}
void Union(int x, int y) {
if (same(x, y))
return;
int tx = Find(x), ty = Find(y);
if (s[tx] < s[ty]) {
s[ty] += s[tx];
t[tx] = ty;
} else if (s[tx] > s[ty]) {
s[tx] += s[ty];
t[ty] = tx;
} else if (tx > ty) {
t[tx] = ty;
s[ty] += s[tx];
} else {
t[ty] = tx;
s[tx] += s[ty];
}
}
int Find(int n) {
if (t[n] == n)
return n;
else
return t[n] = Find(t[n]);
}
bool same(int x, int y) { return Find(x) == Find(y); }
int get_Size(int a) { return s[a]; }
int get_tree(int size) {
int cnt = 1;
vector<int> vec;
rep(i, size) {
if (vec.size() == 0)
vec.push_back(Find(i + 1));
else {
rep(j, vec.size()) {
if (vec[j] == Find(i + 1))
goto ioi;
}
vec.push_back(Find(i + 1));
cnt++;
ioi:;
}
}
return cnt;
}
};
signed main() {
int n, ans[100000];
vector<P> vec[100012];
cin >> n;
fill(ans, ans + n, 2);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
vec[u].push_back({v, w});
vec[v].push_back({u, w});
}
queue<int> que;
ans[0] = 0;
que.push(0);
while (!que.empty()) {
int a = que.front();
que.pop();
rep(i, vec[a + 1].size()) {
if (ans[vec[a + 1][i].first - 1] != 2)
continue;
if (vec[a + 1][i].second % 2 == 0 && ans[a] == 0)
ans[vec[a + 1][i].first - 1] = 0;
if (vec[a + 1][i].second % 2 == 0 && ans[a] == 1)
ans[vec[a + 1][i].first - 1] = 1;
if (vec[a + 1][i].second % 2 == 1 && ans[a] == 0)
ans[vec[a + 1][i].first - 1] = 1;
if (vec[a + 1][i].second % 2 == 1 && ans[a] == 1)
ans[vec[a + 1][i].first - 1] = 0;
que.push(vec[a + 1][i].first - 1);
}
}
rep(i, n) cout << ans[i] << endl;
}
| replace | 115 | 116 | 115 | 116 | 0 | |
p03044 | C++ | Time Limit Exceeded | // Template //
#include <bits/stdc++.h>
using namespace std;
// マクロ //
#define rep(i, N) for (int i = 0; i < N; i++)
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) x.erase(unique(all(x)), x.end())
#define vsum(x) accumulate(all(x), 0)
#define cou(x) cout << x << endl
#define y() cout << "Yes" << endl
#define n() cout << "No" << endl
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define x2(x) (x) * (x)
// 型エイリアス //
using lint = long long;
using vi = vector<int>;
using vli = vector<lint>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using msi = map<string, int>;
// 関数 //
int gcd(int a, int b) {
int t;
while (b != 0) {
t = a % b;
a = b;
b = t;
}
return a;
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
double distance(pii a, pii b) {
double dist;
dist = sqrt(x2(a.first - b.first) + x2(a.second - b.second));
return dist;
}
lint perm(int a) {
lint perm = 1;
for (int i = a; i >= 1; i--) {
perm *= i;
}
return perm;
}
lint comb(int n, int m) { return perm(n) / (perm(n - m) * perm(m)); }
// 定数 //
#define pi acos(-1)
// End of Template //
vvi graph;
vector<tuple<int, int, int>> length;
vli color;
vb colored;
int getLength(int k, int n) {
int l = 0;
int min = 0;
int max = length.size();
int mid = max / 2;
int s = k > n ? n : k;
int b = k > n ? k : n;
while (1) {
if (s > get<0>(length[mid])) {
min = mid;
mid = (mid + max) / 2;
} else if (s < get<0>(length[mid])) {
max = mid;
mid = (mid + min) / 2;
} else {
if (b == get<1>(length[mid])) {
l = get<2>(length[mid]);
return l;
}
}
}
return l;
}
void coloring(int n, lint co) {
colored[n] = true;
for (auto i : graph[n]) {
if (colored[i])
continue;
color[i] = co + getLength(i, n);
coloring(i, color[i]);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
color.resize(N);
colored.assign(N, false);
length.resize(N - 1);
graph.assign(N, vi(0));
int a, b, l;
rep(i, N - 1) {
cin >> a >> b >> l;
graph[a - 1].push_back(b - 1);
graph[b - 1].push_back(a - 1);
length[i] = make_tuple(a > b ? b - 1, a - 1, l : a - 1, b - 1, l);
}
sort(length);
coloring(0, 0);
rep(i, N) { cou(color[i] % 2); }
} | // Template //
#include <bits/stdc++.h>
using namespace std;
// マクロ //
#define rep(i, N) for (int i = 0; i < N; i++)
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) x.erase(unique(all(x)), x.end())
#define vsum(x) accumulate(all(x), 0)
#define cou(x) cout << x << endl
#define y() cout << "Yes" << endl
#define n() cout << "No" << endl
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define x2(x) (x) * (x)
// 型エイリアス //
using lint = long long;
using vi = vector<int>;
using vli = vector<lint>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using msi = map<string, int>;
// 関数 //
int gcd(int a, int b) {
int t;
while (b != 0) {
t = a % b;
a = b;
b = t;
}
return a;
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
double distance(pii a, pii b) {
double dist;
dist = sqrt(x2(a.first - b.first) + x2(a.second - b.second));
return dist;
}
lint perm(int a) {
lint perm = 1;
for (int i = a; i >= 1; i--) {
perm *= i;
}
return perm;
}
lint comb(int n, int m) { return perm(n) / (perm(n - m) * perm(m)); }
// 定数 //
#define pi acos(-1)
// End of Template //
vvi graph;
vector<tuple<int, int, int>> length;
vli color;
vb colored;
int getLength(int k, int n) {
int l = 0;
int min = 0;
int max = length.size();
int mid = max / 2;
int s = k > n ? n : k;
int b = k > n ? k : n;
while (1) {
if (s > get<0>(length[mid])) {
min = mid;
mid = (mid + max) / 2;
} else if (s < get<0>(length[mid])) {
max = mid;
mid = (mid + min) / 2;
} else {
if (b == get<1>(length[mid])) {
l = get<2>(length[mid]);
return l;
} else if (b > get<1>(length[mid])) {
min = mid;
mid = (mid + max) / 2;
} else if (b < get<1>(length[mid])) {
max = mid;
mid = (mid + min) / 2;
}
}
}
return l;
}
void coloring(int n, lint co) {
colored[n] = true;
for (auto i : graph[n]) {
if (colored[i])
continue;
color[i] = co + getLength(i, n);
coloring(i, color[i]);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
color.resize(N);
colored.assign(N, false);
length.resize(N - 1);
graph.assign(N, vi(0));
int a, b, l;
rep(i, N - 1) {
cin >> a >> b >> l;
graph[a - 1].push_back(b - 1);
graph[b - 1].push_back(a - 1);
length[i] = make_tuple(a > b ? b - 1, a - 1, l : a - 1, b - 1, l);
}
sort(length);
coloring(0, 0);
rep(i, N) { cou(color[i] % 2); }
} | insert | 84 | 84 | 84 | 90 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define jh(x, y) x ^= y ^= x ^= y
#define loc(x, y) (x - 1) * m + y
#define rg register
#define inl inline
#define PI 3.141592654
typedef long long ll;
const int N = 6e2 + 5, INF = 0x3f3f3f3f, mod = 998244353;
using namespace std;
namespace fast_IO {
inl ll read() {
rg char c;
rg ll x = 0;
rg bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r')
;
if (c == EOF)
return c;
if (c == '-')
flag = true;
else
x = c ^ 48;
while ((c = getchar()) != ' ' && c != '\n' && c != '\r' && ~c)
x = (x << 1) + (x << 3) + (c ^ 48);
if (flag)
return -x;
return x;
}
inl ll max(rg ll a, rg ll b) {
if (a > b)
return a;
return b;
}
inl ll min(rg ll a, rg ll b) {
if (a < b)
return a;
return b;
}
void write(rg ll x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
write(x / 10);
putchar(x % 10 ^ 48);
}
} // namespace fast_IO
int p[N], b[N], nt[N], w[N], c[N], num;
inl void add(rg int z, rg int y, rg int x) {
b[++num] = y, w[num] = z;
nt[num] = p[x], p[x] = num;
b[++num] = x, w[num] = z;
nt[num] = p[y], p[y] = num;
}
void dfs(rg int x, rg int fa) {
for (rg int e = p[x]; e; e = nt[e]) {
if (b[e] == fa)
continue;
c[b[e]] = c[x] ^ (w[e] & 1);
dfs(b[e], x);
}
}
int main(void) {
rg int n = fast_IO::read();
for (rg int i = 1; i < n; ++i)
add(fast_IO::read(), fast_IO::read(), fast_IO::read());
c[1] = 1;
dfs(1, 0);
for (rg int i = 1; i <= n; ++i)
fast_IO::write(c[i]), putchar('\n');
return 0;
} | #include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define jh(x, y) x ^= y ^= x ^= y
#define loc(x, y) (x - 1) * m + y
#define rg register
#define inl inline
#define PI 3.141592654
typedef long long ll;
const int N = 6e5 + 5, INF = 0x3f3f3f3f, mod = 998244353;
using namespace std;
namespace fast_IO {
inl ll read() {
rg char c;
rg ll x = 0;
rg bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r')
;
if (c == EOF)
return c;
if (c == '-')
flag = true;
else
x = c ^ 48;
while ((c = getchar()) != ' ' && c != '\n' && c != '\r' && ~c)
x = (x << 1) + (x << 3) + (c ^ 48);
if (flag)
return -x;
return x;
}
inl ll max(rg ll a, rg ll b) {
if (a > b)
return a;
return b;
}
inl ll min(rg ll a, rg ll b) {
if (a < b)
return a;
return b;
}
void write(rg ll x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
write(x / 10);
putchar(x % 10 ^ 48);
}
} // namespace fast_IO
int p[N], b[N], nt[N], w[N], c[N], num;
inl void add(rg int z, rg int y, rg int x) {
b[++num] = y, w[num] = z;
nt[num] = p[x], p[x] = num;
b[++num] = x, w[num] = z;
nt[num] = p[y], p[y] = num;
}
void dfs(rg int x, rg int fa) {
for (rg int e = p[x]; e; e = nt[e]) {
if (b[e] == fa)
continue;
c[b[e]] = c[x] ^ (w[e] & 1);
dfs(b[e], x);
}
}
int main(void) {
rg int n = fast_IO::read();
for (rg int i = 1; i < n; ++i)
add(fast_IO::read(), fast_IO::read(), fast_IO::read());
c[1] = 1;
dfs(1, 0);
for (rg int i = 1; i <= n; ++i)
fast_IO::write(c[i]), putchar('\n');
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int>> G[100000];
vector<long long> dis(100000, 0);
for (int i = 0; i < n - 1; i++) {
int f, g, w;
scanf("%d %d %d", &f, &g, &w);
G[f].push_back({g, w});
G[g].push_back({f, w});
}
queue<int> q;
q.push(0);
while (!q.empty()) {
auto node = q.front();
q.pop();
for (auto to : G[node]) {
if (dis[to.first] != 0)
continue;
dis[to.first] = dis[node] + to.second;
q.push(to.first);
}
}
for (int i = 0; i < n; i++)
cout << dis[i] % 2 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int>> G[100000];
vector<long long> dis(100000, 0);
for (int i = 0; i < n - 1; i++) {
int f, g, w;
scanf("%d %d %d", &f, &g, &w);
f--;
g--;
G[f].push_back({g, w});
G[g].push_back({f, w});
}
queue<int> q;
q.push(0);
while (!q.empty()) {
auto node = q.front();
q.pop();
for (auto to : G[node]) {
if (dis[to.first] != 0)
continue;
dis[to.first] = dis[node] + to.second;
q.push(to.first);
}
}
for (int i = 0; i < n; i++)
cout << dis[i] % 2 << endl;
return 0;
} | insert | 11 | 11 | 11 | 13 | 0 | |
p03044 | C++ | Runtime Error |
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
vector<ll> col(100005);
vector<vector<P>> g(100005);
int dfs(int t, int p, int d) {
// cout<<"t"<<t<<"p"<<p<<"d"<<d<<endl;
for (P T : g[t]) {
int to = T.first;
if (to == p)
continue;
int dnow = d + T.second;
if (dnow % 2 == 0)
col[to] = 0;
else
col[to] = 1;
dfs(to, t, dnow);
}
}
int main() {
ll n;
cin >> n;
rep(i, n - 1) {
ll u1, v1, w1;
cin >> u1 >> v1 >> w1;
u1--;
v1--;
g[u1].push_back(make_pair(v1, w1));
g[v1].push_back(make_pair(u1, w1));
}
dfs(0, -1, 0);
rep(i, n) cout << col[i] << endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
vector<ll> col(100005);
vector<vector<P>> g(100005);
void dfs(int t, int p, int d) {
// cout<<"t"<<t<<"p"<<p<<"d"<<d<<endl;
for (P T : g[t]) {
int to = T.first;
if (to == p)
continue;
int dnow = d + T.second;
if (dnow % 2 == 0)
col[to] = 0;
else
col[to] = 1;
dfs(to, t, dnow);
}
}
int main() {
ll n;
cin >> n;
rep(i, n - 1) {
ll u1, v1, w1;
cin >> u1 >> v1 >> w1;
u1--;
v1--;
g[u1].push_back(make_pair(v1, w1));
g[v1].push_back(make_pair(u1, w1));
}
dfs(0, -1, 0);
rep(i, n) cout << col[i] << endl;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Relocate \
freopen("./in.txt", "r", stdin); \
freopen("./out.txt", "w", stdout)
#define up(mi, a, b) for (mi = a; mi <= b; ++mi)
#define down(mi, b, a) for (mi = b; mi >= a; --mi)
#define mfill(a, v) memset(a, v, sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 5005
#define v_n 5005
#define LL long long
#define e_n 5000005
#define MOD 1000000007
#define eps 0.000000001
#define Blen 32
using namespace std;
int N;
struct p {
int par, sz;
};
p ufs[MAX];
vector<int> g[MAX];
int col[MAX];
void Init() {
for (int i = 1; i <= N; i++) {
ufs[i].par = i;
ufs[i].sz = 1;
}
}
int Find(int i) {
if (ufs[i].par == i)
return i;
return ufs[i].par = Find(ufs[i].par);
}
bool Union(int i, int j) {
int si = Find(i), sj = Find(j);
if (si == sj) {
return 0;
} else if (ufs[si].sz < ufs[sj].sz) {
ufs[si].par = sj;
ufs[sj].sz += ufs[si].sz;
} else {
ufs[sj].par = si;
ufs[si].sz += ufs[sj].sz;
}
return 1;
}
struct e {
int s, t;
LL w;
};
vector<e> hh;
void dfs(int r) {
for (auto one : g[r]) {
if (col[one] == 0) {
col[one] = 3 - col[r];
dfs(one);
}
}
}
int main(void) {
int i, j, k;
FAST_IO;
// Relocate;
LL w;
cin >> N;
Init();
up(i, 2, N) {
cin >> j >> k >> w;
if (!(w & 1)) {
Union(j, k);
} else {
hh.push_back(e{j, k, w});
}
}
if (hh.empty()) {
up(i, 1, N) { cout << 0 << endl; }
return 0;
}
int r;
for (auto one : hh) {
int rs = Find(one.s);
int rt = Find(one.t);
r = rs;
g[rs].push_back(rt);
g[rt].push_back(rs);
}
col[r] = 1;
dfs(r);
up(i, 1, N) { cout << col[Find(i)] - 1 << endl; }
return 0;
} | #include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Relocate \
freopen("./in.txt", "r", stdin); \
freopen("./out.txt", "w", stdout)
#define up(mi, a, b) for (mi = a; mi <= b; ++mi)
#define down(mi, b, a) for (mi = b; mi >= a; --mi)
#define mfill(a, v) memset(a, v, sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 5000005
#define v_n 5005
#define LL long long
#define e_n 5000005
#define MOD 1000000007
#define eps 0.000000001
#define Blen 32
using namespace std;
int N;
struct p {
int par, sz;
};
p ufs[MAX];
vector<int> g[MAX];
int col[MAX];
void Init() {
for (int i = 1; i <= N; i++) {
ufs[i].par = i;
ufs[i].sz = 1;
}
}
int Find(int i) {
if (ufs[i].par == i)
return i;
return ufs[i].par = Find(ufs[i].par);
}
bool Union(int i, int j) {
int si = Find(i), sj = Find(j);
if (si == sj) {
return 0;
} else if (ufs[si].sz < ufs[sj].sz) {
ufs[si].par = sj;
ufs[sj].sz += ufs[si].sz;
} else {
ufs[sj].par = si;
ufs[si].sz += ufs[sj].sz;
}
return 1;
}
struct e {
int s, t;
LL w;
};
vector<e> hh;
void dfs(int r) {
for (auto one : g[r]) {
if (col[one] == 0) {
col[one] = 3 - col[r];
dfs(one);
}
}
}
int main(void) {
int i, j, k;
FAST_IO;
// Relocate;
LL w;
cin >> N;
Init();
up(i, 2, N) {
cin >> j >> k >> w;
if (!(w & 1)) {
Union(j, k);
} else {
hh.push_back(e{j, k, w});
}
}
if (hh.empty()) {
up(i, 1, N) { cout << 0 << endl; }
return 0;
}
int r;
for (auto one : hh) {
int rs = Find(one.s);
int rt = Find(one.t);
r = rs;
g[rs].push_back(rt);
g[rt].push_back(rs);
}
col[r] = 1;
dfs(r);
up(i, 1, N) { cout << col[Find(i)] - 1 << endl; }
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p03044 | 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};
int main() {
int n;
cin >> n;
vector<vi> len(n, vi(n, -1));
vector<vi> g(n);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
len[u - 1][v - 1] = w;
len[v - 1][u - 1] = w;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
vi ans(n, -1);
ans[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int i = q.front();
q.pop();
for (auto j : g[i]) {
int w = len[i][j];
if (w == -1 || ans[j] != -1)
continue;
if (w % 2)
ans[j] = !ans[i];
else
ans[j] = ans[i];
q.push(j);
}
}
rep(i, n) cout << ans[i] << 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};
int main() {
int n;
cin >> n;
map<int, map<int, int>> len;
vector<vi> g(n);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
len[u - 1][v - 1] = w;
len[v - 1][u - 1] = w;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
vi ans(n, -1);
ans[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int i = q.front();
q.pop();
for (auto j : g[i]) {
int w = len[i][j];
if (w == -1 || ans[j] != -1)
continue;
if (w % 2)
ans[j] = !ans[i];
else
ans[j] = ans[i];
q.push(j);
}
}
rep(i, n) cout << ans[i] << endl;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define cyes cout << "YES" << endl
#define cno cout << "NO" << endl
#define sp << " " <<
#define cst(x) cout << fixed << setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<char>>;
using que_a = priority_queue<int, vector<int>, greater<int>>;
using que_d = priority_queue<int>;
using pint = pair<int, int>;
int main() {
int n;
cin >> n;
Graph to(n), co(n);
rep(i, n - 1) {
int a, b, w;
cin >> a >> b >> w;
a--;
b--;
to.at(a).push_back(b);
to.at(b).push_back(a);
co.at(a).push_back(w);
co.at(b).push_back(w);
}
vector<int> ans(n, -1);
queue<int> q;
ans.at(0) = 0;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
rep(i, to.at(v).size()) {
int u = to.at(v).at(i);
int w = co.at(v).at(i);
if (ans.at(u) != -1)
continue;
ans.at(u) = (ans.at(v) + w) % 2;
q.push(u);
}
}
rep(i, n) cout << ans.at(i) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define cyes cout << "YES" << endl
#define cno cout << "NO" << endl
#define sp << " " <<
#define cst(x) cout << fixed << setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
using que_a = priority_queue<int, vector<int>, greater<int>>;
using que_d = priority_queue<int>;
using pint = pair<int, int>;
int main() {
int n;
cin >> n;
Graph to(n), co(n);
rep(i, n - 1) {
int a, b, w;
cin >> a >> b >> w;
a--;
b--;
to.at(a).push_back(b);
to.at(b).push_back(a);
co.at(a).push_back(w);
co.at(b).push_back(w);
}
vector<int> ans(n, -1);
queue<int> q;
ans.at(0) = 0;
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
rep(i, to.at(v).size()) {
int u = to.at(v).at(i);
int w = co.at(v).at(i);
if (ans.at(u) != -1)
continue;
ans.at(u) = (ans.at(v) + w) % 2;
q.push(u);
}
}
rep(i, n) cout << ans.at(i) << endl;
return 0;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p03044 | C++ | Runtime Error | #include <iostream>
#include <map>
#include <vector>
using namespace std;
void dfs(const vector<vector<long>> &graph,
const map<pair<long, long>, long> &dist, vector<int> &color, long now,
long from, long res) {
color[now] = res % 2;
for (auto e : graph[now]) {
if (e == from)
continue;
dfs(graph, dist, color, e, now, res + dist.at(make_pair(now, e)));
}
}
int main() {
long N;
cin >> N;
vector<vector<long>> graph(N);
map<pair<long, long>, long> dist;
vector<int> color(N);
for (int i = 0; i < N; ++i) { // 入力。
long a, b, d;
cin >> a >> b >> d;
a--;
b--;
graph[a].push_back(b);
graph[b].push_back(a);
dist.insert(make_pair(make_pair(a, b), d));
dist.insert(make_pair(make_pair(b, a), d));
}
// 頂点0を根にする
color[0] = 0;
dfs(graph, dist, color, 0, -1, 0);
for (int i = 0; i < N; i++)
cout << color[i] << endl;
} | #include <iostream>
#include <map>
#include <vector>
using namespace std;
void dfs(const vector<vector<long>> &graph,
const map<pair<long, long>, long> &dist, vector<int> &color, long now,
long from, long res) {
color[now] = res % 2;
for (auto e : graph[now]) {
if (e == from)
continue;
dfs(graph, dist, color, e, now, res + dist.at(make_pair(now, e)));
}
}
int main() {
long N;
cin >> N;
vector<vector<long>> graph(N);
map<pair<long, long>, long> dist;
vector<int> color(N);
for (int i = 0; i < N - 1; ++i) { // 入力。
long a, b, d;
cin >> a >> b >> d;
a--;
b--;
graph[a].push_back(b);
graph[b].push_back(a);
dist.insert(make_pair(make_pair(a, b), d));
dist.insert(make_pair(make_pair(b, a), d));
}
// 頂点0を根にする
color[0] = 0;
dfs(graph, dist, color, 0, -1, 0);
for (int i = 0; i < N; i++)
cout << color[i] << endl;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAXN 100010
struct NODE {
int w;
int e;
int next;
} edge[MAXN];
int cnt = 1;
int head[MAXN];
int color[MAXN];
void add(int u, int v, int w) {
edge[cnt].w = w;
edge[cnt].e = v;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[cnt].w = w;
edge[cnt].e = u;
edge[cnt].next = head[v];
head[v] = cnt++;
}
bool vis[MAXN];
void dfs(int i, int Color) {
color[i] = Color;
int curedge = head[i];
while (curedge != 0) {
if (vis[edge[curedge].e]) {
curedge = edge[curedge].next;
continue;
}
vis[edge[curedge].e] = true;
int w = edge[curedge].w;
if (w % 2)
dfs(edge[curedge].e, Color ^ 1);
else
dfs(edge[curedge].e, Color);
curedge = edge[curedge].next;
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
vis[1] = true;
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << color[i] << ' ';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAXN 100010
struct NODE {
int w;
int e;
int next;
} edge[MAXN * 2];
int cnt = 1;
int head[MAXN];
int color[MAXN];
void add(int u, int v, int w) {
edge[cnt].w = w;
edge[cnt].e = v;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[cnt].w = w;
edge[cnt].e = u;
edge[cnt].next = head[v];
head[v] = cnt++;
}
bool vis[MAXN];
void dfs(int i, int Color) {
color[i] = Color;
int curedge = head[i];
while (curedge != 0) {
if (vis[edge[curedge].e]) {
curedge = edge[curedge].next;
continue;
}
vis[edge[curedge].e] = true;
int w = edge[curedge].w;
if (w % 2)
dfs(edge[curedge].e, Color ^ 1);
else
dfs(edge[curedge].e, Color);
curedge = edge[curedge].next;
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
vis[1] = true;
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << color[i] << ' ';
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
int N;
int color[100001];
vector<pair<int, int>> G[10000];
void hukasa(int i) {
for (int j = 0; j < G[i].size(); j++) {
if (color[G[i][j].first] == 0) {
color[G[i][j].first] = color[i] + G[i][j].second;
hukasa(G[i][j].first);
}
}
}
int main() {
cin >> N;
for (int i = 0; i < N - 1; i++) {
int s, t, w;
cin >> s >> t >> w;
G[s - 1].push_back(pair<int, int>(t - 1, w));
G[t - 1].push_back(pair<int, int>(s - 1, w));
}
for (int i = 0; i < N; i++) {
if (color[i] == 0) {
color[i] = 1;
hukasa(i);
}
}
for (int i = 0; i < N; i++) {
if (color[i] % 2 == 0) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
int N;
int color[100005];
vector<pair<int, int>> G[100005];
void hukasa(int i) {
for (int j = 0; j < G[i].size(); j++) {
if (color[G[i][j].first] == 0) {
color[G[i][j].first] = color[i] + G[i][j].second;
hukasa(G[i][j].first);
}
}
}
int main() {
cin >> N;
for (int i = 0; i < N - 1; i++) {
int s, t, w;
cin >> s >> t >> w;
G[s - 1].push_back(pair<int, int>(t - 1, w));
G[t - 1].push_back(pair<int, int>(s - 1, w));
}
for (int i = 0; i < N; i++) {
if (color[i] == 0) {
color[i] = 1;
hukasa(i);
}
}
for (int i = 0; i < N; i++) {
if (color[i] % 2 == 0) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
}
| replace | 10 | 12 | 10 | 12 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
void dfs(vector<vector<pair<int, int>>> &graph, vector<int> &ans, int now,
int parent, int color) {
ans.at(now) = color;
for (int i = 0; i < graph.at(now).size(); i++) {
int next = graph.at(now).at(i).first;
if (next == parent)
continue;
if (graph.at(now).at(i).second == 0)
dfs(graph, ans, next, now, color);
else
dfs(graph, ans, next, now, 1 - color);
}
}
int main() {
int N;
cin >> N;
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < N; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
w %= 2;
graph.at(u).push_back(make_pair(v, w));
graph.at(v).push_back(make_pair(u, w));
}
vector<int> ans(N, -1);
dfs(graph, ans, 0, -1, 0);
for (int i = 0; i < N; i++) {
cout << ans.at(i) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
void dfs(vector<vector<pair<int, int>>> &graph, vector<int> &ans, int now,
int parent, int color) {
ans.at(now) = color;
for (int i = 0; i < graph.at(now).size(); i++) {
int next = graph.at(now).at(i).first;
if (next == parent)
continue;
if (graph.at(now).at(i).second == 0)
dfs(graph, ans, next, now, color);
else
dfs(graph, ans, next, now, 1 - color);
}
}
int main() {
int N;
cin >> N;
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
w %= 2;
graph.at(u).push_back(make_pair(v, w));
graph.at(v).push_back(make_pair(u, w));
}
vector<int> ans(N, -1);
dfs(graph, ans, 0, -1, 0);
for (int i = 0; i < N; i++) {
cout << ans.at(i) << endl;
}
return 0;
}
| replace | 24 | 25 | 24 | 25 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)
class edge_t {
public:
edge_t(int vv, int ww) : v(vv), w(ww){};
public:
int v;
int w;
};
int N, a[111111];
std::vector<std::vector<edge_t>> G;
bool visit[111111];
void dfs(int i, int j) {
for (const auto &e : G[i]) {
if (visit[e.v])
continue;
visit[e.v] = true;
int jj = j ^ (e.w & 1);
dfs(e.v, a[e.v] = jj);
}
}
int main(int argc, char **argv) {
std::cin.tie(0);
std::ios_base::sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(6);
std::cerr << std::fixed << std::setprecision(6);
std::cin >> N;
G.resize(N);
int u, v, w;
for (int i = 0; i < N; i++) {
std::cin >> u >> v >> w;
u--;
v--;
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
memset(a, -1, sizeof(a));
a[0] = 0;
memset(visit, 0, sizeof(visit));
dfs(0, 0);
for (int i = 0; i < N; i++)
std::cout << a[i] << std::endl;
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)
class edge_t {
public:
edge_t(int vv, int ww) : v(vv), w(ww){};
public:
int v;
int w;
};
int N, a[111111];
std::vector<std::vector<edge_t>> G;
bool visit[111111];
void dfs(int i, int j) {
for (const auto &e : G[i]) {
if (visit[e.v])
continue;
visit[e.v] = true;
int jj = j ^ (e.w & 1);
dfs(e.v, a[e.v] = jj);
}
}
int main(int argc, char **argv) {
std::cin.tie(0);
std::ios_base::sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(6);
std::cerr << std::fixed << std::setprecision(6);
std::cin >> N;
G.resize(N);
int u, v, w;
for (int i = 0; i < N - 1; i++) {
std::cin >> u >> v >> w;
u--;
v--;
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
memset(a, -1, sizeof(a));
a[0] = 0;
memset(visit, 0, sizeof(visit));
dfs(0, 0);
for (int i = 0; i < N; i++)
std::cout << a[i] << std::endl;
return 0;
}
| replace | 66 | 67 | 66 | 67 | 0 | |
p03044 | C++ | Runtime Error | // Ryo Kamoi
#define DEBUG
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
int INF = 1000000000;
struct edge {
int to;
int len;
};
int n;
int col[10010];
vector<edge> graph[10010];
int main() {
cin >> n;
REP(i, n) { col[i] = -1; }
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
edge e1;
e1.to = v - 1;
e1.len = w;
graph[u - 1].push_back(e1);
edge e2;
e2.to = u - 1;
e2.len = w;
graph[v - 1].push_back(e2);
}
queue<int> que;
que.push(0);
col[0] = 0;
while (!que.empty()) {
int vertex = que.front();
que.pop();
REP(i, graph[vertex].size()) {
int next_v = graph[vertex][i].to;
if (col[next_v] >= 0)
continue;
if (graph[vertex][i].len % 2 == 1) {
col[next_v] = (col[vertex] + 1) % 2;
} else {
col[next_v] = col[vertex];
}
que.push(next_v);
}
}
REP(i, n) { cout << col[i] << endl; }
}
| // Ryo Kamoi
#define DEBUG
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
int INF = 1000000000;
struct edge {
int to;
int len;
};
int n;
int col[100010];
vector<edge> graph[100010];
int main() {
cin >> n;
REP(i, n) { col[i] = -1; }
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
edge e1;
e1.to = v - 1;
e1.len = w;
graph[u - 1].push_back(e1);
edge e2;
e2.to = u - 1;
e2.len = w;
graph[v - 1].push_back(e2);
}
queue<int> que;
que.push(0);
col[0] = 0;
while (!que.empty()) {
int vertex = que.front();
que.pop();
REP(i, graph[vertex].size()) {
int next_v = graph[vertex][i].to;
if (col[next_v] >= 0)
continue;
if (graph[vertex][i].len % 2 == 1) {
col[next_v] = (col[vertex] + 1) % 2;
} else {
col[next_v] = col[vertex];
}
que.push(next_v);
}
}
REP(i, n) { cout << col[i] << endl; }
}
| replace | 27 | 29 | 27 | 29 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
/// cin/cout overloading
template <typename T> ostream &operator<<(ostream &out, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
out << *it << " ";
}
return out;
}
template <typename T> ostream &operator<<(ostream &out, pair<T, T> &P) {
out << P.first << " " << P.second;
return out;
}
template <typename T> istream &operator>>(istream &in, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
in >> *it;
}
return in;
}
template <typename T> istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
/// 图相关
ll N, M;
struct Edge {
int to;
ll weight;
int next;
bool operator<(const Edge &rhs) { return weight < rhs.weight; }
};
struct Edge2 {
ll u, v, w;
Edge2(ll u_, ll v_, ll w_) : u(u_), v(v_), w(w_) {}
Edge2() : u(0ll), v(0ll), w(0ll) {}
bool operator<(const Edge2 &rhs) { return w < rhs.w; }
};
vector<Edge> edges(MAXN);
vi head(MAXN, -1);
// vector<int> matchingx(MAXN, -1);
// vector<int> matchingy(MAXN, -1);
// vector<bool> check(MAXN);
// vector<ll> dis(MAXN);
// vector<bool> vis(MAXN, false);
int cnt = 1;
void addEdge(int from, int to, ll weight) {
edges[cnt].to = to;
edges[cnt].weight = weight;
edges[cnt].next = head[from];
head[from] = cnt++;
}
//
// bool dfs(int u) {
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (!check[v]) {
// check[v] = true;
// if (matchingy[v] == -1 || dfs(matchingy[v])) {
// matchingy[v] = u;
// matchingx[u] = v;
// return true;
// }
// }
// }
// return false;
//}
//
// int hungarian() {
// int ans = 0;
// fill(matchingx.begin(), matchingx.end(), -1);
// fill(matchingy.begin(), matchingy.end(), -1);
// for (int u = 1; u <= N; ++u) {
//// if (matchingx[u] == -1) {
// {
// fill(check.begin(), check.end(), false);
// if (dfs(u)) {
// ++ans;
// }
// }
// }
// return ans;
//}
//
// void dijkstra(const ll s) {
// priority_queue<P, vector<P>, greater<P>> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push(P(0, s));
//////multiple sources
//// for (auto& x : shops) {
//// dis[x] = 0;
//// que.push(P(0, x));
//// }
// while (!que.empty()) {
// P p = que.top();
// que.pop();
//// cout << "pop " << p.second << endl;
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
//// cout << "push " << v << endl;
// que.push(P(dis[v], v));
// }
// }
// }
//}
// void zeroOneBFS(const int s) {
// deque<P> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push_front(P(0, s));
// while (!que.empty()) {
// P p = que.front();
// que.pop_front();
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
// if (edges[i].weight) {
// que.push_back(P(dis[v], v));
// } else {
// que.push_front(P(dis[v], v));
// }
// }
// }
// }
// }
// Union-Find 并查集
class UnionFind {
vector<ll> par;
public:
explicit UnionFind(ll n) : par(n, -1) {}
ll root(ll a) {
if (par[a] < 0) {
return a;
}
return par[a] = root(par[a]);
}
ll size(ll a) { return -par[root(a)]; }
void unite(ll a, ll b) {
a = root(a);
b = root(b);
if (a != b) {
if (size(a) < size(b)) {
swap(a, b);
}
par[a] += par[b];
par[b] = a;
}
}
};
ll kruskal(vector<Edge2> &edges2, const ll V) {
sort(edges2.begin(), edges2.end());
UnionFind uf(V + 10);
ll res = 0;
Edge2 e;
for (int i = 0; i < edges2.size(); ++i) {
e = edges2[i];
if (uf.root(e.u) != uf.root(e.v)) {
uf.unite(e.u, e.v);
res += e.w;
}
}
return res;
}
// 线段树
struct SegmentTreeNode {
ll maxVal;
ll minVal;
ll sum;
ll len;
ll lazy;
ll left, right;
SegmentTreeNode() {}
};
class SegmentTree {
public:
explicit SegmentTree(size_t size, const vll &nums)
: tree(size << 2), nums(nums) {}
void build(ll root, ll left, ll right) {
tree[root].lazy = 0;
tree[root].left = left;
tree[root].right = right;
tree[root].len = right - left + 1;
if (left == right) {
tree[root].maxVal = nums[left];
tree[root].minVal = nums[left];
tree[root].sum = nums[left];
} else {
ll mid = (right - left) / 2 + left;
build(root * 2, left, mid);
build(root * 2 + 1, mid + 1, right);
tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal);
tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal);
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
}
void pushup(ll root) {
tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal);
tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal);
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
//// add single node val
void add(ll root, ll id, ll addVal) {
if (tree[root].left == tree[root].right) {
tree[root].maxVal += addVal;
tree[root].minVal += addVal;
tree[root].sum += addVal;
return;
}
ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left;
if (id <= mid) {
add(root * 2, id, addVal);
} else {
add(root * 2 + 1, id, addVal);
}
pushup(root);
}
void pushdown(ll root) {
if (tree[root].lazy) {
tree[root * 2].lazy += tree[root].lazy;
tree[root * 2 + 1].lazy += tree[root].lazy;
tree[root * 2].sum += tree[root * 2].len * tree[root].lazy;
tree[root * 2 + 1].sum += tree[root * 2 + 1].len * tree[root].lazy;
tree[root * 2].maxVal += tree[root].lazy;
tree[root * 2 + 1].maxVal += tree[root].lazy;
tree[root * 2].minVal += tree[root].lazy;
tree[root * 2 + 1].minVal += tree[root].lazy;
tree[root].lazy = 0;
}
}
//// query range sum
ll querySum(ll root, ll left, ll right) {
if (tree[root].left >= left && tree[root].right <= right) {
return tree[root].sum;
}
if (tree[root].left > right || tree[root].right < left) {
return 0;
}
if (tree[root].lazy) {
pushdown(root);
}
return querySum(root * 2, left, right) +
querySum(root * 2 + 1, left, right);
}
//// query range max/min
ll queryMax(ll root, ll left, ll right) {
if (tree[root].left >= left && tree[root].right <= right) {
return tree[root].maxVal;
}
if (tree[root].left > right || tree[root].right < left) {
return -INF;
}
if (tree[root].lazy) {
pushdown(root);
}
return max(queryMax(root * 2, left, right),
queryMax(root * 2 + 1, left, right));
}
ll queryMin(ll root, ll left, ll right) {
if (tree[root].left >= left && tree[root].right <= right) {
return tree[root].minVal;
}
if (tree[root].left > right || tree[root].right < left) {
return INF;
}
if (tree[root].lazy) {
pushdown(root);
}
return min(queryMin(root * 2, left, right),
queryMin(root * 2 + 1, left, right));
}
//// add range val
void update(ll root, ll left, ll right, ll addVal) {
if (tree[root].left >= left && tree[root].right <= right) {
tree[root].lazy += addVal;
tree[root].sum += tree[root].len * addVal;
tree[root].maxVal += addVal;
tree[root].minVal += addVal;
return;
}
if (tree[root].left > right || tree[root].right < left) {
return;
}
if (tree[root].lazy) {
pushdown(root);
}
update(root * 2, left, right, addVal);
update(root * 2 + 1, left, right, addVal);
pushup(root);
}
private:
vector<SegmentTreeNode> tree;
const vll &nums;
};
/// 组合数
////約数求める //約数
// void divisor(ll n, vector<ll> &ret) {
// for (ll i = 1; i * i <= n; i++) {
// if (n % i == 0) {
// ret.push_back(i);
// if (i * i != n) ret.push_back(n / i);
// }
// }
// sort(ret.begin(), ret.end());
// }
////階乗
ll factorial(ll n) {
ll ret = 1;
for (ll i = 1; i <= n; ++i) {
ret = (ret * i) % MOD;
}
return ret;
}
////モジュラ逆数
ll inv_mod(ll n) {
ll a = n % MOD, b = MOD - 2, ret = 1;
while (b > 0) {
if (b & 1)
ret = (ret * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return ret;
}
ll nPr(ll n, ll r) {
ll ret = 1;
for (ll i = n; i >= n - r + 1; --i) {
ret = ret * (i % MOD) % MOD;
}
return ret;
}
ll nCr(ll n, ll r) { return nPr(n, r) * inv_mod(factorial(r)) % MOD; }
//
// vll F(MAXN), Finv(MAXN), inv(MAXN);
//
// ll pow_mod(ll a, ll b, ll p) {
// ll ret = 1;
// while (b) {
// if (b & 1) ret = (ret * a) % p;
// a = (a * a) % p;
// b >>= 1;
// }
// return ret;
//}
//
// void comb_init() {
// inv[1] = 1;
// for (int i = 2; i < MAXN; ++i) {
// inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
// }
// F[0] = Finv[0] = 1;
// for (int i = 1; i < MAXN; ++i) {
// F[i] = F[i-1] * 1ll * i % MOD;
// Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD;
// }
//}
//
// inline ll comb(ll n, ll m) {
// if (m < 0 || m > n) return 0;
// return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
// }
//
inline ll add_mod(ll a, ll b) { return (a + b) % MOD; }
inline ll mul_mod(ll a, ll b) { return a * b % MOD; }
inline ll sub_mod(ll a, ll b) { return (a + MOD - b) % MOD; }
void dfs(int u, vi &color) {
for (int i = head[u]; i != -1; i = edges[i].next) {
int v = edges[i].to;
if (color[v] == -1) {
if (edges[i].weight % 2 == 0) {
color[v] = color[u];
} else {
color[v] = !color[u];
}
dfs(v, color);
}
}
}
/// main函数
int main() {
cin >> N;
int u, v, w;
for (int i = 1; i < N; ++i) {
cin >> u >> v >> w;
addEdge(u, v, w);
addEdge(v, u, w);
}
vi color(N + 1, -1);
color[1] = 0;
dfs(1, color);
for (int i = 1; i <= N; ++i) {
cout << color[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
/// cin/cout overloading
template <typename T> ostream &operator<<(ostream &out, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
out << *it << " ";
}
return out;
}
template <typename T> ostream &operator<<(ostream &out, pair<T, T> &P) {
out << P.first << " " << P.second;
return out;
}
template <typename T> istream &operator>>(istream &in, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
in >> *it;
}
return in;
}
template <typename T> istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
/// 图相关
ll N, M;
struct Edge {
int to;
ll weight;
int next;
bool operator<(const Edge &rhs) { return weight < rhs.weight; }
};
struct Edge2 {
ll u, v, w;
Edge2(ll u_, ll v_, ll w_) : u(u_), v(v_), w(w_) {}
Edge2() : u(0ll), v(0ll), w(0ll) {}
bool operator<(const Edge2 &rhs) { return w < rhs.w; }
};
vector<Edge> edges(MAXN << 1);
vi head(MAXN, -1);
// vector<int> matchingx(MAXN, -1);
// vector<int> matchingy(MAXN, -1);
// vector<bool> check(MAXN);
// vector<ll> dis(MAXN);
// vector<bool> vis(MAXN, false);
int cnt = 1;
void addEdge(int from, int to, ll weight) {
edges[cnt].to = to;
edges[cnt].weight = weight;
edges[cnt].next = head[from];
head[from] = cnt++;
}
//
// bool dfs(int u) {
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (!check[v]) {
// check[v] = true;
// if (matchingy[v] == -1 || dfs(matchingy[v])) {
// matchingy[v] = u;
// matchingx[u] = v;
// return true;
// }
// }
// }
// return false;
//}
//
// int hungarian() {
// int ans = 0;
// fill(matchingx.begin(), matchingx.end(), -1);
// fill(matchingy.begin(), matchingy.end(), -1);
// for (int u = 1; u <= N; ++u) {
//// if (matchingx[u] == -1) {
// {
// fill(check.begin(), check.end(), false);
// if (dfs(u)) {
// ++ans;
// }
// }
// }
// return ans;
//}
//
// void dijkstra(const ll s) {
// priority_queue<P, vector<P>, greater<P>> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push(P(0, s));
//////multiple sources
//// for (auto& x : shops) {
//// dis[x] = 0;
//// que.push(P(0, x));
//// }
// while (!que.empty()) {
// P p = que.top();
// que.pop();
//// cout << "pop " << p.second << endl;
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
//// cout << "push " << v << endl;
// que.push(P(dis[v], v));
// }
// }
// }
//}
// void zeroOneBFS(const int s) {
// deque<P> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push_front(P(0, s));
// while (!que.empty()) {
// P p = que.front();
// que.pop_front();
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
// if (edges[i].weight) {
// que.push_back(P(dis[v], v));
// } else {
// que.push_front(P(dis[v], v));
// }
// }
// }
// }
// }
// Union-Find 并查集
class UnionFind {
vector<ll> par;
public:
explicit UnionFind(ll n) : par(n, -1) {}
ll root(ll a) {
if (par[a] < 0) {
return a;
}
return par[a] = root(par[a]);
}
ll size(ll a) { return -par[root(a)]; }
void unite(ll a, ll b) {
a = root(a);
b = root(b);
if (a != b) {
if (size(a) < size(b)) {
swap(a, b);
}
par[a] += par[b];
par[b] = a;
}
}
};
ll kruskal(vector<Edge2> &edges2, const ll V) {
sort(edges2.begin(), edges2.end());
UnionFind uf(V + 10);
ll res = 0;
Edge2 e;
for (int i = 0; i < edges2.size(); ++i) {
e = edges2[i];
if (uf.root(e.u) != uf.root(e.v)) {
uf.unite(e.u, e.v);
res += e.w;
}
}
return res;
}
// 线段树
struct SegmentTreeNode {
ll maxVal;
ll minVal;
ll sum;
ll len;
ll lazy;
ll left, right;
SegmentTreeNode() {}
};
class SegmentTree {
public:
explicit SegmentTree(size_t size, const vll &nums)
: tree(size << 2), nums(nums) {}
void build(ll root, ll left, ll right) {
tree[root].lazy = 0;
tree[root].left = left;
tree[root].right = right;
tree[root].len = right - left + 1;
if (left == right) {
tree[root].maxVal = nums[left];
tree[root].minVal = nums[left];
tree[root].sum = nums[left];
} else {
ll mid = (right - left) / 2 + left;
build(root * 2, left, mid);
build(root * 2 + 1, mid + 1, right);
tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal);
tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal);
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
}
void pushup(ll root) {
tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal);
tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal);
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
//// add single node val
void add(ll root, ll id, ll addVal) {
if (tree[root].left == tree[root].right) {
tree[root].maxVal += addVal;
tree[root].minVal += addVal;
tree[root].sum += addVal;
return;
}
ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left;
if (id <= mid) {
add(root * 2, id, addVal);
} else {
add(root * 2 + 1, id, addVal);
}
pushup(root);
}
void pushdown(ll root) {
if (tree[root].lazy) {
tree[root * 2].lazy += tree[root].lazy;
tree[root * 2 + 1].lazy += tree[root].lazy;
tree[root * 2].sum += tree[root * 2].len * tree[root].lazy;
tree[root * 2 + 1].sum += tree[root * 2 + 1].len * tree[root].lazy;
tree[root * 2].maxVal += tree[root].lazy;
tree[root * 2 + 1].maxVal += tree[root].lazy;
tree[root * 2].minVal += tree[root].lazy;
tree[root * 2 + 1].minVal += tree[root].lazy;
tree[root].lazy = 0;
}
}
//// query range sum
ll querySum(ll root, ll left, ll right) {
if (tree[root].left >= left && tree[root].right <= right) {
return tree[root].sum;
}
if (tree[root].left > right || tree[root].right < left) {
return 0;
}
if (tree[root].lazy) {
pushdown(root);
}
return querySum(root * 2, left, right) +
querySum(root * 2 + 1, left, right);
}
//// query range max/min
ll queryMax(ll root, ll left, ll right) {
if (tree[root].left >= left && tree[root].right <= right) {
return tree[root].maxVal;
}
if (tree[root].left > right || tree[root].right < left) {
return -INF;
}
if (tree[root].lazy) {
pushdown(root);
}
return max(queryMax(root * 2, left, right),
queryMax(root * 2 + 1, left, right));
}
ll queryMin(ll root, ll left, ll right) {
if (tree[root].left >= left && tree[root].right <= right) {
return tree[root].minVal;
}
if (tree[root].left > right || tree[root].right < left) {
return INF;
}
if (tree[root].lazy) {
pushdown(root);
}
return min(queryMin(root * 2, left, right),
queryMin(root * 2 + 1, left, right));
}
//// add range val
void update(ll root, ll left, ll right, ll addVal) {
if (tree[root].left >= left && tree[root].right <= right) {
tree[root].lazy += addVal;
tree[root].sum += tree[root].len * addVal;
tree[root].maxVal += addVal;
tree[root].minVal += addVal;
return;
}
if (tree[root].left > right || tree[root].right < left) {
return;
}
if (tree[root].lazy) {
pushdown(root);
}
update(root * 2, left, right, addVal);
update(root * 2 + 1, left, right, addVal);
pushup(root);
}
private:
vector<SegmentTreeNode> tree;
const vll &nums;
};
/// 组合数
////約数求める //約数
// void divisor(ll n, vector<ll> &ret) {
// for (ll i = 1; i * i <= n; i++) {
// if (n % i == 0) {
// ret.push_back(i);
// if (i * i != n) ret.push_back(n / i);
// }
// }
// sort(ret.begin(), ret.end());
// }
////階乗
ll factorial(ll n) {
ll ret = 1;
for (ll i = 1; i <= n; ++i) {
ret = (ret * i) % MOD;
}
return ret;
}
////モジュラ逆数
ll inv_mod(ll n) {
ll a = n % MOD, b = MOD - 2, ret = 1;
while (b > 0) {
if (b & 1)
ret = (ret * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return ret;
}
ll nPr(ll n, ll r) {
ll ret = 1;
for (ll i = n; i >= n - r + 1; --i) {
ret = ret * (i % MOD) % MOD;
}
return ret;
}
ll nCr(ll n, ll r) { return nPr(n, r) * inv_mod(factorial(r)) % MOD; }
//
// vll F(MAXN), Finv(MAXN), inv(MAXN);
//
// ll pow_mod(ll a, ll b, ll p) {
// ll ret = 1;
// while (b) {
// if (b & 1) ret = (ret * a) % p;
// a = (a * a) % p;
// b >>= 1;
// }
// return ret;
//}
//
// void comb_init() {
// inv[1] = 1;
// for (int i = 2; i < MAXN; ++i) {
// inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
// }
// F[0] = Finv[0] = 1;
// for (int i = 1; i < MAXN; ++i) {
// F[i] = F[i-1] * 1ll * i % MOD;
// Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD;
// }
//}
//
// inline ll comb(ll n, ll m) {
// if (m < 0 || m > n) return 0;
// return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
// }
//
inline ll add_mod(ll a, ll b) { return (a + b) % MOD; }
inline ll mul_mod(ll a, ll b) { return a * b % MOD; }
inline ll sub_mod(ll a, ll b) { return (a + MOD - b) % MOD; }
void dfs(int u, vi &color) {
for (int i = head[u]; i != -1; i = edges[i].next) {
int v = edges[i].to;
if (color[v] == -1) {
if (edges[i].weight % 2 == 0) {
color[v] = color[u];
} else {
color[v] = !color[u];
}
dfs(v, color);
}
}
}
/// main函数
int main() {
cin >> N;
int u, v, w;
for (int i = 1; i < N; ++i) {
cin >> u >> v >> w;
addEdge(u, v, w);
addEdge(v, u, w);
}
vi color(N + 1, -1);
color[1] = 0;
dfs(1, color);
for (int i = 1; i <= N; ++i) {
cout << color[i] << endl;
}
return 0;
} | replace | 61 | 62 | 61 | 62 | 0 | |
p03044 | C++ | Runtime Error | /*
─────────────────────
───────────████████──
──────────███▄███████
──────────███████████
──────────███████████
──────────██████─────
──────────█████████──
█───────███████──────
██────████████████───
███──██████████──█───
███████████████──────
███████████████──────
─█████████████───────
──███████████────────
────████████─────────
─────███──██─────────
─────██────█─────────
─────█─────█─────────
─────██────██────────
─────────────────────
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define read(a) \
int a; \
cin >> a;
#define readb(a, b) \
int a, b; \
cin >> a >> b;
#define readc(a, b, c) \
int a, b, c; \
cin >> a >> b >> c;
#define reads(s) \
string s; \
cin >> s;
#define readarr(a, n) \
int a[(n) + 1] = {}; \
fori(i, 1, (n)) { cin >> a[i]; }
#define readmat(a, n, m) \
int a[n + 1][m + 1] = {}; \
fori(i, 1, n) { fori(j, 1, m) cin >> a[i][j]; }
#define print(a) cout << a << endl;
#define printarr(a, n) \
fori(i, 1, n) cout << a[i] << " "; \
cout << endl;
#define printv(v) \
for (int i : v) \
cout << i << " "; \
cout << endl;
#define printmat(a, n, m) \
fori(i, 1, n) { \
fori(j, 1, m) cout << a[i][j] << " "; \
cout << endl; \
}
#define all(v) v.begin(), v.end()
#define sz(s) (int)(s.size())
#define pb push_back
#define fi first
#define se second
#define rz resize
#define fori(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
#define setprec(x) cout << fixed << setprecision(x);
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpi;
typedef vector<vector<int>> vvi;
const int INF = 4e18;
const int mod = 1e9 + 7;
vector<vector<pii>> adj(2e5);
vi vis(2e5), color(2e5);
void bfs(int s) {
vis[s] = 1;
queue<int> q;
q.push(1);
while (!q.empty()) {
auto cur = q.front();
q.pop();
for (auto i : adj[cur]) {
int node = i.fi;
int w = i.se;
if (vis[node])
continue;
if (w % 2)
color[node] = !color[cur];
else
color[node] = color[cur];
vis[node] = 1;
q.push(node);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
freopen("OUTPUT_ERROR.txt", "w", stderr);
#endif
// start from here
read(n) int x, y, w;
fori(i, 1, n - 1) {
cin >> x >> y >> w;
adj[x].pb({y, w});
adj[y].pb({x, w});
}
bfs(1);
fori(i, 1, n) print(color[i])
return 0;
}
| /*
─────────────────────
───────────████████──
──────────███▄███████
──────────███████████
──────────███████████
──────────██████─────
──────────█████████──
█───────███████──────
██────████████████───
███──██████████──█───
███████████████──────
███████████████──────
─█████████████───────
──███████████────────
────████████─────────
─────███──██─────────
─────██────█─────────
─────█─────█─────────
─────██────██────────
─────────────────────
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define read(a) \
int a; \
cin >> a;
#define readb(a, b) \
int a, b; \
cin >> a >> b;
#define readc(a, b, c) \
int a, b, c; \
cin >> a >> b >> c;
#define reads(s) \
string s; \
cin >> s;
#define readarr(a, n) \
int a[(n) + 1] = {}; \
fori(i, 1, (n)) { cin >> a[i]; }
#define readmat(a, n, m) \
int a[n + 1][m + 1] = {}; \
fori(i, 1, n) { fori(j, 1, m) cin >> a[i][j]; }
#define print(a) cout << a << endl;
#define printarr(a, n) \
fori(i, 1, n) cout << a[i] << " "; \
cout << endl;
#define printv(v) \
for (int i : v) \
cout << i << " "; \
cout << endl;
#define printmat(a, n, m) \
fori(i, 1, n) { \
fori(j, 1, m) cout << a[i][j] << " "; \
cout << endl; \
}
#define all(v) v.begin(), v.end()
#define sz(s) (int)(s.size())
#define pb push_back
#define fi first
#define se second
#define rz resize
#define fori(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
#define setprec(x) cout << fixed << setprecision(x);
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpi;
typedef vector<vector<int>> vvi;
const int INF = 4e18;
const int mod = 1e9 + 7;
vector<vector<pii>> adj(2e5);
vi vis(2e5), color(2e5);
void bfs(int s) {
vis[s] = 1;
queue<int> q;
q.push(1);
while (!q.empty()) {
auto cur = q.front();
q.pop();
for (auto i : adj[cur]) {
int node = i.fi;
int w = i.se;
if (vis[node])
continue;
if (w % 2)
color[node] = !color[cur];
else
color[node] = color[cur];
vis[node] = 1;
q.push(node);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// start from here
read(n) int x, y, w;
fori(i, 1, n - 1) {
cin >> x >> y >> w;
adj[x].pb({y, w});
adj[y].pb({x, w});
}
bfs(1);
fori(i, 1, n) print(color[i])
return 0;
}
| delete | 120 | 126 | 120 | 120 | -11 | |
p03044 | C++ | Runtime Error | #include <cstring>
#include <iostream>
using namespace std;
const int Maxv = 100005;
struct edge {
int next, to, w;
} e[Maxv];
int head[Maxv], cnt = 0;
void add_edge(int x, int y, int w) {
cnt++;
e[cnt].to = y;
e[cnt].next = head[x];
e[cnt].w = w;
head[x] = cnt;
}
int ans[Maxv];
void dfs(int v, int f) {
int y, w, i;
for (i = head[v]; i != -1; i = e[i].next) {
y = e[i].to;
w = e[i].w % 2;
if (y != f) {
ans[y] = ans[v] ^ w;
dfs(y, v);
}
}
}
int main() {
memset(head, -1, sizeof(head));
int n, i, j, x, y, w;
cin >> n;
for (i = 1; i < n; i++) {
cin >> x >> y >> w;
add_edge(x, y, w);
add_edge(y, x, w);
}
dfs(1, 0);
for (i = 1; i <= n; i++)
cout << ans[i] << endl;
return 0;
} | #include <cstring>
#include <iostream>
using namespace std;
const int Maxv = 1000005;
struct edge {
int next, to, w;
} e[Maxv];
int head[Maxv], cnt = 0;
void add_edge(int x, int y, int w) {
cnt++;
e[cnt].to = y;
e[cnt].next = head[x];
e[cnt].w = w;
head[x] = cnt;
}
int ans[Maxv];
void dfs(int v, int f) {
int y, w, i;
for (i = head[v]; i != -1; i = e[i].next) {
y = e[i].to;
w = e[i].w % 2;
if (y != f) {
ans[y] = ans[v] ^ w;
dfs(y, v);
}
}
}
int main() {
memset(head, -1, sizeof(head));
int n, i, j, x, y, w;
cin >> n;
for (i = 1; i < n; i++) {
cin >> x >> y >> w;
add_edge(x, y, w);
add_edge(y, x, w);
}
dfs(1, 0);
for (i = 1; i <= n; i++)
cout << ans[i] << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
vector<vector<int>> to, cost;
vi ans;
void dfs(int v, int c = 0) {
ans[v] = c;
rep(i, sz(to[v])) {
if (ans[to[v][i]] != -1)
continue;
int nc = (cost[v][to[v][i]] % 2 == 0 ? c : 1 - c);
dfs(to[v][i], nc);
}
}
int main() {
int n;
cin >> n;
to = vector<vector<int>>(n);
cost = vector<vector<int>>(n);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
--u;
--v;
to[u].pb(v);
cost[u].pb(w);
to[v].pb(u);
cost[v].pb(w);
}
ans = vi(n, -1);
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
vector<vector<int>> to, cost;
vi ans;
void dfs(int v, int c = 0) {
ans[v] = c;
rep(i, sz(to[v])) {
if (ans[to[v][i]] != -1)
continue;
int nc = (cost[v][i] % 2 == 0 ? c : 1 - c);
dfs(to[v][i], nc);
}
}
int main() {
int n;
cin >> n;
to = vector<vector<int>>(n);
cost = vector<vector<int>>(n);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
--u;
--v;
to[u].pb(v);
cost[u].pb(w);
to[v].pb(u);
cost[v].pb(w);
}
ans = vi(n, -1);
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
}
| replace | 46 | 47 | 46 | 47 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (n); i++)
using ll = long long;
typedef pair<int, int> P;
int n, m;
vector<vector<P>> dat(101);
vector<int> col(10, -1);
vector<bool> check(10, false);
int main() {
int n;
cin >> n;
col = vector<int>(n + 10, -1);
check = vector<bool>(n + 10, false);
dat = vector<vector<P>>(n + 10);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
dat[u].push_back(P(v, w));
dat[v].push_back(P(u, w));
}
queue<int> que;
que.push(1);
col[1] = 0;
while (que.size()) {
int q = que.front();
que.pop();
check[q] = true;
for (auto p : dat[q]) {
if (col[p.first] != -1)
continue;
if (dat[q][p.first].second)
col[p.first] = col[q];
else
col[p.first] = abs(1 - col[q]);
if (check[p.first])
continue;
que.push(p.first);
}
}
rep(i, n) { cout << col[i] << endl; }
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (n); i++)
using ll = long long;
typedef pair<int, int> P;
int n, m;
vector<vector<P>> dat(101);
vector<int> col(10, -1);
vector<bool> check(10, false);
int main() {
int n;
cin >> n;
col = vector<int>(n + 10, -1);
check = vector<bool>(n + 10, false);
dat = vector<vector<P>>(n + 10);
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
dat[u].push_back(P(v, w));
dat[v].push_back(P(u, w));
}
queue<int> que;
que.push(1);
col[1] = 0;
while (que.size()) {
int q = que.front();
que.pop();
check[q] = true;
for (auto p : dat[q]) {
if (col[p.first] != -1)
continue;
if (p.second % 2 == 0)
col[p.first] = col[q];
else
col[p.first] = abs(1 - col[q]);
if (check[p.first])
continue;
que.push(p.first);
}
}
rep(i, n) { cout << col[i] << endl; }
} | replace | 33 | 34 | 33 | 34 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;
ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
const ll INF = 1LL << 50;
int main() {
ll N;
cin >> N;
int ans[N];
vector<P> aLst[N];
queue<ll> que;
for (int i = 0; i < N; ++i) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
P tmp0(v, w);
aLst[u].emplace_back(tmp0);
P tmp1(u, w);
aLst[v].emplace_back(tmp1);
}
que.push(0);
ans[0] = 0;
unordered_map<ll, bool> visited;
while (!que.empty()) {
ll cur = que.front();
que.pop();
// cout << "cur=" << cur << endl;
for (size_t i = 0; i < aLst[cur].size(); ++i) {
ll cur_child = aLst[cur][i].first;
if (visited.count(cur_child) == 0) {
que.push(cur_child);
visited[cur_child] = true;
if (aLst[cur][i].second % 2 == 0) {
ans[cur_child] = ans[cur];
} else {
ans[cur_child] = 1 - ans[cur];
}
}
}
}
for (int i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;
ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
const ll INF = 1LL << 50;
int main() {
ll N;
cin >> N;
int ans[N];
vector<P> aLst[N];
queue<ll> que;
for (int i = 0; i < N; ++i) {
ll u, v, w;
cin >> u >> v >> w;
P tmp0(v - 1, w);
aLst[u - 1].emplace_back(tmp0);
P tmp1(u - 1, w);
aLst[v - 1].emplace_back(tmp1);
}
que.push(0);
ans[0] = 0;
unordered_map<ll, bool> visited;
while (!que.empty()) {
ll cur = que.front();
que.pop();
// cout << "cur=" << cur << endl;
for (size_t i = 0; i < aLst[cur].size(); ++i) {
ll cur_child = aLst[cur][i].first;
if (visited.count(cur_child) == 0) {
que.push(cur_child);
visited[cur_child] = true;
if (aLst[cur][i].second % 2 == 0) {
ans[cur_child] = ans[cur];
} else {
ans[cur_child] = 1 - ans[cur];
}
}
}
}
for (int i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
return 0;
} | replace | 58 | 64 | 58 | 62 | 0 | |
p03044 | C++ | Runtime Error | // https://atcoder.jp/contests/abc126/tasks/abc126_d
#include "algorithm"
#include "iostream"
#include "queue"
#include "set"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
using namespace std;
typedef long long ll;
template <typename T> using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U> using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T> inline bool chmax(T &a, T b);
template <typename T> inline bool chmin(T &a, T b);
void print_ints(vector<ll> v);
template <typename T> void drop(T a);
// f: node, s: length
V<VPL> edges;
VL colors;
void rec(ll u, ll p, ll c) {
colors[u] = c;
for (auto edge : edges[u]) {
ll v = edge.first;
ll l = edge.second;
if (v == p)
continue;
ll nc = l % 2 == 0 ? c : 1 - c;
rec(v, u, nc);
}
}
void solve() {
ll N;
cin >> N;
edges = V<VPL>(N, VPL());
rep(i, N - 1) {
ll u, v, w;
cin >> u >> v >> w;
--u, --v;
edges[u].emplace_back(v, w);
edges[v].emplace_back(u, w);
}
// 一つの色を決め、再帰的に見て変が偶数であれば同じ色、奇数であれば違う色、としていくと、「任意の
// 2 点間の距離が偶数 ⇒ 同じ色」「任意の 2 点間の距離が奇数 ⇒
// 違う色」が実現され、後者の対偶より 「任意の 2 点が同じ色 ⇒
// 距離が偶数」が実現されることとなる
colors = VL(N, -1);
rec(0, -1, 1);
rep(i, N) { cout << colors[i] << endl; }
}
VL dists;
void rec_to_fill_dist(ll u, ll p, ll d) {
dists[u] = d;
for (auto edge : edges[u]) {
ll v = edge.first;
ll l = edge.second;
if (v == p)
continue;
rec_to_fill_dist(v, u, d + l);
}
}
void solve2() {
ll N;
cin >> N;
edges = V<VPL>(N, VPL());
rep(i, N - 1) {
ll u, v, w;
cin >> u >> v >> w;
--u, --v;
edges[u].emplace_back(v, w);
edges[v].emplace_back(u, w);
}
// 適当に根を決め、根からの距離を d[i] とすると、 2 点間の距離は d[i] + d[j] -
// 2*d[LCA] よってこの偶奇は d[i] と d[j] の偶奇で決定される
// ここで、 d[i]
// が偶数なら根と同じ色、奇数なら根と違う色を埋めるとすると、「任意の 2
// 点について同じ色」 ⇔ 「2 点の d の偶奇が等しい」 ⇔ 「2 点の距離が偶数」
// となる
colors = VL(N);
colors[0] = 1;
dists = VL(N);
rec_to_fill_dist(0, -1, 0);
repf(u, 1, N + 1) {
colors[u] = dists[u] % 2 == 0 ? colors[0] : 1 - colors[0];
}
rep(u, N) { cout << colors[u] << endl; }
}
struct exit_exception : public std::exception {
const char *what() const throw() { return "Exited"; }
};
#ifndef TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
try {
solve2();
} catch (exit_exception &e) {
}
return 0;
}
#endif
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void print_ints(vector<ll> v) {
rep(i, v.size()) {
if (i > 0) {
cout << " ";
}
cout << v[i];
}
cout << endl;
}
template <typename T> void drop(T res) {
cout << res << endl;
throw exit_exception();
}
| // https://atcoder.jp/contests/abc126/tasks/abc126_d
#include "algorithm"
#include "iostream"
#include "queue"
#include "set"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
using namespace std;
typedef long long ll;
template <typename T> using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U> using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T> inline bool chmax(T &a, T b);
template <typename T> inline bool chmin(T &a, T b);
void print_ints(vector<ll> v);
template <typename T> void drop(T a);
// f: node, s: length
V<VPL> edges;
VL colors;
void rec(ll u, ll p, ll c) {
colors[u] = c;
for (auto edge : edges[u]) {
ll v = edge.first;
ll l = edge.second;
if (v == p)
continue;
ll nc = l % 2 == 0 ? c : 1 - c;
rec(v, u, nc);
}
}
void solve() {
ll N;
cin >> N;
edges = V<VPL>(N, VPL());
rep(i, N - 1) {
ll u, v, w;
cin >> u >> v >> w;
--u, --v;
edges[u].emplace_back(v, w);
edges[v].emplace_back(u, w);
}
// 一つの色を決め、再帰的に見て変が偶数であれば同じ色、奇数であれば違う色、としていくと、「任意の
// 2 点間の距離が偶数 ⇒ 同じ色」「任意の 2 点間の距離が奇数 ⇒
// 違う色」が実現され、後者の対偶より 「任意の 2 点が同じ色 ⇒
// 距離が偶数」が実現されることとなる
colors = VL(N, -1);
rec(0, -1, 1);
rep(i, N) { cout << colors[i] << endl; }
}
VL dists;
void rec_to_fill_dist(ll u, ll p, ll d) {
dists[u] = d;
for (auto edge : edges[u]) {
ll v = edge.first;
ll l = edge.second;
if (v == p)
continue;
rec_to_fill_dist(v, u, d + l);
}
}
void solve2() {
ll N;
cin >> N;
edges = V<VPL>(N, VPL());
rep(i, N - 1) {
ll u, v, w;
cin >> u >> v >> w;
--u, --v;
edges[u].emplace_back(v, w);
edges[v].emplace_back(u, w);
}
// 適当に根を決め、根からの距離を d[i] とすると、 2 点間の距離は d[i] + d[j] -
// 2*d[LCA] よってこの偶奇は d[i] と d[j] の偶奇で決定される
// ここで、 d[i]
// が偶数なら根と同じ色、奇数なら根と違う色を埋めるとすると、「任意の 2
// 点について同じ色」 ⇔ 「2 点の d の偶奇が等しい」 ⇔ 「2 点の距離が偶数」
// となる
colors = VL(N);
colors[0] = 1;
dists = VL(N);
rec_to_fill_dist(0, -1, 0);
repf(u, 1, N) { colors[u] = dists[u] % 2 == 0 ? colors[0] : 1 - colors[0]; }
rep(u, N) { cout << colors[u] << endl; }
}
struct exit_exception : public std::exception {
const char *what() const throw() { return "Exited"; }
};
#ifndef TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
try {
solve2();
} catch (exit_exception &e) {
}
return 0;
}
#endif
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void print_ints(vector<ll> v) {
rep(i, v.size()) {
if (i > 0) {
cout << " ";
}
cout << v[i];
}
cout << endl;
}
template <typename T> void drop(T res) {
cout << res << endl;
throw exit_exception();
}
| replace | 108 | 111 | 108 | 109 | -6 | free(): invalid pointer
|
p03044 | C++ | Runtime Error | /* basic header */
#include <bits/stdc++.h>
/* define */
#define ll long long
#define dou double
#define pb emplace_back
#define mp make_pair
#define sot(a, b) sort(a + 1, a + 1 + b)
#define rep1(i, a, b) for (int i = a; i <= b; ++i)
#define rep0(i, a, b) for (int i = a; i < b; ++i)
#define eps 1e-8
#define int_inf 0x3f3f3f3f
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lson curPos << 1
#define rson curPos << 1 | 1
/* namespace */
using namespace std;
/* header end */
const int maxn = 1e5 + 10;
struct Edge {
int u, v, w;
} edge[maxn];
int n, cnt, ans[maxn], vis[maxn];
vector<int> g[maxn];
void addedge(int u, int v, int w) {
edge[++cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
g[u].push_back(cnt);
}
void bfs() {
queue<int> Q;
Q.push(1);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++) {
int cur = g[x][i];
if (!vis[edge[cur].v]) {
if (edge[cur].w % 2 == 0) {
ans[edge[cur].v] = ans[x];
} else {
ans[edge[cur].v] = 1 - ans[x];
}
Q.push(edge[cur].v);
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
bfs();
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
} | /* basic header */
#include <bits/stdc++.h>
/* define */
#define ll long long
#define dou double
#define pb emplace_back
#define mp make_pair
#define sot(a, b) sort(a + 1, a + 1 + b)
#define rep1(i, a, b) for (int i = a; i <= b; ++i)
#define rep0(i, a, b) for (int i = a; i < b; ++i)
#define eps 1e-8
#define int_inf 0x3f3f3f3f
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lson curPos << 1
#define rson curPos << 1 | 1
/* namespace */
using namespace std;
/* header end */
const int maxn = 1e5 + 10;
struct Edge {
int u, v, w;
} edge[maxn * 2];
int n, cnt, ans[maxn], vis[maxn];
vector<int> g[maxn];
void addedge(int u, int v, int w) {
edge[++cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
g[u].push_back(cnt);
}
void bfs() {
queue<int> Q;
Q.push(1);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++) {
int cur = g[x][i];
if (!vis[edge[cur].v]) {
if (edge[cur].w % 2 == 0) {
ans[edge[cur].v] = ans[x];
} else {
ans[edge[cur].v] = 1 - ans[x];
}
Q.push(edge[cur].v);
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
bfs();
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p03044 | C++ | Runtime Error | // common include
#include <bits/stdc++.h>
using namespace std;
// global variables
#define debug(x) cout << #x << ": " << x << endl
typedef long long ll;
const int M = 1e9 + 7;
const int INF = 1e9;
// typedef
typedef pair<int, int> P;
// sort by P.first (asd)
bool comPair(const P &firstElof, const P &secondElof) {
return firstElof.first < secondElof.first;
}
// moving 4-direction
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
#define MAX_N 100000 // 10^5
template <class Abel> struct UnionFind {
vector<int> par;
vector<int> rank;
vector<Abel> diff_weight;
UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }
void init(int n = 1, Abel SUM_UNITY = 0) {
par.resize(n);
rank.resize(n);
diff_weight.resize(n);
for (int i = 0; i < n; ++i)
par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
diff_weight[x] += diff_weight[par[x]];
return par[x] = r;
}
}
Abel weight(int x) {
root(x);
return diff_weight[x];
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y, Abel w) {
w += weight(x);
w -= weight(y);
x = root(x);
y = root(y);
if (x == y)
return false;
if (rank[x] < rank[y])
swap(x, y), w = -w;
if (rank[x] == rank[y])
++rank[x];
par[y] = x;
diff_weight[y] = w;
return true;
}
Abel diff(int x, int y) { return weight(y) - weight(x); }
};
struct Edge {
int to;
int weight;
};
vector<Edge> Graph[100000];
bool checked[100000];
int color[100000];
void dfs(int start, int c) {
checked[start] = true;
color[start] = c;
for (auto gra : Graph[start]) {
if (!checked[gra.to]) {
if (gra.weight % 2 == 0)
dfs(gra.to, c);
else
(c == 0) ? dfs(gra.to, 1) : dfs(gra.to, 0);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int f, t, w;
cin >> f >> t >> w;
Graph[f].emplace_back(Edge{t, w});
Graph[t].emplace_back(Edge{f, w});
}
dfs(1, 1);
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
}
| // common include
#include <bits/stdc++.h>
using namespace std;
// global variables
#define debug(x) cout << #x << ": " << x << endl
typedef long long ll;
const int M = 1e9 + 7;
const int INF = 1e9;
// typedef
typedef pair<int, int> P;
// sort by P.first (asd)
bool comPair(const P &firstElof, const P &secondElof) {
return firstElof.first < secondElof.first;
}
// moving 4-direction
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
#define MAX_N 100000 // 10^5
template <class Abel> struct UnionFind {
vector<int> par;
vector<int> rank;
vector<Abel> diff_weight;
UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }
void init(int n = 1, Abel SUM_UNITY = 0) {
par.resize(n);
rank.resize(n);
diff_weight.resize(n);
for (int i = 0; i < n; ++i)
par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
diff_weight[x] += diff_weight[par[x]];
return par[x] = r;
}
}
Abel weight(int x) {
root(x);
return diff_weight[x];
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y, Abel w) {
w += weight(x);
w -= weight(y);
x = root(x);
y = root(y);
if (x == y)
return false;
if (rank[x] < rank[y])
swap(x, y), w = -w;
if (rank[x] == rank[y])
++rank[x];
par[y] = x;
diff_weight[y] = w;
return true;
}
Abel diff(int x, int y) { return weight(y) - weight(x); }
};
struct Edge {
int to;
int weight;
};
const int MAX = 1e5 + 5;
vector<Edge> Graph[MAX];
bool checked[MAX];
int color[MAX];
void dfs(int start, int c) {
checked[start] = true;
color[start] = c;
for (auto gra : Graph[start]) {
if (!checked[gra.to]) {
if (gra.weight % 2 == 0)
dfs(gra.to, c);
else
(c == 0) ? dfs(gra.to, 1) : dfs(gra.to, 0);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int f, t, w;
cin >> f >> t >> w;
Graph[f].emplace_back(Edge{t, w});
Graph[t].emplace_back(Edge{f, w});
}
dfs(1, 1);
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
}
| replace | 78 | 81 | 78 | 82 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
string s;
int k;
int INFi = 1000000000;
ll INFl = 1000000000000000;
struct UnionFind {
vector<int> par; // 初期化
vector<int> rank; // 高さ
UnionFind(int n = 0) : par(n), rank(n, 0) {
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;
if (rank[rx] < rank[ry])
rank[rx] = ry;
else
rank[ry] = rx;
if (rank[rx] == rank[ry])
rank[rx]++;
}
bool same(int x, int y) { return root(x) == root(y); }
};
vector<int> color(100000, -1);
vector<pi> G[100000];
void dfs(int s, int c) {
color[s] = c;
for (int i = 0; i < G[s].size(); i++) {
if (color[G[s][i].first] == -1) {
if (G[s][i].second % 2 == 0)
dfs(G[s][i].first, (c == 1 ? 1 : 0));
else
dfs(G[s][i].first, (c == 1 ? 0 : 1));
}
}
}
int main() {
int N;
cin >> N;
int u, v, w;
for (int i = 0; i < N; i++) {
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(pi(v, w));
G[v].push_back(pi(u, w));
}
dfs(0, 1);
for (int i = 0; i < N; i++)
cout << color[i] << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
string s;
int k;
int INFi = 1000000000;
ll INFl = 1000000000000000;
struct UnionFind {
vector<int> par; // 初期化
vector<int> rank; // 高さ
UnionFind(int n = 0) : par(n), rank(n, 0) {
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;
if (rank[rx] < rank[ry])
rank[rx] = ry;
else
rank[ry] = rx;
if (rank[rx] == rank[ry])
rank[rx]++;
}
bool same(int x, int y) { return root(x) == root(y); }
};
vector<int> color(100000, -1);
vector<pi> G[100000];
void dfs(int s, int c) {
color[s] = c;
for (int i = 0; i < G[s].size(); i++) {
if (color[G[s][i].first] == -1) {
if (G[s][i].second % 2 == 0)
dfs(G[s][i].first, (c == 1 ? 1 : 0));
else
dfs(G[s][i].first, (c == 1 ? 0 : 1));
}
}
}
int main() {
int N;
cin >> N;
int u, v, w;
for (int i = 0; i < N - 1; i++) {
cin >> u >> v >> w;
u--;
v--;
G[u].push_back(pi(v, w));
G[v].push_back(pi(u, w));
}
dfs(0, 1);
for (int i = 0; i < N; i++)
cout << color[i] << endl;
} | replace | 64 | 65 | 64 | 65 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 1e5;
struct edge {
int to, cost;
};
int n;
vector<vector<edge>> G(MAXN);
vector<int> color(MAXN, -1);
void dfs(int u, int c) {
color[u] = c;
for (auto e : G[u]) {
if (color[e.to] != -1)
continue;
if (e.cost % 2 == 0)
dfs(e.to, c);
else if (e.cost % 2 == 1)
dfs(e.to, 1 - c);
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int from, to, cost;
cin >> from >> to >> cost;
from--;
to--;
G[from].push_back(edge{to, cost});
G[to].push_back(edge{from, cost});
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
cout << color[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 1e5;
struct edge {
int to, cost;
};
int n;
vector<vector<edge>> G(MAXN);
vector<int> color(MAXN, -1);
void dfs(int u, int c) {
color[u] = c;
for (auto e : G[u]) {
if (color[e.to] != -1)
continue;
if (e.cost % 2 == 0)
dfs(e.to, c);
else if (e.cost % 2 == 1)
dfs(e.to, 1 - c);
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int from, to, cost;
cin >> from >> to >> cost;
from--;
to--;
G[from].push_back(edge{to, cost});
G[to].push_back(edge{from, cost});
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
cout << color[i] << endl;
}
return 0;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(long long N, std::vector<long long> u, std::vector<long long> v,
std::vector<long long> w) {
vector<bool> visited(N + 1, false);
vector<ll> length(N + 1, 0);
vector<vector<pair<ll, ll>>> link_list(N + 1);
for (ll i = 0; i < N; i++) {
link_list[u[i]].push_back(make_pair(v[i], w[i]));
link_list[v[i]].push_back(make_pair(u[i], w[i]));
}
queue<ll> q;
q.push(1);
visited[1] = true;
while (!q.empty()) {
auto node = q.front();
q.pop();
for (const auto &next_node : link_list[node]) {
if (!visited[next_node.first]) {
q.push(next_node.first);
length[next_node.first] = length[node] + next_node.second;
visited[next_node.first] = true;
}
}
}
for (ll i = 1; i <= N; i++) {
printf("%s\n", length[i] % 2 == 0 ? "0" : "1");
}
}
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
long long N;
scanf("%lld", &N);
std::vector<long long> u(N - 1);
std::vector<long long> v(N - 1);
std::vector<long long> w(N - 1);
for (int i = 0; i < N - 1; i++) {
scanf("%lld", &u[i]);
scanf("%lld", &v[i]);
scanf("%lld", &w[i]);
}
solve(N, std::move(u), std::move(v), std::move(w));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(long long N, std::vector<long long> u, std::vector<long long> v,
std::vector<long long> w) {
vector<bool> visited(N + 1, false);
vector<ll> length(N + 1, 0);
vector<vector<pair<ll, ll>>> link_list(N + 1);
for (ll i = 0; i < N - 1; i++) {
link_list[u[i]].push_back(make_pair(v[i], w[i] % 2));
link_list[v[i]].push_back(make_pair(u[i], w[i] % 2));
}
queue<ll> q;
q.push(1);
visited[1] = true;
while (!q.empty()) {
auto node = q.front();
q.pop();
for (const auto &next_node : link_list[node]) {
if (!visited[next_node.first]) {
q.push(next_node.first);
length[next_node.first] = length[node] + next_node.second;
visited[next_node.first] = true;
}
}
}
for (ll i = 1; i <= N; i++) {
printf("%s\n", length[i] % 2 == 0 ? "0" : "1");
}
}
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
long long N;
scanf("%lld", &N);
std::vector<long long> u(N - 1);
std::vector<long long> v(N - 1);
std::vector<long long> w(N - 1);
for (int i = 0; i < N - 1; i++) {
scanf("%lld", &u[i]);
scanf("%lld", &v[i]);
scanf("%lld", &w[i]);
}
solve(N, std::move(u), std::move(v), std::move(w));
return 0;
}
| replace | 9 | 12 | 9 | 12 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <type_traits>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
rep(i, (int)v.size()) {
if (i)
os << ",";
os << v[i];
}
os << "}";
return os;
}
template <typename T, size_t S> void printArray(const T (&array)[S]) {
for (auto val : array)
std::cout << val << ", ";
std::cout << "\n";
}
const int mod = 1e9 + 7;
const ll inf = 1e18 + 5;
struct UnionFind {
vi parent, rank, count;
UnionFind(const int nodeSize)
: parent(nodeSize), rank(nodeSize, 1), count(nodeSize) {
rep(i, nodeSize) parent[i] = i, count[i] = 1;
}
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
int rx = find(x);
int ry = find(y);
if (rx == ry)
return;
if (rank[rx] < rank[ry])
swap(rx, ry);
parent[ry] = rx;
count[rx] += count[ry];
if (rank[rx] == rank[ry])
++rank[rx];
}
bool same(int x, int y) { return find(x) == find(y); }
int num(int x) { return count[find(x)]; }
};
struct Edge {
int to, cost;
};
vector<vector<Edge>> tree;
vl ans;
void dfs(int par, int now) {
for (auto &edge : tree[now])
if (edge.to != par) {
ans[edge.to] = ans[now] + edge.cost;
dfs(now, edge.to);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
int n;
std::cin >> n;
tree.resize(n);
rep(i, n) {
int u, v, w;
std::cin >> u >> v >> w;
u--, v--;
tree[u].pb({v, w}), tree[v].pb({u, w});
}
ans.resize(n);
ans[0] = 0;
dfs(-1, 0);
rep(i, n) std::cout << (ans[i] & 1) << "\n";
}
| #include <bits/stdc++.h>
#include <type_traits>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
rep(i, (int)v.size()) {
if (i)
os << ",";
os << v[i];
}
os << "}";
return os;
}
template <typename T, size_t S> void printArray(const T (&array)[S]) {
for (auto val : array)
std::cout << val << ", ";
std::cout << "\n";
}
const int mod = 1e9 + 7;
const ll inf = 1e18 + 5;
struct UnionFind {
vi parent, rank, count;
UnionFind(const int nodeSize)
: parent(nodeSize), rank(nodeSize, 1), count(nodeSize) {
rep(i, nodeSize) parent[i] = i, count[i] = 1;
}
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
int rx = find(x);
int ry = find(y);
if (rx == ry)
return;
if (rank[rx] < rank[ry])
swap(rx, ry);
parent[ry] = rx;
count[rx] += count[ry];
if (rank[rx] == rank[ry])
++rank[rx];
}
bool same(int x, int y) { return find(x) == find(y); }
int num(int x) { return count[find(x)]; }
};
struct Edge {
int to, cost;
};
vector<vector<Edge>> tree;
vl ans;
void dfs(int par, int now) {
for (auto &edge : tree[now])
if (edge.to != par) {
ans[edge.to] = ans[now] + edge.cost;
dfs(now, edge.to);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
int n;
std::cin >> n;
tree.resize(n);
rep(i, n - 1) {
int u, v, w;
std::cin >> u >> v >> w;
u--, v--;
tree[u].pb({v, w}), tree[v].pb({u, w});
}
ans.resize(n);
ans[0] = 0;
dfs(-1, 0);
rep(i, n) std::cout << (ans[i] & 1) << "\n";
}
| replace | 103 | 104 | 103 | 104 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<int> color;
void dfs(int v, int dis, vector<vector<pair<int, int>>> G) {
if (dis % 2 == 0)
color.at(v) = 0;
else
color.at(v) = 1;
for (auto k : G.at(v)) {
int nv = k.first;
int tmp = k.second;
if (color.at(nv) != -1)
continue;
dfs(nv, dis + tmp, G);
}
}
int main() {
int n;
cin >> n;
vector<vector<pair<int, int>>> G(n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G.at(u).push_back({v, w});
G.at(v).push_back({u, w});
}
color.resize(n, -1);
color.at(0) = 0;
dfs(0, 0, G);
for (int a : color)
cout << a << endl;
} | #include <bits/stdc++.h>
using namespace std;
vector<int> color;
void dfs(int v, int dis, vector<vector<pair<int, int>>> &G) {
if (dis % 2 == 0)
color.at(v) = 0;
else
color.at(v) = 1;
for (auto k : G.at(v)) {
int nv = k.first;
int tmp = k.second;
if (color.at(nv) != -1)
continue;
dfs(nv, dis + tmp, G);
}
}
int main() {
int n;
cin >> n;
vector<vector<pair<int, int>>> G(n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
G.at(u).push_back({v, w});
G.at(v).push_back({u, w});
}
color.resize(n, -1);
color.at(0) = 0;
dfs(0, 0, G);
for (int a : color)
cout << a << endl;
} | replace | 5 | 6 | 5 | 6 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
int n;
scanf("%d", &n);
vector<vector<pii>> g(n);
v1i f(n, 0);
rep(i, n) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
--a;
--b;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
deque<int> d;
d.push_back(0);
int tmp = 0;
v1b e(n, 1);
e[0] = 0;
while (!d.empty()) {
d.pop_front();
for (auto h : g[tmp]) {
if (e[h.fi]) {
e[h.fi] = 0;
d.push_back(h.fi);
f[h.fi] = f[tmp] + h.se;
}
}
tmp = d.front();
}
rep(i, n) {
if (f[i] & 1)
printf("1\n");
else
printf("0\n");
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
int n;
scanf("%d", &n);
vector<vector<pii>> g(n);
v1i f(n, 0);
rep(i, n - 1) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
--a;
--b;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
deque<int> d;
d.push_back(0);
int tmp = 0;
v1b e(n, 1);
e[0] = 0;
while (!d.empty()) {
d.pop_front();
for (auto h : g[tmp]) {
if (e[h.fi]) {
e[h.fi] = 0;
d.push_back(h.fi);
f[h.fi] = f[tmp] + h.se;
}
}
tmp = d.front();
}
rep(i, n) {
if (f[i] & 1)
printf("1\n");
else
printf("0\n");
}
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <cmath>
#include <map>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INF = 1e18;
#define foreach(ix, a) for (auto &(ix) : (a))
vector<vector<pair<ll, ll>>> edge(11000);
ll seen[11000];
void dfs(ll i, ll co) {
for (pair<ll, ll> v : edge[i]) {
ll p = v.first, dis = v.second;
if (seen[p] != -1)
continue;
seen[p] = (co + dis) % 2;
dfs(p, seen[p]);
}
}
int main() {
int n, m;
cin >> n;
rep(i, n) seen[i] = -1;
rep(i, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
edge[u].push_back({v, w});
edge[v].push_back({u, w});
}
seen[0] = 0;
dfs(0, seen[0]);
rep(i, n) { cout << seen[i] << endl; }
} | #include <bits/stdc++.h>
#include <cmath>
#include <map>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define been(ix) (ix).begin(), (ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INF = 1e18;
#define foreach(ix, a) for (auto &(ix) : (a))
vector<vector<pair<ll, ll>>> edge(110000);
ll seen[110000];
void dfs(ll i, ll co) {
for (pair<ll, ll> v : edge[i]) {
ll p = v.first, dis = v.second;
if (seen[p] != -1)
continue;
seen[p] = (co + dis) % 2;
dfs(p, seen[p]);
}
}
int main() {
int n, m;
cin >> n;
rep(i, n) seen[i] = -1;
rep(i, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
edge[u].push_back({v, w});
edge[v].push_back({u, w});
}
seen[0] = 0;
dfs(0, seen[0]);
rep(i, n) { cout << seen[i] << endl; }
} | replace | 11 | 13 | 11 | 13 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX_V 100000
#define MAX_E 100000
vector<pair<int, int>> G[MAX_V];
int color[MAX_V];
bool dfs(int v, int c) {
// printf("%d %d\n", v, c);
color[v] = c + 1;
for (int i = 0; i < G[v].size(); i++) {
if (color[G[v][i].first] == 0) // not visited
{
dfs(G[v][i].first, c ^ G[v][i].second);
}
}
}
int main() {
int n;
int u, v, w;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> u >> v >> w;
G[u].push_back(make_pair(v, w % 2));
G[v].push_back(make_pair(u, w % 2));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << color[i] - 1 << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define MAX_V 100001
#define MAX_E 100001
vector<pair<int, int>> G[MAX_V];
int color[MAX_V];
bool dfs(int v, int c) {
// printf("%d %d\n", v, c);
color[v] = c + 1;
for (int i = 0; i < G[v].size(); i++) {
if (color[G[v][i].first] == 0) // not visited
{
dfs(G[v][i].first, c ^ G[v][i].second);
}
}
}
int main() {
int n;
int u, v, w;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> u >> v >> w;
G[u].push_back(make_pair(v, w % 2));
G[v].push_back(make_pair(u, w % 2));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << color[i] - 1 << endl;
}
} | replace | 3 | 5 | 3 | 5 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef long double ld;
#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using edge = pair<int, llint>;
using Graph = vector<vector<edge>>;
llint n;
Graph G;
vector<llint> dir;
void dfs(int v, int p, int cur) {
dir[v] = cur;
for (auto e : G[v]) {
if (e.first == p)
continue;
if (e.second & 1)
dfs(e.first - 1, v, 1 - cur); // 長さが奇数
else
dfs(e.first, v, cur);
}
}
void solve() {
cin >> n;
G.assign(n, vector<edge>());
for (int i = 0; i < n - 1; i++) {
llint a, b, c;
cin >> a >> b >> c;
a--;
b--;
G[a].push_back(edge(b, c));
G[b].push_back(edge(a, c));
}
dir.assign(n, 0);
dfs(0, -1, 1);
for (auto d : dir)
cout << d << endl;
}
int main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef long double ld;
#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using edge = pair<int, llint>;
using Graph = vector<vector<edge>>;
llint n;
Graph G;
vector<llint> dir;
void dfs(int v, int p, int cur) {
dir[v] = cur;
for (auto e : G[v]) {
if (e.first == p)
continue;
if (e.second & 1)
dfs(e.first, v, 1 - cur); // 長さが奇数
else
dfs(e.first, v, cur);
}
}
void solve() {
cin >> n;
G.assign(n, vector<edge>());
for (int i = 0; i < n - 1; i++) {
llint a, b, c;
cin >> a >> b >> c;
a--;
b--;
G[a].push_back(edge(b, c));
G[b].push_back(edge(a, c));
}
dir.assign(n, 0);
dfs(0, -1, 1);
for (auto d : dir)
cout << d << endl;
}
int main() {
solve();
return 0;
}
| replace | 31 | 32 | 31 | 32 | -11 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef long long int llint;
#define MM 1000000000
#define MOD MM + 7
#define pi pair<int, int>
#define pl pair<ll, ll>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; }
int main() {
ll N;
cin >> N;
vector<vector<pair<ll, ll>>> G(N);
for (int i = 0; i < N - 1; i++) {
ll a, b, w;
cin >> a >> b >> w;
a--;
b--;
w %= (ll)2;
G[a].push_back({b, w});
G[b].push_back({a, w});
}
vector<ll> dist(N, -1);
queue<ll> que;
for (ll v = 0; v < N; v++) {
if (dist[v] != -1)
continue;
dist[v] = 1;
que.push(v);
while (que.size()) {
ll s = que.front();
que.pop();
for (ll nv = 0; nv < G[s].size(); nv++) {
if (dist[G[s][nv].first] == -1) {
dist[G[s][nv].first] = dist[s] + G[s][nv].second;
dist[G[s][nv].first] %= 2;
que.push(dist[G[s][nv].first]);
}
}
}
}
for (int i = 0; i < N; i++) {
cout << dist[i] << endl;
}
}
| #include <algorithm>
#include <bitset>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef long long int llint;
#define MM 1000000000
#define MOD MM + 7
#define pi pair<int, int>
#define pl pair<ll, ll>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
template <typename T> T GCD(T u, T v) { return v ? GCD(v, u % v) : u; }
int main() {
ll N;
cin >> N;
vector<vector<pair<ll, ll>>> G(N);
for (int i = 0; i < N - 1; i++) {
ll a, b, w;
cin >> a >> b >> w;
a--;
b--;
w %= (ll)2;
G[a].push_back({b, w});
G[b].push_back({a, w});
}
vector<ll> dist(N, -1);
queue<ll> que;
for (ll v = 0; v < N; v++) {
if (dist[v] != -1)
continue;
dist[v] = 1;
que.push(v);
while (que.size()) {
ll s = que.front();
que.pop();
for (ll nv = 0; nv < G[s].size(); nv++) {
if (dist[G[s][nv].first] == -1) {
dist[G[s][nv].first] = dist[s] + G[s][nv].second;
dist[G[s][nv].first] %= 2;
que.push(G[s][nv].first);
}
}
}
}
for (int i = 0; i < N; i++) {
cout << dist[i] << endl;
}
}
| replace | 55 | 56 | 55 | 56 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
long long u[110000], v[110000], w[110000];
cin >> n;
bool isw[110000][3];
bool f = false;
long count = 0;
for (int i = 0; i < 10050; i++) // 初期化
{
isw[i][0] = false;
isw[i][1] = false;
}
isw[1][0] = true;
isw[1][1] = true; // 1の色を決めておく
for (int i = 1; i < n; i++) {
cin >> u[i] >> v[i] >> w[i];
}
while (!f) {
for (int i = 1; i < n; i++) {
if (isw[u[i]][1] == true) // u[i]の色が確定してるとき
{
isw[v[i]][0] = isw[u[i]][0]; // v[i]の色を決める
// cout << v[i] << " paint " << isw[v[i]][0] << " because of " << u[i]
// << endl;;
if (w[i] % 2 == 1) // 距離が奇数のとき
{
isw[v[i]][0] = !isw[v[i]][0]; // 色を反転
}
// cout << isw[v[i]][1] << endl;
if (isw[v[i]][1] == false) // 色を決めたとき
{
count++;
// cout << "count++" << endl;
}
isw[v[i]][1] = true; // 確定フラグ
}
if (isw[v[i]][1] == true) // v[i]の色が確定してるとき
{
isw[u[i]][0] = isw[v[i]][0]; // u[i]の色を決める
// cout << u[i] << " paint " << isw[u[i]][0] << " because of " << v[i]
// << endl;;
if (w[i] % 2 == 1) {
isw[u[i]][0] = !isw[u[i]][0];
}
// cout << isw[v[i]][1] << endl;
if (isw[u[i]][1] == false) {
count++;
// cout << "count++" << endl;
}
isw[u[i]][1] = true; // 確定フラグ
}
if (count == n - 1)
f = true;
}
}
for (int i = 1; i <= n; i++) {
if (isw[i][0] == 1)
cout << 1 << endl;
else
cout << 0 << endl;
}
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
long long u[110000], v[110000], w[110000];
cin >> n;
bool isw[110000][3];
bool f = false;
long count = 0;
for (int i = 0; i < 10050; i++) // 初期化
{
isw[i][0] = false;
isw[i][1] = false;
}
isw[1][0] = true;
isw[1][1] = true; // 1の色を決めておく
for (int i = 1; i < n; i++) {
cin >> u[i] >> v[i] >> w[i];
}
while (!f) {
for (int i = 1; i < n; i++) {
if (isw[u[i]][1] == true) // u[i]の色が確定してるとき
{
isw[v[i]][0] = isw[u[i]][0]; // v[i]の色を決める
// cout << v[i] << " paint " << isw[v[i]][0] << " because of " << u[i]
// << endl;;
if (w[i] % 2 == 1) // 距離が奇数のとき
{
isw[v[i]][0] = !isw[v[i]][0]; // 色を反転
}
// cout << isw[v[i]][1] << endl;
if (isw[v[i]][1] == false) // 色を決めたとき
{
count++;
// cout << "count++" << endl;
}
isw[v[i]][1] = true; // 確定フラグ
}
if (isw[v[i]][1] == true) // v[i]の色が確定してるとき
{
isw[u[i]][0] = isw[v[i]][0]; // u[i]の色を決める
// cout << u[i] << " paint " << isw[u[i]][0] << " because of " << v[i]
// << endl;;
if (w[i] % 2 == 1) {
isw[u[i]][0] = !isw[u[i]][0];
}
// cout << isw[v[i]][1] << endl;
if (isw[u[i]][1] == false) {
count++;
// cout << "count++" << endl;
}
isw[u[i]][1] = true; // 確定フラグ
}
if (count == n - 1)
f = true;
}
for (int i = n; i > 1; i--) {
if (isw[u[i]][1] == true) // u[i]の色が確定してるとき
{
isw[v[i]][0] = isw[u[i]][0]; // v[i]の色を決める
// cout << v[i] << " paint " << isw[v[i]][0] << " because of " << u[i]
// << endl;;
if (w[i] % 2 == 1) // 距離が奇数のとき
{
isw[v[i]][0] = !isw[v[i]][0]; // 色を反転
}
// cout << isw[v[i]][1] << endl;
if (isw[v[i]][1] == false) // 色を決めたとき
{
count++;
// cout << "count++" << endl;
}
isw[v[i]][1] = true; // 確定フラグ
}
if (isw[v[i]][1] == true) // v[i]の色が確定してるとき
{
isw[u[i]][0] = isw[v[i]][0]; // u[i]の色を決める
// cout << u[i] << " paint " << isw[u[i]][0] << " because of " << v[i]
// << endl;;
if (w[i] % 2 == 1) {
isw[u[i]][0] = !isw[u[i]][0];
}
// cout << isw[v[i]][1] << endl;
if (isw[u[i]][1] == false) {
count++;
// cout << "count++" << endl;
}
isw[u[i]][1] = true; // 確定フラグ
}
if (count == n - 1)
f = true;
}
}
for (int i = 1; i <= n; i++) {
if (isw[i][0] == 1)
cout << 1 << endl;
else
cout << 0 << endl;
}
} | insert | 62 | 62 | 62 | 100 | TLE | |
p03044 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int dfs(int u, vector<vector<int>> G, vector<vector<int>> G_w,
vector<int> &visit, vector<int> &nagasa) {
visit[u] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (visit[v] == 0) {
nagasa[v] = (nagasa[u] + G_w[u][i] % 2) % 2;
dfs(v, G, G_w, visit, nagasa);
}
}
return 0;
}
int main() {
int N;
cin >> N;
vector<vector<int>> G(N);
vector<vector<int>> G_w(N);
vector<int> nagasa(N, 0);
vector<int> visit(N, 0);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u - 1].push_back(v - 1);
G[v - 1].push_back(u - 1);
G_w[u - 1].push_back(w);
G_w[v - 1].push_back(w);
}
dfs(0, G, G_w, visit, nagasa);
for (int i = 0; i < N; i++) {
if (nagasa[i] == 0) {
cout << '1' << endl;
} else {
cout << '0' << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int dfs(int u, vector<vector<int>> &G, vector<vector<int>> &G_w,
vector<int> &visit, vector<int> &nagasa) {
visit[u] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (visit[v] == 0) {
nagasa[v] = (nagasa[u] + G_w[u][i] % 2) % 2;
dfs(v, G, G_w, visit, nagasa);
}
}
return 0;
}
int main() {
int N;
cin >> N;
vector<vector<int>> G(N);
vector<vector<int>> G_w(N);
vector<int> nagasa(N, 0);
vector<int> visit(N, 0);
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u - 1].push_back(v - 1);
G[v - 1].push_back(u - 1);
G_w[u - 1].push_back(w);
G_w[v - 1].push_back(w);
}
dfs(0, G, G_w, visit, nagasa);
for (int i = 0; i < N; i++) {
if (nagasa[i] == 0) {
cout << '1' << endl;
} else {
cout << '0' << endl;
}
}
return 0;
} | replace | 5 | 6 | 5 | 6 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
vector<vector<P>> v(11111);
int out[111111];
void dfs(int now, int pre) {
REP(i, v[now].size()) {
if (v[now][i].FI == pre)
continue;
if (v[now][i].SE % 2) {
out[v[now][i].FI] = 1 - out[now];
} else {
out[v[now][i].FI] = out[now];
}
dfs(v[now][i].FI, now);
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
REP(i, n - 1) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
v[a].EB(b, c);
v[b].EB(a, c);
}
dfs(0, -1);
REP(i, n) cout << out[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
vector<vector<P>> v(111111);
int out[111111];
void dfs(int now, int pre) {
REP(i, v[now].size()) {
if (v[now][i].FI == pre)
continue;
if (v[now][i].SE % 2) {
out[v[now][i].FI] = 1 - out[now];
} else {
out[v[now][i].FI] = out[now];
}
dfs(v[now][i].FI, now);
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
REP(i, n - 1) {
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
v[a].EB(b, c);
v[b].EB(a, c);
}
dfs(0, -1);
REP(i, n) cout << out[i] << endl;
return 0;
}
| replace | 33 | 34 | 33 | 34 | 0 | |
p03044 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
#define fi first
#define se second
vector<pair<int, int>> adj[10001];
int color[10001];
bool visited[10001];
void dfs(pair<int, int> cur) {
visited[cur.fi] = true;
for (int i = 0; i < adj[cur.fi].size(); i++) {
if (!visited[adj[cur.fi][i].fi]) {
if (adj[cur.fi][i].se % 2 == 0)
color[adj[cur.fi][i].fi] = color[cur.fi];
else
color[adj[cur.fi][i].fi] = 1 - color[cur.fi];
dfs(adj[cur.fi][i]);
}
}
}
int main() {
int a, x, y, z;
cin >> a;
for (int i = 1; i < a; i++) {
cin >> x >> y >> z;
adj[x].push_back({y, z});
adj[y].push_back({x, z});
}
pair<int, int> param;
param.fi = 1;
dfs(param);
for (int i = 1; i <= a; i++)
cout << color[i] << endl;
} | #include <iostream>
#include <vector>
using namespace std;
#define fi first
#define se second
vector<pair<int, int>> adj[100001];
int color[100001];
bool visited[100001];
void dfs(pair<int, int> cur) {
visited[cur.fi] = true;
for (int i = 0; i < adj[cur.fi].size(); i++) {
if (!visited[adj[cur.fi][i].fi]) {
if (adj[cur.fi][i].se % 2 == 0)
color[adj[cur.fi][i].fi] = color[cur.fi];
else
color[adj[cur.fi][i].fi] = 1 - color[cur.fi];
dfs(adj[cur.fi][i]);
}
}
}
int main() {
int a, x, y, z;
cin >> a;
for (int i = 1; i < a; i++) {
cin >> x >> y >> z;
adj[x].push_back({y, z});
adj[y].push_back({x, z});
}
pair<int, int> param;
param.fi = 1;
dfs(param);
for (int i = 1; i <= a; i++)
cout << color[i] << endl;
} | replace | 5 | 8 | 5 | 8 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
void addEdge(vector<pair<int, int>> g[], int u, int v, int w) {
// g[u].push_back({v, w});
// g[v].push_back({u, w});
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
void dfs_util(vector<pair<int, int>> g[], int s, bool col[], int pcol,
vector<bool> &visited) {
visited[s] = true;
for (int i = 0; i < g[s].size(); ++i) {
if (visited[g[s][i].first]) {
continue;
}
if (g[s][i].second % 2)
col[g[s][i].first] = !pcol;
else
col[g[s][i].first] = pcol;
dfs_util(g, g[s][i].first, col, col[g[s][i].first], visited);
}
}
void dfs(vector<pair<int, int>> g[], int n, bool col[]) {
col[1] = 0;
vector<bool> visited(n + 1, false);
dfs_util(g, 1, col, col[1], visited);
for (int i = 1; i <= n; ++i)
cout << col[i] << ' ';
}
int main() {
// freopen("a.txt", "r", stdin);
int N, u, v, w;
cin >> N;
vector<pair<int, int>> g[10010];
for (int i = 0; i < N - 1; ++i) {
cin >> u >> v >> w;
addEdge(g, u, v, w);
}
bool col[100101];
dfs(g, N, col);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void addEdge(vector<pair<int, int>> g[], int u, int v, int w) {
// g[u].push_back({v, w});
// g[v].push_back({u, w});
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
void dfs_util(vector<pair<int, int>> g[], int s, bool col[], int pcol,
vector<bool> &visited) {
visited[s] = true;
for (int i = 0; i < g[s].size(); ++i) {
if (visited[g[s][i].first]) {
continue;
}
if (g[s][i].second % 2)
col[g[s][i].first] = !pcol;
else
col[g[s][i].first] = pcol;
dfs_util(g, g[s][i].first, col, col[g[s][i].first], visited);
}
}
void dfs(vector<pair<int, int>> g[], int n, bool col[]) {
col[1] = 0;
vector<bool> visited(n + 1, false);
dfs_util(g, 1, col, col[1], visited);
for (int i = 1; i <= n; ++i)
cout << col[i] << ' ';
}
int main() {
// freopen("a.txt", "r", stdin);
int N, u, v, w;
cin >> N;
vector<pair<int, int>> g[100100];
for (int i = 0; i < N - 1; ++i) {
cin >> u >> v >> w;
addEdge(g, u, v, w);
}
bool col[100101];
dfs(g, N, col);
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p03044 | C++ | Runtime Error | /*Author: Gautam*/
#include <bits/stdc++.h>
#define FastIO \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
#define MOD 1000000007
#define ull unsigned long long int
#define newline cout << endl
/* TYPE DEFINITIONS */
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
/* MATH */
bool isPrime(ull n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ull i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
ll pow(ll a, ll b, ll m) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
//
/* MACROS */
#define all(A) (A).begin(), (A).end()
#define arrall(A, n) (A), ((A) + (n))
#define rall(A) (A).rbegin(), (A).rend()
#define sz(A) (int)(A).size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ln(X) (int)(X).length()
#define square(X) ((X) * (X))
#define cube(X) ((X) * (X) * (X))
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define fore(it, a) \
for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
std::vector<pi> v[100005];
bool visited[10000];
bool color[10000];
void dfs(int s, int su) {
if (visited[s])
return;
visited[s] = true;
if (su & 1)
color[s] = true;
for (auto i : v[s]) {
dfs(i.first, su + i.second);
}
}
void solve() {
int n;
cin >> n;
forn(i, n - 1) {
int a, b, w;
cin >> a >> b >> w;
v[a].pb({b, w});
v[b].pb({a, w});
}
fora(i, 1, n) {
if (!visited[i]) {
dfs(i, 0);
}
}
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
}
int main() {
FastIO;
ll t = 1;
// cin>>t;
while (t--) {
solve();
}
} | /*Author: Gautam*/
#include <bits/stdc++.h>
#define FastIO \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
#define MOD 1000000007
#define ull unsigned long long int
#define newline cout << endl
/* TYPE DEFINITIONS */
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
/* MATH */
bool isPrime(ull n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ull i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
ll pow(ll a, ll b, ll m) {
ll res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
//
/* MACROS */
#define all(A) (A).begin(), (A).end()
#define arrall(A, n) (A), ((A) + (n))
#define rall(A) (A).rbegin(), (A).rend()
#define sz(A) (int)(A).size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ln(X) (int)(X).length()
#define square(X) ((X) * (X))
#define cube(X) ((X) * (X) * (X))
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define fore(it, a) \
for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
std::vector<pi> v[100005];
bool visited[100005];
bool color[100005];
void dfs(int s, ll su) {
if (visited[s])
return;
visited[s] = true;
if (su & 1)
color[s] = true;
for (auto i : v[s]) {
dfs(i.first, su + i.second);
}
}
void solve() {
int n;
cin >> n;
forn(i, n - 1) {
int a, b, w;
cin >> a >> b >> w;
v[a].pb({b, w});
v[b].pb({a, w});
}
fora(i, 1, n) {
if (!visited[i]) {
dfs(i, 0);
}
}
for (int i = 1; i <= n; i++) {
cout << color[i] << endl;
}
}
int main() {
FastIO;
ll t = 1;
// cin>>t;
while (t--) {
solve();
}
} | replace | 64 | 67 | 64 | 67 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using Edge = pair<int, ll>;
using Graph = vector<vector<Edge>>;
int N;
Graph G;
vector<int> dir;
void dfs(int v, int p, int c) {
dir[v] = c;
for (auto e : G[v]) {
if (e.first == p)
continue;
if (e.second & 1)
dfs(e.first, v, 1 - c);
else
dfs(e.first, v, c);
}
}
int main() {
while (cin >> N) {
G.assign(N, vector<Edge>());
rep(i, N) {
int u, v;
ll w;
cin >> u >> v >> w;
u--, v--;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
dir.assign(N, 0);
dfs(0, -1, 1);
for (auto v : dir)
cout << v << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using Edge = pair<int, ll>;
using Graph = vector<vector<Edge>>;
int N;
Graph G;
vector<int> dir;
void dfs(int v, int p, int c) {
dir[v] = c;
for (auto e : G[v]) {
if (e.first == p)
continue;
if (e.second & 1)
dfs(e.first, v, 1 - c);
else
dfs(e.first, v, c);
}
}
int main() {
while (cin >> N) {
G.assign(N, vector<Edge>());
rep(i, N - 1) {
int u, v;
ll w;
cin >> u >> v >> w;
u--, v--;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
dir.assign(N, 0);
dfs(0, -1, 1);
for (auto v : dir)
cout << v << endl;
}
}
| replace | 28 | 29 | 28 | 29 | 0 | |
p03044 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
const int MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<int> ans;
void dfs(const vector<vector<pair<ll, ll>>> g, int start, int color) {
ans[start] = color;
for (auto e : g[start]) {
if (ans[e.first] == -1) {
if (e.second % 2 == 0) {
dfs(g, e.first, color);
} else {
dfs(g, e.first, 1 - color);
}
}
}
}
int main() {
int N;
cin >> N;
vector<vector<pair<ll, ll>>> graph(N);
rep(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
graph[u - 1].push_back({v - 1, w});
graph[v - 1].push_back({u - 1, w});
}
ans.assign(N, -1);
dfs(graph, 0, 1);
rep(i, N) { cout << ans[i] << endl; }
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
const int MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<int> ans;
void dfs(vector<vector<pair<ll, ll>>> &g, int start, int color) {
ans[start] = color;
for (auto e : g[start]) {
if (ans[e.first] == -1) {
if (e.second % 2 == 0) {
dfs(g, e.first, color);
} else {
dfs(g, e.first, 1 - color);
}
}
}
}
int main() {
int N;
cin >> N;
vector<vector<pair<ll, ll>>> graph(N);
rep(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
graph[u - 1].push_back({v - 1, w});
graph[v - 1].push_back({u - 1, w});
}
ans.assign(N, -1);
dfs(graph, 0, 1);
rep(i, N) { cout << ans[i] << endl; }
}
| replace | 34 | 35 | 34 | 35 | TLE | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
InputIterator last) {
return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
for (auto &i : v) {
stream >> i;
}
return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
stream >> p.first >> p.second;
return stream;
}
int main() {
int64_t n;
std::cin >> n;
std::vector<bool> f(n, false);
std::vector<int64_t> l(n, 0);
std::vector<std::map<int64_t, int64_t>> no(n);
for (int64_t i = 0; i < n; i++) {
int64_t u, v, w;
std::cin >> u >> v >> w;
u--;
v--;
no[u].insert({v, w});
no[v].insert({u, w});
}
f[0] = true;
l[0] = 0;
std::set<std::pair<int64_t, int64_t>> q;
for (const auto &i : no[0]) {
if (!f[i.first]) {
q.insert({l[0] + i.second, i.first});
}
}
while (!q.empty()) {
const auto cost = q.cbegin()->first;
const auto node = q.cbegin()->second;
if (!f[node]) {
f[node] = true;
l[node] = cost;
for (const auto &i : no[node]) {
if (!f[i.first]) {
q.insert({cost + i.second, i.first});
}
}
}
q.erase(q.begin());
}
for (const auto i : l) {
if (i % 2 == 0) {
std::cout << 0 << std::endl;
} else {
std::cout << 1 << std::endl;
}
}
return 0;
} | #include <bits/stdc++.h>
template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
InputIterator last) {
return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
for (auto &i : v) {
stream >> i;
}
return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
stream >> p.first >> p.second;
return stream;
}
int main() {
int64_t n;
std::cin >> n;
std::vector<bool> f(n, false);
std::vector<int64_t> l(n, 0);
std::vector<std::map<int64_t, int64_t>> no(n);
for (int64_t i = 0; i < n - 1; i++) {
int64_t u, v, w;
std::cin >> u >> v >> w;
u--;
v--;
no[u].insert({v, w});
no[v].insert({u, w});
}
f[0] = true;
l[0] = 0;
std::set<std::pair<int64_t, int64_t>> q;
for (const auto &i : no[0]) {
if (!f[i.first]) {
q.insert({l[0] + i.second, i.first});
}
}
while (!q.empty()) {
const auto cost = q.cbegin()->first;
const auto node = q.cbegin()->second;
if (!f[node]) {
f[node] = true;
l[node] = cost;
for (const auto &i : no[node]) {
if (!f[i.first]) {
q.insert({cost + i.second, i.first});
}
}
}
q.erase(q.begin());
}
for (const auto i : l) {
if (i % 2 == 0) {
std::cout << 0 << std::endl;
} else {
std::cout << 1 << std::endl;
}
}
return 0;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p03044 | C++ | Runtime Error | #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
if (x > n) {
return x % n;
} else if (x < 0) {
return x % n + n;
} else
return x;
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return (b);
}
else {
return (gcd(b, a % b));
}
}
int main() {
ll n;
cin >> n;
vector<vp> g(n);
rep(i, 0, n) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back(make_pair(v, w % 2));
g[v].push_back(make_pair(u, w % 2));
}
vll cost(n, -1);
cost[0] = 0;
queue<ll> q;
q.push(0);
while (!q.empty()) {
ll tmp = q.front();
q.pop();
for (auto u : g[tmp]) {
if (cost[u.first] == -1) {
q.push(u.first);
cost[u.first] = cost[tmp] + u.second;
cost[u.first] %= 2;
}
}
}
rep(i, 0, n) { cout << cost[i] % 2 << endl; }
}
| #include "bits/stdc++.h"
#include "math.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
if (x > n) {
return x % n;
} else if (x < 0) {
return x % n + n;
} else
return x;
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return (b);
}
else {
return (gcd(b, a % b));
}
}
int main() {
ll n;
cin >> n;
vector<vp> g(n);
rep(i, 0, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back(make_pair(v, w % 2));
g[v].push_back(make_pair(u, w % 2));
}
vll cost(n, -1);
cost[0] = 0;
queue<ll> q;
q.push(0);
while (!q.empty()) {
ll tmp = q.front();
q.pop();
for (auto u : g[tmp]) {
if (cost[u.first] == -1) {
q.push(u.first);
cost[u.first] = cost[tmp] + u.second;
cost[u.first] %= 2;
}
}
}
rep(i, 0, n) { cout << cost[i] % 2 << endl; }
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 100005;
// 頂点番号と距離を記録したい.
/*
方針
rootから見ていく。親との距離が偶数なら同じ色、違うなら違う色を塗れば
よさそう。それで任意の組み合わせについて条件が達成されるが...?
λ関数でdfsを実装するのは理由がわからないけど無理そう。なので外部に.
*/
vector<P> ale[MAX];
int n;
vector<int> color(MAX, -1);
void dfs(int v, bool clr = 0, int p = -1) {
color[v] = clr;
for (P u : ale[v]) {
if (u.first == p)
continue;
if (u.second % 2 == 0) {
dfs(u.first, clr, v);
} else {
dfs(u.first, !clr, v);
}
}
}
int main() {
cin >> n;
rep(i, n) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
ale[u].push_back(make_pair(v, w));
ale[v].push_back(make_pair(u, w));
}
dfs(0);
rep(i, n) { cout << color[i] << endl; }
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 100005;
// 頂点番号と距離を記録したい.
/*
方針
rootから見ていく。親との距離が偶数なら同じ色、違うなら違う色を塗れば
よさそう。それで任意の組み合わせについて条件が達成されるが...?
λ関数でdfsを実装するのは理由がわからないけど無理そう。なので外部に.
*/
vector<P> ale[MAX];
int n;
vector<int> color(MAX, -1);
void dfs(int v, bool clr = 0, int p = -1) {
color[v] = clr;
for (P u : ale[v]) {
if (u.first == p)
continue;
if (u.second % 2 == 0) {
dfs(u.first, clr, v);
} else {
dfs(u.first, !clr, v);
}
}
}
int main() {
cin >> n;
rep(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
ale[u].push_back(make_pair(v, w));
ale[v].push_back(make_pair(u, w));
}
dfs(0);
rep(i, n) { cout << color[i] << endl; }
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
struct edge {
int to, w;
};
vector<edge> G[100001];
int col[10001];
void dfs(int s, int d, int prev = -1) {
for (auto e : G[s]) {
if (e.to == prev)
continue;
if ((e.w + d) % 2 == 1)
col[e.to] = 1;
else
col[e.to] = 0;
dfs(e.to, (e.w + d) % 2, s);
}
}
int main() {
int N;
cin >> N;
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
G[u].emplace_back(edge{v, w});
G[v].emplace_back(edge{u, w});
}
dfs(0, 0);
REP(i, N) { cout << col[i] << endl; }
}
/*
*/ | #include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
struct edge {
int to, w;
};
vector<edge> G[100001];
int col[100001];
void dfs(int s, int d, int prev = -1) {
for (auto e : G[s]) {
if (e.to == prev)
continue;
if ((e.w + d) % 2 == 1)
col[e.to] = 1;
else
col[e.to] = 0;
dfs(e.to, (e.w + d) % 2, s);
}
}
int main() {
int N;
cin >> N;
REP(i, N - 1) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
G[u].emplace_back(edge{v, w});
G[v].emplace_back(edge{u, w});
}
dfs(0, 0);
REP(i, N) { cout << col[i] << endl; }
}
/*
*/ | replace | 33 | 34 | 33 | 34 | 0 | |
p03044 | C++ | Runtime Error | #define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
vector<pii> g[100001];
int c[100001];
void dfs(int p, int bef, int x) {
c[p] = x;
for (pii P : g[p]) {
if (P.first != bef) {
int to = P.first;
int w = P.second;
dfs(to, p, x ^ (w % 2));
}
}
}
int main() {
int n;
cin >> n;
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
g[u - 1].push_back(pii(v, w));
g[v - 1].push_back(pii(u, w));
}
dfs(0, -1, 0);
REP(i, n) cout << c[i] << endl;
return 0;
}
| #define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
vector<pii> g[100001];
int c[100001];
void dfs(int p, int bef, int x) {
c[p] = x;
for (pii P : g[p]) {
if (P.first != bef) {
int to = P.first;
int w = P.second;
dfs(to, p, x ^ (w % 2));
}
}
}
int main() {
int n;
cin >> n;
REP(i, n - 1) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
g[u].push_back(pii(v, w));
g[v].push_back(pii(u, w));
}
dfs(0, -1, 0);
REP(i, n) cout << c[i] << endl;
return 0;
}
| replace | 46 | 48 | 46 | 50 | 0 | |
p03044 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int SIZE = 50010;
int f[SIZE][20], d[SIZE], dist[SIZE];
int ver[2 * SIZE], Next[2 * SIZE], edge[2 * SIZE], head[SIZE];
int T, n, m, tot, t;
queue<int> q;
void add(int x, int y, int z) {
ver[++tot] = y;
edge[tot] = z;
Next[tot] = head[x];
head[x] = tot;
}
void bfs() {
q.push(1);
d[1] = 1;
while (q.size()) {
int x = q.front();
q.pop();
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (d[y])
continue;
d[y] = d[x] + 1;
dist[y] = dist[x] + edge[i];
f[y][0] = x;
for (int j = 1; j <= t; j++)
f[y][j] = f[f[y][j - 1]][j - 1];
q.push(y);
}
}
}
int lca(int x, int y) {
if (d[x] > d[y])
swap(x, y);
for (int i = t; i >= 0; i--)
if (d[f[y][i]] >= d[x])
y = f[y][i];
if (x == y)
return x;
for (int i = t; i >= 0; i--)
if (f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
int main() {
cin >> n;
m = n - 1;
t = (int)(log(n) / log(2)) + 1;
for (int i = 1; i <= n; i++)
head[i] = d[i] = 0;
tot = 0;
for (int i = 1; i < n; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(x, y, z), add(y, x, z);
}
bfs();
/*for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", dist[x] + dist[y] - 2 * dist[lca(x, y)]);
}*/
for (int i = 1; i <= n; i++) {
if (dist[i] % 2 == 0)
cout << 0 << endl;
else
cout << 1 << endl;
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int SIZE = 100010;
int f[SIZE][20], d[SIZE], dist[SIZE];
int ver[2 * SIZE], Next[2 * SIZE], edge[2 * SIZE], head[SIZE];
int T, n, m, tot, t;
queue<int> q;
void add(int x, int y, int z) {
ver[++tot] = y;
edge[tot] = z;
Next[tot] = head[x];
head[x] = tot;
}
void bfs() {
q.push(1);
d[1] = 1;
while (q.size()) {
int x = q.front();
q.pop();
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (d[y])
continue;
d[y] = d[x] + 1;
dist[y] = dist[x] + edge[i];
f[y][0] = x;
for (int j = 1; j <= t; j++)
f[y][j] = f[f[y][j - 1]][j - 1];
q.push(y);
}
}
}
int lca(int x, int y) {
if (d[x] > d[y])
swap(x, y);
for (int i = t; i >= 0; i--)
if (d[f[y][i]] >= d[x])
y = f[y][i];
if (x == y)
return x;
for (int i = t; i >= 0; i--)
if (f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
int main() {
cin >> n;
m = n - 1;
t = (int)(log(n) / log(2)) + 1;
for (int i = 1; i <= n; i++)
head[i] = d[i] = 0;
tot = 0;
for (int i = 1; i < n; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(x, y, z), add(y, x, z);
}
bfs();
/*for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", dist[x] + dist[y] - 2 * dist[lca(x, y)]);
}*/
for (int i = 1; i <= n; i++) {
if (dist[i] % 2 == 0)
cout << 0 << endl;
else
cout << 1 << endl;
}
} | replace | 7 | 8 | 7 | 8 | 0 | |
p03044 | C++ | Runtime Error | #include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)
using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;
vi colour;
vvii adj;
int dfs(int node, int father, int c, int dist) {
if (dist % 2 == 0)
colour[node] = c;
for (ii i : adj[node]) {
if (i.ff != father)
dfs(i.ff, node, c, dist + i.ss);
}
}
void solve() {
int n;
cin >> n;
colour.resize(n, 0);
adj.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
adj[u].pub({v, w});
adj[v].pub({u, w});
}
dfs(0, -1, 1, 0);
for (int i = 0; i < n; i++)
cout << colour[i] << endl;
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
} | #include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)
using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;
vi colour;
vvii adj;
void dfs(int node, int father, int c, int dist) {
if (dist % 2 == 0)
colour[node] = c;
for (ii i : adj[node]) {
if (i.ff != father)
dfs(i.ff, node, c, dist + i.ss);
}
}
void solve() {
int n;
cin >> n;
colour.resize(n, 0);
adj.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
adj[u].pub({v, w});
adj[v].pub({u, w});
}
dfs(0, -1, 1, 0);
for (int i = 0; i < n; i++)
cout << colour[i] << endl;
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p03044 | C++ | Time Limit Exceeded | //
// Created by kuroneko on 2019-07-06.
//
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
int d[100000];
int N;
struct edge {
int to;
int cost;
};
void dfs(int node, vector<vector<edge>> v, int p_color) {
d[node] = p_color;
for (int i = 0; i < v[node].size(); i++) {
int next_node = v[node][i].to;
if (d[next_node] == -1) {
int color = p_color;
if (v[node][i].cost % 2 == 1)
color = 1 - color;
dfs(next_node, v, color);
}
}
}
int main() {
cin >> N;
vector<vector<edge>> tree(N);
for (int i = 0; i < N; i++)
d[i] = -1;
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
edge e = {v - 1, w};
edge re = {u - 1, w};
tree[u - 1].emplace_back(e);
tree[v - 1].emplace_back(re);
}
dfs(0, tree, 0);
for (int i = 0; i < N; i++) {
cout << d[i] << endl;
}
return 0;
} | //
// Created by kuroneko on 2019-07-06.
//
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
int d[100000];
int N;
struct edge {
int to;
int cost;
};
void dfs(int node, const vector<vector<edge>> &v, int p_color) {
d[node] = p_color;
for (int i = 0; i < v[node].size(); i++) {
int next_node = v[node][i].to;
if (d[next_node] == -1) {
int color = p_color;
if (v[node][i].cost % 2 == 1)
color = 1 - color;
dfs(next_node, v, color);
}
}
}
int main() {
cin >> N;
vector<vector<edge>> tree(N);
for (int i = 0; i < N; i++)
d[i] = -1;
for (int i = 0; i < N - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
edge e = {v - 1, w};
edge re = {u - 1, w};
tree[u - 1].emplace_back(e);
tree[v - 1].emplace_back(re);
}
dfs(0, tree, 0);
for (int i = 0; i < N; i++) {
cout << d[i] << endl;
}
return 0;
} | replace | 21 | 22 | 21 | 22 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.