Submission #681136

#TimeUsernameProblemLanguageResultExecution timeMemory
681136arnold518Reconstruction Project (JOI22_reconstruction)C++17
42 / 100
5032 ms225988 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 500;
const int MAXM = 1e5;
const int MAXQ = 1e6;
const int INF = 1e9+7;

struct Edge
{
	int u, v, w;
};

int N, M, Q;
Edge A[MAXM+10];
pii B[MAXQ+10];

vector<int> V[MAXM+10];

struct UF
{
	int par[MAXN+10], sz[MAXN+10];
	void init()
	{
		for(int i=1; i<=N; i++) par[i]=i, sz[i]=1;
	}
	int Find(int x)
	{
		if(x==par[x]) return x;
		return par[x]=Find(par[x]);
	}
	void Union(int x, int y)
	{
		x=Find(x); y=Find(y);
		if(x==y) return;
		if(sz[x]>sz[y]) swap(x, y);
		if(sz[x]==sz[y]) sz[y]++;
		par[x]=y;
	}
};

ll ans[MAXQ+10];

int main()
{
	scanf("%d%d", &N, &M);
	for(int i=1; i<=M; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		A[i]={u, v, w};
	}
	sort(A+1, A+M+1, [&](const Edge &p, const Edge &q) { return p.w<q.w; });
	scanf("%d", &Q);
	for(int i=1; i<=Q; i++) scanf("%d", &B[i].first), B[i].second=i;
	sort(B+1, B+Q+1);

	UF uf;
	for(int i=M; i>=1; i--)
	{
		uf.init();
		uf.Union(A[i].u, A[i].v);
		V[i].push_back(i);
		for(auto it : V[i+1])
		{
			if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
			uf.Union(A[it].u, A[it].v);
			V[i].push_back(it);
		}
	}
	A[M+1].w=INF;

	vector<int> V2;
	for(int i=1, j=1; i<=M+1; i++)
	{
		for(; j<=Q && B[j].first<=A[i].w; j++)
		{
			uf.init();
			int l=0, r=0;
			while(l<V2.size() || r<V[i].size())
			{
				int it;
				if(l<V2.size() && (r==V[i].size() || B[j].first-A[V2[l]].w<A[V[i][r]].w-B[j].first)) it=V2[l++];
				else it=V[i][r++];

				if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
				uf.Union(A[it].u, A[it].v);
				ans[B[j].second]+=abs(A[it].w-B[j].first);
			}
		}

		if(i>M) break;

		vector<int> VV;

		uf.init();
		uf.Union(A[i].u, A[i].v);
		VV.push_back(i);
		for(auto it : V2)
		{
			if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
			uf.Union(A[it].u, A[it].v);
			VV.push_back(it);
		}
		V2=VV;
	}
	for(int i=1; i<=Q; i++) printf("%lld\n", ans[i]);

}

Compilation message (stderr)

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:84:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |    while(l<V2.size() || r<V[i].size())
      |          ~^~~~~~~~~~
reconstruction.cpp:84:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |    while(l<V2.size() || r<V[i].size())
      |                         ~^~~~~~~~~~~~
reconstruction.cpp:87:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     if(l<V2.size() && (r==V[i].size() || B[j].first-A[V2[l]].w<A[V[i][r]].w-B[j].first)) it=V2[l++];
      |        ~^~~~~~~~~~
reconstruction.cpp:87:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     if(l<V2.size() && (r==V[i].size() || B[j].first-A[V2[l]].w<A[V[i][r]].w-B[j].first)) it=V2[l++];
      |                        ~^~~~~~~~~~~~~
reconstruction.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
reconstruction.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |   scanf("%d%d%d", &u, &v, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
reconstruction.cpp:59:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |  for(int i=1; i<=Q; i++) scanf("%d", &B[i].first), B[i].second=i;
      |                          ~~~~~^~~~~~~~~~~~~~~~~~~
#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...