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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 4000
#define offset 8010
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ii> iii;
typedef pair<map<ll, ll>, ll> mi;
typedef pair<ii, ii> iiii;
typedef vector<ll> vi;
const ll MOD = 1e9 + 7;
vector<int> grafo[tam];
int sum[tam], ans[tam];
void dfs(int num, int pa, int tot) {
tot += sum[num];
ans[num] = tot;
for (int v : grafo[num]) {
if (v == pa)
continue;
dfs(v, num, tot);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int iz, der;
forr(i, 0, n - 1) {
cin >> iz >> der;
grafo[iz].pb(der);
grafo[der].pb(iz);
}
int valor;
forr(i, 0, m) {
cin >> iz >> valor;
sum[iz] += valor;
}
dfs(1, 1, 0);
forr(i, 1, n + 1) cout << ans[i] << ' ';
}
| #include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 410000
#define offset 8010
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ii> iii;
typedef pair<map<ll, ll>, ll> mi;
typedef pair<ii, ii> iiii;
typedef vector<ll> vi;
const ll MOD = 1e9 + 7;
vector<int> grafo[tam];
int sum[tam], ans[tam];
void dfs(int num, int pa, int tot) {
tot += sum[num];
ans[num] = tot;
for (int v : grafo[num]) {
if (v == pa)
continue;
dfs(v, num, tot);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int iz, der;
forr(i, 0, n - 1) {
cin >> iz >> der;
grafo[iz].pb(der);
grafo[der].pb(iz);
}
int valor;
forr(i, 0, m) {
cin >> iz >> valor;
sum[iz] += valor;
}
dfs(1, 1, 0);
forr(i, 1, n + 1) cout << ans[i] << ' ';
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v) \
{ \
for (auto x : v) \
cout << x << ' '; \
cout << endl; \
}
#define printn(v, n) \
{ \
for (int _i = 0; _i < n; _i++) \
cout << *(v + _i) << ' '; \
cout << endl; \
}
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
const int MAXN = 1e5 + 20;
int n, q;
int a[MAXN], b[MAXN], t[MAXN], r[MAXN], p[MAXN], x[MAXN], csum[MAXN] = {};
unordered_set<int> graph[MAXN];
int vis[MAXN] = {};
int tt;
void dfs(int i) {
t[i] = tt;
vis[i] = 1;
for (auto x : graph[i]) {
if (!vis[x]) {
tt++;
dfs(x);
}
}
r[t[i]] = tt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
graph[a[i]].insert(b[i]);
graph[b[i]].insert(a[i]);
}
for (int i = 0; i < q; i++)
cin >> p[i] >> x[i];
tt = 1;
dfs(1);
for (int i = 0; i < q; i++) {
csum[t[p[i]]] += x[i];
csum[r[t[p[i]]] + 1] -= x[i];
}
for (int i = 1; i <= n; i++) {
csum[i] += csum[i - 1];
}
for (int i = 1; i <= n; i++) {
cout << csum[t[i]] << endl;
}
return 0;
}
| #include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v) \
{ \
for (auto x : v) \
cout << x << ' '; \
cout << endl; \
}
#define printn(v, n) \
{ \
for (int _i = 0; _i < n; _i++) \
cout << *(v + _i) << ' '; \
cout << endl; \
}
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
const int MAXN = 2e5 + 20;
int n, q;
int a[MAXN], b[MAXN], t[MAXN], r[MAXN], p[MAXN], x[MAXN], csum[MAXN] = {};
unordered_set<int> graph[MAXN];
int vis[MAXN] = {};
int tt;
void dfs(int i) {
t[i] = tt;
vis[i] = 1;
for (auto x : graph[i]) {
if (!vis[x]) {
tt++;
dfs(x);
}
}
r[t[i]] = tt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
graph[a[i]].insert(b[i]);
graph[b[i]].insert(a[i]);
}
for (int i = 0; i < q; i++)
cin >> p[i] >> x[i];
tt = 1;
dfs(1);
for (int i = 0; i < q; i++) {
csum[t[p[i]]] += x[i];
csum[r[t[p[i]]] + 1] -= x[i];
}
for (int i = 1; i <= n; i++) {
csum[i] += csum[i - 1];
}
for (int i = 1; i <= n; i++) {
cout << csum[t[i]] << endl;
}
return 0;
}
| replace | 50 | 51 | 50 | 51 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (long long i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long i = (a); i >= (b); i--)
#define MOD 1000000007
#define LLONG_MAXs 9223372036854775800
#define ALL(a) (a).begin(), (a).end()
#include <cmath>
#include <iostream>
using namespace std;
bool isPrimeNum(ll x) { // 素数である場合 true を返す
if (x <= 1) { // 1以下である場合は素数でないことがすぐにわかる
return false;
}
// sqrt( double型 ) は引数の平方根を double型で返すので、int型でキャスト
int n = (int)sqrt((double)x);
for (int i = 2; i <= n; i++) {
if (x % i == 0) { // 割り切る整数がある場合、即判定終了
return false;
}
}
return true; // 割り切る整数がない場合、素数である
}
ll myPow(ll x, ll n, ll m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
constexpr ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
constexpr ll abs(ll a, ll b) {
if (a >= b)
return a - b;
if (a < b)
return b - a;
}
constexpr double dabs(double a, double b) {
if (a >= b)
return a - b;
if (a < b)
return b - a;
}
constexpr ll min(ll a, ll b) {
if (a >= b)
return b;
if (a < b)
return a;
}
constexpr ll max(ll a, ll b) {
if (a >= b)
return a;
if (a < b)
return b;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dx8[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy8[8] = {0, 1, 0, -1, 1, 1, -1, -1};
class UnionFind {
public:
// 親の番号を格納する。親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 作るときはParentの値を全て-1にする
// こうすると全てバラバラになる
UnionFind(int N) { Parent = vector<int>(N, -1); }
// Aがどのグループに属しているか調べる
int root(int A) {
if (Parent[A] < 0)
return A;
return Parent[A] = root(Parent[A]);
}
// 自分のいるグループの頂点数を調べる
int size(int A) {
return -Parent[root(A)]; // 親をとってきたい]
}
bool issame(int x, int y) { return root(x) == root(y); }
// AとBをくっ付ける
bool connect(int A, int B) {
// AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついてるからくっ付けない
return false;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
long long fac[510000], finv[510000], inv[510000];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < 510000; 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 modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
string replaceAll(string &replacedStr, string from, string to) {
unsigned int pos = replacedStr.find(from);
int toLen = to.length();
if (from.empty()) {
return replacedStr;
}
while ((pos = replacedStr.find(from, pos)) != std::string::npos) {
replacedStr.replace(pos, from.length(), to);
pos += toLen;
}
return replacedStr;
}
void yn(bool flag) {
if (flag) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return;
}
void YN(bool flag) {
if (flag) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return;
}
std::vector<ll> enum_div(ll n) // nの約数を列挙
{
std::vector<ll> ret;
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
ret.push_back(n);
return ret;
}
void Ssort(int no, char *month[]) {
int i, j;
char *temp;
for (i = 0; i < no; i++) {
for (j = i + 1; j < no; j++) {
if (strcmp((month[i]), (month[j])) > 0) {
temp = *(month + i);
*(month + i) = *(month + j);
*(month + j) = temp;
}
}
}
}
const double PI = 3.14159265358979323846;
struct Edge {
int to; // 辺の行き先
int weight; // 辺の重み
Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
// <最短距離, 頂点の番号>
using P = pair<ll, ll>;
ll ans[20000] = {};
ll flg[20000] = {};
vector<bool> visited;
Graph G(20000);
void dfs(int now_v, const Graph &G) {
visited[now_v] = true;
// cout << G[now_v][0].to << endl;
for (auto next : G[now_v]) {
if (visited[next.to])
continue;
// flg[next.to]++;
ans[next.to] += ans[now_v];
dfs(next.to, G);
}
}
int main(void) {
ll N, Q;
cin >> N >> Q;
// グラフ
Graph G(N);
fill_n(visited.begin(), N, false);
for (int i = 0; i < N - 1; ++i) {
int from, to, weight;
cin >> from >> to;
from--;
to--;
G[from].push_back(Edge(to, 1));
G[to].push_back(Edge(from, 1));
}
for (int i = 0; i < Q; ++i) {
int no, num;
cin >> no >> num;
no--;
ans[no] += num;
}
// cout << G[0][0].to << endl;
dfs(0, G);
FOR(i, 0, N - 1) { cout << ans[i] << " "; }
cout << endl;
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (long long i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long i = (a); i >= (b); i--)
#define MOD 1000000007
#define LLONG_MAXs 9223372036854775800
#define ALL(a) (a).begin(), (a).end()
#include <cmath>
#include <iostream>
using namespace std;
bool isPrimeNum(ll x) { // 素数である場合 true を返す
if (x <= 1) { // 1以下である場合は素数でないことがすぐにわかる
return false;
}
// sqrt( double型 ) は引数の平方根を double型で返すので、int型でキャスト
int n = (int)sqrt((double)x);
for (int i = 2; i <= n; i++) {
if (x % i == 0) { // 割り切る整数がある場合、即判定終了
return false;
}
}
return true; // 割り切る整数がない場合、素数である
}
ll myPow(ll x, ll n, ll m) {
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
constexpr ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
constexpr ll abs(ll a, ll b) {
if (a >= b)
return a - b;
if (a < b)
return b - a;
}
constexpr double dabs(double a, double b) {
if (a >= b)
return a - b;
if (a < b)
return b - a;
}
constexpr ll min(ll a, ll b) {
if (a >= b)
return b;
if (a < b)
return a;
}
constexpr ll max(ll a, ll b) {
if (a >= b)
return a;
if (a < b)
return b;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dx8[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy8[8] = {0, 1, 0, -1, 1, 1, -1, -1};
class UnionFind {
public:
// 親の番号を格納する。親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 作るときはParentの値を全て-1にする
// こうすると全てバラバラになる
UnionFind(int N) { Parent = vector<int>(N, -1); }
// Aがどのグループに属しているか調べる
int root(int A) {
if (Parent[A] < 0)
return A;
return Parent[A] = root(Parent[A]);
}
// 自分のいるグループの頂点数を調べる
int size(int A) {
return -Parent[root(A)]; // 親をとってきたい]
}
bool issame(int x, int y) { return root(x) == root(y); }
// AとBをくっ付ける
bool connect(int A, int B) {
// AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついてるからくっ付けない
return false;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
long long fac[510000], finv[510000], inv[510000];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < 510000; 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 modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
string replaceAll(string &replacedStr, string from, string to) {
unsigned int pos = replacedStr.find(from);
int toLen = to.length();
if (from.empty()) {
return replacedStr;
}
while ((pos = replacedStr.find(from, pos)) != std::string::npos) {
replacedStr.replace(pos, from.length(), to);
pos += toLen;
}
return replacedStr;
}
void yn(bool flag) {
if (flag) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return;
}
void YN(bool flag) {
if (flag) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return;
}
std::vector<ll> enum_div(ll n) // nの約数を列挙
{
std::vector<ll> ret;
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
ret.push_back(n);
return ret;
}
void Ssort(int no, char *month[]) {
int i, j;
char *temp;
for (i = 0; i < no; i++) {
for (j = i + 1; j < no; j++) {
if (strcmp((month[i]), (month[j])) > 0) {
temp = *(month + i);
*(month + i) = *(month + j);
*(month + j) = temp;
}
}
}
}
const double PI = 3.14159265358979323846;
struct Edge {
int to; // 辺の行き先
int weight; // 辺の重み
Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
// <最短距離, 頂点の番号>
using P = pair<ll, ll>;
ll ans[20000] = {};
ll flg[20000] = {};
vector<bool> visited;
Graph G(20000);
void dfs(int now_v, const Graph &G) {
visited[now_v] = true;
// cout << G[now_v][0].to << endl;
for (auto next : G[now_v]) {
if (visited[next.to])
continue;
// flg[next.to]++;
ans[next.to] += ans[now_v];
dfs(next.to, G);
}
}
int main(void) {
ll N, Q;
cin >> N >> Q;
// グラフ
Graph G(N);
visited.resize(N);
fill_n(visited.begin(), N, false);
for (int i = 0; i < N - 1; ++i) {
int from, to, weight;
cin >> from >> to;
from--;
to--;
G[from].push_back(Edge(to, 1));
G[to].push_back(Edge(from, 1));
}
for (int i = 0; i < Q; ++i) {
int no, num;
cin >> no >> num;
no--;
ans[no] += num;
}
// cout << G[0][0].to << endl;
dfs(0, G);
FOR(i, 0, N - 1) { cout << ans[i] << " "; }
cout << endl;
return 0;
}
| insert | 269 | 269 | 269 | 270 | -11 | |
p02936 | Python | Time Limit Exceeded | import sys
sys.setrecursionlimit(10000000)
N, Q = [int(x) for x in input().split()]
# V = [set() for _ in range(N)]
V = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = [int(x) for x in input().split()]
a -= 1
b -= 1
# V[a].add(b)
V[a].append(b)
C = [0] * N
for _ in range(Q):
p, x = [int(x) for x in input().split()]
p -= 1
C[p] += x
ANS = [0] * N
def dfs(n, val):
ANS[n] += val + C[n]
for next in V[n]:
dfs(next, val + C[n])
def main():
dfs(0, 0)
print(" ".join([str(x) for x in ANS]))
if __name__ == "__main__":
main()
| import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
N, Q = [int(x) for x in input().split()]
# V = [set() for _ in range(N)]
V = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = [int(x) for x in input().split()]
a -= 1
b -= 1
# V[a].add(b)
V[a].append(b)
C = [0] * N
for _ in range(Q):
p, x = [int(x) for x in input().split()]
p -= 1
C[p] += x
ANS = [0] * N
def dfs(n, val):
ANS[n] += val + C[n]
for next in V[n]:
dfs(next, val + C[n])
def main():
dfs(0, 0)
print(" ".join([str(x) for x in ANS]))
if __name__ == "__main__":
main()
| insert | 1 | 1 | 1 | 3 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
--a, --b;
to[a].push_back(b);
to[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
--a, --b;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
} | insert | 26 | 26 | 26 | 27 | -11 | |
p02936 | Python | Runtime Error | def solve(N, Q, ABs, PXs):
values = {i: 0 for i in range(Q + 1)}
ans = [-1 for i in range(N + 1)]
vertexes = {}
for a, b in ABs:
vertexes.setdefault(a, []).append(b)
vertexes.setdefault(b, []).append(a)
for p, x in PXs:
values[p] += x
queue = []
queue.append((1, 0))
while queue:
index, parent_value = queue.pop()
value = parent_value + values[index]
ans[index] = value
for child in vertexes[index]:
if ans[child] == -1:
queue.append((child, value))
return " ".join([str(a) for a in ans[1:]])
if __name__ == "__main__":
N, Q = tuple(map(int, input().split(" ")))
ABs = [tuple(map(int, input().split(" "))) for _ in range(N - 1)]
PXs = [tuple(map(int, input().split(" "))) for _ in range(Q)]
print(solve(N, Q, ABs, PXs))
| def solve(N, Q, ABs, PXs):
values = {i: 0 for i in range(N + 1)}
ans = [-1 for i in range(N + 1)]
vertexes = {}
for a, b in ABs:
vertexes.setdefault(a, []).append(b)
vertexes.setdefault(b, []).append(a)
for p, x in PXs:
values[p] += x
queue = []
queue.append((1, 0))
while queue:
index, parent_value = queue.pop()
value = parent_value + values[index]
ans[index] = value
for child in vertexes[index]:
if ans[child] == -1:
queue.append((child, value))
return " ".join([str(a) for a in ans[1:]])
if __name__ == "__main__":
N, Q = tuple(map(int, input().split(" ")))
ABs = [tuple(map(int, input().split(" "))) for _ in range(N - 1)]
PXs = [tuple(map(int, input().split(" "))) for _ in range(Q)]
print(solve(N, Q, ABs, PXs))
| replace | 1 | 2 | 1 | 2 | KeyError: 4 | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02936/Python/s276659083.py", line 29, in <module>
print(solve(N, Q, ABs, PXs))
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02936/Python/s276659083.py", line 17, in solve
value = parent_value + values[index]
KeyError: 4
|
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
typedef pair<int, int> P;
int main(void) {
int n, q, i, a, b, v[100001], x, p1, x1;
P pp[100001];
scanf("%d %d", &n, &q);
for (i = 0; i < n - 1; i++)
scanf("%d %d", &pp[i].first, &pp[i].second);
sort(pp, pp + n - 1);
for (i = 1; i <= n; i++)
v[i] = 0;
for (i = 0; i < q; i++) {
scanf("%d %d", &p1, &x1);
v[p1] += x1;
}
for (i = 0; i < n - 1; i++) {
a = pp[i].first;
b = pp[i].second;
x = v[a];
v[b] += x;
}
printf("%d", v[1]);
for (i = 2; i <= n; i++)
printf(" %d", v[i]);
printf("\n");
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
typedef pair<int, int> P;
int main(void) {
int n, q, i, a, b, v[200001], x, p1, x1;
P pp[200001];
scanf("%d %d", &n, &q);
for (i = 0; i < n - 1; i++)
scanf("%d %d", &pp[i].first, &pp[i].second);
sort(pp, pp + n - 1);
for (i = 1; i <= n; i++)
v[i] = 0;
for (i = 0; i < q; i++) {
scanf("%d %d", &p1, &x1);
v[p1] += x1;
}
for (i = 0; i < n - 1; i++) {
a = pp[i].first;
b = pp[i].second;
x = v[a];
v[b] += x;
}
printf("%d", v[1]);
for (i = 2; i <= n; i++)
printf(" %d", v[i]);
printf("\n");
return 0;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, q, tot;
int head[200005], fa[200005], ans[200005];
struct node {
int to, net;
} e[200005];
void add(int x, int y) {
e[++tot].to = y;
e[tot].net = head[x];
head[x] = tot;
}
int a[200005];
void dfs(int x, int last) {
for (int i = head[x]; i; i = e[i].net) {
int to = e[i].to;
if (to == last)
continue;
fa[to] = x;
ans[to] += ans[x];
dfs(to, x);
}
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for (int i = 1; i <= q; i++) {
int x, v;
scanf("%d%d", &x, &v);
ans[x] += v;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, q, tot;
int head[200005], fa[200005], ans[200005];
struct node {
int to, net;
} e[400005];
void add(int x, int y) {
e[++tot].to = y;
e[tot].net = head[x];
head[x] = tot;
}
int a[200005];
void dfs(int x, int last) {
for (int i = head[x]; i; i = e[i].net) {
int to = e[i].to;
if (to == last)
continue;
fa[to] = x;
ans[to] += ans[x];
dfs(to, x);
}
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for (int i = 1; i <= q; i++) {
int x, v;
scanf("%d%d", &x, &v);
ans[x] += v;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <typename tn> void read(tn &a) {
tn x = 0, f = 1;
char c = ' ';
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + c - '0';
a = x * f;
}
template <typename tn> void print(tn a) {
if (a < 0)
putchar('-'), a = -a;
if (a > 9)
print(a / 10);
putchar(a % 10 + '0');
}
const int NR = 1e5 + 10;
struct Edge {
int next, to;
} edge[NR * 2];
struct segment_tree {
int l, r, sum, add;
} t[NR * 4];
int w[NR], fa[NR], hson[NR], dep[NR], siz[NR], top[NR], num;
int xu[NR], xu2[NR], head[NR], cnt;
void add_edge(int x, int y) {
edge[++cnt].next = head[x];
edge[cnt].to = y;
head[x] = cnt;
}
void dfs1(int x, int father, int depth) {
dep[x] = depth;
fa[x] = father;
siz[x] = 1;
int max_x = 0;
for (int i = head[x]; i; i = edge[i].next) {
if (edge[i].to == father)
continue;
dfs1(edge[i].to, x, depth + 1);
siz[x] += siz[edge[i].to];
if (siz[edge[i].to] > max_x)
max_x = siz[edge[i].to], hson[x] = edge[i].to;
}
}
void dfs2(int x, int topp) {
xu[x] = ++num;
xu2[num] = w[x];
top[x] = topp;
if (!hson[x])
return;
dfs2(hson[x], topp);
for (int i = head[x]; i; i = edge[i].next)
if (edge[i].to != fa[x] && edge[i].to != hson[x])
dfs2(edge[i].to, edge[i].to);
}
void build(int p, int l, int r) {
// printf("%d %d\n",l,r);
t[p].l = l;
t[p].r = r;
if (l == r) {
t[p].sum = xu2[l];
return;
}
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
t[p].sum = 1ll * (t[p * 2].sum + t[p * 2 + 1].sum);
}
void spread(int p) {
if (t[p].add) {
t[p * 2].sum += 1ll * t[p].add * (t[p * 2].r - t[p * 2].l + 1);
t[p * 2 + 1].sum += 1ll * t[p].add * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
t[p * 2].add += t[p].add;
t[p * 2 + 1].add += t[p].add;
t[p].add = 0;
}
}
void change(int p, int l, int r, int d) {
if (l <= t[p].l && r >= t[p].r) {
t[p].sum += 1ll * d * (t[p].r - t[p].l + 1);
t[p].add += d;
return;
}
spread(p);
int mid = t[p].l + t[p].r >> 1;
if (l <= mid)
change(p * 2, l, r, d);
if (r > mid)
change(p * 2 + 1, l, r, d);
t[p].sum = (t[p * 2].sum + t[p * 2 + 1].sum);
}
int ask(int p, int l, int r) {
if (l <= t[p].l && r >= t[p].r)
return t[p].sum;
spread(p);
int mid = t[p].l + t[p].r >> 1;
int val = 0;
if (l <= mid)
val += ask(p * 2, l, r), val;
if (r > mid)
val += ask(p * 2 + 1, l, r), val;
return val;
}
void ask1(int x, int y, int z) {
z;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
change(1, xu[top[x]], xu[x], z);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
change(1, xu[x], xu[y], z);
}
int ask2(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
ans = ans + ask(1, xu[top[x]], xu[x]);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
ans = (ans + ask(1, xu[x], xu[y]));
return ans;
}
void ask3(int x, int z) { change(1, xu[x], xu[x] + siz[x] - 1, z); }
int ask4(int x) { return ask(1, xu[x], xu[x] + siz[x] - 1); }
int n, x, y, q, s1, s2;
int main() {
read(n);
read(q);
for (int i = 1; i < n; i++) {
read(x);
read(y);
add_edge(x, y);
add_edge(y, x);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
for (int i = 1; i <= q; i++) {
read(s1);
read(s2);
ask3(s1, s2);
}
for (int i = 1; i <= n; i++)
print(ask(1, xu[i], xu[i])), putchar(' ');
puts("");
/*
scanf("%d%d%d%d",&n,&m,&r,&MOD);
for(int i=1;i<=n;i++) scanf("%d",&w[i]);
int x,y;
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
dfs1(r,0,1);
dfs2(r,r);
build(1,1,n);
int s1,z;
while(m--){
scanf("%d",&s1);
if(s1==1) scanf("%d%d%d",&x,&y,&z),ask1(x,y,z);
if(s1==2) scanf("%d%d",&x,&y),printf("%d\n",ask2(x,y));
if(s1==3) scanf("%d%d",&x,&z),ask3(x,z);
if(s1==4) scanf("%d",&x),printf("%d\n",ask4(x));
}
*/
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename tn> void read(tn &a) {
tn x = 0, f = 1;
char c = ' ';
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + c - '0';
a = x * f;
}
template <typename tn> void print(tn a) {
if (a < 0)
putchar('-'), a = -a;
if (a > 9)
print(a / 10);
putchar(a % 10 + '0');
}
const int NR = 2e5 + 10;
struct Edge {
int next, to;
} edge[NR * 2];
struct segment_tree {
int l, r, sum, add;
} t[NR * 4];
int w[NR], fa[NR], hson[NR], dep[NR], siz[NR], top[NR], num;
int xu[NR], xu2[NR], head[NR], cnt;
void add_edge(int x, int y) {
edge[++cnt].next = head[x];
edge[cnt].to = y;
head[x] = cnt;
}
void dfs1(int x, int father, int depth) {
dep[x] = depth;
fa[x] = father;
siz[x] = 1;
int max_x = 0;
for (int i = head[x]; i; i = edge[i].next) {
if (edge[i].to == father)
continue;
dfs1(edge[i].to, x, depth + 1);
siz[x] += siz[edge[i].to];
if (siz[edge[i].to] > max_x)
max_x = siz[edge[i].to], hson[x] = edge[i].to;
}
}
void dfs2(int x, int topp) {
xu[x] = ++num;
xu2[num] = w[x];
top[x] = topp;
if (!hson[x])
return;
dfs2(hson[x], topp);
for (int i = head[x]; i; i = edge[i].next)
if (edge[i].to != fa[x] && edge[i].to != hson[x])
dfs2(edge[i].to, edge[i].to);
}
void build(int p, int l, int r) {
// printf("%d %d\n",l,r);
t[p].l = l;
t[p].r = r;
if (l == r) {
t[p].sum = xu2[l];
return;
}
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
t[p].sum = 1ll * (t[p * 2].sum + t[p * 2 + 1].sum);
}
void spread(int p) {
if (t[p].add) {
t[p * 2].sum += 1ll * t[p].add * (t[p * 2].r - t[p * 2].l + 1);
t[p * 2 + 1].sum += 1ll * t[p].add * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
t[p * 2].add += t[p].add;
t[p * 2 + 1].add += t[p].add;
t[p].add = 0;
}
}
void change(int p, int l, int r, int d) {
if (l <= t[p].l && r >= t[p].r) {
t[p].sum += 1ll * d * (t[p].r - t[p].l + 1);
t[p].add += d;
return;
}
spread(p);
int mid = t[p].l + t[p].r >> 1;
if (l <= mid)
change(p * 2, l, r, d);
if (r > mid)
change(p * 2 + 1, l, r, d);
t[p].sum = (t[p * 2].sum + t[p * 2 + 1].sum);
}
int ask(int p, int l, int r) {
if (l <= t[p].l && r >= t[p].r)
return t[p].sum;
spread(p);
int mid = t[p].l + t[p].r >> 1;
int val = 0;
if (l <= mid)
val += ask(p * 2, l, r), val;
if (r > mid)
val += ask(p * 2 + 1, l, r), val;
return val;
}
void ask1(int x, int y, int z) {
z;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
change(1, xu[top[x]], xu[x], z);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
change(1, xu[x], xu[y], z);
}
int ask2(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
ans = ans + ask(1, xu[top[x]], xu[x]);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
ans = (ans + ask(1, xu[x], xu[y]));
return ans;
}
void ask3(int x, int z) { change(1, xu[x], xu[x] + siz[x] - 1, z); }
int ask4(int x) { return ask(1, xu[x], xu[x] + siz[x] - 1); }
int n, x, y, q, s1, s2;
int main() {
read(n);
read(q);
for (int i = 1; i < n; i++) {
read(x);
read(y);
add_edge(x, y);
add_edge(y, x);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
for (int i = 1; i <= q; i++) {
read(s1);
read(s2);
ask3(s1, s2);
}
for (int i = 1; i <= n; i++)
print(ask(1, xu[i], xu[i])), putchar(' ');
puts("");
/*
scanf("%d%d%d%d",&n,&m,&r,&MOD);
for(int i=1;i<=n;i++) scanf("%d",&w[i]);
int x,y;
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
dfs1(r,0,1);
dfs2(r,r);
build(1,1,n);
int s1,z;
while(m--){
scanf("%d",&s1);
if(s1==1) scanf("%d%d%d",&x,&y,&z),ask1(x,y,z);
if(s1==2) scanf("%d%d",&x,&y),printf("%d\n",ask2(x,y));
if(s1==3) scanf("%d%d",&x,&z),ask3(x,z);
if(s1==4) scanf("%d",&x),printf("%d\n",ask4(x));
}
*/
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02936 | C++ | Runtime Error |
#include <bits/stdc++.h>
class kin {
public:
inline void open(FILE *, int);
inline void close(void);
inline void scan(void);
inline kin &operator>(char &);
inline kin &operator>(int &);
inline kin &operator>(long long &);
inline kin &operator>(double &);
inline kin &operator>(long double &);
inline kin &operator>(char *);
template <class T> inline void get(T *, int);
private:
FILE *fp;
char *buf;
int siz;
int idx;
} in;
class kout {
public:
inline void open(FILE *, int);
inline void close(void);
inline void print(void);
inline kout &operator<(char);
inline kout &operator<(int);
inline kout &operator<(long long);
inline kout &operator<(double);
inline kout &operator<(long double);
inline kout &operator<(const char *);
template <class T> inline void put(T *, int, char, char);
private:
FILE *fp;
char *buf;
int siz;
int idx;
} out;
template <class E, class V> class graph {
public:
inline void open(int, int);
inline void close(void);
inline void add(E);
inline V &operator[](int);
inline graph();
inline graph(int, int);
private:
int size;
int sizv;
E *date;
V *datv;
};
struct dse;
struct dsv;
struct dse {
int start;
int end;
dse *next;
};
struct dsv {
int score;
dse *first;
};
graph<dse, dsv> g;
inline void dfs(dse *x) {
g[x->end].score += g[x->start].score;
for (dse *i = g[x->end].first; i; i = i->next) {
if (i->end != x->start)
dfs(i);
}
}
int main(int argc, char **argv) {
in.open(stdin, 512);
out.open(stdout, 512);
in.scan();
int n;
in > n;
int m = n - 1;
g.open(n + 1, m * 2);
for (int i = 0; i <= n; ++i)
g[i].score = 0;
int q;
in > q;
for (int i = 0; i < m; ++i) {
int a, b;
in > a > b;
--a;
--b;
g.add((dse){a, b, nullptr});
g.add((dse){b, a, nullptr});
}
for (int i = 0; i < q; ++i) {
int p, x;
in > p > x;
g[p - 1].score += x;
}
dse dum = {n, 0, nullptr};
dfs(&dum);
for (int i = 0; i < m; ++i)
out < g[i].score < ' ';
out < g[m].score < '\n';
g.close();
out.print();
in.close();
out.close();
return 0;
}
template <class E, class V>
inline void graph<E, V>::open(int sizea, int sizva) {
size = 0;
sizv = sizva;
date = new E[sizea];
datv = new V[sizva];
for (int i = 0; i < sizva; ++i)
datv[i].first = nullptr;
return;
}
template <class E, class V> inline void graph<E, V>::close(void) {
sizv = 0;
size = 0;
delete[] date;
date = nullptr;
delete[] datv;
datv = nullptr;
return;
}
template <class E, class V> inline void graph<E, V>::add(E val) {
date[size] = val;
date[size].next = datv[val.start].first;
datv[val.start].first = &date[size++];
return;
}
template <class E, class V> inline V &graph<E, V>::operator[](int num) {
return datv[num];
}
template <class E, class V> inline graph<E, V>::graph() {
size = sizv = 0;
date = nullptr;
datv = nullptr;
}
template <class E, class V> inline graph<E, V>::graph(int sizea, int sizva) {
open(sizea, sizva);
}
inline void kin::open(FILE *fpa, int siza) {
fp = fpa;
buf = new char[siza];
siz = siza;
idx = 0;
return;
}
inline void kin::close(void) {
fp = nullptr;
delete[] buf;
buf = nullptr;
siz = 0;
idx = 0;
return;
}
inline void kin::scan(void) {
int readsiz =
(int)std::fread((void *)buf, (std::size_t)1, (std::size_t)siz, fp);
if (readsiz != siz)
buf[readsiz] = '\x00';
idx = 0;
return;
}
inline kin &kin::operator>(char &var) {
if (!buf[idx]) {
var = '\x00';
return *this;
}
var = buf[idx];
if (++idx == siz)
scan();
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(int &var) {
if (!buf[idx]) {
var = 0;
return *this;
}
int sign = -1;
if (buf[idx] == '-') {
sign = 1;
if (++idx == siz)
scan();
}
var = 0;
while (buf[idx] >= '0') {
var = var * 10 - (int)(buf[idx] - '0');
if (++idx == siz)
scan();
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(long long &var) {
if (!buf[idx]) {
var = 0LL;
return *this;
}
long long sign = -1LL;
if (buf[idx] == '-') {
sign = 1LL;
if (++idx == siz)
scan();
}
var = 0LL;
while (buf[idx] >= '0') {
var = var * 10LL - (long long)(buf[idx] - '0');
if (++idx == siz)
scan();
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(double &var) {
if (!buf[idx]) {
var = 0.0;
return *this;
}
double sign = -1.0;
if (buf[idx] == '-') {
sign = 1.0;
if (++idx == siz)
scan();
}
var = 0.0;
while (buf[idx] >= '0') {
var = var * 10.0 - (double)(buf[idx] - '0');
if (++idx == siz)
scan();
}
if (buf[idx] == '.') {
if (++idx == siz)
scan();
double dig = 1.0;
while (buf[idx] >= '0') {
var -= (double)(buf[idx] - '0') * (dig /= 10.0);
if (++idx == siz)
scan();
}
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(long double &var) {
if (!buf[idx]) {
var = 0.0L;
return *this;
}
long double sign = -1.0L;
if (buf[idx] == '-') {
sign = 1.0L;
if (++idx == siz)
scan();
}
var = 0.0L;
while (buf[idx] >= '0') {
var = var * 10.0L - (long double)(buf[idx] - '0');
if (++idx == siz)
scan();
}
if (buf[idx] == '.') {
if (++idx == siz)
scan();
long double dig = 1.0L;
while (buf[idx] >= '0') {
var -= (long double)(buf[idx] - '0') * (dig /= 10.0L);
if (++idx == siz)
scan();
}
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(char *var) {
if (!buf[idx]) {
var[0] = '\x00';
return *this;
}
int ptr = 0;
while (buf[idx] >= '!') {
var[ptr++] = buf[idx];
if (++idx == siz)
scan();
}
var[ptr] = '\x00';
if (++idx == siz)
scan();
return *this;
}
template <class T> inline void kin::get(T *var, int num) {
for (int i = 0; i < num; ++i)
(*this) > var[i];
return;
}
inline void kout::open(FILE *fpa, int siza) {
fp = fpa;
buf = new char[siza];
siz = siza;
idx = 0;
return;
}
inline void kout::close(void) {
fp = nullptr;
delete[] buf;
buf = nullptr;
siz = 0;
idx = 0;
return;
}
inline void kout::print(void) {
std::fwrite((void *)buf, (std::size_t)1, (std::size_t)idx, fp);
idx = 0;
return;
}
inline kout &kout::operator<(char val) {
buf[idx] = val;
if (++idx == siz)
print();
return *this;
}
inline kout &kout::operator<(int val) {
if (val < 0) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1;
char dig[10];
int ptr = 0;
do {
int tmp = val / 10;
dig[ptr++] = (char)-(val - tmp * 10) + '0';
val = tmp;
} while (val);
while (ptr--) {
buf[idx] = dig[ptr];
if (++idx == siz)
print();
}
return *this;
}
inline kout &kout::operator<(long long val) {
if (val < 0LL) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1LL;
char dig[19];
int ptr = 0;
do {
long long tmp = val / 10LL;
dig[ptr++] = (char)-(val - tmp * 10LL) + '0';
val = tmp;
} while (val);
while (ptr--) {
buf[idx] = dig[ptr];
if (++idx == siz)
print();
}
return *this;
}
inline kout &kout::operator<(double val) {
if (val < 0.0) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1.0;
double dig = 1.0;
while (val / dig <= -10.0)
dig *= 10.0;
int tmp;
while (dig >= 1.0) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (double)tmp * dig;
dig /= 10.0;
}
buf[idx] = '.';
if (++idx == siz)
print();
for (int i = 0; i < 12; ++i) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (double)tmp * dig;
dig /= 10.0;
}
return *this;
}
inline kout &kout::operator<(long double val) {
if (val < 0.0L) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1.0L;
long double dig = 1.0L;
while (val / dig <= -10.0L)
dig *= 10.0L;
int tmp;
while (dig >= 1.0L) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (long double)tmp * dig;
dig /= 10.0L;
}
buf[idx] = '.';
if (++idx == siz)
print();
for (int i = 0; i < 16; ++i) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (long double)tmp * dig;
dig /= 10.0L;
}
return *this;
}
inline kout &kout::operator<(const char *val) {
for (int i = 0; val[i]; ++i) {
buf[idx] = val[i];
if (++idx == siz)
print();
}
return *this;
}
template <class T> inline void kout::put(T *val, int num, char spc, char end) {
--num;
for (int i = 0; i < num; ++i)
(*this) < val[i] < spc;
(*this) < val[num] < end;
return;
}
|
#include <bits/stdc++.h>
class kin {
public:
inline void open(FILE *, int);
inline void close(void);
inline void scan(void);
inline kin &operator>(char &);
inline kin &operator>(int &);
inline kin &operator>(long long &);
inline kin &operator>(double &);
inline kin &operator>(long double &);
inline kin &operator>(char *);
template <class T> inline void get(T *, int);
private:
FILE *fp;
char *buf;
int siz;
int idx;
} in;
class kout {
public:
inline void open(FILE *, int);
inline void close(void);
inline void print(void);
inline kout &operator<(char);
inline kout &operator<(int);
inline kout &operator<(long long);
inline kout &operator<(double);
inline kout &operator<(long double);
inline kout &operator<(const char *);
template <class T> inline void put(T *, int, char, char);
private:
FILE *fp;
char *buf;
int siz;
int idx;
} out;
template <class E, class V> class graph {
public:
inline void open(int, int);
inline void close(void);
inline void add(E);
inline V &operator[](int);
inline graph();
inline graph(int, int);
private:
int size;
int sizv;
E *date;
V *datv;
};
struct dse;
struct dsv;
struct dse {
int start;
int end;
dse *next;
};
struct dsv {
int score;
dse *first;
};
graph<dse, dsv> g;
inline void dfs(dse *x) {
g[x->end].score += g[x->start].score;
for (dse *i = g[x->end].first; i; i = i->next) {
if (i->end != x->start)
dfs(i);
}
}
int main(int argc, char **argv) {
in.open(stdin, 512);
out.open(stdout, 512);
in.scan();
int n;
in > n;
int m = n - 1;
g.open(m * 2, n + 1);
for (int i = 0; i <= n; ++i)
g[i].score = 0;
int q;
in > q;
for (int i = 0; i < m; ++i) {
int a, b;
in > a > b;
--a;
--b;
g.add((dse){a, b, nullptr});
g.add((dse){b, a, nullptr});
}
for (int i = 0; i < q; ++i) {
int p, x;
in > p > x;
g[p - 1].score += x;
}
dse dum = {n, 0, nullptr};
dfs(&dum);
for (int i = 0; i < m; ++i)
out < g[i].score < ' ';
out < g[m].score < '\n';
g.close();
out.print();
in.close();
out.close();
return 0;
}
template <class E, class V>
inline void graph<E, V>::open(int sizea, int sizva) {
size = 0;
sizv = sizva;
date = new E[sizea];
datv = new V[sizva];
for (int i = 0; i < sizva; ++i)
datv[i].first = nullptr;
return;
}
template <class E, class V> inline void graph<E, V>::close(void) {
sizv = 0;
size = 0;
delete[] date;
date = nullptr;
delete[] datv;
datv = nullptr;
return;
}
template <class E, class V> inline void graph<E, V>::add(E val) {
date[size] = val;
date[size].next = datv[val.start].first;
datv[val.start].first = &date[size++];
return;
}
template <class E, class V> inline V &graph<E, V>::operator[](int num) {
return datv[num];
}
template <class E, class V> inline graph<E, V>::graph() {
size = sizv = 0;
date = nullptr;
datv = nullptr;
}
template <class E, class V> inline graph<E, V>::graph(int sizea, int sizva) {
open(sizea, sizva);
}
inline void kin::open(FILE *fpa, int siza) {
fp = fpa;
buf = new char[siza];
siz = siza;
idx = 0;
return;
}
inline void kin::close(void) {
fp = nullptr;
delete[] buf;
buf = nullptr;
siz = 0;
idx = 0;
return;
}
inline void kin::scan(void) {
int readsiz =
(int)std::fread((void *)buf, (std::size_t)1, (std::size_t)siz, fp);
if (readsiz != siz)
buf[readsiz] = '\x00';
idx = 0;
return;
}
inline kin &kin::operator>(char &var) {
if (!buf[idx]) {
var = '\x00';
return *this;
}
var = buf[idx];
if (++idx == siz)
scan();
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(int &var) {
if (!buf[idx]) {
var = 0;
return *this;
}
int sign = -1;
if (buf[idx] == '-') {
sign = 1;
if (++idx == siz)
scan();
}
var = 0;
while (buf[idx] >= '0') {
var = var * 10 - (int)(buf[idx] - '0');
if (++idx == siz)
scan();
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(long long &var) {
if (!buf[idx]) {
var = 0LL;
return *this;
}
long long sign = -1LL;
if (buf[idx] == '-') {
sign = 1LL;
if (++idx == siz)
scan();
}
var = 0LL;
while (buf[idx] >= '0') {
var = var * 10LL - (long long)(buf[idx] - '0');
if (++idx == siz)
scan();
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(double &var) {
if (!buf[idx]) {
var = 0.0;
return *this;
}
double sign = -1.0;
if (buf[idx] == '-') {
sign = 1.0;
if (++idx == siz)
scan();
}
var = 0.0;
while (buf[idx] >= '0') {
var = var * 10.0 - (double)(buf[idx] - '0');
if (++idx == siz)
scan();
}
if (buf[idx] == '.') {
if (++idx == siz)
scan();
double dig = 1.0;
while (buf[idx] >= '0') {
var -= (double)(buf[idx] - '0') * (dig /= 10.0);
if (++idx == siz)
scan();
}
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(long double &var) {
if (!buf[idx]) {
var = 0.0L;
return *this;
}
long double sign = -1.0L;
if (buf[idx] == '-') {
sign = 1.0L;
if (++idx == siz)
scan();
}
var = 0.0L;
while (buf[idx] >= '0') {
var = var * 10.0L - (long double)(buf[idx] - '0');
if (++idx == siz)
scan();
}
if (buf[idx] == '.') {
if (++idx == siz)
scan();
long double dig = 1.0L;
while (buf[idx] >= '0') {
var -= (long double)(buf[idx] - '0') * (dig /= 10.0L);
if (++idx == siz)
scan();
}
}
var *= sign;
if (++idx == siz)
scan();
return *this;
}
inline kin &kin::operator>(char *var) {
if (!buf[idx]) {
var[0] = '\x00';
return *this;
}
int ptr = 0;
while (buf[idx] >= '!') {
var[ptr++] = buf[idx];
if (++idx == siz)
scan();
}
var[ptr] = '\x00';
if (++idx == siz)
scan();
return *this;
}
template <class T> inline void kin::get(T *var, int num) {
for (int i = 0; i < num; ++i)
(*this) > var[i];
return;
}
inline void kout::open(FILE *fpa, int siza) {
fp = fpa;
buf = new char[siza];
siz = siza;
idx = 0;
return;
}
inline void kout::close(void) {
fp = nullptr;
delete[] buf;
buf = nullptr;
siz = 0;
idx = 0;
return;
}
inline void kout::print(void) {
std::fwrite((void *)buf, (std::size_t)1, (std::size_t)idx, fp);
idx = 0;
return;
}
inline kout &kout::operator<(char val) {
buf[idx] = val;
if (++idx == siz)
print();
return *this;
}
inline kout &kout::operator<(int val) {
if (val < 0) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1;
char dig[10];
int ptr = 0;
do {
int tmp = val / 10;
dig[ptr++] = (char)-(val - tmp * 10) + '0';
val = tmp;
} while (val);
while (ptr--) {
buf[idx] = dig[ptr];
if (++idx == siz)
print();
}
return *this;
}
inline kout &kout::operator<(long long val) {
if (val < 0LL) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1LL;
char dig[19];
int ptr = 0;
do {
long long tmp = val / 10LL;
dig[ptr++] = (char)-(val - tmp * 10LL) + '0';
val = tmp;
} while (val);
while (ptr--) {
buf[idx] = dig[ptr];
if (++idx == siz)
print();
}
return *this;
}
inline kout &kout::operator<(double val) {
if (val < 0.0) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1.0;
double dig = 1.0;
while (val / dig <= -10.0)
dig *= 10.0;
int tmp;
while (dig >= 1.0) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (double)tmp * dig;
dig /= 10.0;
}
buf[idx] = '.';
if (++idx == siz)
print();
for (int i = 0; i < 12; ++i) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (double)tmp * dig;
dig /= 10.0;
}
return *this;
}
inline kout &kout::operator<(long double val) {
if (val < 0.0L) {
buf[idx] = '-';
if (++idx == siz)
print();
} else
val *= -1.0L;
long double dig = 1.0L;
while (val / dig <= -10.0L)
dig *= 10.0L;
int tmp;
while (dig >= 1.0L) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (long double)tmp * dig;
dig /= 10.0L;
}
buf[idx] = '.';
if (++idx == siz)
print();
for (int i = 0; i < 16; ++i) {
buf[idx] = (char)-(tmp = (int)(val / dig)) + '0';
if (++idx == siz)
print();
val -= (long double)tmp * dig;
dig /= 10.0L;
}
return *this;
}
inline kout &kout::operator<(const char *val) {
for (int i = 0; val[i]; ++i) {
buf[idx] = val[i];
if (++idx == siz)
print();
}
return *this;
}
template <class T> inline void kout::put(T *val, int num, char spc, char end) {
--num;
for (int i = 0; i < num; ++i)
(*this) < val[i] < spc;
(*this) < val[num] < end;
return;
}
| replace | 88 | 89 | 88 | 89 | -6 | free(): invalid pointer
|
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589;
int n, q;
int p[200000];
vector<int> v[200000];
int ans[200000];
void dfs(int par, int from) {
ans[from] += p[from] + ans[par];
for (int x : v[from]) {
if (x != par)
dfs(from, x);
}
}
int main() {
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
rep(i, q) {
int tmp, x;
cin >> tmp >> x;
p[tmp] += x;
}
dfs(0, 1);
rep(i, n) cout << ans[i + 1] << " ";
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589;
int n, q;
int p[200001];
vector<int> v[200001];
int ans[200001];
void dfs(int par, int from) {
ans[from] += p[from] + ans[par];
for (int x : v[from]) {
if (x != par)
dfs(from, x);
}
}
int main() {
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
rep(i, q) {
int tmp, x;
cin >> tmp >> x;
p[tmp] += x;
}
dfs(0, 1);
rep(i, n) cout << ans[i + 1] << " ";
cout << endl;
} | replace | 12 | 15 | 12 | 15 | 0 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
int Q;
cin >> N;
cin >> Q;
vector<int> eda(N - 1);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
eda.at(b - 1) = a - 1;
}
vector<int> answer(N, 0);
for (int i = 0; i < Q; i++) {
int p;
int x;
cin >> p >> x;
answer.at(p - 1) += x;
}
for (int i = 1; i < N; i++) {
answer.at(i) += answer.at(eda.at(i));
}
for (int i = 0; i < N; i++) {
cout << answer.at(i) << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
int Q;
cin >> N;
cin >> Q;
vector<int> eda(N);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
eda.at(b - 1) = a - 1;
}
vector<int> answer(N, 0);
for (int i = 0; i < Q; i++) {
int p;
int x;
cin >> p >> x;
answer.at(p - 1) += x;
}
for (int i = 1; i < N; i++) {
answer.at(i) += answer.at(eda.at(i));
}
for (int i = 0; i < N; i++) {
cout << answer.at(i) << endl;
}
} | replace | 9 | 10 | 9 | 10 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)
|
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <numeric>
#define ll long long
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define pb emplace_back
#define mp make_pair
using namespace std;
const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
#else
#define PRINT(A)
#endif
typedef pair<ll, ll> P;
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;
}
ll R, C, N, M, K, Q;
vector<ll> v[200000];
vector<ll> cost(100000, 0);
vector<ll> res(200000, -1);
void dfs(ll node, ll lastCost = 0) {
PRINT(node)
PRINT(res[node])
if (res[node] != -1) {
PRINT("FINISH")
return;
}
lastCost += cost[node];
res[node] = lastCost;
irep(itr, v[node]) {
PRINT(*itr)
dfs(*itr, lastCost);
}
return;
}
signed main() {
// cout << fixed << setprecision(10);
cin >> N >> Q;
rep(i, N - 1) {
ll from, to;
cin >> from >> to;
from--;
to--;
v[from].pb(to);
v[to].pb(from);
}
rep(i, Q) {
ll node, val;
cin >> node >> val;
cost[--node] += val;
}
dfs(0);
rep(i, N) { cout << res[i] << endl; }
// cout << max << endl;
}
| #include <bits/stdc++.h>
#include <numeric>
#define ll long long
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define pb emplace_back
#define mp make_pair
using namespace std;
const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
#else
#define PRINT(A)
#endif
typedef pair<ll, ll> P;
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;
}
ll R, C, N, M, K, Q;
vector<ll> v[300000];
vector<ll> cost(300000, 0);
vector<ll> res(300000, -1);
void dfs(ll node, ll lastCost = 0) {
PRINT(node)
PRINT(res[node])
if (res[node] != -1) {
PRINT("FINISH")
return;
}
lastCost += cost[node];
res[node] = lastCost;
irep(itr, v[node]) {
PRINT(*itr)
dfs(*itr, lastCost);
}
return;
}
signed main() {
// cout << fixed << setprecision(10);
cin >> N >> Q;
rep(i, N - 1) {
ll from, to;
cin >> from >> to;
from--;
to--;
v[from].pb(to);
v[to].pb(from);
}
rep(i, Q) {
ll node, val;
cin >> node >> val;
cost[--node] += val;
}
dfs(0);
rep(i, N) { cout << res[i] << endl; }
// cout << max << endl;
}
| replace | 45 | 48 | 45 | 48 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
typedef pair<ll, ll> P;
#define bit(n) (1LL << (n))
// #define int long long
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }
#define mod (ll)(1e9 + 7)
#define INF LLONG_MAX
template <ll ModVal> struct ModInt {
ll x;
ModInt(ll _x = 0) : x((_x % ModVal + ModVal) % ModVal) {}
ModInt operator-() const { return ModInt(-x); }
ModInt &operator+=(const ModInt a) {
x += a.x;
if (x >= ModVal)
x -= ModVal;
return *this;
}
ModInt &operator-=(const ModInt a) {
x = x + ModVal - a.x;
if (x >= ModVal)
x -= ModVal;
return *this;
}
ModInt &operator*=(const ModInt a) {
x *= a.x;
x %= ModVal;
return *this;
}
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll tmp = a / b;
ll d = ext_gcd(b, a - b * tmp, y, x);
y -= tmp * x;
return d;
}
// 逆元
ModInt inv(const ModInt a) {
ll u, v;
ext_gcd(a.x, ModVal, u, v);
return ModInt(u);
}
ModInt &operator/=(const ModInt a) { return (*this) *= inv(a); }
ModInt operator+(const ModInt a) const {
ModInt retval(*this);
return retval += a;
}
ModInt operator-(const ModInt a) const {
ModInt retval(*this);
return retval -= a;
}
ModInt operator*(const ModInt a) const {
ModInt retval(*this);
return retval *= a;
}
ModInt operator/(const ModInt a) const {
ModInt retval(*this);
return retval /= a;
}
ModInt pow(ll n) {
ModInt ans(1);
while (n) {
if (n & 1)
ans = ans * x;
*this = (*this) * (*this);
n = n >> 1;
}
return ans;
}
constexpr const ll &value() { return this->x; }
};
template <ll ModVal> ostream &operator<<(ostream &os, const ModInt<ModVal> &a) {
os << a.x;
return os;
}
using mint = ModInt<mod>;
template <typename T> class Combination {
public:
Combination(ll _max_n) : max_n(_max_n), factional(max_n + 1), inv(max_n + 1) {
factional[0] = 1;
inv[0] = 1;
for (ll i = 0; i < max_n; i++) {
factional[i + 1] = factional[i] * (i + 1); // n!(mod M)
inv[i + 1] = inv[i] / (i + 1); // k!^(M-2) (mod M)
}
}
// nCk
T choose(ll n, ll k) {
if (n == 0 && k == 0)
return 1;
if (n < k || n < 0)
return 0;
T tmp = inv[n - k] * inv[k];
return tmp * factional[n];
}
private:
const ll max_n;
std::vector<T> factional;
std::vector<T> inv;
};
using Comb = Combination<mint>;
ll val[100001] = {};
vll m[100001];
void dfs(int i, int parent) {
FORm(it, m[i]) {
if (*it != parent) {
val[*it] += val[i];
dfs(*it, i);
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n, q;
cin >> n >> q;
rep(i, n - 1) {
ll a, b;
cin >> a >> b;
m[a].push_back(b);
m[b].push_back(a);
}
rep(i, q) {
ll p, x;
cin >> p >> x;
val[p] += x;
}
dfs(1, 0);
REP(i, n + 1) { cout << val[i] << " "; }
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
typedef pair<ll, ll> P;
#define bit(n) (1LL << (n))
// #define int long long
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }
#define mod (ll)(1e9 + 7)
#define INF LLONG_MAX
template <ll ModVal> struct ModInt {
ll x;
ModInt(ll _x = 0) : x((_x % ModVal + ModVal) % ModVal) {}
ModInt operator-() const { return ModInt(-x); }
ModInt &operator+=(const ModInt a) {
x += a.x;
if (x >= ModVal)
x -= ModVal;
return *this;
}
ModInt &operator-=(const ModInt a) {
x = x + ModVal - a.x;
if (x >= ModVal)
x -= ModVal;
return *this;
}
ModInt &operator*=(const ModInt a) {
x *= a.x;
x %= ModVal;
return *this;
}
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll tmp = a / b;
ll d = ext_gcd(b, a - b * tmp, y, x);
y -= tmp * x;
return d;
}
// 逆元
ModInt inv(const ModInt a) {
ll u, v;
ext_gcd(a.x, ModVal, u, v);
return ModInt(u);
}
ModInt &operator/=(const ModInt a) { return (*this) *= inv(a); }
ModInt operator+(const ModInt a) const {
ModInt retval(*this);
return retval += a;
}
ModInt operator-(const ModInt a) const {
ModInt retval(*this);
return retval -= a;
}
ModInt operator*(const ModInt a) const {
ModInt retval(*this);
return retval *= a;
}
ModInt operator/(const ModInt a) const {
ModInt retval(*this);
return retval /= a;
}
ModInt pow(ll n) {
ModInt ans(1);
while (n) {
if (n & 1)
ans = ans * x;
*this = (*this) * (*this);
n = n >> 1;
}
return ans;
}
constexpr const ll &value() { return this->x; }
};
template <ll ModVal> ostream &operator<<(ostream &os, const ModInt<ModVal> &a) {
os << a.x;
return os;
}
using mint = ModInt<mod>;
template <typename T> class Combination {
public:
Combination(ll _max_n) : max_n(_max_n), factional(max_n + 1), inv(max_n + 1) {
factional[0] = 1;
inv[0] = 1;
for (ll i = 0; i < max_n; i++) {
factional[i + 1] = factional[i] * (i + 1); // n!(mod M)
inv[i + 1] = inv[i] / (i + 1); // k!^(M-2) (mod M)
}
}
// nCk
T choose(ll n, ll k) {
if (n == 0 && k == 0)
return 1;
if (n < k || n < 0)
return 0;
T tmp = inv[n - k] * inv[k];
return tmp * factional[n];
}
private:
const ll max_n;
std::vector<T> factional;
std::vector<T> inv;
};
using Comb = Combination<mint>;
ll val[1000001] = {};
vll m[1000001];
void dfs(int i, int parent) {
FORm(it, m[i]) {
if (*it != parent) {
val[*it] += val[i];
dfs(*it, i);
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n, q;
cin >> n >> q;
rep(i, n - 1) {
ll a, b;
cin >> a >> b;
m[a].push_back(b);
m[b].push_back(a);
}
rep(i, q) {
ll p, x;
cin >> p >> x;
val[p] += x;
}
dfs(1, 0);
REP(i, n + 1) { cout << val[i] << " "; }
cout << endl;
return 0;
}
| replace | 146 | 148 | 146 | 148 | 0 | |
p02936 | C++ | Runtime Error | #ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#endif
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VLL = vector<ll>;
using VVLL = vector<vector<ll>>;
using VB = vector<bool>;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1000000007;
const ll INF_LL = 1'000'000'000'000'000'007;
#define __overload3(_1, _2, _3, name, ...) name
#define rep(...) \
__overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__)
#define repeat(times) repFromUntil(_name##__LINE__, 0, times)
#define repUntil(name, times) repFromUntil(name, 0, times)
#define repFromUntil(name, from, until) \
for (int name = from, name##__until = (until); name < name##__until; name++)
#define repr(...) \
__overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__)
#define reprUntil(name, times) reprFromUntil(name, 0, times)
#define reprFromUntil(name, from, until) \
for (int name = (until)-1, name##__from = (from); name >= name##__from; \
name--)
#define EXIT(out) \
do { \
OUT(out); \
exit(0); \
} while (0)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define fs first
#define sn second
#ifdef LOCAL
#define debug(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
debugos << (v) << newl; \
} while (0)
#define debugv(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
for (auto e : (v)) { \
debugos << e << " "; \
} \
debugos << newl; \
} while (0)
#define debuga(m, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > "; \
for (int x = 0; x < (w); x++) { \
debugos << (m)[x] << " "; \
} \
debugos << newl; \
} while (0)
#define debugaa(m, h, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > \n"; \
for (int y = 0; y < (h); y++) { \
for (int x = 0; x < (w); x++) { \
debugos << (m)[y][x] << " "; \
} \
debugos << newl; \
} \
} while (0)
#else
#define debug(v) \
do { \
v; \
} while (0)
#define debugv(v) \
do \
; \
while (0)
#define debuga(m, w) \
do \
; \
while (0)
#define debugaa(m, h, w) \
do \
; \
while (0)
#endif
#define newl "\n"
constexpr int dr[] = {1, -1, 0, 0}; // LRUD
constexpr int dc[] = {0, 0, 1, -1};
bool inside(int r, int c, int H, int W) {
return 0 <= r and r < H and 0 <= c and c < W;
}
template <typename T, typename U> bool chmin(T &var, U x) {
if (var > x) {
var = x;
return true;
} else
return false;
}
template <typename T, typename U> bool chmax(T &var, U x) {
if (var < x) {
var = x;
return true;
} else
return false;
}
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
ll power(ll e, int t, ll mod = INF_LL) {
ll res = 1;
while (t) {
if (t & 1)
res = (res * e) % mod;
t >>= 1;
e = (e * e) % mod;
}
return res;
}
template <typename T> T divceil(T m, T d) {
assert(m >= 0 and d > 0);
return (m + d - 1) / d;
}
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
string operator*(const string &s, int times) {
string res = "";
rep(times) res += s;
return res;
}
class MyScanner {
public:
int offset = 0;
char nc() {
#ifdef LOCAL
return getchar();
#else
static char buf[100000], *L = buf, *R = buf;
return L == R && (R = (L = buf) + fread(buf, 1, 100000, stdin), L == R)
? EOF
: *L++;
#endif
}
template <typename T> void input_integer(T &var) {
var = 0;
T sign = 1;
int cc = nc();
for (; cc < '0' || '9' < cc; cc = nc())
if (cc == '-')
sign = -1;
for (; '0' <= cc && cc <= '9'; cc = nc())
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
var += offset;
}
int c() {
char c;
while (c = nc(), c == ' ' or c == '\n')
;
return c;
}
MyScanner &operator>>(char &var) {
var = c();
return *this;
}
MyScanner &operator>>(int &var) {
input_integer<int>(var);
return *this;
}
MyScanner &operator>>(ll &var) {
input_integer<ll>(var);
return *this;
}
MyScanner &operator>>(string &var) {
var = "";
int cc = nc();
for (; !isvisiblechar(cc); cc = nc())
;
for (; isvisiblechar(cc); cc = nc())
var.push_back(cc);
return *this;
}
template <typename T> operator T() {
T x;
*this >> x;
return x;
}
template <typename T> void operator()(T &t) { *this >> t; }
template <typename T, typename... Ts> void operator()(T &t, Ts &...ts) {
*this >> t;
this->operator()(ts...);
}
template <typename Iter> void iter(Iter first, Iter last) {
while (first != last)
*this >> *first, first++;
}
VI vi(int n) {
VI res(n);
iter(all(res));
return res;
}
VVI vvi(int n, int m) {
VVI res(n);
rep(i, n) res[i] = vi(m);
return res;
}
VLL vll(int n) {
VLL res(n);
iter(all(res));
return res;
}
VVLL vvll(int n, int m) {
VVLL res(n);
rep(i, n) res[i] = vll(m);
return res;
}
template <typename T> vector<T> v(int n) {
vector<T> res(n);
iter(all(res));
return res;
}
private:
int isvisiblechar(int c) { return 0x21 <= c && c <= 0x7E; }
} IN, IN1{-1};
class MyPrinter {
public:
template <typename T> void output_integer(T var) {
if (var == 0) {
putchar('0');
return;
}
if (var < 0)
putchar('-'), var = -var;
char stack[32];
int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10), var /= 10;
while (stack_p)
putchar(stack[--stack_p]);
}
MyPrinter &operator<<(char c) {
putchar(c);
return *this;
}
MyPrinter &operator<<(double x) {
printf("%.10f", x);
return *this;
}
template <typename T> MyPrinter &operator<<(T var) {
output_integer<T>(var);
return *this;
}
MyPrinter &operator<<(char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const string &str) {
const char *p = str.c_str();
const char *l = p + str.size();
while (p < l)
putchar(*p++);
return *this;
}
template <typename T> void operator()(T x) { *this << x << newl; }
template <typename T, typename... Ts> void operator()(T x, Ts... xs) {
*this << x << " ";
this->operator()(xs...);
}
template <typename Iter> void iter(Iter s, Iter t) {
if (s == t)
*this << "\n";
else {
for (; s != t; s++) {
*this << *s << " \n"[next(s, 1) == t];
}
}
}
template <typename Range> void range(const Range &r) {
iter(begin(r), end(r));
}
} OUT;
class DebugPrint {
public:
template <typename T> DebugPrint &operator<<(const T &v) {
#ifdef LOCAL
cerr << v;
#endif
return *this;
}
} debugos;
template <typename OutStream, typename T, typename U>
OutStream &operator<<(OutStream &out, const pair<T, U> &var) {
return out << var.first << " " << var.second;
}
template <typename OutStream, typename Tuple, size_t I, size_t N,
enable_if_t<I == N> * = nullptr>
OutStream &tuple_impl(OutStream &out, const Tuple &var) {
return out;
}
template <typename OutStream, typename Tuple, size_t I, size_t N,
enable_if_t<I != N> * = nullptr>
OutStream &tuple_impl(OutStream &out, const Tuple &var) {
out << get<I>(var) << " ";
return tuple_impl<OutStream, Tuple, I + 1, N>(out, var);
}
template <typename OutStream, typename... Ts>
OutStream &operator<<(OutStream &out, const tuple<Ts...> &var) {
return tuple_impl<OutStream, tuple<Ts...>, 0, sizeof...(Ts)>(out, var);
}
template <typename InStream, typename T, typename U>
InStream &operator>>(InStream &in, pair<T, U> &var) {
return in >> var.first >> var.second;
}
template <typename InStream, typename Tuple, size_t I, size_t N,
enable_if_t<I == N> * = nullptr>
InStream &tuple_impl(InStream &in, Tuple &var) {
return in;
}
template <typename InStream, typename Tuple, size_t I, size_t N,
enable_if_t<I != N> * = nullptr>
InStream &tuple_impl(InStream &in, Tuple &var) {
in >> get<I>(var);
return tuple_impl<InStream, Tuple, I + 1, N>(in, var);
}
template <typename InStream, typename... Ts>
InStream &operator>>(InStream &in, tuple<Ts...> &var) {
return tuple_impl<InStream, tuple<Ts...>, 0, sizeof...(Ts)>(in, var);
}
class HLD {
private:
VI index, sz, par;
// last vertex in ascending heavy path
VI nxt;
public:
HLD(VVI &graph)
: index(graph.size()), sz(graph.size(), 1), par(graph.size(), -1),
nxt(graph.size()) {
dfs_sz(graph, 0);
dfs_hld(graph, 0);
debugv(index);
}
private:
void dfs_sz(VVI &graph, int v) {
if (graph[v].size() >= 2 and graph[v][0] == par[v])
swap(graph[v][0], graph[v][1]);
for (auto &c : graph[v])
if (c != par[v]) {
par[c] = v;
dfs_sz(graph, c);
sz[v] += sz[c];
if (sz[c] > sz[graph[v][0]])
swap(c, graph[v][0]);
}
}
void dfs_hld(const VVI &graph, int v) {
static int t = 0;
index[v] = t++;
for (auto c : graph[v])
if (c != par[v]) {
nxt[c] = (c == graph[v][0] ? nxt[v] : c);
dfs_hld(graph, c);
}
if (sz[v] != t - index[v]) {
while (true)
;
}
}
public:
template <typename F> void path_vertex(int u, int v, F f) {
while (true) {
if (index[u] > index[v])
swap(u, v);
// debug(u); debug(v);
// debug(nxt[u]); debug(index[u]);
f(max(index[nxt[v]], index[u]), index[v] + 1);
if (nxt[u] != nxt[v])
v = par[nxt[v]];
else
break;
}
}
template <typename F> void path_edge(int u, int v, F f) {
while (true) {
if (index[u] > index[v])
swap(u, v);
if (nxt[u] != nxt[v]) {
f(index[nxt[v]], index[v] + 1);
v = par[nxt[v]];
} else {
if (u != v)
f(index[u] + 1, index[v] + 1);
break;
}
}
}
template <typename F> void subtree_vertex(int u, F f) { f(u, u + sz[u]); }
template <typename F> void subtree_edge(int u, F f) { f(u + 1, u + sz[u]); }
int vertex(int u) { return index[u]; }
};
int main() {
int n = IN, q = IN;
VVI graph(n);
rep(n - 1) {
int u = IN1, v = IN1;
graph[u].push_back(v);
graph[v].push_back(u);
}
HLD hld(graph);
VLL acc(n + 1);
rep(q) {
int p = IN, x = IN;
p--;
hld.subtree_vertex(p, [&](auto arg1, auto arg2) {
debug(arg1);
debug(arg2);
acc[arg1] += x;
acc[arg2] -= x;
});
}
rep(i, n) acc[i + 1] += acc[i];
OUT << acc[hld.vertex(0)];
rep(i, 1, n) OUT << " " << acc[hld.vertex(i)];
OUT << newl;
}
| #ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#endif
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VLL = vector<ll>;
using VVLL = vector<vector<ll>>;
using VB = vector<bool>;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1000000007;
const ll INF_LL = 1'000'000'000'000'000'007;
#define __overload3(_1, _2, _3, name, ...) name
#define rep(...) \
__overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__)
#define repeat(times) repFromUntil(_name##__LINE__, 0, times)
#define repUntil(name, times) repFromUntil(name, 0, times)
#define repFromUntil(name, from, until) \
for (int name = from, name##__until = (until); name < name##__until; name++)
#define repr(...) \
__overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__)
#define reprUntil(name, times) reprFromUntil(name, 0, times)
#define reprFromUntil(name, from, until) \
for (int name = (until)-1, name##__from = (from); name >= name##__from; \
name--)
#define EXIT(out) \
do { \
OUT(out); \
exit(0); \
} while (0)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define fs first
#define sn second
#ifdef LOCAL
#define debug(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
debugos << (v) << newl; \
} while (0)
#define debugv(v) \
do { \
debugos << "L" << __LINE__ << " " << #v << " > "; \
for (auto e : (v)) { \
debugos << e << " "; \
} \
debugos << newl; \
} while (0)
#define debuga(m, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > "; \
for (int x = 0; x < (w); x++) { \
debugos << (m)[x] << " "; \
} \
debugos << newl; \
} while (0)
#define debugaa(m, h, w) \
do { \
debugos << "L" << __LINE__ << " " << #m << " > \n"; \
for (int y = 0; y < (h); y++) { \
for (int x = 0; x < (w); x++) { \
debugos << (m)[y][x] << " "; \
} \
debugos << newl; \
} \
} while (0)
#else
#define debug(v) \
do { \
v; \
} while (0)
#define debugv(v) \
do \
; \
while (0)
#define debuga(m, w) \
do \
; \
while (0)
#define debugaa(m, h, w) \
do \
; \
while (0)
#endif
#define newl "\n"
constexpr int dr[] = {1, -1, 0, 0}; // LRUD
constexpr int dc[] = {0, 0, 1, -1};
bool inside(int r, int c, int H, int W) {
return 0 <= r and r < H and 0 <= c and c < W;
}
template <typename T, typename U> bool chmin(T &var, U x) {
if (var > x) {
var = x;
return true;
} else
return false;
}
template <typename T, typename U> bool chmax(T &var, U x) {
if (var < x) {
var = x;
return true;
} else
return false;
}
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
ll power(ll e, int t, ll mod = INF_LL) {
ll res = 1;
while (t) {
if (t & 1)
res = (res * e) % mod;
t >>= 1;
e = (e * e) % mod;
}
return res;
}
template <typename T> T divceil(T m, T d) {
assert(m >= 0 and d > 0);
return (m + d - 1) / d;
}
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
string operator*(const string &s, int times) {
string res = "";
rep(times) res += s;
return res;
}
class MyScanner {
public:
int offset = 0;
char nc() {
#ifdef LOCAL
return getchar();
#else
static char buf[100000], *L = buf, *R = buf;
return L == R && (R = (L = buf) + fread(buf, 1, 100000, stdin), L == R)
? EOF
: *L++;
#endif
}
template <typename T> void input_integer(T &var) {
var = 0;
T sign = 1;
int cc = nc();
for (; cc < '0' || '9' < cc; cc = nc())
if (cc == '-')
sign = -1;
for (; '0' <= cc && cc <= '9'; cc = nc())
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
var += offset;
}
int c() {
char c;
while (c = nc(), c == ' ' or c == '\n')
;
return c;
}
MyScanner &operator>>(char &var) {
var = c();
return *this;
}
MyScanner &operator>>(int &var) {
input_integer<int>(var);
return *this;
}
MyScanner &operator>>(ll &var) {
input_integer<ll>(var);
return *this;
}
MyScanner &operator>>(string &var) {
var = "";
int cc = nc();
for (; !isvisiblechar(cc); cc = nc())
;
for (; isvisiblechar(cc); cc = nc())
var.push_back(cc);
return *this;
}
template <typename T> operator T() {
T x;
*this >> x;
return x;
}
template <typename T> void operator()(T &t) { *this >> t; }
template <typename T, typename... Ts> void operator()(T &t, Ts &...ts) {
*this >> t;
this->operator()(ts...);
}
template <typename Iter> void iter(Iter first, Iter last) {
while (first != last)
*this >> *first, first++;
}
VI vi(int n) {
VI res(n);
iter(all(res));
return res;
}
VVI vvi(int n, int m) {
VVI res(n);
rep(i, n) res[i] = vi(m);
return res;
}
VLL vll(int n) {
VLL res(n);
iter(all(res));
return res;
}
VVLL vvll(int n, int m) {
VVLL res(n);
rep(i, n) res[i] = vll(m);
return res;
}
template <typename T> vector<T> v(int n) {
vector<T> res(n);
iter(all(res));
return res;
}
private:
int isvisiblechar(int c) { return 0x21 <= c && c <= 0x7E; }
} IN, IN1{-1};
class MyPrinter {
public:
template <typename T> void output_integer(T var) {
if (var == 0) {
putchar('0');
return;
}
if (var < 0)
putchar('-'), var = -var;
char stack[32];
int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10), var /= 10;
while (stack_p)
putchar(stack[--stack_p]);
}
MyPrinter &operator<<(char c) {
putchar(c);
return *this;
}
MyPrinter &operator<<(double x) {
printf("%.10f", x);
return *this;
}
template <typename T> MyPrinter &operator<<(T var) {
output_integer<T>(var);
return *this;
}
MyPrinter &operator<<(char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const char *str_p) {
while (*str_p)
putchar(*(str_p++));
return *this;
}
MyPrinter &operator<<(const string &str) {
const char *p = str.c_str();
const char *l = p + str.size();
while (p < l)
putchar(*p++);
return *this;
}
template <typename T> void operator()(T x) { *this << x << newl; }
template <typename T, typename... Ts> void operator()(T x, Ts... xs) {
*this << x << " ";
this->operator()(xs...);
}
template <typename Iter> void iter(Iter s, Iter t) {
if (s == t)
*this << "\n";
else {
for (; s != t; s++) {
*this << *s << " \n"[next(s, 1) == t];
}
}
}
template <typename Range> void range(const Range &r) {
iter(begin(r), end(r));
}
} OUT;
class DebugPrint {
public:
template <typename T> DebugPrint &operator<<(const T &v) {
#ifdef LOCAL
cerr << v;
#endif
return *this;
}
} debugos;
template <typename OutStream, typename T, typename U>
OutStream &operator<<(OutStream &out, const pair<T, U> &var) {
return out << var.first << " " << var.second;
}
template <typename OutStream, typename Tuple, size_t I, size_t N,
enable_if_t<I == N> * = nullptr>
OutStream &tuple_impl(OutStream &out, const Tuple &var) {
return out;
}
template <typename OutStream, typename Tuple, size_t I, size_t N,
enable_if_t<I != N> * = nullptr>
OutStream &tuple_impl(OutStream &out, const Tuple &var) {
out << get<I>(var) << " ";
return tuple_impl<OutStream, Tuple, I + 1, N>(out, var);
}
template <typename OutStream, typename... Ts>
OutStream &operator<<(OutStream &out, const tuple<Ts...> &var) {
return tuple_impl<OutStream, tuple<Ts...>, 0, sizeof...(Ts)>(out, var);
}
template <typename InStream, typename T, typename U>
InStream &operator>>(InStream &in, pair<T, U> &var) {
return in >> var.first >> var.second;
}
template <typename InStream, typename Tuple, size_t I, size_t N,
enable_if_t<I == N> * = nullptr>
InStream &tuple_impl(InStream &in, Tuple &var) {
return in;
}
template <typename InStream, typename Tuple, size_t I, size_t N,
enable_if_t<I != N> * = nullptr>
InStream &tuple_impl(InStream &in, Tuple &var) {
in >> get<I>(var);
return tuple_impl<InStream, Tuple, I + 1, N>(in, var);
}
template <typename InStream, typename... Ts>
InStream &operator>>(InStream &in, tuple<Ts...> &var) {
return tuple_impl<InStream, tuple<Ts...>, 0, sizeof...(Ts)>(in, var);
}
class HLD {
private:
VI index, sz, par;
// last vertex in ascending heavy path
VI nxt;
public:
HLD(VVI &graph)
: index(graph.size()), sz(graph.size(), 1), par(graph.size(), -1),
nxt(graph.size()) {
dfs_sz(graph, 0);
dfs_hld(graph, 0);
debugv(index);
}
private:
void dfs_sz(VVI &graph, int v) {
if (graph[v].size() >= 2 and graph[v][0] == par[v])
swap(graph[v][0], graph[v][1]);
for (auto &c : graph[v])
if (c != par[v]) {
par[c] = v;
dfs_sz(graph, c);
sz[v] += sz[c];
if (sz[c] > sz[graph[v][0]])
swap(c, graph[v][0]);
}
}
void dfs_hld(const VVI &graph, int v) {
static int t = 0;
index[v] = t++;
for (auto c : graph[v])
if (c != par[v]) {
nxt[c] = (c == graph[v][0] ? nxt[v] : c);
dfs_hld(graph, c);
}
if (sz[v] != t - index[v]) {
while (true)
;
}
}
public:
template <typename F> void path_vertex(int u, int v, F f) {
while (true) {
if (index[u] > index[v])
swap(u, v);
// debug(u); debug(v);
// debug(nxt[u]); debug(index[u]);
f(max(index[nxt[v]], index[u]), index[v] + 1);
if (nxt[u] != nxt[v])
v = par[nxt[v]];
else
break;
}
}
template <typename F> void path_edge(int u, int v, F f) {
while (true) {
if (index[u] > index[v])
swap(u, v);
if (nxt[u] != nxt[v]) {
f(index[nxt[v]], index[v] + 1);
v = par[nxt[v]];
} else {
if (u != v)
f(index[u] + 1, index[v] + 1);
break;
}
}
}
template <typename F> void subtree_vertex(int u, F f) {
f(index[u], index[u] + sz[u]);
}
template <typename F> void subtree_edge(int u, F f) { f(u + 1, u + sz[u]); }
int vertex(int u) { return index[u]; }
};
int main() {
int n = IN, q = IN;
VVI graph(n);
rep(n - 1) {
int u = IN1, v = IN1;
graph[u].push_back(v);
graph[v].push_back(u);
}
HLD hld(graph);
VLL acc(n + 1);
rep(q) {
int p = IN, x = IN;
p--;
hld.subtree_vertex(p, [&](auto arg1, auto arg2) {
debug(arg1);
debug(arg2);
acc[arg1] += x;
acc[arg2] -= x;
});
}
rep(i, n) acc[i + 1] += acc[i];
OUT << acc[hld.vertex(0)];
rep(i, 1, n) OUT << " " << acc[hld.vertex(i)];
OUT << newl;
}
| replace | 450 | 451 | 450 | 453 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ULL = unsigned long long;
int x[100000] = {};
int p[100000];
int dp[100000];
int solve2(int i) {
if (dp[i] != -1)
return dp[i];
if (i == 0)
return dp[i] = x[0];
return dp[i] = solve2(p[i]) + x[i];
}
void solve() {
int n, q;
cin >> n >> q;
{
vector<vector<int>> e(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
e[a].push_back(b);
e[b].push_back(a);
}
queue<int> Q;
Q.push(0);
while (!Q.empty()) {
int qp = Q.front();
Q.pop();
rep(i, e[qp].size()) {
if (p[qp] != -1)
if (e[qp][i] == p[qp])
continue;
Q.push(e[qp][i]);
p[e[qp][i]] = qp;
}
}
}
rep(i, q) {
int pp, xx;
cin >> pp >> xx;
pp--;
x[pp] += xx;
}
rep(i, n) { dp[i] = -1; }
rep(i, n) { cout << solve2(i) << endl; }
}
int main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ULL = unsigned long long;
int x[200000] = {};
int p[200000];
int dp[200000];
int solve2(int i) {
if (dp[i] != -1)
return dp[i];
if (i == 0)
return dp[i] = x[0];
return dp[i] = solve2(p[i]) + x[i];
}
void solve() {
int n, q;
cin >> n >> q;
{
vector<vector<int>> e(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
e[a].push_back(b);
e[b].push_back(a);
}
queue<int> Q;
Q.push(0);
while (!Q.empty()) {
int qp = Q.front();
Q.pop();
rep(i, e[qp].size()) {
if (p[qp] != -1)
if (e[qp][i] == p[qp])
continue;
Q.push(e[qp][i]);
p[e[qp][i]] = qp;
}
}
}
rep(i, q) {
int pp, xx;
cin >> pp >> xx;
pp--;
x[pp] += xx;
}
rep(i, n) { dp[i] = -1; }
rep(i, n) { cout << solve2(i) << endl; }
}
int main() {
solve();
return 0;
}
| replace | 6 | 9 | 6 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <math.h>
#include <vector>
std::vector<long> to[20005];
std::vector<long long> ans;
void dfs(int a, int p = -1) {
// printf("a=%d\n",a);
// printf("to[%d].size()=%ld\n",a,to[a].size());
for (int i = 0; i < to[a].size(); i++) {
if (to[a][i] == p) {
continue;
}
ans[to[a][i]] += ans[a];
// printf("ans[%ld] += %ld\n",to[a][i], ans[a]);
dfs(to[a][i], a);
}
return;
}
int main() {
long N, Q;
scanf("%ld %ld", &N, &Q);
// printf("N=%ld, Q=%ld\n",N,Q);
ans.resize(200005);
int temp1, temp2;
for (int i = 0; i < N - 1; i++) {
scanf("%d", &temp1);
scanf("%d", &temp2);
temp1--;
temp2--;
// printf("tp[%d].push_back(%d)\n",temp1,temp2);
to[temp1].push_back(temp2);
to[temp2].push_back(temp1);
}
// printf("to[0][0]=%ld\n",to[0][0]);
for (int i = 0; i < Q; i++) {
scanf("%d", &temp1);
scanf("%d", &temp2);
temp1--;
ans[temp1] += temp2;
}
dfs(0);
// printf("============\n");
printf("%llu", ans[0]);
for (int i = 1; i < N; i++) {
printf(" %llu", ans[i]);
}
printf("\n");
return 0;
} | #include <iostream>
#include <math.h>
#include <vector>
std::vector<long> to[200005];
std::vector<long long> ans;
void dfs(int a, int p = -1) {
// printf("a=%d\n",a);
// printf("to[%d].size()=%ld\n",a,to[a].size());
for (int i = 0; i < to[a].size(); i++) {
if (to[a][i] == p) {
continue;
}
ans[to[a][i]] += ans[a];
// printf("ans[%ld] += %ld\n",to[a][i], ans[a]);
dfs(to[a][i], a);
}
return;
}
int main() {
long N, Q;
scanf("%ld %ld", &N, &Q);
// printf("N=%ld, Q=%ld\n",N,Q);
ans.resize(200005);
int temp1, temp2;
for (int i = 0; i < N - 1; i++) {
scanf("%d", &temp1);
scanf("%d", &temp2);
temp1--;
temp2--;
// printf("tp[%d].push_back(%d)\n",temp1,temp2);
to[temp1].push_back(temp2);
to[temp2].push_back(temp1);
}
// printf("to[0][0]=%ld\n",to[0][0]);
for (int i = 0; i < Q; i++) {
scanf("%d", &temp1);
scanf("%d", &temp2);
temp1--;
ans[temp1] += temp2;
}
dfs(0);
// printf("============\n");
printf("%llu", ans[0]);
for (int i = 1; i < N; i++) {
printf(" %llu", ans[i]);
}
printf("\n");
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b) \
for (long long i = (long long)(a); i >= (long long)(b); i--)
#define MIN3(a, b, c) \
(a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))
#define MAX(a, b) (a) > (b) ? (a) : (b)
#define MIN2(a, b) (a) < (b) ? (a) : (b)
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
#define ull unsigned long long
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;
#define deb(x) cerr << #x << " here " << x << endl;
#define endl "\n"
#define printCase() "Case #" << caseNum << ": "
inline bool is_palindrome(const string &s) {
return std::equal(s.begin(), s.end(), s.rbegin());
}
const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
#define ff first
#define ss second
/*----------------------------------------------------------------------*/
const int N = 1e5 + 5;
vi g[N];
ll vals[N];
int n, q, u, v;
void dfs(int curr, ll val = 0, int par = -1) {
vals[curr] += val;
for (auto nxt : g[curr]) {
if (nxt != par) {
dfs(nxt, vals[curr], curr);
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
memset(vals, 0, sizeof(vals));
cin >> n >> q;
FOR(i, 0, n - 1) {
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
FOR(i, 0, q) {
cin >> u >> v;
u--;
vals[u] += v;
}
dfs(0);
FOR(i, 0, n) { cout << vals[i] << " "; }
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b) \
for (long long i = (long long)(a); i >= (long long)(b); i--)
#define MIN3(a, b, c) \
(a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))
#define MAX(a, b) (a) > (b) ? (a) : (b)
#define MIN2(a, b) (a) < (b) ? (a) : (b)
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
#define ull unsigned long long
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;
#define deb(x) cerr << #x << " here " << x << endl;
#define endl "\n"
#define printCase() "Case #" << caseNum << ": "
inline bool is_palindrome(const string &s) {
return std::equal(s.begin(), s.end(), s.rbegin());
}
const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
#define ff first
#define ss second
/*----------------------------------------------------------------------*/
const int N = 2e5 + 5;
vi g[N];
ll vals[N];
int n, q, u, v;
void dfs(int curr, ll val = 0, int par = -1) {
vals[curr] += val;
for (auto nxt : g[curr]) {
if (nxt != par) {
dfs(nxt, vals[curr], curr);
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
memset(vals, 0, sizeof(vals));
cin >> n >> q;
FOR(i, 0, n - 1) {
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
FOR(i, 0, q) {
cin >> u >> v;
u--;
vals[u] += v;
}
dfs(0);
FOR(i, 0, n) { cout << vals[i] << " "; }
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 20;
int n, q, a, b, num[MAXN], ans[MAXN];
vector<int> adj[MAXN];
bool vis[MAXN];
void dfs(int v, int sm) {
vis[v] = true;
sm += num[v];
ans[v] = sm;
for (int u : adj[v])
if (!vis[u])
dfs(u, sm);
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> q;
for (int i = 1; i < n; i++)
cin >> a >> b, adj[a].push_back(b), adj[b].push_back(a);
for (int i = 0; i < q; i++) {
cin >> a >> b;
num[a] += b;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
cout << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 * 1000 + 20;
int n, q, a, b, num[MAXN], ans[MAXN];
vector<int> adj[MAXN];
bool vis[MAXN];
void dfs(int v, int sm) {
vis[v] = true;
sm += num[v];
ans[v] = sm;
for (int u : adj[v])
if (!vis[u])
dfs(u, sm);
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> q;
for (int i = 1; i < n; i++)
cin >> a >> b, adj[a].push_back(b), adj[b].push_back(a);
for (int i = 0; i < q; i++) {
cin >> a >> b;
num[a] += b;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
cout << '\n';
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<bool> seen(200005, false);
vector<int> counter;
void dfs(vector<vector<int>> vec, int v) {
seen.at(v) = true;
for (int i = 0; i < vec.at(v).size(); i++) {
if (seen.at(vec.at(v).at(i))) {
continue;
}
counter[vec[v][i]] += counter[v];
dfs(vec, vec.at(v).at(i));
}
}
int main() {
int N, Q;
cin >> N >> Q;
vector<vector<int>> to(N, vector<int>(0));
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
to[a - 1].push_back(b - 1);
to[b - 1].push_back(a - 1);
}
counter.assign(N, 0);
for (int i = 0; i < Q; i++) {
int p, x;
cin >> p >> x;
counter[p - 1] += x;
}
dfs(to, 0);
for (int i = 0; i < N; i++) {
cout << counter[i] << " ";
}
}
| #include <bits/stdc++.h>
using namespace std;
vector<bool> seen(200005, false);
vector<int> counter;
void dfs(vector<vector<int>> &vec, int v) {
seen.at(v) = true;
for (int i = 0; i < vec.at(v).size(); i++) {
if (seen.at(vec.at(v).at(i))) {
continue;
}
counter[vec[v][i]] += counter[v];
dfs(vec, vec.at(v).at(i));
}
}
int main() {
int N, Q;
cin >> N >> Q;
vector<vector<int>> to(N, vector<int>(0));
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
to[a - 1].push_back(b - 1);
to[b - 1].push_back(a - 1);
}
counter.assign(N, 0);
for (int i = 0; i < Q; i++) {
int p, x;
cin >> p >> x;
counter[p - 1] += x;
}
dfs(to, 0);
for (int i = 0; i < N; i++) {
cout << counter[i] << " ";
}
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p02936 | C++ | Runtime Error | //
// main.cpp
// ABC
//
// Created by Amarsanaa Davaasuren on 2019/08/31.
// Copyright © 2019 Amarsanaa Davaasuren. All rights reserved.
//
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
class UnionFind {
public:
vector<int> Parent;
UnionFind(int N) { Parent = vector<int>(N, -1); }
int root(int node) {
if (Parent[node] < 0) {
return node;
} else {
return Parent[node] = root(Parent[node]);
}
}
int size(int node) { return -root(node); }
bool connect(int A, int B) {
int Aroot = root(A);
int Broot = root(B);
if (Aroot != Broot) {
if (size(Aroot) < size(Broot))
swap(Aroot, Broot);
Parent[Aroot] += Parent[Broot];
Parent[Broot] = Aroot;
return true;
} else {
return false;
}
}
};
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
using ll = long long;
int main() {
int N, Q;
cin >> N >> Q;
vector<vector<int>> G;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
vector<ll> value(N);
rep(i, Q) {
int p, x;
cin >> p >> x;
p--;
value[p] += x;
}
auto dfs = [&](auto dfs, int child, int parent) -> void {
for (int relative : G[child])
if (relative != parent) {
value[relative] += value[child];
dfs(dfs, relative, child);
}
};
dfs(dfs, 0, -1);
rep(i, N) cout << value[i] << (i == N - 1 ? '\n' : ' ');
}
| //
// main.cpp
// ABC
//
// Created by Amarsanaa Davaasuren on 2019/08/31.
// Copyright © 2019 Amarsanaa Davaasuren. All rights reserved.
//
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
class UnionFind {
public:
vector<int> Parent;
UnionFind(int N) { Parent = vector<int>(N, -1); }
int root(int node) {
if (Parent[node] < 0) {
return node;
} else {
return Parent[node] = root(Parent[node]);
}
}
int size(int node) { return -root(node); }
bool connect(int A, int B) {
int Aroot = root(A);
int Broot = root(B);
if (Aroot != Broot) {
if (size(Aroot) < size(Broot))
swap(Aroot, Broot);
Parent[Aroot] += Parent[Broot];
Parent[Broot] = Aroot;
return true;
} else {
return false;
}
}
};
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
using ll = long long;
int main() {
int N, Q;
cin >> N >> Q;
vector<vector<int>> G(N);
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
vector<ll> value(N);
rep(i, Q) {
int p, x;
cin >> p >> x;
p--;
value[p] += x;
}
auto dfs = [&](auto dfs, int child, int parent) -> void {
for (int relative : G[child])
if (relative != parent) {
value[relative] += value[child];
dfs(dfs, relative, child);
}
};
dfs(dfs, 0, -1);
rep(i, N) cout << value[i] << (i == N - 1 ? '\n' : ' ');
}
| replace | 54 | 55 | 54 | 55 | -11 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
vector<int> res(100001);
void dfs(int cp, vector<vector<int>> &v, vector<int> &myPoint, int sump) {
int nsump = myPoint[cp] + sump;
res[cp] = nsump;
for (int i = 0; i < v[cp].size(); ++i) {
int next = v[cp][i];
dfs(next, v, myPoint, nsump);
}
}
int main() {
int n, q;
cin >> n >> q;
vector<vector<int>> v(n + 1);
int a, b;
for (int i = 0; i < n - 1; ++i) {
cin >> a >> b;
v[a].push_back(b);
}
vector<int> myPoint(n + 1);
for (int i = 0; i < q; ++i) {
cin >> a >> b;
myPoint[a] += b;
}
dfs(1, v, myPoint, 0);
for (int i = 1; i < n; ++i) {
cout << res[i] << " ";
}
cout << res[n] << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
vector<int> res(1000001);
void dfs(int cp, vector<vector<int>> &v, vector<int> &myPoint, int sump) {
int nsump = myPoint[cp] + sump;
res[cp] = nsump;
for (int i = 0; i < v[cp].size(); ++i) {
int next = v[cp][i];
dfs(next, v, myPoint, nsump);
}
}
int main() {
int n, q;
cin >> n >> q;
vector<vector<int>> v(n + 1);
int a, b;
for (int i = 0; i < n - 1; ++i) {
cin >> a >> b;
v[a].push_back(b);
}
vector<int> myPoint(n + 1);
for (int i = 0; i < q; ++i) {
cin >> a >> b;
myPoint[a] += b;
}
dfs(1, v, myPoint, 0);
for (int i = 1; i < n; ++i) {
cout << res[i] << " ";
}
cout << res[n] << endl;
return 0;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep_down(i, n) for (int i = n - 1; i >= 0; --i)
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
const int INF = 1000000007;
int N, Q;
vector<int> T[100001];
ll X[100001];
ll Ans[100001];
void dfs(int num, ll value) {
rep(i, T[num].size()) {
int next = T[num][i];
dfs(next, value + X[num]);
}
Ans[num] = value + X[num];
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
int a, b;
rep(i, N - 1) {
cin >> a >> b;
a--;
b--;
T[a].push_back(b);
}
int p;
ll x;
rep(i, Q) {
cin >> p >> x;
p--;
X[p] += x;
}
dfs(0, 0);
rep(i, N) cout << Ans[i] << ' ';
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep_down(i, n) for (int i = n - 1; i >= 0; --i)
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
const int INF = 1000000007;
int N, Q;
vector<int> T[200002];
ll X[200002];
ll Ans[200002];
void dfs(int num, ll value) {
rep(i, T[num].size()) {
int next = T[num][i];
dfs(next, value + X[num]);
}
Ans[num] = value + X[num];
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
int a, b;
rep(i, N - 1) {
cin >> a >> b;
a--;
b--;
T[a].push_back(b);
}
int p;
ll x;
rep(i, Q) {
cin >> p >> x;
p--;
X[p] += x;
}
dfs(0, 0);
rep(i, N) cout << Ans[i] << ' ';
return 0;
} | replace | 20 | 23 | 20 | 23 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define ll long long
#define el '\n'
using namespace std;
const int N = 2e5 + 10;
int ans[N];
vector<int> adl[N];
int n, q;
int lazy[N];
void dfs(int node, int p) {
ans[node] = lazy[node];
for (auto child : adl[node]) {
if (child == p)
continue;
lazy[child] += lazy[node];
dfs(child, node);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
adl[a].pb(b);
adl[b].pb(a);
}
while (q--) {
int node, val;
cin >> node >> val;
lazy[node] += val;
}
dfs(1, 1);
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << el;
return 0;
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define ll long long
#define el '\n'
using namespace std;
const int N = 2e5 + 10;
int ans[N];
vector<int> adl[N];
int n, q;
int lazy[N];
void dfs(int node, int p) {
ans[node] = lazy[node];
for (auto child : adl[node]) {
if (child == p)
continue;
lazy[child] += lazy[node];
dfs(child, node);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adl[a].pb(b);
adl[b].pb(a);
}
while (q--) {
int node, val;
cin >> node >> val;
lazy[node] += val;
}
dfs(1, 1);
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << el;
return 0;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional> // std::function<void(int)>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
bool done[d5];
int ans[d5], sum[d5];
vector<int> adj[d5];
void dfs(int root, int extra) {
done[root] = true;
ans[root] = sum[root] + extra;
for (auto u : adj[root]) {
if (!done[u]) {
dfs(u, extra + sum[root]);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, u, v, q, x, p;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < q; i++) {
cin >> x >> p;
sum[x - 1] += p;
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
cout << endl;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional> // std::function<void(int)>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 200100;
bool done[d5];
int ans[d5], sum[d5];
vector<int> adj[d5];
void dfs(int root, int extra) {
done[root] = true;
ans[root] = sum[root] + extra;
for (auto u : adj[root]) {
if (!done[u]) {
dfs(u, extra + sum[root]);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, u, v, q, x, p;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < q; i++) {
cin >> x >> p;
sum[x - 1] += p;
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
cout << endl;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const double PI = acos(-1.0);
const int N = 200005;
const int MOD = 1e9 + 7;
const int inf = 1 << 28;
typedef long long ll;
vector<int> g[N];
int a[N], ans[N];
bool vis[N];
void dfs(int x, int c) {
if (vis[x])
return;
vis[x] = 1;
ans[x] += a[x] + c;
for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i];
dfs(v, ans[x]);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q, z, x;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> z >> x;
g[z].push_back(x);
g[x].push_back(z);
}
while (q--) {
cin >> z >> x;
a[z] += x;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
}
| #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const double PI = acos(-1.0);
const int N = 200005;
const int MOD = 1e9 + 7;
const int inf = 1 << 28;
typedef long long ll;
vector<int> g[N];
int a[N], ans[N];
bool vis[N];
void dfs(int x, int c) {
if (vis[x])
return;
vis[x] = 1;
ans[x] += a[x] + c;
for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i];
dfs(v, ans[x]);
}
}
int main() {
/* #ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif*/
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q, z, x;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> z >> x;
g[z].push_back(x);
g[x].push_back(z);
}
while (q--) {
cin >> z >> x;
a[z] += x;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
}
| replace | 32 | 36 | 32 | 36 | -11 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;
vector<int> v[100000];
int visited[100000] = {0};
int ans[100000] = {0};
void dfs(int x) {
visited[x] += 1;
for (int i = 0; i < v[x].size(); i++) {
int nx = v[x][i];
if (visited[nx] == 0) {
ans[nx] += ans[x];
dfs(nx);
}
}
}
int main() {
int N, Q;
cin >> N >> Q;
rep(i, N - 1) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
rep(i, Q) {
int s, t;
cin >> s >> t;
s--;
ans[s] += t;
}
dfs(0);
rep(i, N) {
cout << ans[i];
if (i != N - 1)
cout << " ";
else
cout << endl;
}
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;
vector<int> v[200000];
int visited[200000] = {0};
int ans[200000] = {0};
void dfs(int x) {
visited[x] += 1;
for (int i = 0; i < v[x].size(); i++) {
int nx = v[x][i];
if (visited[nx] == 0) {
ans[nx] += ans[x];
dfs(nx);
}
}
}
int main() {
int N, Q;
cin >> N >> Q;
rep(i, N - 1) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
rep(i, Q) {
int s, t;
cin >> s >> t;
s--;
ans[s] += t;
}
dfs(0);
rep(i, N) {
cout << ans[i];
if (i != N - 1)
cout << " ";
else
cout << endl;
}
return 0;
} | replace | 20 | 23 | 20 | 23 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define watch(x) cout << (#x) << "=" << (x) << endl
#define mset(d, val) memset(d, val, sizeof(d))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define F first
#define S second
#define PI 3.14159265358979323846264338327
#define INF 2e14
#define MOD 998244353
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
#define MAXN 100005
class LazySegmentTree {
private:
int size_;
vector<ll> v, lazy;
void update(int s, int e, ll val, int k, int l, int r) {
push(k, l, r);
if (r < s || e < l)
return;
if (s <= l && r <= e) {
lazy[k] += val;
push(k, l, r);
} else {
update(s, e, val, k * 2, l, (l + r) >> 1);
update(s, e, val, k * 2 + 1, ((l + r) >> 1) + 1, r);
v[k] = merge(v[k * 2], v[k * 2 + 1]);
}
}
ll query(int s, int e, int k, int l, int r) {
push(k, l, r);
if (r < s || e < l)
return 0;
if (s <= l && r <= e)
return v[k];
ll lc = query(s, e, k * 2, l, (l + r) >> 1);
ll rc = query(s, e, k * 2 + 1, ((l + r) >> 1) + 1, r);
return merge(lc, rc);
}
public:
LazySegmentTree() : v(vector<ll>()), lazy(vector<ll>()){};
LazySegmentTree(int n) {
for (size_ = 1; size_ < n;)
size_ <<= 1;
v.resize(size_ * 4);
lazy.resize(size_ * 4);
}
inline void push(int k, int l, int r) {
if (lazy[k] != 0) {
v[k] += lazy[k];
if (l != r) {
lazy[k * 2] += lazy[k];
lazy[k * 2 + 1] += lazy[k];
}
lazy[k] = 0;
}
}
inline ll merge(ll x, ll y) { return x + y; }
inline void update(int l, int r, ll val) {
update(l, r, val, 1, 0, size_ - 1);
}
inline ll query(int l, int r) { return query(l, r, 1, 0, size_ - 1); }
};
int n, q;
vi adj[MAXN];
ll arr[MAXN];
int in[MAXN], out[MAXN];
int tmr = 0;
void dfs(int u, int p) {
in[u] = tmr++;
for (int v : adj[u]) {
if (v == p)
continue;
dfs(v, u);
}
out[u] = tmr;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
LazySegmentTree st(n);
forn(i, 0, n - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(0, -1);
forn(it, 0, q) {
int u;
ll inc;
cin >> u >> inc;
u--;
st.update(in[u], out[u] - 1, inc);
}
forn(i, 0, n) { cout << st.query(in[i], in[i]) << " "; }
cout << '\n';
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define watch(x) cout << (#x) << "=" << (x) << endl
#define mset(d, val) memset(d, val, sizeof(d))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define F first
#define S second
#define PI 3.14159265358979323846264338327
#define INF 2e14
#define MOD 998244353
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
#define MAXN 200005
class LazySegmentTree {
private:
int size_;
vector<ll> v, lazy;
void update(int s, int e, ll val, int k, int l, int r) {
push(k, l, r);
if (r < s || e < l)
return;
if (s <= l && r <= e) {
lazy[k] += val;
push(k, l, r);
} else {
update(s, e, val, k * 2, l, (l + r) >> 1);
update(s, e, val, k * 2 + 1, ((l + r) >> 1) + 1, r);
v[k] = merge(v[k * 2], v[k * 2 + 1]);
}
}
ll query(int s, int e, int k, int l, int r) {
push(k, l, r);
if (r < s || e < l)
return 0;
if (s <= l && r <= e)
return v[k];
ll lc = query(s, e, k * 2, l, (l + r) >> 1);
ll rc = query(s, e, k * 2 + 1, ((l + r) >> 1) + 1, r);
return merge(lc, rc);
}
public:
LazySegmentTree() : v(vector<ll>()), lazy(vector<ll>()){};
LazySegmentTree(int n) {
for (size_ = 1; size_ < n;)
size_ <<= 1;
v.resize(size_ * 4);
lazy.resize(size_ * 4);
}
inline void push(int k, int l, int r) {
if (lazy[k] != 0) {
v[k] += lazy[k];
if (l != r) {
lazy[k * 2] += lazy[k];
lazy[k * 2 + 1] += lazy[k];
}
lazy[k] = 0;
}
}
inline ll merge(ll x, ll y) { return x + y; }
inline void update(int l, int r, ll val) {
update(l, r, val, 1, 0, size_ - 1);
}
inline ll query(int l, int r) { return query(l, r, 1, 0, size_ - 1); }
};
int n, q;
vi adj[MAXN];
ll arr[MAXN];
int in[MAXN], out[MAXN];
int tmr = 0;
void dfs(int u, int p) {
in[u] = tmr++;
for (int v : adj[u]) {
if (v == p)
continue;
dfs(v, u);
}
out[u] = tmr;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
LazySegmentTree st(n);
forn(i, 0, n - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(0, -1);
forn(it, 0, q) {
int u;
ll inc;
cin >> u >> inc;
u--;
st.update(in[u], out[u] - 1, inc);
}
forn(i, 0, n) { cout << st.query(in[i], in[i]) << " "; }
cout << '\n';
return 0;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
using namespace std;
void dfs(ll int u, ll int p, ll int c, vector<vector<ll int>> adj,
vector<ll int> pcost, vector<ll int> &cost, ll int n) {
cost[u] = pcost[u] + c;
for (auto v : adj[u]) {
if (v == p)
continue;
dfs(v, u, cost[u], adj, pcost, cost, n);
}
return;
}
int main() {
ll int n, m, x, y;
cin >> n >> m;
vector<vector<ll int>> adj(n + 1);
for (ll int i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<ll int> pcost(n + 1, 0);
for (ll int i = 0; i < m; i++) {
cin >> x >> y;
pcost[x] += y;
}
vector<ll int> cost(n + 1, 0);
dfs(1LL, -1LL, 0LL, adj, pcost, cost, n);
for (ll int i = 1; i <= n; i++)
cout << cost[i] << " ";
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
void dfs(ll int u, ll int p, ll int c, vector<vector<ll int>> &adj,
vector<ll int> &pcost, vector<ll int> &cost, ll int n) {
cost[u] = pcost[u] + c;
for (auto v : adj[u]) {
if (v == p)
continue;
dfs(v, u, cost[u], adj, pcost, cost, n);
}
return;
}
int main() {
ll int n, m, x, y;
cin >> n >> m;
vector<vector<ll int>> adj(n + 1);
for (ll int i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<ll int> pcost(n + 1, 0);
for (ll int i = 0; i < m; i++) {
cin >> x >> y;
pcost[x] += y;
}
vector<ll int> cost(n + 1, 0);
dfs(1LL, -1LL, 0LL, adj, pcost, cost, n);
for (ll int i = 1; i <= n; i++)
cout << cost[i] << " ";
} | replace | 3 | 5 | 3 | 5 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (ll)1e18
#define all(x) (x).begin(), (x).end()
#define print(a) \
; \
for (auto x : a) \
cout << x << " "; \
cout << "\n";
#define mset(a) \
; \
memset(a, 0, sizeof(a));
const ll N = 1e5 + 5;
ll n, q;
vector<vi> v(N);
ll val[N];
void dfs(ll a, ll b) {
val[a] += val[b];
for (auto x : v[a]) {
if (x != b)
dfs(x, a);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
mset(val);
for (int i = 0; i < n - 1; i++) {
ll a, b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
for (int i = 0; i < q; i++) {
ll a, b;
cin >> a >> b;
val[a] += b;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << val[i] << " ";
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (ll)1e18
#define all(x) (x).begin(), (x).end()
#define print(a) \
; \
for (auto x : a) \
cout << x << " "; \
cout << "\n";
#define mset(a) \
; \
memset(a, 0, sizeof(a));
const ll N = 2e5 + 5;
ll n, q;
vector<vi> v(N);
ll val[N];
void dfs(ll a, ll b) {
val[a] += val[b];
for (auto x : v[a]) {
if (x != b)
dfs(x, a);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
mset(val);
for (int i = 0; i < n - 1; i++) {
ll a, b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
for (int i = 0; i < q; i++) {
ll a, b;
cin >> a >> b;
val[a] += b;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << val[i] << " ";
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int res[100005];
int c[100005];
bool vist[100005];
vector<int> adj[100005];
void bfs(int root, int sum) {
queue<pii> q;
q.push(make_pair(root, sum));
while (!q.empty()) {
pii e = q.front();
q.pop();
int u = e.first;
int s = e.second;
int m = adj[u].size();
s += c[u];
res[u] = s;
vist[u] = true;
for (int k = 0; k < m; ++k) {
int v = adj[u][k];
if (!vist[v]) {
q.push(make_pair(v, s));
}
}
}
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d %d", &u, &v);
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
for (int i = 0; i < q; ++i) {
int p, x;
scanf("%d %d", &p, &x);
c[p - 1] += x;
}
bfs(0, 0);
for (int i = 0; i < n; ++i) {
printf("%d", res[i]);
if (i == n - 1) {
printf("\n");
} else {
printf(" ");
}
}
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int res[200005];
int c[200005];
bool vist[200005];
vector<int> adj[200005];
void bfs(int root, int sum) {
queue<pii> q;
q.push(make_pair(root, sum));
while (!q.empty()) {
pii e = q.front();
q.pop();
int u = e.first;
int s = e.second;
int m = adj[u].size();
s += c[u];
res[u] = s;
vist[u] = true;
for (int k = 0; k < m; ++k) {
int v = adj[u][k];
if (!vist[v]) {
q.push(make_pair(v, s));
}
}
}
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d %d", &u, &v);
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
for (int i = 0; i < q; ++i) {
int p, x;
scanf("%d %d", &p, &x);
c[p - 1] += x;
}
bfs(0, 0);
for (int i = 0; i < n; ++i) {
printf("%d", res[i]);
if (i == n - 1) {
printf("\n");
} else {
printf(" ");
}
}
return 0;
}
| replace | 20 | 24 | 20 | 24 | 0 | |
p02936 | C++ | Runtime Error | /**
* created: 11.05.2020 23:51:19
**/
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
} | /**
* created: 11.05.2020 23:51:19
**/
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
} | insert | 37 | 37 | 37 | 38 | -11 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
} | replace | 18 | 19 | 18 | 19 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, q;
int par[200000];
int ran[200000];
vector<int> ch[200000];
long long dp[200000];
int y[200000];
bool used[200000];
void dfs(int now, int mow) {
for (int i = 0; i < ch[now].size(); i++) {
if (par[ch[now][i]] == -1) {
par[ch[now][i]] = now;
ran[ch[now][i]] = mow + 1;
dfs(now, mow + 1);
}
}
return;
}
void solve(int now, int mow) {
used[now] = 1;
dp[now] += mow;
dp[now] += y[now];
for (int i = 0; i < ch[now].size(); i++) {
if (used[ch[now][i]]) {
continue;
}
solve(ch[now][i], dp[now]);
}
}
int main() {
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
par[i] = -1;
cin >> a >> b;
a--;
b--;
ch[a].push_back(b);
ch[b].push_back(a);
}
par[0] = 1e9 + 7;
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
p--;
y[p] += x;
}
//
dfs(0, 0);
solve(0, 0);
for (int i = 0; i < n; i++) {
cout << dp[i];
if (i != n - 1) {
cout << " ";
} else {
cout << endl;
}
}
return 0;
} | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, q;
int par[200000];
int ran[200000];
vector<int> ch[200000];
long long dp[200000];
int y[200000];
bool used[200000];
void dfs(int now, int mow) {
for (int i = 0; i < ch[now].size(); i++) {
if (par[ch[now][i]] == -1) {
par[ch[now][i]] = now;
ran[ch[now][i]] = mow + 1;
dfs(ch[now][i], mow + 1);
}
}
return;
}
void solve(int now, int mow) {
used[now] = 1;
dp[now] += mow;
dp[now] += y[now];
for (int i = 0; i < ch[now].size(); i++) {
if (used[ch[now][i]]) {
continue;
}
solve(ch[now][i], dp[now]);
}
}
int main() {
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
par[i] = -1;
cin >> a >> b;
a--;
b--;
ch[a].push_back(b);
ch[b].push_back(a);
}
par[0] = 1e9 + 7;
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
p--;
y[p] += x;
}
//
dfs(0, 0);
solve(0, 0);
for (int i = 0; i < n; i++) {
cout << dp[i];
if (i != n - 1) {
cout << " ";
} else {
cout << endl;
}
}
return 0;
} | replace | 21 | 22 | 21 | 22 | TLE | |
p02936 | 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;
const int INF = 1001001001;
vector<int> tos[20005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : tos[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
tos[a].push_back(b);
tos[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << ' '; }
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
const int INF = 1001001001;
vector<int> tos[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : tos[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
tos[a].push_back(b);
tos[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << ' '; }
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string.h>
#include <string>
#include <vector>
#define LOOP(N) for (int i = 0; i < (N); ++i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;
struct Node {
int num;
vector<int> children;
int add;
int counter;
Node() {
counter = 0;
add = 0;
}
};
void add_all(vector<Node> &nodes, int p, int add = 0) {
nodes[p].add += add;
nodes[p].counter += nodes[p].add;
for (auto child : nodes[p].children) {
add_all(nodes, child, nodes[p].add);
}
nodes[p].add = 0;
}
int main() {
int N, Q;
cin >> N >> Q;
vector<double> v(N, 0);
vector<Node> nodes(N);
REP(i, N) nodes[i].num = i;
int a, b;
FOR(i, 1, N) {
cin >> a >> b;
nodes[a - 1].children.push_back(b - 1);
}
int p, x;
REP(i, Q) {
cin >> p >> x;
nodes[p - 1].add += x;
}
REP(i, N) add_all(nodes, i);
cout << nodes[0].counter;
FOR(i, 1, N) cout << " " << nodes[i].counter;
}
| #include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string.h>
#include <string>
#include <vector>
#define LOOP(N) for (int i = 0; i < (N); ++i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;
struct Node {
int num;
vector<int> children;
int add;
int counter;
Node() {
counter = 0;
add = 0;
}
};
void add_all(vector<Node> &nodes, int p, int add = 0) {
nodes[p].add += add;
if (nodes[p].add == 0)
return;
nodes[p].counter += nodes[p].add;
for (auto child : nodes[p].children) {
add_all(nodes, child, nodes[p].add);
}
nodes[p].add = 0;
}
int main() {
int N, Q;
cin >> N >> Q;
vector<double> v(N, 0);
vector<Node> nodes(N);
REP(i, N) nodes[i].num = i;
int a, b;
FOR(i, 1, N) {
cin >> a >> b;
nodes[a - 1].children.push_back(b - 1);
}
int p, x;
REP(i, Q) {
cin >> p >> x;
nodes[p - 1].add += x;
}
REP(i, N) add_all(nodes, i);
cout << nodes[0].counter;
FOR(i, 1, N) cout << " " << nodes[i].counter;
}
| insert | 35 | 35 | 35 | 37 | TLE | |
p02936 | Python | Time Limit Exceeded | import sys
sys.setrecursionlimit(3 * 10**5)
def main() -> None:
def dfs(v, par, cnt):
ans[v] = cnt + vs[v]
for c in edges[v]:
if c == par:
continue
dfs(c, v, ans[v])
N, Q = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
a, b = a - 1, b - 1
edges[a].append(b)
edges[b].append(a)
vs = [0] * N
for _ in range(Q):
p, x = map(int, input().split())
vs[p - 1] += x
ans = [0] * N
dfs(0, -1, 0)
print(*ans)
if __name__ == "__main__":
main()
| import sys
sys.setrecursionlimit(3 * 10**5)
input = sys.stdin.buffer.readline
def main() -> None:
def dfs(v, par, cnt):
ans[v] = cnt + vs[v]
for c in edges[v]:
if c == par:
continue
dfs(c, v, ans[v])
N, Q = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
a, b = a - 1, b - 1
edges[a].append(b)
edges[b].append(a)
vs = [0] * N
for _ in range(Q):
p, x = map(int, input().split())
vs[p - 1] += x
ans = [0] * N
dfs(0, -1, 0)
print(*ans)
if __name__ == "__main__":
main()
| insert | 3 | 3 | 3 | 4 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
cout << "-1" << endl; \
return 0
#define MM << " " <<
#define Endl endl
signed main() {
const int MEM = 60;
int N, Q;
int A[MEM], B[MEM];
int p[MEM], x[MEM];
vector<int> G[MEM];
cin >> N >> Q;
REP(i, N - 1) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
G[A[i]].push_back(B[i]);
G[B[i]].push_back(A[i]);
}
REP(i, Q) {
cin >> p[i] >> x[i];
p[i]--;
}
queue<int> P;
P.push(0);
int d[214514] = {};
bool visit[214514] = {};
REP(i, Q) { d[p[i]] += x[i]; }
while (!P.empty()) {
int T = P.front();
P.pop();
visit[T] = true;
REP(i, G[T].size()) {
if (!visit[G[T][i]]) {
P.push(G[T][i]);
d[G[T][i]] += d[T];
}
}
}
REP(i, N) {
if (i != 0)
cout << " ";
cout << d[i];
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
cout << "-1" << endl; \
return 0
#define MM << " " <<
#define Endl endl
signed main() {
const int MEM = 214514;
int N, Q;
int A[MEM], B[MEM];
int p[MEM], x[MEM];
vector<int> G[MEM];
cin >> N >> Q;
REP(i, N - 1) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
G[A[i]].push_back(B[i]);
G[B[i]].push_back(A[i]);
}
REP(i, Q) {
cin >> p[i] >> x[i];
p[i]--;
}
queue<int> P;
P.push(0);
int d[214514] = {};
bool visit[214514] = {};
REP(i, Q) { d[p[i]] += x[i]; }
while (!P.empty()) {
int T = P.front();
P.pop();
visit[T] = true;
REP(i, G[T].size()) {
if (!visit[G[T][i]]) {
P.push(G[T][i]);
d[G[T][i]] += d[T];
}
}
}
REP(i, N) {
if (i != 0)
cout << " ";
cout << d[i];
}
cout << endl;
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
vector<int> graph[250000];
bool besucht[250000];
ll query[250000];
ll res[250000];
void dfs(int p, ll sum) {
// if( besucht[p] ) return;
besucht[p] = true;
res[p] = sum;
for (auto next : graph[p]) {
dfs(next, sum + query[next]);
}
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 0; i < q; ++i) {
int p;
ll x;
cin >> p >> x;
query[p - 1] += x;
}
dfs(0, query[0]);
for (int i = 0; i < n; ++i) {
cout << res[i] << ' ';
}
cout << endl;
return 0;
} | #include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
vector<int> graph[250000];
bool besucht[250000];
ll query[250000];
ll res[250000];
void dfs(int p, ll sum) {
// if( besucht[p] ) return;
besucht[p] = true;
res[p] = sum;
for (auto next : graph[p]) {
dfs(next, sum + query[next]);
}
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
graph[a].push_back(b);
}
for (int i = 0; i < q; ++i) {
int p;
ll x;
cin >> p >> x;
query[p - 1] += x;
}
dfs(0, query[0]);
for (int i = 0; i < n; ++i) {
cout << res[i] << ' ';
}
cout << endl;
return 0;
} | delete | 30 | 31 | 30 | 30 | -11 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f3f;
const int maxn = 1e5 + 500;
ll T, n, m;
vector<ll> num[maxn];
ll numb[maxn];
ll f[maxn];
int main() {
cin >> n >> m;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
num[a].push_back(b);
f[b] = a;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
numb[a] += b;
}
ll sum = 0;
for (int i = 1; i < n + 1; i++) {
numb[i] += numb[f[i]];
cout << numb[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f3f;
const int maxn = 2e5 + 500;
ll T, n, m;
vector<ll> num[maxn];
ll numb[maxn];
ll f[maxn];
int main() {
cin >> n >> m;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
num[a].push_back(b);
f[b] = a;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
numb[a] += b;
}
ll sum = 0;
for (int i = 1; i < n + 1; i++) {
numb[i] += numb[f[i]];
cout << numb[i] << endl;
}
return 0;
}
| replace | 20 | 21 | 20 | 21 | 0 | |
p02936 | 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;
vector<vector<int>> v(1000000);
vector<int> ans(1000000, 0);
void rec(int i, int j = -1) {
for (int k : v[i]) {
if (k == j)
continue;
ans[k] += ans[i];
rec(k, j);
}
}
void solve() {
int n, q;
cin >> n >> q;
v.resize(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
rec(0);
rep(i, n) cout << ans[i] << " ";
}
int main() {
solve();
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
vector<vector<int>> v(1000000);
vector<int> ans(1000000, 0);
void rec(int i, int j = -1) {
for (int k : v[i]) {
if (k == j)
continue;
ans[k] += ans[i];
rec(k, i);
}
}
void solve() {
int n, q;
cin >> n >> q;
v.resize(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
rec(0);
rep(i, n) cout << ans[i] << " ";
}
int main() {
solve();
return 0;
} | replace | 12 | 13 | 12 | 13 | -11 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
const int MAX = 510000;
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;
stack<int> ANSAN[200002];
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int ANS;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
rep(i, n) cout << ans[i] << " ";
cout << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
const int MAX = 510000;
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;
stack<int> ANSAN[200002];
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int ANS;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
rep(i, n) cout << ans[i] << " ";
cout << endl;
} | insert | 48 | 48 | 48 | 49 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02936 | C++ | Runtime Error | using namespace std;
#include <bits/stdc++.h>
#define st first
#define nd second
#define FORE(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define FORD(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define TR(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
const int MAXN = 1e5 + 1;
long long f[MAXN];
vector<int> g[MAXN];
int n, m;
void dfs(int u, int parent) {
TR(g[u], it) {
int v = *it;
if (v == parent)
continue;
f[v] += f[u];
dfs(v, u);
}
}
int main() {
#define TASK "NAME"
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
f[i] = 0;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= m; ++i) {
int u, p;
cin >> u >> p;
f[u] += p;
}
dfs(1, -1);
for (int i = 1; i <= n; ++i)
cout << f[i] << " ";
return 0;
}
| using namespace std;
#include <bits/stdc++.h>
#define st first
#define nd second
#define FORE(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define FORD(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define TR(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
const int MAXN = 2e5 + 1;
long long f[MAXN];
vector<int> g[MAXN];
int n, m;
void dfs(int u, int parent) {
TR(g[u], it) {
int v = *it;
if (v == parent)
continue;
f[v] += f[u];
dfs(v, u);
}
}
int main() {
#define TASK "NAME"
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
f[i] = 0;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= m; ++i) {
int u, p;
cin >> u >> p;
f[u] += p;
}
dfs(1, -1);
for (int i = 1; i <= n; ++i)
cout << f[i] << " ";
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test \
int t; \
cin >> t; \
while (t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007
vi adj[123456];
bool vis[123456];
int var[123456];
int ans[123456];
void dfs(int u) {
vis[u] = 1;
int c = 1;
for (int x : adj[u]) {
if (!vis[x]) {
ans[x] += ans[u];
dfs(x);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
//_______________________________-code
//starts-_______________________________________________
int n, q;
cin >> n >> q;
zip(i, 0, n - 1) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
zip(i, 0, q) {
int a, b;
cin >> a >> b;
var[a] += b;
ans[a] += b;
}
dfs(1);
rep(i, 1, n) { cout << ans[i] << " "; }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test \
int t; \
cin >> t; \
while (t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007
vi adj[1234567];
bool vis[1234567];
int var[1234567];
int ans[1234567];
void dfs(int u) {
vis[u] = 1;
int c = 1;
for (int x : adj[u]) {
if (!vis[x]) {
ans[x] += ans[u];
dfs(x);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
//_______________________________-code
//starts-_______________________________________________
int n, q;
cin >> n >> q;
zip(i, 0, n - 1) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
zip(i, 0, q) {
int a, b;
cin >> a >> b;
var[a] += b;
ans[a] += b;
}
dfs(1);
rep(i, 1, n) { cout << ans[i] << " "; }
return 0;
}
| replace | 36 | 40 | 36 | 40 | 0 | |
p02936 | C++ | Runtime Error | /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Code author: Saurabh Singhal
Code copy karne vaale tera muh kaala
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ppb pop_back
using namespace std;
typedef long long int ll;
typedef vector<int> vi_t;
typedef vector<ll> vll_t;
int t = 1;
vll_t a, b;
vector<int> grp[200001];
int val[100001];
int main() {
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen("input.txt", "r", stdin);
// // for writing output to output.txt
// freopen("output.txt", "w", stdout);
// #endif
memset(val, 0, sizeof(val));
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
grp[u].push_back(v);
}
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
val[p] += x;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < grp[i].size(); j++) {
val[grp[i][j]] += val[i];
}
}
for (int i = 1; i <= n; i++) {
cout << val[i] << " ";
}
cout << endl;
} | /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Code author: Saurabh Singhal
Code copy karne vaale tera muh kaala
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ppb pop_back
using namespace std;
typedef long long int ll;
typedef vector<int> vi_t;
typedef vector<ll> vll_t;
int t = 1;
vll_t a, b;
vector<int> grp[200005];
int val[200005];
int main() {
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen("input.txt", "r", stdin);
// // for writing output to output.txt
// freopen("output.txt", "w", stdout);
// #endif
memset(val, 0, sizeof(val));
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
grp[u].push_back(v);
}
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
val[p] += x;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < grp[i].size(); j++) {
val[grp[i][j]] += val[i];
}
}
for (int i = 1; i <= n; i++) {
cout << val[i] << " ";
}
cout << endl;
} | replace | 16 | 18 | 16 | 18 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
vector<int> to[100005];
int cnt[100005], ans[100005];
void dfs(int x, int fa, int sum) {
// cout<<sum<<' '<<x<<endl;
sum += cnt[x];
ans[x] = sum;
if (to[x].size() == 1 && fa != 0)
return;
for (int i = 0; i < to[x].size(); i++) {
int v = to[x][i];
if (v == fa)
continue;
dfs(v, x, sum);
}
return;
}
int main() {
int n;
cin >> n;
int q;
cin >> q;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
to[a].push_back(b);
to[b].push_back(a);
}
for (int i = 1; i <= q; i++) {
int p, x;
cin >> p >> x;
cnt[p] += x;
}
dfs(1, 0, 0);
for (int i = 1; i <= n; i++) {
cout << ans[i] << ' ';
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
vector<int> to[200005];
int cnt[200005], ans[200005];
void dfs(int x, int fa, int sum) {
// cout<<sum<<' '<<x<<endl;
sum += cnt[x];
ans[x] = sum;
if (to[x].size() == 1 && fa != 0)
return;
for (int i = 0; i < to[x].size(); i++) {
int v = to[x][i];
if (v == fa)
continue;
dfs(v, x, sum);
}
return;
}
int main() {
int n;
cin >> n;
int q;
cin >> q;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
to[a].push_back(b);
to[b].push_back(a);
}
for (int i = 1; i <= q; i++) {
int p, x;
cin >> p >> x;
cnt[p] += x;
}
dfs(1, 0, 0);
for (int i = 1; i <= n; i++) {
cout << ans[i] << ' ';
}
return 0;
} | replace | 10 | 12 | 10 | 12 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> g[20000 + 5];
int a[20000 + 5];
int f(int u, int p, int sum) {
a[u] += sum;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == p) {
continue;
}
f(v, u, a[u]);
}
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
while (q--) {
int x, y;
cin >> x >> y;
a[x - 1] += y;
}
f(0, -1, 0);
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
vector<int> g[200000 + 1];
int a[200000 + 1];
int f(int u, int p, int sum) {
a[u] += sum;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == p) {
continue;
}
f(v, u, a[u]);
}
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
while (q--) {
int x, y;
cin >> x >> y;
a[x - 1] += y;
}
f(0, -1, 0);
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
} | replace | 4 | 6 | 4 | 6 | 0 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define lli long long int
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q, i, j;
cin >> n >> q;
vector<vector<int>> adj;
adj.resize(n);
lli a, b;
for (i = 0; i < n - 1; i++) {
cin >> a >> b;
adj[a - 1].push_back(b - 1);
adj[b - 1].push_back(a - 1);
}
vector<lli> val;
val.resize(n);
for (i = 0; i < q; i++) {
cin >> a >> b;
val[a - 1] += b;
}
vector<int> par;
for (i = 0; i < n; i++)
par[i] = -1;
par[0] = 0;
queue<int> q1;
q1.push(0);
while (!q1.empty()) {
int x = q1.front();
q1.pop();
for (i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
if (par[y] == -1) {
par[y] = x;
val[y] += val[x];
q1.push(y);
}
}
}
for (i = 0; i < n; i++)
cout << val[i] << " ";
cout << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define lli long long int
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q, i, j;
cin >> n >> q;
vector<vector<int>> adj;
adj.resize(n);
lli a, b;
for (i = 0; i < n - 1; i++) {
cin >> a >> b;
adj[a - 1].push_back(b - 1);
adj[b - 1].push_back(a - 1);
}
vector<lli> val;
val.resize(n);
for (i = 0; i < q; i++) {
cin >> a >> b;
val[a - 1] += b;
}
vector<int> par;
par.resize(n);
for (i = 0; i < n; i++)
par[i] = -1;
par[0] = 0;
queue<int> q1;
q1.push(0);
while (!q1.empty()) {
int x = q1.front();
q1.pop();
for (i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
if (par[y] == -1) {
par[y] = x;
val[y] += val[x];
q1.push(y);
}
}
}
for (i = 0; i < n; i++)
cout << val[i] << " ";
cout << endl;
return 0;
} | insert | 25 | 25 | 25 | 26 | -11 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int SIZE = 100024;
int N, Q;
int p[SIZE];
long long cnt[SIZE];
long long sum[SIZE];
long long Solve(int t) {
if (t == 1)
return cnt[1];
if (sum[t] < 0) {
sum[t] = Solve(p[t]) + cnt[t];
}
return sum[t];
}
int main() {
while (cin >> N >> Q) {
memset(cnt, 0, sizeof(cnt));
memset(sum, -1, sizeof(sum));
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
p[b] = a;
}
for (int i = 0; i < Q; ++i) {
int t, x;
cin >> t >> x;
cnt[t] += x;
}
for (int i = 1; i <= N; ++i) {
if (i != 1)
cout << " ";
cout << Solve(i);
}
cout << endl;
}
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int SIZE = 200024;
int N, Q;
int p[SIZE];
long long cnt[SIZE];
long long sum[SIZE];
long long Solve(int t) {
if (t == 1)
return cnt[1];
if (sum[t] < 0) {
sum[t] = Solve(p[t]) + cnt[t];
}
return sum[t];
}
int main() {
while (cin >> N >> Q) {
memset(cnt, 0, sizeof(cnt));
memset(sum, -1, sizeof(sum));
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
p[b] = a;
}
for (int i = 0; i < Q; ++i) {
int t, x;
cin >> t >> x;
cnt[t] += x;
}
for (int i = 1; i <= N; ++i) {
if (i != 1)
cout << " ";
cout << Solve(i);
}
cout << endl;
}
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
vi adj[100005];
int a[100005] = {0}, visited[100005] = {0};
void dfs(int node) {
visited[node] = 1;
for (int i = 0; i < adj[node].size(); i++) {
if (visited[adj[node][i]] == 0) {
a[adj[node][i]] += a[node];
dfs(adj[node][i]);
}
}
}
int main() {
ios
int n,
q, x, y;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
while (q--) {
cin >> x >> y;
a[x] += y;
}
dfs(1);
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
}
| #include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
vi adj[200005];
int a[200005] = {0}, visited[200005] = {0};
void dfs(int node) {
visited[node] = 1;
for (int i = 0; i < adj[node].size(); i++) {
if (visited[adj[node][i]] == 0) {
a[adj[node][i]] += a[node];
dfs(adj[node][i]);
}
}
}
int main() {
ios
int n,
q, x, y;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
while (q--) {
cin >> x >> y;
a[x] += y;
}
dfs(1);
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
}
| replace | 17 | 19 | 17 | 19 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);
const int MAX_N = (int)(1e5 + 5);
int n, q;
vector<int> graph[MAX_N];
ll lazy[MAX_N];
ll actual[MAX_N];
int main(void) {
// Here your code !
scanf("%d %d", &n, &q);
for (int i = 1; i <= n - 1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 1; i <= q; ++i) {
int p;
ll x;
scanf("%d %lld", &p, &x);
lazy[p] += x;
}
queue<tuple<int, int, ll>> que;
que.push(make_tuple(1, 0, 0LL));
while (!que.empty()) {
auto cur = que.front();
que.pop();
int node = get<0>(cur);
int parent = get<1>(cur);
ll acc = get<2>(cur);
acc += lazy[node];
actual[node] += acc;
for (auto &e : graph[node]) {
if (e == parent)
continue;
que.push(make_tuple(e, node, acc));
}
}
for (int i = 1; i <= n; ++i) {
printf("%lld ", actual[i]);
}
printf("\n");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);
const int MAX_N = (int)(2e5 + 5);
int n, q;
vector<int> graph[MAX_N];
ll lazy[MAX_N];
ll actual[MAX_N];
int main(void) {
// Here your code !
scanf("%d %d", &n, &q);
for (int i = 1; i <= n - 1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 1; i <= q; ++i) {
int p;
ll x;
scanf("%d %lld", &p, &x);
lazy[p] += x;
}
queue<tuple<int, int, ll>> que;
que.push(make_tuple(1, 0, 0LL));
while (!que.empty()) {
auto cur = que.front();
que.pop();
int node = get<0>(cur);
int parent = get<1>(cur);
ll acc = get<2>(cur);
acc += lazy[node];
actual[node] += acc;
for (auto &e : graph[node]) {
if (e == parent)
continue;
que.push(make_tuple(e, node, acc));
}
}
for (int i = 1; i <= n; ++i) {
printf("%lld ", actual[i]);
}
printf("\n");
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> graph[100000];
vector<int> counter(100000, 0);
vector<int> ans(100000, 0);
void dfs(int n, int c) {
ans[n] = c + counter[n];
for (auto t : graph[n]) {
dfs(t, ans[n]);
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
}
for (int i = 0; i < Q; i++) {
int p, x;
cin >> p >> x;
p--;
counter[p] += x;
}
set<int> in;
for (int i = 0; i < N; i++)
in.insert(i);
for (int i = 0; i < N; i++) {
for (auto ee : graph[i]) {
in.erase(ee);
}
}
for (int i = 0; i < N; i++) {
if (in.count(i) == 0)
continue;
dfs(i, 0);
}
for (int i = 0; i < N; i++) {
cout << ans[i] << ' ';
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
vector<int> graph[200000];
vector<int> counter(200000, 0);
vector<int> ans(200000, 0);
void dfs(int n, int c) {
ans[n] = c + counter[n];
for (auto t : graph[n]) {
dfs(t, ans[n]);
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
}
for (int i = 0; i < Q; i++) {
int p, x;
cin >> p >> x;
p--;
counter[p] += x;
}
set<int> in;
for (int i = 0; i < N; i++)
in.insert(i);
for (int i = 0; i < N; i++) {
for (auto ee : graph[i]) {
in.erase(ee);
}
}
for (int i = 0; i < N; i++) {
if (in.count(i) == 0)
continue;
dfs(i, 0);
}
for (int i = 0; i < N; i++) {
cout << ans[i] << ' ';
}
cout << endl;
return 0;
}
| replace | 4 | 7 | 4 | 7 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
ll N, Q;
vector<ll> G[200000];
vector<ll> result(20000, 0);
vector<bool> F(200000, false);
void dfs(ll v, ll p = -1) {
if (!F[v]) {
F[v] = true;
if (p != -1) {
result[v] += result[p];
}
rep(i, G[v].size()) {
if (!F[G[v][i]])
dfs(G[v][i], v);
}
}
}
int main() {
cin >> N >> Q;
F.resize(N);
rep(i, N - 1) {
ll p, c;
cin >> p >> c;
G[p - 1].push_back(c - 1);
G[c - 1].push_back(p - 1);
}
rep(i, Q) {
ll p, x;
cin >> p >> x;
result[p - 1] += x;
}
dfs(0);
rep(i, N) cout << result[i] << ' ';
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
ll N, Q;
vector<ll> G[200000];
vector<ll> result(200000, 0);
vector<bool> F(200000, false);
void dfs(ll v, ll p = -1) {
if (!F[v]) {
F[v] = true;
if (p != -1) {
result[v] += result[p];
}
rep(i, G[v].size()) {
if (!F[G[v][i]])
dfs(G[v][i], v);
}
}
}
int main() {
cin >> N >> Q;
F.resize(N);
rep(i, N - 1) {
ll p, c;
cin >> p >> c;
G[p - 1].push_back(c - 1);
G[c - 1].push_back(p - 1);
}
rep(i, Q) {
ll p, x;
cin >> p >> x;
result[p - 1] += x;
}
dfs(0);
rep(i, N) cout << result[i] << ' ';
cout << endl;
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02936 | C++ | Runtime Error | #pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma region UnionFind
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
vector<vector<int>> v(100100);
vector<int> imos(100100, 0);
void dfs(int now, int p, int counter) {
imos[now] += counter;
for (auto next : v[now]) {
if (next == p)
continue;
dfs(next, now, imos[now]);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
imos[p] += x;
}
dfs(1, 0, 0);
for (int i = 1; i <= n; ++i)
cout << imos[i] << " ";
} | #pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma region UnionFind
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
vector<vector<int>> v(200100);
vector<int> imos(200100, 0);
void dfs(int now, int p, int counter) {
imos[now] += counter;
for (auto next : v[now]) {
if (next == p)
continue;
dfs(next, now, imos[now]);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
imos[p] += x;
}
dfs(1, 0, 0);
for (int i = 1; i <= n; ++i)
cout << imos[i] << " ";
} | replace | 85 | 87 | 85 | 87 | 0 | |
p02936 | 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;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
// 典型問題、木を直線化する
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
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;
typedef long long ll;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
// 典型問題、木を直線化する
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
} | insert | 29 | 29 | 29 | 30 | -11 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, // less_equal
tree_order_statistics_node_update>
oset;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 2e3 + 2;
#define sz(x) (int)(x).size()
#define f first
vector<int> g[MAXN];
ll prop[MAXN], val[MAXN];
void dfs(int u, int p, ll cnt) {
val[u] = cnt;
for (auto &k : g[u]) {
if (k != p) {
dfs(k, u, cnt + prop[k]);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
prop[p] += x;
}
dfs(1, -1, prop[1]);
for (int i = 1; i <= n; i++)
cout << val[i] << ' ';
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, // less_equal
tree_order_statistics_node_update>
oset;
const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 2e3 + 2;
#define sz(x) (int)(x).size()
#define f first
vector<int> g[MAXN];
ll prop[MAXN], val[MAXN];
void dfs(int u, int p, ll cnt) {
val[u] = cnt;
for (auto &k : g[u]) {
if (k != p) {
dfs(k, u, cnt + prop[k]);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
prop[p] += x;
}
dfs(1, -1, prop[1]);
for (int i = 1; i <= n; i++)
cout << val[i] << ' ';
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M (1000000007)
#define N (100010)
int n, Q, ans[N];
vector<int> G[N], q[N];
void dfs(int x, int fa, int sum) {
for (int i = 0; i < q[x].size(); i++)
sum += q[x][i];
ans[x] = sum;
for (int i = 0; i < G[x].size(); i++) {
int y = G[x][i];
if (y == fa)
continue;
dfs(y, x, sum);
}
}
int main() {
scanf("%d%d", &n, &Q);
int x, y;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= Q; ++i) {
scanf("%d%d", &x, &y);
q[x].push_back(y);
}
dfs(1, 0, 0);
for (int i = 1; i <= n; ++i)
printf("%d ", ans[i]);
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M (1000000007)
#define N (200010)
int n, Q, ans[N];
vector<int> G[N], q[N];
void dfs(int x, int fa, int sum) {
for (int i = 0; i < q[x].size(); i++)
sum += q[x][i];
ans[x] = sum;
for (int i = 0; i < G[x].size(); i++) {
int y = G[x][i];
if (y == fa)
continue;
dfs(y, x, sum);
}
}
int main() {
scanf("%d%d", &n, &Q);
int x, y;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= Q; ++i) {
scanf("%d%d", &x, &y);
q[x].push_back(y);
}
dfs(1, 0, 0);
for (int i = 1; i <= n; ++i)
printf("%d ", ans[i]);
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int MAXQ = 1e5 + 10;
int n, q;
int a[MAXN], b[MAXN], p[MAXQ], x[MAXQ];
bool visited[MAXN];
ll cnt[MAXN];
vector<int> graph[MAXN];
void dfs(int cur) {
visited[cur] = true;
for (int i = 0; i < graph[cur].size(); i++) {
int next = graph[cur][i];
if (!visited[next]) {
cnt[next] += cnt[cur];
dfs(next);
}
}
}
void solve() {
for (int i = 0; i < n - 1; i++) {
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
for (int i = 0; i < q; i++) {
cnt[p[i]] += x[i];
}
dfs(0);
for (int i = 0; i < n; i++) {
if (i) {
cout << " ";
}
cout << cnt[i];
}
cout << endl;
}
int main() {
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int i = 0; i < q; i++) {
cin >> p[i] >> x[i];
p[i]--;
}
solve();
}
| #include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
const int MAXQ = 2e5 + 10;
int n, q;
int a[MAXN], b[MAXN], p[MAXQ], x[MAXQ];
bool visited[MAXN];
ll cnt[MAXN];
vector<int> graph[MAXN];
void dfs(int cur) {
visited[cur] = true;
for (int i = 0; i < graph[cur].size(); i++) {
int next = graph[cur][i];
if (!visited[next]) {
cnt[next] += cnt[cur];
dfs(next);
}
}
}
void solve() {
for (int i = 0; i < n - 1; i++) {
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
for (int i = 0; i < q; i++) {
cnt[p[i]] += x[i];
}
dfs(0);
for (int i = 0; i < n; i++) {
if (i) {
cout << " ";
}
cout << cnt[i];
}
cout << endl;
}
int main() {
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int i = 0; i < q; i++) {
cin >> p[i] >> x[i];
p[i]--;
}
solve();
}
| replace | 5 | 7 | 5 | 7 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long int ll;
#define EPS (1e-7)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//-------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, q;
cin >> n >> q;
vector<vector<ll>> graph(n);
for (ll i = 0; i < n - 1; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<ll> cost(n, 0);
for (ll i = 0; i < q; i++) {
ll p, x;
cin >> p >> x;
p--;
cost[p] += x;
}
vector<bool> seen(n, false);
vector<ll> d(n, 0);
stack<ll> st;
st.push(0);
d[0] = cost[0];
while (!st.empty()) {
ll now = st.top();
seen[now] = true;
st.pop();
for (auto v : graph[now]) {
if (!seen[v]) {
st.push(v);
d[v] += d[now] + cost[v];
}
}
seen[now] = false;
}
for (auto ans : d) {
cout << ans << ' ';
}
cout << endl;
}
| #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long int ll;
#define EPS (1e-7)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//-------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, q;
cin >> n >> q;
vector<vector<ll>> graph(n);
for (ll i = 0; i < n - 1; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<ll> cost(n, 0);
for (ll i = 0; i < q; i++) {
ll p, x;
cin >> p >> x;
p--;
cost[p] += x;
}
vector<bool> seen(n, false);
vector<ll> d(n, 0);
stack<ll> st;
st.push(0);
d[0] = cost[0];
while (!st.empty()) {
ll now = st.top();
seen[now] = true;
st.pop();
for (auto v : graph[now]) {
if (!seen[v]) {
st.push(v);
d[v] += d[now] + cost[v];
}
}
}
for (auto ans : d) {
cout << ans << ' ';
}
cout << endl;
}
| delete | 66 | 67 | 66 | 66 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
Graph graph(20010);
vector<int> score(20010, 0);
void dfs(int cur, int prev) {
score[cur] += score[prev];
// 各頂点にスコアを加算する
for (auto next : graph.at(cur)) {
if (next == prev)
continue;
dfs(next, cur);
}
}
int main() {
int N, Q;
scanf("%d%d", &N, &Q);
for (int i = 0; i < N - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
// 辺の情報から無向グラフを作成
graph.at(a).push_back(b);
graph.at(b).push_back(a);
}
for (int i = 0; i < Q; i++) {
int p, x;
scanf("%d%d", &p, &x);
score.at(p) += x;
}
dfs(1, 0);
for (int i = 1; i <= N; i++) {
printf("%d ", score.at(i));
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
Graph graph(200100);
vector<int> score(200100, 0);
void dfs(int cur, int prev) {
score[cur] += score[prev];
// 各頂点にスコアを加算する
for (auto next : graph.at(cur)) {
if (next == prev)
continue;
dfs(next, cur);
}
}
int main() {
int N, Q;
scanf("%d%d", &N, &Q);
for (int i = 0; i < N - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
// 辺の情報から無向グラフを作成
graph.at(a).push_back(b);
graph.at(b).push_back(a);
}
for (int i = 0; i < Q; i++) {
int p, x;
scanf("%d%d", &p, &x);
score.at(p) += x;
}
dfs(1, 0);
for (int i = 1; i <= N; i++) {
printf("%d ", score.at(i));
}
return 0;
}
| replace | 5 | 7 | 5 | 7 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using namespace std;
int N, Q;
ll tr[101010];
vector<int> g[101010];
void solve(int v, int p, ll sum) {
for (int u : g[v]) {
if (u == p)
continue;
solve(u, v, sum + tr[v]);
}
tr[v] += sum;
}
int main() {
cin >> N >> Q;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
rep(i, Q) {
ll p, x;
cin >> p >> x;
p--;
tr[p] += x;
}
solve(0, -1, 0);
rep(i, N) {
if (i)
cout << " ";
cout << tr[i];
}
cout << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using namespace std;
int N, Q;
ll tr[301010];
vector<int> g[301010];
void solve(int v, int p, ll sum) {
for (int u : g[v]) {
if (u == p)
continue;
solve(u, v, sum + tr[v]);
}
tr[v] += sum;
}
int main() {
cin >> N >> Q;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
rep(i, Q) {
ll p, x;
cin >> p >> x;
p--;
tr[p] += x;
}
solve(0, -1, 0);
rep(i, N) {
if (i)
cout << " ";
cout << tr[i];
}
cout << endl;
}
| replace | 7 | 9 | 7 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
vector<vector<int>> v(100000);
vector<ll> ans(100001, 0LL);
vector<bool> used(100001);
void search(int p, int parent, ll sum) {
used[p] = true;
sum += ans[p];
ans[p] = sum;
for (int i = 0; i < v[p].size(); i++) {
if (v[p][i] != parent && used[v[p][i]] == false) {
search(v[p][i], p, sum);
}
}
}
int main() {
int n, q;
cin >> n >> q;
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a - 1].pb(b - 1);
v[b - 1].pb(a - 1);
}
for (int i = 0; i < q; i++) {
ll a, b;
cin >> a >> b;
ans[a - 1LL] += b;
}
search(0, 0, 0LL);
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
vector<vector<int>> v(200001);
vector<ll> ans(200001, 0LL);
vector<bool> used(200001);
void search(int p, int parent, ll sum) {
used[p] = true;
sum += ans[p];
ans[p] = sum;
for (int i = 0; i < v[p].size(); i++) {
if (v[p][i] != parent && used[v[p][i]] == false) {
search(v[p][i], p, sum);
}
}
}
int main() {
int n, q;
cin >> n >> q;
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a - 1].pb(b - 1);
v[b - 1].pb(a - 1);
}
for (int i = 0; i < q; i++) {
ll a, b;
cin >> a >> b;
ans[a - 1LL] += b;
}
search(0, 0, 0LL);
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
} | replace | 14 | 17 | 14 | 17 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono;
using namespace std;
using namespace __gnu_pbds;
#define st first
#define nd second
void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
out << v.size() << '\n';
for (auto e : v)
out << e << ' ';
return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) \
do { \
cerr << #x "[]: "; \
for (int _ = 0; _ < n; ++_) \
cerr << x[_] << " "; \
cerr << '\n'; \
} while (0)
#define dbg_ok cerr << "OK!\n"
#define maxn 100001
#define M 1000000007
#define int long long
#define inf 1e18
#define ninf -1e17
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int>> ht;
vector<int> adj[maxn];
vector<int> ans(maxn);
void dfs(int u, int p) {
if (p != -1) {
ans[u] += ans[p];
}
for (auto v : adj[u]) {
if (v != p) {
dfs(v, u);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int p, q;
cin >> p >> q;
adj[p].push_back(q);
adj[q].push_back(p);
}
while (q--) {
int x, y;
cin >> x >> y;
ans[x] += y;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
}
| #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono;
using namespace std;
using namespace __gnu_pbds;
#define st first
#define nd second
void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
out << v.size() << '\n';
for (auto e : v)
out << e << ' ';
return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) \
do { \
cerr << #x "[]: "; \
for (int _ = 0; _ < n; ++_) \
cerr << x[_] << " "; \
cerr << '\n'; \
} while (0)
#define dbg_ok cerr << "OK!\n"
#define maxn 300001
#define M 1000000007
#define int long long
#define inf 1e18
#define ninf -1e17
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int>> ht;
vector<int> adj[maxn];
vector<int> ans(maxn);
void dfs(int u, int p) {
if (p != -1) {
ans[u] += ans[p];
}
for (auto v : adj[u]) {
if (v != p) {
dfs(v, u);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int p, q;
cin >> p >> q;
adj[p].push_back(q);
adj[q].push_back(p);
}
while (q--) {
int x, y;
cin >> x >> y;
ans[x] += y;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
}
| replace | 42 | 43 | 42 | 43 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using Graph = vector<vector<int>>;
/* 4方向への隣接頂点への移動を表すベクトル */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const long long INF = 1LL << 60;
const int maxi = 1e6 + 10;
vector<int> value;
Graph G;
// d: 頂点 v の深さ
void dfs(int now, int parent, int score) {
value[now] += score;
// for(auto nv : G[now]) {
// if (nv == parent) continue;
// dfs(G,nv,now, value[now]);
// }
for (int i = 0; i < G[now].size(); i++) {
if (G[now][i] == parent)
continue;
dfs(G[now][i], now, value[now]);
}
}
int main() {
// 頂点数と辺数
int N, Q;
cin >> N >> Q;
G.resize(N);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < Q; i++) {
int u, x;
cin >> u >> x;
u--;
value[u] += x;
}
dfs(0, -1, 0);
for (int i = 0; i < N; i++) {
cout << value[i] << " ";
}
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using Graph = vector<vector<int>>;
/* 4方向への隣接頂点への移動を表すベクトル */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const long long INF = 1LL << 60;
const int maxi = 1e6 + 10;
vector<int> value;
Graph G;
// d: 頂点 v の深さ
void dfs(int now, int parent, int score) {
value[now] += score;
// for(auto nv : G[now]) {
// if (nv == parent) continue;
// dfs(G,nv,now, value[now]);
// }
for (int i = 0; i < G[now].size(); i++) {
if (G[now][i] == parent)
continue;
dfs(G[now][i], now, value[now]);
}
}
int main() {
// 頂点数と辺数
int N, Q;
cin >> N >> Q;
G.resize(N);
value.resize(N);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < Q; i++) {
int u, x;
cin >> u >> x;
u--;
value[u] += x;
}
dfs(0, -1, 0);
for (int i = 0; i < N; i++) {
cout << value[i] << " ";
}
cout << endl;
} | insert | 47 | 47 | 47 | 48 | -11 | |
p02936 | C++ | Runtime Error | /*author : Yashvardhan
Sunday, August 18, 2019
5:58 PM
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#define printArr(name, from, to) \
for (int x = from; x < to; x++) \
cout << name[x] << " ";
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < v.size(); ++i)
os << v[i] << " ";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
for (auto it : v)
os << it << " ";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << v.ff << " " << v.ss;
return os;
}
const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;
const int N = 1e5 + 5;
vi g[N];
int sz[N];
int vis[N];
int arr[N];
int segt[4 * N];
int lazy[4 * N];
int ind = 0;
int takemod(int a) { return ((a % mod) + mod) % mod; }
int pow(int a, int b, int m) {
int ans = 1;
while (b) {
if (b & 1)
ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }
int getsizes(int source) {
arr[ind] = source;
ind++;
vis[source] = 1;
int ans = 0;
for (auto i : g[source]) {
if (!vis[i]) {
ans += (1 + getsizes(i));
}
}
return sz[source] = ans;
}
void push(int v, int tl, int tr) {
segt[v] += (tr - tl + 1) * lazy[v];
if (tr != tl) {
lazy[2 * v] += lazy[v];
lazy[2 * v + 1] += lazy[v];
}
lazy[v] = 0;
}
void build(int v, int tl, int tr) {
if (tl == tr)
segt[v] = arr[tl];
else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
segt[v] = segt[2 * v] + segt[2 * v + 1];
}
}
void update(int v, int tl, int tr, int l, int r, int toadd) {
push(v, tl, tr);
if (l > tr || r < tl)
return;
if (l <= tl && r >= tr) {
lazy[v] += toadd;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
update(2 * v, tl, tm, l, r, toadd);
update(2 * v + 1, tm + 1, tr, l, r, toadd);
segt[v] = segt[2 * v] + segt[2 * v + 1];
}
int query(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (l > tr || r < tl)
return 0;
if (l <= tl && r >= tr)
return segt[v];
int tm = (tl + tr) / 2;
return query(2 * v, tl, tm, l, r) + query(2 * v + 1, tm + 1, tr, l, r);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
time_t t1, t2;
t1 = clock();
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
getsizes(1);
map<int, int> mapi;
for (int i = 0; i < n; i++) {
mapi[arr[i]] = i;
}
while (q--) {
int curr, val;
cin >> curr >> val;
update(1, 0, n - 1, mapi[curr], mapi[curr] + sz[curr], val);
}
for (int i = 1; i <= n; i++) {
int val = query(1, 0, n - 1, mapi[i], mapi[i]);
cout << val << " ";
}
cout << endl;
t2 = clock();
cerr << endl << t2 - t1 << endl;
return 0;
}
| /*author : Yashvardhan
Sunday, August 18, 2019
5:58 PM
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#define printArr(name, from, to) \
for (int x = from; x < to; x++) \
cout << name[x] << " ";
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < v.size(); ++i)
os << v[i] << " ";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
for (auto it : v)
os << it << " ";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << v.ff << " " << v.ss;
return os;
}
const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;
const int N = 2e5 + 5;
vi g[N];
int sz[N];
int vis[N];
int arr[N];
int segt[4 * N];
int lazy[4 * N];
int ind = 0;
int takemod(int a) { return ((a % mod) + mod) % mod; }
int pow(int a, int b, int m) {
int ans = 1;
while (b) {
if (b & 1)
ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }
int getsizes(int source) {
arr[ind] = source;
ind++;
vis[source] = 1;
int ans = 0;
for (auto i : g[source]) {
if (!vis[i]) {
ans += (1 + getsizes(i));
}
}
return sz[source] = ans;
}
void push(int v, int tl, int tr) {
segt[v] += (tr - tl + 1) * lazy[v];
if (tr != tl) {
lazy[2 * v] += lazy[v];
lazy[2 * v + 1] += lazy[v];
}
lazy[v] = 0;
}
void build(int v, int tl, int tr) {
if (tl == tr)
segt[v] = arr[tl];
else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
segt[v] = segt[2 * v] + segt[2 * v + 1];
}
}
void update(int v, int tl, int tr, int l, int r, int toadd) {
push(v, tl, tr);
if (l > tr || r < tl)
return;
if (l <= tl && r >= tr) {
lazy[v] += toadd;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
update(2 * v, tl, tm, l, r, toadd);
update(2 * v + 1, tm + 1, tr, l, r, toadd);
segt[v] = segt[2 * v] + segt[2 * v + 1];
}
int query(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (l > tr || r < tl)
return 0;
if (l <= tl && r >= tr)
return segt[v];
int tm = (tl + tr) / 2;
return query(2 * v, tl, tm, l, r) + query(2 * v + 1, tm + 1, tr, l, r);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
time_t t1, t2;
t1 = clock();
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
getsizes(1);
map<int, int> mapi;
for (int i = 0; i < n; i++) {
mapi[arr[i]] = i;
}
while (q--) {
int curr, val;
cin >> curr >> val;
update(1, 0, n - 1, mapi[curr], mapi[curr] + sz[curr], val);
}
for (int i = 1; i <= n; i++) {
int val = query(1, 0, n - 1, mapi[i], mapi[i]);
cout << val << " ";
}
cout << endl;
t2 = clock();
cerr << endl << t2 - t1 << endl;
return 0;
}
| replace | 66 | 67 | 66 | 67 | 0 |
108
|
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> G[200005];
ll val[20005];
void dfs(int p, int u) {
for (auto v : G[u]) {
if (v != p) {
val[v] += val[u];
dfs(u, v);
}
}
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
p--;
val[p] += x;
}
dfs(-1, 0);
for (int i = 0; i < n; i++) {
if (i)
cout << ' ';
cout << val[i];
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> G[200005];
ll val[200005];
void dfs(int p, int u) {
for (auto v : G[u]) {
if (v != p) {
val[v] += val[u];
dfs(u, v);
}
}
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < q; i++) {
int p, x;
cin >> p >> x;
p--;
val[p] += x;
}
dfs(-1, 0);
for (int i = 0; i < n; i++) {
if (i)
cout << ' ';
cout << val[i];
}
cout << endl;
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02936 | 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;
vector<int> to[20005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
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;
typedef long long ll;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <vector>
struct youso {
int parent = -1;
int count = 0;
std::vector<int> edge;
// std::vector<int> child;
};
youso y[100000];
void maketree(int parent, int ima) {
y[ima].parent = parent;
for (int i = 0; i < y[ima].edge.size(); i++) {
if (parent == y[ima].edge[i])
continue;
else {
maketree(ima, y[ima].edge[i]);
// y[ima].child.push_back(y[ima].edge[i]);
}
}
}
void countup(int ima, int x) {
y[ima].count += x;
for (int i = 0; i < y[ima]./*child*/ edge.size(); i++) {
if (y[ima].parent != y[ima].edge[i]) {
countup(y[ima]./*child*/ edge[i], y[ima].count);
}
}
}
int main() {
int n, q, a, b, p, x; // a,b,pは引く1する必要がある
std::cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
std::cin >> a >> b;
y[a - 1].edge.push_back(b - 1);
y[b - 1].edge.push_back(a - 1);
}
maketree(-1, 0);
for (int i = 0; i < q; i++) {
std::cin >> p >> x;
y[p - 1].count += x;
}
countup(0, 0);
std::cout << y[0].count;
for (int i = 1; i < n; i++) {
std::cout << ' ' << y[i].count;
}
std::cout << std::endl;
}
| #include <iostream>
#include <vector>
struct youso {
int parent = -1;
int count = 0;
std::vector<int> edge;
// std::vector<int> child;
};
youso y[200000];
void maketree(int parent, int ima) {
y[ima].parent = parent;
for (int i = 0; i < y[ima].edge.size(); i++) {
if (parent == y[ima].edge[i])
continue;
else {
maketree(ima, y[ima].edge[i]);
// y[ima].child.push_back(y[ima].edge[i]);
}
}
}
void countup(int ima, int x) {
y[ima].count += x;
for (int i = 0; i < y[ima]./*child*/ edge.size(); i++) {
if (y[ima].parent != y[ima].edge[i]) {
countup(y[ima]./*child*/ edge[i], y[ima].count);
}
}
}
int main() {
int n, q, a, b, p, x; // a,b,pは引く1する必要がある
std::cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
std::cin >> a >> b;
y[a - 1].edge.push_back(b - 1);
y[b - 1].edge.push_back(a - 1);
}
maketree(-1, 0);
for (int i = 0; i < q; i++) {
std::cin >> p >> x;
y[p - 1].count += x;
}
countup(0, 0);
std::cout << y[0].count;
for (int i = 1; i < n; i++) {
std::cout << ' ' << y[i].count;
}
std::cout << std::endl;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N, Q;
int main() {
cin >> N >> Q;
vector<vector<int>> e(N);
vector<int> par(N, -1); // 親
for (int i = 0; i < N - 1; i++) {
int from, to;
cin >> from >> to;
from--;
to--;
e[from].push_back(to);
e[to].push_back(from);
par[to] = from;
}
vector<int> cnt(N, 0);
for (int i = 0; i < Q; i++) {
int p, x;
cin >> p >> x;
p--;
cnt[p] += x;
}
par[0] = -1;
for (int i = 0; i < N; i++) {
int ans = cnt[i];
int p = par[i];
while (p != -1) {
ans += cnt[p];
p = par[p];
}
cout << ans << " " << flush;
}
cout << endl;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N, Q;
int main() {
cin >> N >> Q;
vector<vector<int>> e(N);
vector<int> par(N, -1); // 親
for (int i = 0; i < N - 1; i++) {
int from, to;
cin >> from >> to;
from--;
to--;
e[from].push_back(to);
e[to].push_back(from);
par[to] = from;
}
vector<int> cnt(N, 0);
for (int i = 0; i < Q; i++) {
int p, x;
cin >> p >> x;
p--;
cnt[p] += x;
}
par[0] = -1;
vector<int> ans(N);
ans[0] = cnt[0];
cout << ans[0] << " " << flush;
for (int i = 1; i < N; i++) {
ans[i] = cnt[i] + ans[par[i]];
cout << ans[i] << " " << flush;
}
cout << endl;
} | replace | 29 | 37 | 29 | 35 | TLE | |
p02936 | C++ | Time Limit Exceeded | #include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
#define rep(i, n) for (int i = 0; i < n; i++)
#define drep(i, n) for (int i = n - 1; 0 <= i; i--)
#define yes(ans) \
if (ans) \
cout << "yes" << endl; \
else \
cout << "no" << endl;
#define Yes(ans) \
if (ans) \
cout << "Yes" << endl; \
else \
cout << "No" << endl;
#define YES(ans) \
if (ans) \
cout << "YES" << endl; \
else \
cout << "NO" << endl;
#define POSSIBLE(ans) \
if (ans) \
cout << "POSSIBLE" << endl; \
else \
cout << "IMPOSSIBLE" << endl;
#define Pi 3.1415926535897932384626
class UnionFind {
public:
// 親の番号を格納する。親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 作るときはParentの値を全て-1にする
// こうすると全てバラバラになる
UnionFind(int N) { Parent = vector<int>(N, -1); }
// Aがどのグループに属しているか調べる
int root(int A) {
if (Parent[A] < 0)
return A;
return Parent[A] = root(Parent[A]);
}
// 自分のいるグループの頂点数を調べる
int size(int A) {
return -Parent[root(A)]; // 親をとってきたい
}
// AとBをくっ付ける
bool connect(int A, int B) {
// AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついてるからくっ付けない
return false;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
// aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
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;
}
bool check(int a, int b) { return a > b; }
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// aCbを1000000007で割った余りを求める
llint convination(llint a, llint b) {
llint ans = 1;
for (llint i = 0; i < b; i++) {
ans *= a - i;
ans %= 1000000007;
}
for (llint i = 1; i <= b; i++) {
ans *= modinv(i, 1000000007);
ans %= 1000000007;
}
return ans;
}
// aのb乗を1000000007で割った余りを求める
llint power(llint a, llint b) {
if (b == 1)
return a;
if (b == 0)
return 1;
llint tmp = power(a, b / 2);
tmp *= tmp;
tmp %= 1000000007;
if (b % 2 == 1) {
tmp *= a;
tmp %= 1000000007;
}
return tmp;
}
void dfs(vint &ans, vvint kids, int p) {
for (int i : kids[p]) {
ans[i] += ans[p];
dfs(ans, kids, i);
}
return;
}
int main() {
int n, q;
cin >> n >> q;
vvint kids(n);
vint ans(n, 0);
rep(i, n - 1) {
int x, y;
cin >> x >> y;
x--;
y--;
kids[x].push_back(y);
}
rep(i, q) {
int p, x;
cin >> p >> x;
ans[p - 1] += x;
}
dfs(ans, kids, 0);
rep(i, n) cout << ans[i] << " ";
cout << endl;
return 0;
} | #include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
#define rep(i, n) for (int i = 0; i < n; i++)
#define drep(i, n) for (int i = n - 1; 0 <= i; i--)
#define yes(ans) \
if (ans) \
cout << "yes" << endl; \
else \
cout << "no" << endl;
#define Yes(ans) \
if (ans) \
cout << "Yes" << endl; \
else \
cout << "No" << endl;
#define YES(ans) \
if (ans) \
cout << "YES" << endl; \
else \
cout << "NO" << endl;
#define POSSIBLE(ans) \
if (ans) \
cout << "POSSIBLE" << endl; \
else \
cout << "IMPOSSIBLE" << endl;
#define Pi 3.1415926535897932384626
class UnionFind {
public:
// 親の番号を格納する。親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 作るときはParentの値を全て-1にする
// こうすると全てバラバラになる
UnionFind(int N) { Parent = vector<int>(N, -1); }
// Aがどのグループに属しているか調べる
int root(int A) {
if (Parent[A] < 0)
return A;
return Parent[A] = root(Parent[A]);
}
// 自分のいるグループの頂点数を調べる
int size(int A) {
return -Parent[root(A)]; // 親をとってきたい
}
// AとBをくっ付ける
bool connect(int A, int B) {
// AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついてるからくっ付けない
return false;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
// aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
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;
}
bool check(int a, int b) { return a > b; }
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// aCbを1000000007で割った余りを求める
llint convination(llint a, llint b) {
llint ans = 1;
for (llint i = 0; i < b; i++) {
ans *= a - i;
ans %= 1000000007;
}
for (llint i = 1; i <= b; i++) {
ans *= modinv(i, 1000000007);
ans %= 1000000007;
}
return ans;
}
// aのb乗を1000000007で割った余りを求める
llint power(llint a, llint b) {
if (b == 1)
return a;
if (b == 0)
return 1;
llint tmp = power(a, b / 2);
tmp *= tmp;
tmp %= 1000000007;
if (b % 2 == 1) {
tmp *= a;
tmp %= 1000000007;
}
return tmp;
}
void dfs(vint &ans, vvint &kids, int p) {
for (int i : kids[p]) {
ans[i] += ans[p];
dfs(ans, kids, i);
}
return;
}
int main() {
int n, q;
cin >> n >> q;
vvint kids(n);
vint ans(n, 0);
rep(i, n - 1) {
int x, y;
cin >> x >> y;
x--;
y--;
kids[x].push_back(y);
}
rep(i, q) {
int p, x;
cin >> p >> x;
ans[p - 1] += x;
}
dfs(ans, kids, 0);
rep(i, n) cout << ans[i] << " ";
cout << endl;
return 0;
} | replace | 162 | 163 | 162 | 163 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define tll tuple<ll, ll, ll>
#define all(c) c.begin(), c.end()
const ll inf = 1000000001;
const ll INF = 1e18;
const ll MOD = 1000000007;
vector<ll> g[202020];
ll score[202020];
void dfs(int node, ll prev) {
score[node] += score[prev];
for (int i = 0; i < g[node].size(); i++) {
if (g[node][i] == prev)
continue;
dfs(g[node][i], node);
}
}
int main() {
fill(score, score + 202020, 0);
ll n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 0; i < q; i++) {
int p;
ll x;
cin >> p >> x;
score[p] += x;
}
dfs(1, score[1]);
for (int i = 1; i <= n; i++)
cout << score[i] << " ";
cout << endl;
// cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define tll tuple<ll, ll, ll>
#define all(c) c.begin(), c.end()
const ll inf = 1000000001;
const ll INF = 1e18;
const ll MOD = 1000000007;
vector<ll> g[202020];
ll score[202020];
void dfs(int node, ll prev) {
score[node] += score[prev];
for (int i = 0; i < g[node].size(); i++) {
if (g[node][i] == prev)
continue;
dfs(g[node][i], node);
}
}
int main() {
fill(score, score + 202020, 0);
ll n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 0; i < q; i++) {
int p;
ll x;
cin >> p >> x;
score[p] += x;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << score[i] << " ";
cout << endl;
// cout << ans << endl;
} | replace | 50 | 51 | 50 | 51 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
struct edge {
int to, id;
};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod 1000000007
using graph = vector<vector<ll>>;
int jikan;
bool seen[100010];
ll sum[100010];
ll ans[100010];
void dfs(const graph &G, int v, ll parent, ll val) {
// seen[v] = true;
// root[v].push_back(v);
sum[v] += val;
for (auto next_v : G[v]) {
// ans[next_v] = v+1;
// first[next_v] = jikan;
// ans[next_v] += ans[v];
if (next_v == parent)
continue;
dfs(G, next_v, v, sum[v]);
// root[v].push_back(next_v);
/*for(auto hoge : root[next_v]){
root[v].push_back(hoge);
}*/
}
}
int main() {
ll n, q;
cin >> n >> q;
graph g(n);
graph root(n);
ll ans[n];
rep(i, n) {
seen[i] = false;
ans[i] = 0;
}
rep(i, n - 1) {
ll a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
rep(i, q) {
ll a, b;
cin >> a >> b;
sum[a - 1] += b;
}
dfs(g, 0, -1, 0);
rep(i, n) {
if (i < n - 1)
cout << sum[i] << ' ';
else
cout << sum[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
struct edge {
int to, id;
};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod 1000000007
using graph = vector<vector<ll>>;
int jikan;
bool seen[200010];
ll sum[200010];
ll ans[200010];
void dfs(const graph &G, int v, ll parent, ll val) {
// seen[v] = true;
// root[v].push_back(v);
sum[v] += val;
for (auto next_v : G[v]) {
// ans[next_v] = v+1;
// first[next_v] = jikan;
// ans[next_v] += ans[v];
if (next_v == parent)
continue;
dfs(G, next_v, v, sum[v]);
// root[v].push_back(next_v);
/*for(auto hoge : root[next_v]){
root[v].push_back(hoge);
}*/
}
}
int main() {
ll n, q;
cin >> n >> q;
graph g(n);
graph root(n);
ll ans[n];
rep(i, n) {
seen[i] = false;
ans[i] = 0;
}
rep(i, n - 1) {
ll a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
rep(i, q) {
ll a, b;
cin >> a >> b;
sum[a - 1] += b;
}
dfs(g, 0, -1, 0);
rep(i, n) {
if (i < n - 1)
cout << sum[i] << ' ';
else
cout << sum[i] << endl;
}
} | replace | 13 | 16 | 13 | 16 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<ll, ll> Pair;
#define mp(x, y) make_pair(x, y)
typedef queue<int> qi;
typedef queue<string> qs;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = (a); i <= (b); i++)
#define repm(i, n) for (int i = n; i >= 0; i--)
#define all(v) v.begin(), v.end()
// sort( all(v) ) などと使える
// nが素数かをO(√n)で判断
bool is_prime(int n) {
bool res = true;
if (n == 1)
return false;
else if (n == 2)
return true;
else {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res = false;
break;
}
}
}
return res;
}
int perm(int n, int r) {
if (r == 1)
return n;
return n * perm(n - 1, r - 1);
}
int fact(int n) {
if (n == 1)
return 1;
return fact(n - 1) * n;
}
int comb(int n, int r) {
int a = min(n, r), b = max(n, r);
return perm(a, b) / fact(a);
}
/*
ll pow_mod(ll a, ll n, ll mod) {
ll res = 1;
ll base = a;
while (n) {
if (n & 1) res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
*/
// aの逆元 (a^(-1))を求める
ll inv_mod(ll a, ll mod) {
ll b = mod, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= mod;
if (u < 0)
u += mod;
return u;
}
/*
ll fact_mod(ll n, ll mod) {
if (n == 1) return 1;
return n * fact_mod(n - 1, mod) * mod;
}
*/
// 階乗 (mod とりバージョン)
ll fact_mod(ll n, ll mod) {
ll f = 1;
for (int i = 2; i <= n; i++)
f = f * (i % mod) % mod;
return f;
}
ll pow_mod(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = (res * x) % mod; // ビット演算(最下位ビットが1のとき)
x = (x * x) % mod;
n >>= 1; // 右シフト(n = n >> 1)
}
return res;
}
// 組み合わせ nCr を求める (modあり)
ll comb_mod(ll n, ll r, ll mod) {
if (r > n - r)
r = n - r;
if (r == 0)
return 1;
ll a = 1;
for (int i = 0; i < r; i++)
a = a * ((n - i) % mod) % mod;
ll b = pow_mod(fact_mod(r, mod), mod - 2, mod);
return (a % mod) * (b % mod) % mod;
}
int N, Q;
vvi G;
vll c;
vector<bool> seen;
void dfs(int v) {
seen[v] = true;
for (auto nv : G[v]) {
if (seen[nv])
continue;
else {
c[nv] += c[v];
dfs(nv);
}
}
}
int main() {
cin >> N >> Q;
G.resize(N);
c.resize(N, 0);
seen.assign(N, false);
rep(i, N) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, Q) {
int x;
ll p;
cin >> x >> p;
x--;
c[x] += p;
}
// DFS
dfs(0);
// output
rep(i, N) cout << c[i] << " ";
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<ll, ll> Pair;
#define mp(x, y) make_pair(x, y)
typedef queue<int> qi;
typedef queue<string> qs;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = (a); i <= (b); i++)
#define repm(i, n) for (int i = n; i >= 0; i--)
#define all(v) v.begin(), v.end()
// sort( all(v) ) などと使える
// nが素数かをO(√n)で判断
bool is_prime(int n) {
bool res = true;
if (n == 1)
return false;
else if (n == 2)
return true;
else {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res = false;
break;
}
}
}
return res;
}
int perm(int n, int r) {
if (r == 1)
return n;
return n * perm(n - 1, r - 1);
}
int fact(int n) {
if (n == 1)
return 1;
return fact(n - 1) * n;
}
int comb(int n, int r) {
int a = min(n, r), b = max(n, r);
return perm(a, b) / fact(a);
}
/*
ll pow_mod(ll a, ll n, ll mod) {
ll res = 1;
ll base = a;
while (n) {
if (n & 1) res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
*/
// aの逆元 (a^(-1))を求める
ll inv_mod(ll a, ll mod) {
ll b = mod, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= mod;
if (u < 0)
u += mod;
return u;
}
/*
ll fact_mod(ll n, ll mod) {
if (n == 1) return 1;
return n * fact_mod(n - 1, mod) * mod;
}
*/
// 階乗 (mod とりバージョン)
ll fact_mod(ll n, ll mod) {
ll f = 1;
for (int i = 2; i <= n; i++)
f = f * (i % mod) % mod;
return f;
}
ll pow_mod(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = (res * x) % mod; // ビット演算(最下位ビットが1のとき)
x = (x * x) % mod;
n >>= 1; // 右シフト(n = n >> 1)
}
return res;
}
// 組み合わせ nCr を求める (modあり)
ll comb_mod(ll n, ll r, ll mod) {
if (r > n - r)
r = n - r;
if (r == 0)
return 1;
ll a = 1;
for (int i = 0; i < r; i++)
a = a * ((n - i) % mod) % mod;
ll b = pow_mod(fact_mod(r, mod), mod - 2, mod);
return (a % mod) * (b % mod) % mod;
}
int N, Q;
vvi G;
vll c;
vector<bool> seen;
void dfs(int v) {
seen[v] = true;
for (auto nv : G[v]) {
if (seen[nv])
continue;
else {
c[nv] += c[v];
dfs(nv);
}
}
}
int main() {
cin >> N >> Q;
G.resize(N);
c.resize(N, 0);
seen.assign(N, false);
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, Q) {
int x;
ll p;
cin >> x >> p;
x--;
c[x] += p;
}
// DFS
dfs(0);
// output
rep(i, N) cout << c[i] << " ";
} | replace | 172 | 173 | 172 | 173 | -11 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 100005;
vector<int> v[N];
ll ans[N];
void dfs(int x, ll add, int p) {
if (x)
ans[x] += add;
for (int i = 0; i < v[x].size(); i++)
if (v[x][i] != p)
dfs(v[x][i], ans[x], x);
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
while (q--) {
int par, val;
cin >> par >> val;
ans[par - 1] += val;
}
dfs(0, ans[0], -1);
for (int i = 0; i < n; i++)
cout << ans[i] << " ";
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200005;
vector<int> v[N];
ll ans[N];
void dfs(int x, ll add, int p) {
if (x)
ans[x] += add;
for (int i = 0; i < v[x].size(); i++)
if (v[x][i] != p)
dfs(v[x][i], ans[x], x);
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
while (q--) {
int par, val;
cin >> par >> val;
ans[par - 1] += val;
}
dfs(0, ans[0], -1);
for (int i = 0; i < n; i++)
cout << ans[i] << " ";
cout << endl;
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define N 200123
#define mod 1000000007
long long weight[N];
vector<int> adj[N];
void dfs(int u, int par) {
weight[u] += weight[par];
for (int v : adj[u])
if (v != par)
dfs(v, u);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
int n, q, a;
long long b;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
fill(weight, weight + N, 0);
for (int i = 0; i < q; i++) {
cin >> a >> b;
weight[a] += b;
}
dfs(1, 0);
for (int i = 0; i < n; i++) {
if (i)
cout << " ";
cout << weight[i + 1];
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define N 200123
#define mod 1000000007
long long weight[N];
vector<int> adj[N];
void dfs(int u, int par) {
weight[u] += weight[par];
for (int v : adj[u])
if (v != par)
dfs(v, u);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q, a;
long long b;
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
fill(weight, weight + N, 0);
for (int i = 0; i < q; i++) {
cin >> a >> b;
weight[a] += b;
}
dfs(1, 0);
for (int i = 0; i < n; i++) {
if (i)
cout << " ";
cout << weight[i + 1];
}
return 0;
} | delete | 19 | 23 | 19 | 19 | -11 | |
p02936 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
vector<bool> seen(200000, false);
void dfs(vector<vector<int>> con, int num, vector<int> &ope) {
seen.at(num) = true;
for (int con_ele : con.at(num)) {
if (seen.at(con_ele) == false) {
ope.at(con_ele) += ope.at(num);
dfs(con, con_ele, ope);
}
}
};
int main() {
int n, q;
cin >> n >> q;
vector<vector<int>> points(n);
for (int i = 0; i < n - 1; i++) {
int a_in, b_in;
cin >> a_in >> b_in;
points.at(a_in - 1).push_back(b_in - 1);
points.at(b_in - 1).push_back(a_in - 1);
}
vector<int> ope(n, 0);
for (int i = 0; i < q; i++) {
int p_in, x_in;
cin >> p_in >> x_in;
ope.at(p_in - 1) += x_in;
}
dfs(points, 0, ope);
for (int ope_ele : ope) {
cout << ope_ele << " ";
}
cout << endl;
}
| #include "bits/stdc++.h"
using namespace std;
vector<bool> seen(200000, false);
void dfs(vector<vector<int>> &con, int num, vector<int> &ope) {
seen.at(num) = true;
for (int con_ele : con.at(num)) {
if (seen.at(con_ele) == false) {
ope.at(con_ele) += ope.at(num);
dfs(con, con_ele, ope);
}
}
};
int main() {
int n, q;
cin >> n >> q;
vector<vector<int>> points(n);
for (int i = 0; i < n - 1; i++) {
int a_in, b_in;
cin >> a_in >> b_in;
points.at(a_in - 1).push_back(b_in - 1);
points.at(b_in - 1).push_back(a_in - 1);
}
vector<int> ope(n, 0);
for (int i = 0; i < q; i++) {
int p_in, x_in;
cin >> p_in >> x_in;
ope.at(p_in - 1) += x_in;
}
dfs(points, 0, ope);
for (int ope_ele : ope) {
cout << ope_ele << " ";
}
cout << endl;
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int n;
vector<int> v[200010];
vector<int> ans(20010, 0);
void dfs(int i, int p = -1) {
for (int j : v[i]) {
if (j == p)
continue;
ans[j] += ans[i];
dfs(j, i);
}
}
int main() {
// vector<int> v[200010];
int q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int n;
vector<int> v[200010];
vector<int> ans(200100, 0);
void dfs(int i, int p = -1) {
for (int j : v[i]) {
if (j == p)
continue;
ans[j] += ans[i];
dfs(j, i);
}
}
int main() {
// vector<int> v[200010];
int q;
cin >> n >> q;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <string.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
LL head[N], cnt = 0, sum[N], pre_sum[N];
struct edge {
LL to, next;
} p[N * 2];
void add(LL x, LL y) {
p[++cnt].to = y;
p[cnt].next = head[x];
head[x] = cnt;
}
void spread(LL x) {
for (LL i = head[x]; i; i = p[i].next) {
pre_sum[p[i].to] += pre_sum[x];
}
sum[x] += pre_sum[x];
pre_sum[x] = 0;
}
int main() {
LL n, q;
cin >> n >> q;
memset(sum, 0, sizeof(sum));
memset(pre_sum, 0, sizeof(pre_sum));
for (LL i = 1; i < n; i++) {
LL x, y;
cin >> x >> y;
add(x, y);
add(y, x);
}
for (LL i = 1; i <= q; i++) {
LL x, v;
cin >> x >> v;
pre_sum[x] += v;
// spread(x);
}
for (LL i = 1; i <= n; i++)
spread(i);
for (LL i = 1; i <= n; i++)
i != n ? cout << sum[i] << ' ' : cout << sum[i] << endl;
return 0;
}
| #include <iostream>
#include <string.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long LL;
LL head[N], cnt = 0, sum[N], pre_sum[N];
struct edge {
LL to, next;
} p[N * 2];
void add(LL x, LL y) {
p[++cnt].to = y;
p[cnt].next = head[x];
head[x] = cnt;
}
void spread(LL x) {
for (LL i = head[x]; i; i = p[i].next) {
pre_sum[p[i].to] += pre_sum[x];
}
sum[x] += pre_sum[x];
pre_sum[x] = 0;
}
int main() {
LL n, q;
cin >> n >> q;
memset(sum, 0, sizeof(sum));
memset(pre_sum, 0, sizeof(pre_sum));
for (LL i = 1; i < n; i++) {
LL x, y;
cin >> x >> y;
add(x, y);
add(y, x);
}
for (LL i = 1; i <= q; i++) {
LL x, v;
cin >> x >> v;
pre_sum[x] += v;
// spread(x);
}
for (LL i = 1; i <= n; i++)
spread(i);
for (LL i = 1; i <= n; i++)
i != n ? cout << sum[i] << ' ' : cout << sum[i] << endl;
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 1e5 + 5;
vector<LL> GRAPH[MAX];
LL a[MAX];
bool vis[MAX];
LL b[MAX];
void DFS(LL x) {
if (vis[x])
return;
vis[x] = true;
for (int i = 0; i < GRAPH[x].size(); i++) {
if (!vis[GRAPH[x][i]]) {
b[GRAPH[x][i]] += b[x];
DFS(GRAPH[x][i]);
}
}
}
int main() {
int n, q;
cin >> n >> q;
int m = n - 1;
memset(vis, false, sizeof(vis));
while (m--) {
LL x, y;
cin >> x >> y;
GRAPH[x].push_back(y);
GRAPH[y].push_back(x);
}
while (q--) {
LL p, x;
cin >> p >> x;
a[p] += x;
}
for (int i = 1; i <= n; i++) {
b[i] = a[i];
}
DFS(1);
for (int i = 1; i <= n; i++) {
cout << b[i] << " ";
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 2e5 + 5;
vector<LL> GRAPH[MAX];
LL a[MAX];
bool vis[MAX];
LL b[MAX];
void DFS(LL x) {
if (vis[x])
return;
vis[x] = true;
for (int i = 0; i < GRAPH[x].size(); i++) {
if (!vis[GRAPH[x][i]]) {
b[GRAPH[x][i]] += b[x];
DFS(GRAPH[x][i]);
}
}
}
int main() {
int n, q;
cin >> n >> q;
int m = n - 1;
memset(vis, false, sizeof(vis));
while (m--) {
LL x, y;
cin >> x >> y;
GRAPH[x].push_back(y);
GRAPH[y].push_back(x);
}
while (q--) {
LL p, x;
cin >> p >> x;
a[p] += x;
}
for (int i = 1; i <= n; i++) {
b[i] = a[i];
}
DFS(1);
for (int i = 1; i <= n; i++) {
cout << b[i] << " ";
}
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02936 | C++ | Runtime Error | // includes
#include "bits/stdc++.h"
using namespace std;
// macros
#define ll long long
#define MOD 998244353 // 1000000007 // 100000000 //
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define sz(x) ((ll)(x).size())
#define ft first
#define sd second
#define pb push_back
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define itr(it, x) for (auto it = x.begin(); it != x.end(); it++)
#define mem(a, b) memset(a, (ll)b, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define edge(v, x, y) \
v[x].pb(y); \
v[y].pb(x);
#define popc __builtin_popcount
#define ANS(s) \
{ \
cout << s << "\n"; \
return; \
}
// functions
template <typename T> void unique(T &c) {
c.erase(std::unique(c.begin(), c.end()), c.end());
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
for (int i = 0; i < vec.size(); i++) {
os << vec[i];
if (i + 1 != vec.size())
os << " ";
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
os << "(" << itr->first << ", " << itr->second << ")";
auto titr = itr;
if (++titr != mp.end())
os << " ";
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
os << "(" << itr->first << ", " << itr->second << ")";
auto titr = itr;
if (++titr != mp.end())
os << " ";
}
return os;
}
// constants
const ll N = 1e5 + 5;
const ll M = 1e6 + 5;
const ll A = 1e7 + 5;
const ll inf = 1e9;
const long long linf = 1LL << 60;
const double er = 1e-10;
const double pi = 3.141592653589793238463;
const ll lx[4] = {0, 1, -1, 0};
const ll ly[4] = {1, 0, 0, -1};
const ll dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const ll dy[8] = {1, -1, 0, 0, 1, 1, -1, -1};
// io
struct fast_io {
fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
}
} fast_io_;
vector<ll> v[N];
ll a[N];
void dfs(ll u, ll p) {
a[u] += a[p];
for (auto x : v[u]) {
if (x != p)
dfs(x, u);
}
}
void solve() {
ll n, m;
cin >> n >> m;
rep(i, n - 1) {
ll x, y;
cin >> x >> y;
edge(v, x, y);
}
rep(i, m) {
ll p, x;
cin >> p >> x;
a[p] += x;
}
dfs(1, 0);
rep(i, n) cout << a[i + 1] << " ";
}
int main(int argc, char const *argv[]) {
ll t = 1; // cin >> t;
while (t--) {
solve();
}
return 0;
}
| // includes
#include "bits/stdc++.h"
using namespace std;
// macros
#define ll long long
#define MOD 998244353 // 1000000007 // 100000000 //
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define sz(x) ((ll)(x).size())
#define ft first
#define sd second
#define pb push_back
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define itr(it, x) for (auto it = x.begin(); it != x.end(); it++)
#define mem(a, b) memset(a, (ll)b, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define edge(v, x, y) \
v[x].pb(y); \
v[y].pb(x);
#define popc __builtin_popcount
#define ANS(s) \
{ \
cout << s << "\n"; \
return; \
}
// functions
template <typename T> void unique(T &c) {
c.erase(std::unique(c.begin(), c.end()), c.end());
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
for (int i = 0; i < vec.size(); i++) {
os << vec[i];
if (i + 1 != vec.size())
os << " ";
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
for (auto itr = st.begin(); itr != st.end(); ++itr) {
os << *itr;
auto titr = itr;
if (++titr != st.end())
os << " ";
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
os << "(" << itr->first << ", " << itr->second << ")";
auto titr = itr;
if (++titr != mp.end())
os << " ";
}
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
os << "(" << itr->first << ", " << itr->second << ")";
auto titr = itr;
if (++titr != mp.end())
os << " ";
}
return os;
}
// constants
const ll N = 1e5 + 5;
const ll M = 1e6 + 5;
const ll A = 1e7 + 5;
const ll inf = 1e9;
const long long linf = 1LL << 60;
const double er = 1e-10;
const double pi = 3.141592653589793238463;
const ll lx[4] = {0, 1, -1, 0};
const ll ly[4] = {1, 0, 0, -1};
const ll dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const ll dy[8] = {1, -1, 0, 0, 1, 1, -1, -1};
// io
struct fast_io {
fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
}
} fast_io_;
vector<ll> v[M];
ll a[M];
void dfs(ll u, ll p) {
a[u] += a[p];
for (auto x : v[u]) {
if (x != p)
dfs(x, u);
}
}
void solve() {
ll n, m;
cin >> n >> m;
rep(i, n - 1) {
ll x, y;
cin >> x >> y;
edge(v, x, y);
}
rep(i, m) {
ll p, x;
cin >> p >> x;
a[p] += x;
}
dfs(1, 0);
rep(i, n) cout << a[i + 1] << " ";
}
int main(int argc, char const *argv[]) {
ll t = 1; // cin >> t;
while (t--) {
solve();
}
return 0;
}
| replace | 142 | 144 | 142 | 144 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define rep(i, e) for (int i = 0; i < (e); ++i)
int dd[100010] = {};
vector<vector<int>> d(100010);
int ans[100010] = {};
int n, q, a, b, p, x;
int tmp;
void dfs(int x, int tmp) {
ans[x] += tmp;
for (auto next : d[x]) {
dfs(next, tmp + dd[next]);
}
}
int main() {
cin >> n >> q;
rep(i, n - 1) {
cin >> a >> b;
d[a].emplace_back(b);
}
rep(i, q) {
cin >> p >> x;
dd[p] += x;
}
dfs(1, dd[1]);
REP(i, 1, n) { cout << ans[i] << " "; }
cout << ans[n] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define rep(i, e) for (int i = 0; i < (e); ++i)
int dd[200010] = {};
vector<vector<int>> d(200010);
int ans[200010] = {};
int n, q, a, b, p, x;
int tmp;
void dfs(int x, int tmp) {
ans[x] += tmp;
for (auto next : d[x]) {
dfs(next, tmp + dd[next]);
}
}
int main() {
cin >> n >> q;
rep(i, n - 1) {
cin >> a >> b;
d[a].emplace_back(b);
}
rep(i, q) {
cin >> p >> x;
dd[p] += x;
}
dfs(1, dd[1]);
REP(i, 1, n) { cout << ans[i] << " "; }
cout << ans[n] << endl;
return 0;
} | replace | 5 | 8 | 5 | 8 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
const int INT_INF = 1001001001;
// stoi(s) : string→int stoll(s) :string→longlong int→string to_string(i)
const double PI = acos(-1.0);
// 小数点の表し方 cout << fixed << setprecision(5);
const int MAXN = 100100;
ll num[MAXN] = {0};
bool seen[MAXN] = {false};
void dfs(Graph &G, int n) {
seen[n] = true;
for (auto x : G[n]) {
if (seen[x])
continue;
num[x] += num[n];
dfs(G, x);
}
}
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n - 1), b(n - 1);
vector<int> p(q), x(q);
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int i = 0; i < q; i++) {
cin >> p[i] >> x[i];
p[i]--;
num[p[i]] += x[i];
}
Graph G(n);
for (int i = 0; i < n - 1; i++) {
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
dfs(G, 0);
for (int i = 0; i < n; i++) {
cout << num[i] << " ";
}
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
const int INT_INF = 1001001001;
// stoi(s) : string→int stoll(s) :string→longlong int→string to_string(i)
const double PI = acos(-1.0);
// 小数点の表し方 cout << fixed << setprecision(5);
const int MAXN = 200100;
ll num[MAXN] = {0};
bool seen[MAXN] = {false};
void dfs(Graph &G, int n) {
seen[n] = true;
for (auto x : G[n]) {
if (seen[x])
continue;
num[x] += num[n];
dfs(G, x);
}
}
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n - 1), b(n - 1);
vector<int> p(q), x(q);
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int i = 0; i < q; i++) {
cin >> p[i] >> x[i];
p[i]--;
num[p[i]] += x[i];
}
Graph G(n);
for (int i = 0; i < n - 1; i++) {
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
dfs(G, 0);
for (int i = 0; i < n; i++) {
cout << num[i] << " ";
}
cout << endl;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02936 | C++ | Runtime Error | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;
vector<vector<int>> G(404040);
vector<int> score(404040, 0);
vector<int> ans(404040, -1);
int dfs(int x, int sum) {
if (ans[x] != -1)
return 0;
sum += score[x];
ans[x] = sum;
for (auto nex : G[x]) {
dfs(nex, sum);
}
}
int main() {
int n, q;
cin >> n >> q;
rep(i, 0, n - 1) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, 0, q) {
int p, x;
cin >> p >> x;
score[p] += x;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << ans[i] << endl;
}
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;
vector<vector<int>> G(404040);
vector<int> score(404040, 0);
vector<int> ans(404040, -1);
int dfs(int x, int sum) {
if (ans[x] != -1)
return 0;
sum += score[x];
ans[x] = sum;
for (auto nex : G[x]) {
dfs(nex, sum);
}
return 0;
}
int main() {
int n, q;
cin >> n >> q;
rep(i, 0, n - 1) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, 0, q) {
int p, x;
cin >> p >> x;
score[p] += x;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << ans[i] << endl;
}
} | insert | 22 | 22 | 22 | 23 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <assert.h>
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
i64 n, q;
cin >> n >> q;
vector<i64> edge[n];
for (i64 i = 0; i < n - 1; i++) {
i64 a, b;
cin >> a >> b;
a--;
b--;
edge[a].push_back(b);
edge[b].push_back(a);
}
vector<i64> ans(n);
for (i64 i = 0; i < q; i++) {
i64 p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
vector<bool> tmp(n);
queue<i64> que;
que.push(0);
tmp[0] = true;
while (que.size()) {
i64 p = que.front();
que.pop();
for (i64 i : edge[p])
if (!tmp[i]) {
ans[i] += ans[p];
que.push(i);
}
}
for (i64 i = 0; i < n; i++)
cout << ans[i] << (i + 1 == n ? "\n" : " ");
return 0;
}
| #include <assert.h>
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
i64 n, q;
cin >> n >> q;
vector<i64> edge[n];
for (i64 i = 0; i < n - 1; i++) {
i64 a, b;
cin >> a >> b;
a--;
b--;
edge[a].push_back(b);
edge[b].push_back(a);
}
vector<i64> ans(n);
for (i64 i = 0; i < q; i++) {
i64 p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
vector<bool> tmp(n);
queue<i64> que;
que.push(0);
tmp[0] = true;
while (que.size()) {
i64 p = que.front();
que.pop();
for (i64 i : edge[p])
if (!tmp[i]) {
ans[i] += ans[p];
que.push(i);
tmp[i] = true;
}
}
for (i64 i = 0; i < n; i++)
cout << ans[i] << (i + 1 == n ? "\n" : " ");
return 0;
}
| insert | 35 | 35 | 35 | 36 | TLE | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// ki
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == v)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int N, Q;
cin >> N >> Q;
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(N);
for (int i = 0; i < Q; ++i) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
for (int i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// ki
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
ans[u] += ans[v];
dfs(u, v);
}
}
int main() {
int N, Q;
cin >> N >> Q;
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(N);
for (int i = 0; i < Q; ++i) {
int p, x;
cin >> p >> x;
--p;
ans[p] += x;
}
dfs(0);
for (int i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
return 0;
} | replace | 12 | 13 | 12 | 13 | -11 | |
p02936 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N, Q;
cin >> N >> Q;
int a, b;
vector<vector<int>> AB(N + 5, vector<int>(N + 5));
for (int i = 0; i < N - 1; i++) {
cin >> a >> b;
AB[a].push_back(b);
AB[b].push_back(a);
}
int p, x;
int Score[200005] = {};
for (int j = 0; j < Q; j++) {
cin >> p >> x;
Score[p] += x;
}
vector<int> Now;
vector<int> Next{1};
bool Exists[200005] = {};
Exists[1] = true;
while (!Next.empty()) {
Now = Next;
Next.clear();
for (int nw : Now) {
for (int nxt : AB[nw]) {
if (!Exists[nxt]) {
Exists[nxt] = true;
Score[nxt] += Score[nw];
Next.push_back(nxt);
}
}
}
}
for (int i = 1; i <= N; i++)
cout << Score[i] << " ";
cout << endl;
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N, Q;
cin >> N >> Q;
int a, b;
vector<vector<int>> AB(N + 5, vector<int>());
for (int i = 0; i < N - 1; i++) {
cin >> a >> b;
AB[a].push_back(b);
AB[b].push_back(a);
}
int p, x;
int Score[200005] = {};
for (int j = 0; j < Q; j++) {
cin >> p >> x;
Score[p] += x;
}
vector<int> Now;
vector<int> Next{1};
bool Exists[200005] = {};
Exists[1] = true;
while (!Next.empty()) {
Now = Next;
Next.clear();
for (int nw : Now) {
for (int nxt : AB[nw]) {
if (!Exists[nxt]) {
Exists[nxt] = true;
Score[nxt] += Score[nw];
Next.push_back(nxt);
}
}
}
}
for (int i = 1; i <= N; i++)
cout << Score[i] << " ";
cout << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 200000;
int n, m, p[maxn], add[maxn], a, b;
vector<int> v[maxn];
void dfs(int node, int pa) {
for (int i : v[node]) {
if (i != pa) {
add[i] += add[node];
dfs(i, node);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
add[a] += b;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
if (i != 1)
cout << " ";
cout << add[i];
}
cout << "\n";
}
| #include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 200005;
int n, m, p[maxn], add[maxn], a, b;
vector<int> v[maxn];
void dfs(int node, int pa) {
for (int i : v[node]) {
if (i != pa) {
add[i] += add[node];
dfs(i, node);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
add[a] += b;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
if (i != 1)
cout << " ";
cout << add[i];
}
cout << "\n";
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <stdio.h>
// 入力受付用の変数
#define BUF 200002
char str[BUF];
// 入力された値
#define LIM_A 100001
int N, Q;
int a[LIM_A];
int b[LIM_A];
int p[LIM_A];
int x[LIM_A];
typedef struct {
int root;
int count;
long ans;
} Tree;
Tree v[LIM_A];
void receive_input() {
fgets(str, sizeof(str), stdin);
sscanf(str, "%d %d", &N, &Q);
for (size_t i = 1; i <= N - 1; i++) {
scanf("%d %d", &a[i], &b[i]);
}
for (size_t i = 1; i <= Q; i++) {
scanf("%d %d", &p[i], &x[i]);
}
}
void solve() {
for (size_t i = 1; i <= N - 1; i++) {
v[b[i]].root = a[i];
}
for (size_t i = 1; i <= Q; i++) {
v[p[i]].count += x[i];
}
for (size_t i = 1; i <= N; i++) {
v[i].ans += v[i].count;
if (v[i].root != 0) {
v[i].ans += v[v[i].root].ans;
}
printf("%ld ", v[i].ans);
}
printf("\n");
}
int main(void) {
receive_input();
solve();
return 0;
}
| #include <algorithm>
#include <stdio.h>
// 入力受付用の変数
#define BUF 200002
char str[BUF];
// 入力された値
#define LIM_A 200002
int N, Q;
int a[LIM_A];
int b[LIM_A];
int p[LIM_A];
int x[LIM_A];
typedef struct {
int root;
int count;
long ans;
} Tree;
Tree v[LIM_A];
void receive_input() {
fgets(str, sizeof(str), stdin);
sscanf(str, "%d %d", &N, &Q);
for (size_t i = 1; i <= N - 1; i++) {
scanf("%d %d", &a[i], &b[i]);
}
for (size_t i = 1; i <= Q; i++) {
scanf("%d %d", &p[i], &x[i]);
}
}
void solve() {
for (size_t i = 1; i <= N - 1; i++) {
v[b[i]].root = a[i];
}
for (size_t i = 1; i <= Q; i++) {
v[p[i]].count += x[i];
}
for (size_t i = 1; i <= N; i++) {
v[i].ans += v[i].count;
if (v[i].root != 0) {
v[i].ans += v[v[i].root].ans;
}
printf("%ld ", v[i].ans);
}
printf("\n");
}
int main(void) {
receive_input();
solve();
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> v[100010];
int n, i, j, x, y, a[100001], q, nod, val;
bool viz[100001];
void df(int nod) {
int i;
viz[nod] = 1;
for (i = 0; i < v[nod].size(); i++)
if (!viz[v[nod][i]]) {
a[v[nod][i]] += a[nod];
df(v[nod][i]);
}
}
int main() {
cin >> n >> q;
for (i = 1; i < n; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (i = 1; i <= q; i++) {
cin >> nod >> val;
a[nod] += val;
}
df(1);
for (i = 1; i <= n; i++)
cout << a[i] << ' ';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
vector<int> v[200010];
int n, i, j, x, y, a[200001], q, nod, val;
bool viz[200001];
void df(int nod) {
int i;
viz[nod] = 1;
for (i = 0; i < v[nod].size(); i++)
if (!viz[v[nod][i]]) {
a[v[nod][i]] += a[nod];
df(v[nod][i]);
}
}
int main() {
cin >> n >> q;
for (i = 1; i < n; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (i = 1; i <= q; i++) {
cin >> nod >> val;
a[nod] += val;
}
df(1);
for (i = 1; i <= n; i++)
cout << a[i] << ' ';
return 0;
}
| replace | 2 | 5 | 2 | 5 | 0 | |
p02936 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
void rec(int parent, int self, int cntsum, vector<set<int>> connect,
vector<int> cntarr, vector<int> &ans) {
connect[self].erase(parent);
for (auto itr = connect[self].begin(); itr != connect[self].end(); itr++) {
int child = *itr;
int copycntsum = cntsum + cntarr[child];
rec(self, child, copycntsum, connect, cntarr, ans);
}
ans[self] = cntsum;
}
int main() {
int n, q;
cin >> n >> q;
vector<set<int>> connect(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
connect[a].insert(b);
connect[b].insert(a);
}
vector<int> cntarr(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
cntarr[p] += x;
}
vector<int> ans(n);
rec(-1, 0, cntarr[0], connect, cntarr, ans);
rep(i, n) { cout << ans[i] << " "; }
cout << endl;
} | #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>;
void rec(int parent, int self, int cntsum, vector<set<int>> &connect,
vector<int> &cntarr, vector<int> &ans) {
connect[self].erase(parent);
for (auto itr = connect[self].begin(); itr != connect[self].end(); itr++) {
int child = *itr;
int copycntsum = cntsum + cntarr[child];
rec(self, child, copycntsum, connect, cntarr, ans);
}
ans[self] = cntsum;
}
int main() {
int n, q;
cin >> n >> q;
vector<set<int>> connect(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
connect[a].insert(b);
connect[b].insert(a);
}
vector<int> cntarr(n);
rep(i, q) {
int p, x;
cin >> p >> x;
p--;
cntarr[p] += x;
}
vector<int> ans(n);
rec(-1, 0, cntarr[0], connect, cntarr, ans);
rep(i, n) { cout << ans[i] << " "; }
cout << endl;
} | replace | 6 | 8 | 6 | 8 | TLE | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010, M = 2 * N;
int n, m;
int h[N], e[M], ne[M], idx;
int w[N];
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
void dfs(int u, int fa, int s) {
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (j == fa)
continue;
w[j] += s;
dfs(j, u, w[j]);
}
}
int main() {
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
for (int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
for (int i = 0; i < m; i++) {
int p, x;
scanf("%d%d", &p, &x);
w[p] += x;
}
dfs(1, -1, w[1]);
for (int i = 1; i <= n; i++)
cout << w[i] << ' ';
puts("");
return 0;
} | #include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 200010, M = 2 * N;
int n, m;
int h[N], e[M], ne[M], idx;
int w[N];
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
void dfs(int u, int fa, int s) {
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (j == fa)
continue;
w[j] += s;
dfs(j, u, w[j]);
}
}
int main() {
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
for (int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
for (int i = 0; i < m; i++) {
int p, x;
scanf("%d%d", &p, &x);
w[p] += x;
}
dfs(1, -1, w[1]);
for (int i = 1; i <= n; i++)
cout << w[i] << ' ';
puts("");
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
vector<int> g[100001];
ll score[100001];
void dfs(int v, int pa, ll sum) {
score[v] += sum;
for (int i = 0; i < g[v].size(); i++) {
if (g[v][i] != pa) {
dfs(g[v][i], v, score[v]);
}
}
}
int main() {
int n, q, a, b, p;
ll x;
scanf("%d%d", &n, &q);
for (int i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 0; i < q; i++) {
scanf("%d %lld", &p, &x);
score[p] += x;
}
dfs(1, -1, 0LL);
for (int i = 1; i <= n; i++) {
printf("%lld ", score[i]);
}
printf("\n");
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
vector<int> g[200001];
ll score[200001];
void dfs(int v, int pa, ll sum) {
score[v] += sum;
for (int i = 0; i < g[v].size(); i++) {
if (g[v][i] != pa) {
dfs(g[v][i], v, score[v]);
}
}
}
int main() {
int n, q, a, b, p;
ll x;
scanf("%d%d", &n, &q);
for (int i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 0; i < q; i++) {
scanf("%d %lld", &p, &x);
score[p] += x;
}
dfs(1, -1, 0LL);
for (int i = 1; i <= n; i++) {
printf("%lld ", score[i]);
}
printf("\n");
} | replace | 6 | 8 | 6 | 8 | 0 | |
p02936 | C++ | Runtime Error | /* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
// algorithm library
#include <algorithm>
#include <cmath>
#include <cstring>
#include <numeric>
#include <random>
// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
/* ---------- Namespace ---------- */
using namespace std;
/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>
/* ---------- Constants */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;
const int MAX_N = 100000;
int N, Q;
vector<int> table[MAX_N];
int cnt[MAX_N];
int ret[MAX_N];
void dfs(int node, int par, int score) {
score += cnt[node];
ret[node] = score;
for (int next : table[node]) {
if (next == par)
continue;
dfs(next, node, score);
}
}
signed main() {
cin >> N >> Q;
for (int j = 0; j < N - 1; j++) {
int a, b;
cin >> a >> b;
a--;
b--;
table[a].push_back(b);
table[b].push_back(a);
}
for (int q = 0; q < Q; q++) {
int p, x;
cin >> p >> x;
p--;
cnt[p] += x;
}
dfs(0, -1, 0);
for (int i = 0; i < N; i++) {
cout << ret[i];
if (i != N - 1)
cout << " ";
}
cout << endl;
return 0;
} | /* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
// algorithm library
#include <algorithm>
#include <cmath>
#include <cstring>
#include <numeric>
#include <random>
// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
/* ---------- Namespace ---------- */
using namespace std;
/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>
/* ---------- Constants */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;
const int MAX_N = 200000;
int N, Q;
vector<int> table[MAX_N];
int cnt[MAX_N];
int ret[MAX_N];
void dfs(int node, int par, int score) {
score += cnt[node];
ret[node] = score;
for (int next : table[node]) {
if (next == par)
continue;
dfs(next, node, score);
}
}
signed main() {
cin >> N >> Q;
for (int j = 0; j < N - 1; j++) {
int a, b;
cin >> a >> b;
a--;
b--;
table[a].push_back(b);
table[b].push_back(a);
}
for (int q = 0; q < Q; q++) {
int p, x;
cin >> p >> x;
p--;
cnt[p] += x;
}
dfs(0, -1, 0);
for (int i = 0; i < N; i++) {
cout << ret[i];
if (i != N - 1)
cout << " ";
}
cout << endl;
return 0;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int n, q, a, b, dfn[maxn], in[maxn], out[maxn], del[maxn], sum, ans[maxn];
vector<int> to[maxn];
void dfs(int x, int f) {
dfn[++dfn[0]] = x;
in[x] = dfn[0];
for (int i = 0; i < to[x].size(); ++i) {
int t = to[x][i];
if (t == f)
continue;
dfs(t, x);
}
out[x] = dfn[0];
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 1; i < n; ++i) {
scanf("%d %d", &a, &b);
to[a].push_back(b);
to[b].push_back(a);
}
dfs(1, 0);
for (int i = 1; i <= q; ++i) {
scanf("%d %d", &a, &b);
del[in[a]] += b;
del[out[a] + 1] -= b;
}
for (int i = 1; i <= n; ++i) {
sum += del[i];
ans[dfn[i]] = sum;
}
for (int i = 1; i <= n; ++i)
printf("%d ", ans[i]);
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n, q, a, b, dfn[maxn], in[maxn], out[maxn], del[maxn], sum, ans[maxn];
vector<int> to[maxn];
void dfs(int x, int f) {
dfn[++dfn[0]] = x;
in[x] = dfn[0];
for (int i = 0; i < to[x].size(); ++i) {
int t = to[x][i];
if (t == f)
continue;
dfs(t, x);
}
out[x] = dfn[0];
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 1; i < n; ++i) {
scanf("%d %d", &a, &b);
to[a].push_back(b);
to[b].push_back(a);
}
dfs(1, 0);
for (int i = 1; i <= q; ++i) {
scanf("%d %d", &a, &b);
del[in[a]] += b;
del[out[a] + 1] -= b;
}
for (int i = 1; i <= n; ++i) {
sum += del[i];
ans[dfn[i]] = sum;
}
for (int i = 1; i <= n; ++i)
printf("%d ", ans[i]);
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int n, q;
vector<vector<int>> G;
vector<ll> a, V;
void dfs(int v, ll val) {
V[v] = a[v] + val;
for (const auto nv : G[v])
if (V[nv] == 0)
dfs(nv, V[v]);
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
G.resize(n);
V.assign(n, 0);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
a.resize(n, 0);
rep(i, q) {
ll x, y;
cin >> x >> y;
x--;
a[x] += y;
}
dfs(0, 0ll);
for (const auto &v : V)
cout << v << " ";
cout << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int n, q;
vector<vector<int>> G;
vector<ll> a, V;
void dfs(int v, int from) {
V[v] = a[v] + V[from];
for (const auto nv : G[v]) {
if (nv == from)
continue;
dfs(nv, v);
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
G.resize(n);
V.assign(n, 0);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
a.resize(n, 0);
rep(i, q) {
ll x, y;
cin >> x >> y;
x--;
a[x] += y;
}
dfs(0, 0ll);
for (const auto &v : V)
cout << v << " ";
cout << endl;
}
| replace | 16 | 21 | 16 | 23 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename A, typename B> istream &operator>>(istream &, pair<A, B> &);
template <typename A> istream &operator>>(istream &, vector<A> &);
void add(int &, int);
int mul(int, int);
int powz(int, int);
template <typename A> ostream &operator<<(ostream &, vector<A> &);
const int N = 100002;
int dp[N], ans[N];
vi adj[N];
void dfs(int u, int p) {
if (p != -1) {
ans[u] += ans[p];
}
for (auto i : adj[u]) {
if (i != p) {
dfs(i, u);
}
}
}
void solve() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].pb(y);
adj[y].pb(x);
}
while (q--) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0, -1);
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tc = 1;
//~ cin>>tc;
for (int _ = 0; _ < tc; _++) {
//~ cout<<"Case #"<<_+1<<": ";
solve();
if (_ != tc - 1)
cout << "\n";
}
}
int mul(int a, int b) { return ((a)*1ll * (b)) % mod; }
void add(int &a, int b) {
a += b;
if (a >= mod)
a -= mod;
}
int powz(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = mul(res, a);
b /= 2;
a = mul(a, a);
}
return res;
}
template <typename A, typename B>
istream &operator>>(istream &input, pair<A, B> &x) {
input >> x.F >> x.S;
return input;
}
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
for (auto &i : x)
input >> i;
return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
for (auto &i : x)
output << i << ' ';
return output;
}
| #include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename A, typename B> istream &operator>>(istream &, pair<A, B> &);
template <typename A> istream &operator>>(istream &, vector<A> &);
void add(int &, int);
int mul(int, int);
int powz(int, int);
template <typename A> ostream &operator<<(ostream &, vector<A> &);
const int N = 200002;
int dp[N], ans[N];
vi adj[N];
void dfs(int u, int p) {
if (p != -1) {
ans[u] += ans[p];
}
for (auto i : adj[u]) {
if (i != p) {
dfs(i, u);
}
}
}
void solve() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].pb(y);
adj[y].pb(x);
}
while (q--) {
int p, x;
cin >> p >> x;
p--;
ans[p] += x;
}
dfs(0, -1);
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tc = 1;
//~ cin>>tc;
for (int _ = 0; _ < tc; _++) {
//~ cout<<"Case #"<<_+1<<": ";
solve();
if (_ != tc - 1)
cout << "\n";
}
}
int mul(int a, int b) { return ((a)*1ll * (b)) % mod; }
void add(int &a, int b) {
a += b;
if (a >= mod)
a -= mod;
}
int powz(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = mul(res, a);
b /= 2;
a = mul(a, a);
}
return res;
}
template <typename A, typename B>
istream &operator>>(istream &input, pair<A, B> &x) {
input >> x.F >> x.S;
return input;
}
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
for (auto &i : x)
input >> i;
return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
for (auto &i : x)
output << i << ' ';
return output;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
int label[200005];
int smd[200005];
int n, q;
void dfs_out(int node, int last, int s) {
smd[node] = label[node] + s;
for (int i = 0; i < adj[node].size(); i++) {
int other = adj[node][i];
if (other == last)
continue;
dfs_out(other, node, smd[node]);
}
}
int main() {
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 0; i < q; i++) {
int nodenum, val;
cin >> nodenum >> val;
label[nodenum] += val;
}
dfs_out(1, -1, 0);
for (int i = 1; i <= n; i++) {
cout << smd[i] << ' ';
}
} | #include <bits/stdc++.h>
using namespace std;
vector<int> adj[200005];
int label[200005];
int smd[200005];
int n, q;
void dfs_out(int node, int last, int s) {
smd[node] = label[node] + s;
for (int i = 0; i < adj[node].size(); i++) {
int other = adj[node][i];
if (other == last)
continue;
dfs_out(other, node, smd[node]);
}
}
int main() {
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 0; i < q; i++) {
int nodenum, val;
cin >> nodenum >> val;
label[nodenum] += val;
}
dfs_out(1, -1, 0);
for (int i = 1; i <= n; i++) {
cout << smd[i] << ' ';
}
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02936 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
int N;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int e : to[v]) {
if (p == e) {
continue;
}
ans[e] += ans[v];
dfs(e, v);
}
}
int main() {
int N, Q;
cin >> N >> Q;
for (int n = 0; n < N - 1; ++n) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(N);
for (int n = 0; n < N - 1; ++n) {
int p, q;
cin >> p >> q;
p--;
ans[p] += q;
}
dfs(0);
for (int n = 0; n < N; ++n) {
cout << ans[n] << endl;
}
return 0;
} | #include "bits/stdc++.h"
using namespace std;
int N;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
for (int e : to[v]) {
if (p == e) {
continue;
}
ans[e] += ans[v];
dfs(e, v);
}
}
int main() {
int N, Q;
cin >> N >> Q;
for (int n = 0; n < N - 1; ++n) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
ans.resize(N);
for (int n = 0; n < Q; ++n) {
int p, q;
cin >> p >> q;
p--;
ans[p] += q;
}
dfs(0);
for (int n = 0; n < N; ++n) {
cout << ans[n] << endl;
}
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02936 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> adjList[112123];
long long v[112123];
bool seen[112123];
void dfs(int x) {
seen[x] = true;
for (int y : adjList[x]) {
if (seen[y])
continue;
v[y] += v[x];
dfs(y);
}
}
int main() {
cin >> n >> q;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
adjList[x].push_back(y);
adjList[y].push_back(x);
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
v[x] += y;
}
dfs(1);
for (int i = 1; i <= n; i++) {
cout << v[i] << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> adjList[212123];
long long v[212123];
bool seen[212123];
void dfs(int x) {
seen[x] = true;
for (int y : adjList[x]) {
if (seen[y])
continue;
v[y] += v[x];
dfs(y);
}
}
int main() {
cin >> n >> q;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
adjList[x].push_back(y);
adjList[y].push_back(x);
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
v[x] += y;
}
dfs(1);
for (int i = 1; i <= n; i++) {
cout << v[i] << '\n';
}
return 0;
}
| replace | 5 | 8 | 5 | 8 | 0 | |
p02936 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// typedef long long unsigned int ll;
typedef long long int ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define MAX_N 100000
int n, q;
vector<int> a[MAX_N];
int temp_a, temp_b;
int c[MAX_N];
void solver(int i, int parent) {
for (int j : a[i]) {
if (j != parent) {
c[j] += c[i];
solver(j, i);
}
}
return;
}
int main(void) {
int i, p, x;
cin >> n >> q;
REP(i, n - 1) {
cin >> temp_a >> temp_b;
temp_a--;
temp_b--;
a[temp_a].push_back(temp_b);
a[temp_b].push_back(temp_a);
}
REP(i, n) { c[i] = 0; }
REP(i, q) {
cin >> p >> x;
c[p - 1] += x;
}
solver(0, -1);
REP(i, n) { cout << c[i] << " "; }
cout << endl;
return 0;
} | #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// typedef long long unsigned int ll;
typedef long long int ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define MAX_N 200000
int n, q;
vector<int> a[MAX_N];
int temp_a, temp_b;
int c[MAX_N];
void solver(int i, int parent) {
for (int j : a[i]) {
if (j != parent) {
c[j] += c[i];
solver(j, i);
}
}
return;
}
int main(void) {
int i, p, x;
cin >> n >> q;
REP(i, n - 1) {
cin >> temp_a >> temp_b;
temp_a--;
temp_b--;
a[temp_a].push_back(temp_b);
a[temp_b].push_back(temp_a);
}
REP(i, n) { c[i] = 0; }
REP(i, q) {
cin >> p >> x;
c[p - 1] += x;
}
solver(0, -1);
REP(i, n) { cout << c[i] << " "; }
cout << endl;
return 0;
} | replace | 29 | 30 | 29 | 30 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.