Submission #19132401


Source Code Expand

Copy
#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
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...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 2e5 + 10;
vector<int> a[N];
int stamp = 0;
int tin[N], tout[N], dep[N];
int v[N];

void DFS(int u, int parent) {
  dep[u] = parent < 0 ? 0 : dep[parent] + 1;
  v[stamp] = u;
  tin[u] = stamp++;
  for (auto& v : a[u]) {
    if (v == parent) continue;
    DFS(v, u);
  }
  tout[u] = stamp;
}

int main() {
  int n;
  cin >> n;
  vector<array<int, 2>> e(n - 1);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    e[i] = {x, y};
    a[x].push_back(y);
    a[y].push_back(x);
  }
  DFS(0, -1);
  int m;
  cin >> m;
  vector<int64> cnt(n + 1);
  while (m--) {
    int op, k, d;
    cin >> op >> k >> d;
    --op; --k;
    int x = e[k][0], y = e[k][1];
    if (dep[x] > dep[y]) {
      swap(x, y);
      op ^= 1;
    }
    if (op == 0) {
      cnt[0] += d;
      cnt[tin[y]] -= d;
      cnt[tout[y]] += d;
      cnt[n] -= d;
    } else {
      cnt[tin[y]] += d;
      cnt[tout[y]] -= d;
    }
  }
  for (int i = 1; i < n; ++i) cnt[i] += cnt[i - 1];
  vector<int64> ret(n);
  for (int i = 0; i < n; ++i) ret[v[i]] = cnt[i];
  for (int i = 0; i < n; ++i) cout << ret[i] << '\n';
  return 0;
}

Submission Info

Submission Time
Task E - Through Path
User cuiaoxiang
Language C++ (Clang 10.0.0)
Score 500
Code Size 3727 Byte
Status AC
Exec Time 731 ms
Memory 30332 KB

Compile Error

./Main.cpp:78:42: warning: unused variable 'inf' [-Wunused-const-variable]
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
                                         ^
./Main.cpp:79:11: warning: unused variable 'MOD' [-Wunused-const-variable]
const int MOD = 1e9 + 7;
          ^
2 warnings generated.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status AC
AC × 36
Set Name Test Cases
Sample
All 01_sample.txt, 02_sample.txt, 03_sample.txt, 04_small.txt, 05_small.txt, 06_small.txt, 07_small.txt, 08_small.txt, 09_small.txt, 10_small.txt, 11_small.txt, 12_small.txt, 13_small.txt, 14_small.txt, 15_small.txt, 16_small.txt, 17_small.txt, 18_small.txt, 19_small.txt, 20_small.txt, 21_small.txt, 22_small.txt, 23_small.txt, 24_large.txt, 25_large.txt, 26_large.txt, 27_max.txt, 28_max.txt, 29_max.txt, 30_path.txt, 31_path.txt, 32_path.txt, 33_star.txt, 34_star.txt, 35_star.txt, 36_star.txt
Case Name Status Exec Time Memory
01_sample.txt AC 11 ms 7852 KB
02_sample.txt AC 11 ms 7708 KB
03_sample.txt AC 8 ms 7820 KB
04_small.txt AC 16 ms 7716 KB
05_small.txt AC 16 ms 7832 KB
06_small.txt AC 11 ms 7756 KB
07_small.txt AC 11 ms 7692 KB
08_small.txt AC 8 ms 7768 KB
09_small.txt AC 15 ms 7764 KB
10_small.txt AC 9 ms 7820 KB
11_small.txt AC 11 ms 7760 KB
12_small.txt AC 12 ms 7760 KB
13_small.txt AC 13 ms 7800 KB
14_small.txt AC 8 ms 7812 KB
15_small.txt AC 10 ms 7832 KB
16_small.txt AC 11 ms 7828 KB
17_small.txt AC 13 ms 7700 KB
18_small.txt AC 13 ms 7844 KB
19_small.txt AC 17 ms 7780 KB
20_small.txt AC 14 ms 7804 KB
21_small.txt AC 9 ms 7808 KB
22_small.txt AC 11 ms 7700 KB
23_small.txt AC 13 ms 7704 KB
24_large.txt AC 579 ms 20372 KB
25_large.txt AC 529 ms 18424 KB
26_large.txt AC 220 ms 10408 KB
27_max.txt AC 713 ms 22284 KB
28_max.txt AC 689 ms 22284 KB
29_max.txt AC 687 ms 22508 KB
30_path.txt AC 731 ms 27036 KB
31_path.txt AC 723 ms 30332 KB
32_path.txt AC 724 ms 25844 KB
33_star.txt AC 649 ms 23040 KB
34_star.txt AC 683 ms 22644 KB
35_star.txt AC 657 ms 22960 KB
36_star.txt AC 671 ms 22956 KB