Submission #807389

#TimeUsernameProblemLanguageResultExecution timeMemory
807389hugo_pmReconstruction Project (JOI22_reconstruction)C++17
0 / 100
1 ms340 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

struct uf {
	int N;
	vector<int> repr, taille;
	uf(int _N) : N(_N), repr(_N), taille(_N, 1) {
		iota(repr.begin(), repr.end(), 0);
	}
	int find(int u) {
		if (repr[u] == u) return u;
		return repr[u] = find(repr[u]);
	}
	bool unite(int u, int v) {
		u = find(u), v = find(v);
		if (u == v) return false;
		if (taille[u] < taille[v]) swap(u, v);
		repr[v] = u;
		taille[u] += taille[v];
		return true;
	}
};
struct Edge {
	int u, v, p;
};
int nbNode, nbEdge, nbReq;
vector<Edge> edges;
vector<pii> reqs;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> nbNode >> nbEdge;
	edges.resize(nbEdge);
	for (auto &[u,v,p] : edges) {
		cin >> u >> v >> p;
		--u, --v;
	}
	sort(all(edges), [&] (Edge a, Edge b) { return a.p < b.p; });
	vector<int> timesTwo;
	rep(i, 0, nbEdge) rep(j, i+1, nbEdge) {
		// |a - x| = |b - x|, a < b
		// il faut a < x < b
		// x - a = b - x <-> 2x = a+b <-> x = (a+b)/2
		timesTwo.push_back(edges[i].p + edges[j].p);
	}
	sort(all(timesTwo));
	auto getMst = [&] (int X) {
		sort(all(edges), [&] (Edge a, Edge b) { return abs(a.p-X) < abs(b.p-X); });
		vector<int> taken;
		uf mst(nbNode);
		for (auto [u, v, p] : edges) {
			if (mst.unite(u,v)) taken.push_back(p);
		}
		return taken;
	};
	cin >> nbReq;
	reqs.resize(nbReq);
	rep(iReq, 0, nbReq) {
		cin >> reqs[iReq].first;
		reqs[iReq].second = iReq;
	}
	sort(all(reqs));
	vector<int> ans(nbReq);
	int ptr = 0;
	auto v = getMst(ptr);
	for (auto [X, iReq] : reqs) {
		while (ptr < (int)timesTwo.size() - 1 && 2*X > timesTwo[ptr]) {
			++ptr;
		}
		auto v = getMst(timesTwo[ptr]/2);
		for (int p : v) {
			ans[iReq] += abs(p-X);
		}
	}
	rep(i, 0, nbReq) cout << ans[i] << '\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...