Submission #887315

#TimeUsernameProblemLanguageResultExecution timeMemory
887315chanhchuong123Janjetina (COCI21_janjetina)C++14
110 / 110
283 ms15564 KiB
#include <bits/stdc++.h> using namespace std; #define task "HOTPOT" #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int MAX = 100010; int sz[MAX]; int nNode, k; int bit[MAX]; bool rem[MAX]; long long res = 0; vector<pair<int, int>> adj[MAX]; void update(int id, int delta) { for (++id; id < MAX; id += id & -id) bit[id] += delta; } int get(int id) { int res = 0; id = min(id, MAX - 1); for (++id; id > 0; id -= id & -id) res += bit[id]; return res; } int dfsSZ(int u, int p) { sz[u] = 1; for (auto [v, w]: adj[u]) if (v != p && !rem[v]) { sz[u] += dfsSZ(v, u); } return sz[u]; } int findCentroid(int u, int p, int n) { for (auto [v, w]: adj[u]) if (v != p && !rem[v]) { if ((sz[v] << 1) > n) return findCentroid(v, u, n); } return u; } vector<pair<int, int>> nodes; void dfs(int u, int p, int mxx, int dep) { nodes.emplace_back(mxx, dep); for (auto [v, w]: adj[u]) if (v != p && !rem[v]) dfs(v, u, max(mxx, w), dep + 1); } void solve(int u, int p) { int c = findCentroid(u, p, dfsSZ(u, p)); rem[c] = true; nodes.clear(); dfs(c, p, 0, 0); sort(all(nodes)); for (auto [mxx, dep]: nodes) { int maxL = mxx - k - dep; res += get(maxL); update(dep, +1); } for (auto [mxx, dep]: nodes) { update(dep, -1); } for (auto [v, w]: adj[c]) if (v != p && !rem[v]) { nodes.clear(); dfs(v, p, w, 1); sort(all(nodes)); for (auto [mxx, dep]: nodes) { int maxL = mxx - k - dep; res -= get(maxL); update(dep, +1); } for (auto [mxx, dep]: nodes) { update(dep, -1); } } for (auto [v, w]: adj[c]) if (v != p && !rem[v]) { solve(v, c); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } cin >> nNode >> k; for (int i = 1; i < nNode; ++i) { int u, v, w; cin >> u >> v >> w; adj[u].emplace_back(v, w); adj[v].emplace_back(u, w); } solve(1, -1); cout << res * 2; return 0; }

Compilation message (stderr)

Main.cpp: In function 'int dfsSZ(int, int)':
Main.cpp:32:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   32 |     for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
      |               ^
Main.cpp: In function 'int findCentroid(int, int, int)':
Main.cpp:39:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   39 |     for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
      |               ^
Main.cpp: In function 'void dfs(int, int, int, int)':
Main.cpp:50:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   50 |     for (auto [v, w]: adj[u]) if (v != p && !rem[v])
      |               ^
Main.cpp: In function 'void solve(int, int)':
Main.cpp:60:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   60 |     for (auto [mxx, dep]: nodes) {
      |               ^
Main.cpp:65:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   65 |     for (auto [mxx, dep]: nodes) {
      |               ^
Main.cpp:69:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   69 |     for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
      |               ^
Main.cpp:73:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   73 |         for (auto [mxx, dep]: nodes) {
      |                   ^
Main.cpp:78:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |         for (auto [mxx, dep]: nodes) {
      |                   ^
Main.cpp:83:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   83 |     for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
      |               ^
Main.cpp: In function 'int main()':
Main.cpp:92:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |   freopen(task".inp", "r",  stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...