Submission #876975

#TimeUsernameProblemLanguageResultExecution timeMemory
876975406Reconstruction Project (JOI22_reconstruction)C++17
42 / 100
5078 ms9312 KiB
//#pragma GCC optimize ("O3,unroll-loops") //#pragma GCC target("avx2,tune=native") //besmellah //accept sho :D //de akhe mashti NM * DSU nabayad AC she? //oon moghe NM log bood hichi //alan vali NM DSU ee #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) //#define int long long using namespace std; using ar = array<int, 2>; using ar5 = array<int, 5>; const int M = 1e5 + 5, N = 505, Q = 1e6 + 5; const long long INF = 1ll << 30; namespace dsu { int dpr[N]; void init() { iota(dpr, dpr + N, 0); } int gpr(int x) { return dpr[x] == x ? x : dpr[x] = gpr(dpr[x]); } bool merge(int u, int v) { u = gpr(u), v = gpr(v); //if (u == v) return false; //if (dpr[u] < dpr[v]) swap(u, v); //dpr[v] += dpr[u]; dpr[u] = v; return u != v; } } bool cmp(const ar5 &X, const ar5 &Y) { return X[0] < Y[0]; } vector<ar> adj[N]; int n, m, W[N], L[M]; ar5 E[M]; bitset<N> seen; vector<ar5> undo, bk, Es; void dfsmn(int v, int p) { seen[v] = true; for (auto [u, w]: adj[v]) if (u != p) { W[u] = min(W[v], w); dfsmn(u, v); } } void build() { FOR(i, 0, n) adj[i].clear(); seen = 0; for (auto [w, u, v, l, t]: Es) { adj[u].push_back({v, w}); adj[v].push_back({u, w}); } } ar5 validate(vector<ar5> &E) { vector<ar5> ok; ok.reserve(E.size()); dsu::init(); ar5 del = {-1, -1, -1, -1, -1}; for (auto [w, u, v, l, t]: E) { if (dsu::merge(u, v)) ok.push_back({w, u, v, l, t}); else del = {w, u, v, l, t}; } E.swap(ok); return del; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; FOR(i, 0, m) { cin >> E[i][1] >> E[i][2] >> E[i][0]; --E[i][1], --E[i][2]; if (E[i][1] > E[i][2]) swap(E[i][1], E[i][2]); } sort(E, E + m); FOR(i, 0, m) { auto [w, u, v, l, t] = E[i]; build(); W[u] = INF; dfsmn(u, u); if (seen[v]) E[i][3] = (W[v] + w + 1) / 2; else E[i][3] = -INF; Es.insert(lower_bound(Es.begin(), Es.end(), E[i], cmp), E[i]); validate(Es); } for (int i = m - 1; i >= 0; --i) { auto &[w, u, v, l, t] = E[i]; t = 1; bk.insert(lower_bound(bk.begin(), bk.end(), E[i], cmp), E[i]); //bk.push_back({w, u, v, l, 1}); undo.push_back(validate(bk)); } int q; cin >> q; int p = 0, minl = -INF; long long td, W, cnt = 0; //ar3 dar vaghe ar4-e vector<ar5> fr; FOR(i, 0, q) { int x; cin >> x; bool t = false; while (p < m && x >= E[p][0]) { ar5 X = {-E[p][0], E[p][1], E[p][2], E[p][3], 0}; fr.insert(lower_bound(fr.begin(), fr.end(), X, cmp), X); auto U = undo.back(); undo.pop_back(); bk.erase(find(bk.begin(), bk.end(), E[p])); if (U[0] != -1) { bk.insert(lower_bound(bk.begin(), bk.end(), U, cmp), U); //bk.push_back(U); } ++p; t = true; } if (t || x >= minl) { //build_mst() (M bar hadeaksar) assert(++cnt <= 3 * m); validate(fr); dsu::init(); W = td = 0; minl = INF; //al = bk + fr (bt) vector<ar5> al; //badihish al.resize(fr.size() + bk.size()); for (auto &A: fr) A[0] += x; for (auto &A: bk) A[0] -= x; merge(fr.begin(), fr.end(), bk.begin(), bk.end(), al.begin(), cmp); for (auto &A: fr) A[0] -= x; for (auto &A: bk) A[0] += x; //sort(al.begin(), al.end()); for (auto [w, u, v, l, t]: al) { w = (t ? w + x : x - w); if (dsu::merge(u, v)) { W += (t ? w : -w); td += (t ? -1 : +1); } else if (t) { minl = min(minl, l); } else { fr.erase(find(fr.begin(), fr.end(), ar5{-w, u, v, l, 0})); } } } cout << 1ll * td * x + W << '\n'; } return 0; }

Compilation message (stderr)

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:153:40: warning: 'W' may be used uninitialized in this function [-Wmaybe-uninitialized]
  153 |                 cout << 1ll * td * x + W << '\n';
      |                                        ^
reconstruction.cpp:153:34: warning: 'td' may be used uninitialized in this function [-Wmaybe-uninitialized]
  153 |                 cout << 1ll * td * x + W << '\n';
      |                         ~~~~~~~~~^~~
#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...