Submission #1108674

#TimeUsernameProblemLanguageResultExecution timeMemory
1108674cpptowinPaths (RMI21_paths)C++17
36 / 100
1033 ms27720 KiB
#include <bits/stdc++.h> #define fo(i, d, c) for (int i = d; i <= c; i++) #define fod(i, c, d) for (int i = c; i >= d; i--) #define maxn 1000010 #define N 2010 #define fi first #define se second #define pb emplace_back #define en cout << "\n"; #define ll long long #define bitcount(x) __builtin_popcountll(x) #define pii pair<int, int> #define vii vector<pii> #define lb(x) x & -x #define bit(i, j) ((i >> j) & 1) #define offbit(i, j) (i ^ (1LL << j)) #define onbit(i, j) (i | (1LL << j)) #define vi vector<int> #define all(x) x.begin(), x.end() #define ss(x) (int)x.size() #define UNIQUE(v) v.erase(unique(all(v)),v.end()) template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } using namespace std; const int nsqrt = 450; const int mod = 1e9 + 7; void add(int &x, int k) { x += k; x %= mod; if(x < 0) x += mod; } void del(int &x, int k) { x -= k; x %= mod; if(x < 0) x += mod; } vii ke[maxn]; int n,k; namespace sub4 { ll f[N]; int pos[N]; int cc = 0; int topo[N]; int h[N],par[N][11]; void dfs(int u) { pos[u] = ++cc; topo[cc] = u; for(auto [v,w] : ke[u]) if(v != par[u][0]) { par[v][0] = u; h[v] = h[u] + 1; fo(i,1,10) par[v][i] = par[par[v][i - 1]][i - 1]; f[v] = f[u] + w; dfs(v); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); int del = h[u] - h[v]; fod(i, 10, 0) if (bit(del, i)) u = par[u][i]; if (u == v) return u; fod(i, 10, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i]; return par[u][0]; } void solve() { fo(i,1,n) { ll ans = 0; cc = 0; fo(i,1,n) { f[i] = 0; fo(j,0,10) par[i][j] = 0; } dfs(i); int cnt = k; priority_queue<pair<ll,int>> q; fo(i,1,n) if(ss(ke[i]) == 1) { q.push({f[i],i}); } set<int> st; while(cnt > 0 and ss(q)) { auto [du,u] = q.top(); q.pop(); auto it = st.lower_bound(pos[u]); ll val = f[u]; if(it != st.begin()) { auto it1 = prev(it); minimize(val,f[u] - f[lca(topo[*it1],u)]); } if(it != st.end()) { minimize(val,f[u] - f[lca(topo[*it],u)]); } if(val >= q.top().fi) { ans += val; st.insert(pos[u]); cnt--; } else q.push({val,u}); } cout << ans;en; } } } main() { #define name "TASK" if (fopen(name ".inp", "r")) { freopen(name ".inp", "r", stdin); freopen(name ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; fo(i,1,n - 1) { int u,v,w; cin >> u >> v >> w; ke[u].pb(v,w); ke[v].pb(u,w); } sub4::solve(); }

Compilation message (stderr)

Main.cpp:136:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  136 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...