Submission #996365

# Submission time Handle Problem Language Result Execution time Memory
996365 2024-06-10T13:56:46 Z cadmiumsky Reconstruction Project (JOI22_reconstruction) C++17
0 / 100
1 ms 2652 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int mmax = 1e5 + 5;

vector<tii> scand[mmax]; 

struct DSU {
   vector<int> dsu;
   int cost;
   void init(int n) {
      dsu.assign(n, 0);
      cost = 0;
      iota(all(dsu), 0);
   }
   int f(int x) { return dsu[x] == x? x : dsu[x] = f(dsu[x]); }
   bool unite(int w, int x, int y) {
      x = f(x);
      y = f(y);
      if(x == y) return 0;
      dsu[x] = y;
      cost += w;
      return 1;
   }
   bool unite(tii a) {
      return unite(get<0>(a), get<1>(a), get<2>(a));
   }
};

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n, m;
   cin >> n >> m;
   
   map<int, int> fpozc;
   vector<tii> edges(m);
   for(auto &[w, x, y] : edges) cin >> x >> y >> w, --x, --y;
   
   sort(all(edges));
   
   auto addTo = [&](tii a, vector<tii>& nv, vector<tii> R) {
      R.insert(R.begin(), a);
      DSU d;
      d.init(n);
      nv.clear();
      for(auto t : R)
         if(d.unite(t)) nv.emplace_back(t); 
   };
   
   for(int i = m - 1; i > 0; i--) {
      addTo(edges[i], scand[i], scand[i + 1]);
   }
   
   vector<tii> pref;
   int q, p = 0;
   cin >> q;
   for(int i = 0; i < q; i++) {
      int X;
      cin >> X;
      while(p < m && get<0>(edges[p]) <= X)
         addTo(edges[p], pref, pref),
         p++;
      vector<tii> L, R, mine;
      for(auto [w, x, y] : pref) L.emplace_back(X - w, x, y);
      for(auto [w, x, y] : scand[p]) R.emplace_back(w - X, x, y);
      merge(all(L), all(R), back_inserter(mine));
      DSU d;
      d.init(n);
      for(auto x : mine)
         d.unite(x);
      cout << d.cost << '\n';
      
   }
}

/**
      Istenem! Nu poate fi real.
-- Surse verificate
*/

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -