답안 #974058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
974058 2024-05-02T16:48:10 Z happy_node Reconstruction Project (JOI22_reconstruction) C++17
42 / 100
5000 ms 418468 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MX=505;

int N,M,Q;

struct DSU {
	int par[MX],sz[MX];

	int fd(int v) {
		return par[v]==v?v:par[v]=fd(par[v]);
	}

	bool merge(int u, int v) {
		u=fd(u),v=fd(v);
		if(u==v) return false;
		if(sz[u]>sz[v]) swap(u,v);
		par[u]=v;
		sz[v]+=sz[u];
		return true;
	}

	void reset() {
		for(int i=1;i<=N;i++) par[i]=i,sz[i]=1;
	}
} ds;

set<pair<int,int>> adj[MX];

const int inf=2e9;

array<int,3> bfs(int S, int T) {
	queue<int> q;
	q.push(S);
	
	vector<array<int,3>> dist(N+1,{-1,-1,-1});
	dist[S]={inf,inf,inf};

	while(!q.empty()) {
		auto v=q.front(); q.pop();
		for(auto [u,w]:adj[v]) {
			if(dist[u][0]!=-1) continue;

			array<int,3> cur={w,min(u,v),max(u,v)};
			dist[u]=min(cur,dist[v]);
			q.push(u);
		}
	}

	return dist[T];
}

array<int,3> BFS(int S, int T) {
	queue<int> q;
	q.push(S);
	
	vector<array<int,3>> dist(N+1,{-1,-1,-1});
	dist[S]={0,0,0};

	while(!q.empty()) {
		auto v=q.front(); q.pop();
		for(auto [u,w]:adj[v]) {
			if(dist[u][0]!=-1) continue;

			array<int,3> cur={w,min(u,v),max(u,v)};
			dist[u]=max(cur,dist[v]);
			q.push(u);
		}
	}

	return dist[T];
}

array<int,3> e[100000+5];
vector<int> PF[100000+5], SF[100000+5];

set<array<int,4>> E;
map<array<int,3>, int> id;

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);

	cin>>N>>M;

	vector<array<int,3>> edges;

	for(int i=0;i<M;i++) {
		int u,v,w;
		cin>>u>>v>>w;
		e[i]={w,u,v};
		id[e[i]]=i;
		edges.push_back({w,u,v});
	}		

	sort(edges.begin(),edges.end());

	int i=0;
	for(auto [w,u,v]:edges) {
		assert(E.size()<N);
		auto [ww,uu,vv]=bfs(u,v);
		if(ww!=-1) {
			adj[uu].erase({vv,ww});
			adj[vv].erase({uu,ww});
			E.erase({ww,uu,vv,id[{ww,uu,vv}]});
		}
		adj[u].insert({v,w});
		adj[v].insert({u,w});
		E.insert({w,u,v,id[{w,u,v}]});
		for(auto [ww,uu,vv,id]:E) {
			PF[i].push_back(id);
		}
		reverse(PF[i].begin(),PF[i].end());
		i++;
	}

	reverse(edges.begin(),edges.end());
	E.clear();
	for(int i=1;i<=N;i++) adj[i].clear();

	i=M-1;
	for(auto [w,u,v]:edges) {
		assert(E.size()<N);
		auto [ww,uu,vv]=BFS(u,v);
		if(ww!=-1) {
			adj[uu].erase({vv,ww});
			adj[vv].erase({uu,ww});
			E.erase({ww,uu,vv,id[{ww,uu,vv}]});
		}
		adj[u].insert({v,w});
		adj[v].insert({u,w});
		E.insert({w,u,v,id[{w,u,v}]});
		for(auto [ww,uu,vv,id]:E) {
			SF[i].push_back(id);
		}
		i--;
	}

	reverse(edges.begin(),edges.end());

	cin>>Q;

	int p=-1;
	for(int i=0;i<Q;i++) {
		int x;
		cin>>x;
		
		while(p+1<edges.size() && x>=edges[p+1][0]) {
			p++;
		}

		ds.reset();
		ll res=0;
		if(p==(int)edges.size()-1) {
			// PF back()
			for(auto id:PF[p]) {
				auto [w,u,v]=e[id];
				res+=x-w;
			}
		} else if(p==-1) {
			// SF[0]
			for(auto id:SF[0]) {
				auto [w,u,v]=e[id];
				res+=w-x;
			}
		} else {
			// PF[p], Sf[p+1]
			int i=0,j=0,c=0; // 2 pointers
			while(i<PF[p].size()&&j<SF[p+1].size()&&c<N-1) {
				int id=PF[p][i];
				int ID=SF[p+1][j];

				if(x-e[id][0]<e[ID][0]-x) {
					if(ds.merge(e[id][1],e[id][2]))
						res+=x-e[id][0],c+=1;
					i++;
				} else {
					if(ds.merge(e[ID][1],e[ID][2]))
						res+=e[ID][0]-x,c+=1;
					j++;
				}
			}
			while(i<PF[p].size() && c<N-1) {
				int id=PF[p][i];
				auto [w,u,v]=e[id];
				if(ds.merge(u,v))
					res+=x-w,c+=1;
				i++;
			}

			while(j<SF[p+1].size() && c<N-1) {
				int id=SF[p+1][j];
				auto [w,u,v]=e[id];
				if(ds.merge(u,v))
					res+=w-x,c+=1;
				j++;
			}
		}

		cout<<res<<'\n';
	}
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from reconstruction.cpp:1:
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:101:18: warning: comparison of integer expressions of different signedness: 'std::set<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  101 |   assert(E.size()<N);
      |          ~~~~~~~~^~
reconstruction.cpp:124:18: warning: comparison of integer expressions of different signedness: 'std::set<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  124 |   assert(E.size()<N);
      |          ~~~~~~~~^~
reconstruction.cpp:149:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |   while(p+1<edges.size() && x>=edges[p+1][0]) {
      |         ~~~^~~~~~~~~~~~~
reconstruction.cpp:170:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |    while(i<PF[p].size()&&j<SF[p+1].size()&&c<N-1) {
      |          ~^~~~~~~~~~~~~
reconstruction.cpp:170:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |    while(i<PF[p].size()&&j<SF[p+1].size()&&c<N-1) {
      |                          ~^~~~~~~~~~~~~~~
reconstruction.cpp:184:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |    while(i<PF[p].size() && c<N-1) {
      |          ~^~~~~~~~~~~~~
reconstruction.cpp:192:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  192 |    while(j<SF[p+1].size() && c<N-1) {
      |          ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5068 KB Output is correct
11 Correct 1 ms 4952 KB Output is correct
12 Correct 1 ms 4952 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5068 KB Output is correct
11 Correct 1 ms 4952 KB Output is correct
12 Correct 1 ms 4952 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3933 ms 417172 KB Output is correct
21 Correct 2451 ms 416644 KB Output is correct
22 Correct 3318 ms 416632 KB Output is correct
23 Correct 3546 ms 416984 KB Output is correct
24 Correct 3773 ms 416848 KB Output is correct
25 Correct 3892 ms 416940 KB Output is correct
26 Correct 3944 ms 416972 KB Output is correct
27 Correct 3845 ms 416884 KB Output is correct
28 Correct 4086 ms 417012 KB Output is correct
29 Correct 2977 ms 414260 KB Output is correct
30 Correct 3954 ms 417144 KB Output is correct
31 Correct 3941 ms 416940 KB Output is correct
32 Correct 3929 ms 417092 KB Output is correct
33 Correct 3968 ms 416928 KB Output is correct
34 Correct 2394 ms 387076 KB Output is correct
35 Correct 3901 ms 416888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Execution timed out 5095 ms 418468 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5068 KB Output is correct
11 Correct 1 ms 4952 KB Output is correct
12 Correct 1 ms 4952 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 2 ms 4952 KB Output is correct
20 Execution timed out 5033 ms 19616 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5068 KB Output is correct
11 Correct 1 ms 4952 KB Output is correct
12 Correct 1 ms 4952 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3933 ms 417172 KB Output is correct
21 Correct 2451 ms 416644 KB Output is correct
22 Correct 3318 ms 416632 KB Output is correct
23 Correct 3546 ms 416984 KB Output is correct
24 Correct 3773 ms 416848 KB Output is correct
25 Correct 3892 ms 416940 KB Output is correct
26 Correct 3944 ms 416972 KB Output is correct
27 Correct 3845 ms 416884 KB Output is correct
28 Correct 4086 ms 417012 KB Output is correct
29 Correct 2977 ms 414260 KB Output is correct
30 Correct 3954 ms 417144 KB Output is correct
31 Correct 3941 ms 416940 KB Output is correct
32 Correct 3929 ms 417092 KB Output is correct
33 Correct 3968 ms 416928 KB Output is correct
34 Correct 2394 ms 387076 KB Output is correct
35 Correct 3901 ms 416888 KB Output is correct
36 Correct 4411 ms 417384 KB Output is correct
37 Correct 2940 ms 417176 KB Output is correct
38 Correct 3768 ms 417268 KB Output is correct
39 Correct 4058 ms 417264 KB Output is correct
40 Correct 4285 ms 416976 KB Output is correct
41 Correct 4331 ms 417284 KB Output is correct
42 Correct 4386 ms 417216 KB Output is correct
43 Correct 4455 ms 417288 KB Output is correct
44 Correct 4387 ms 417260 KB Output is correct
45 Correct 3102 ms 414204 KB Output is correct
46 Correct 4448 ms 417664 KB Output is correct
47 Correct 4472 ms 417244 KB Output is correct
48 Correct 4563 ms 417336 KB Output is correct
49 Correct 4470 ms 417232 KB Output is correct
50 Correct 2407 ms 387448 KB Output is correct
51 Correct 4176 ms 417248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5068 KB Output is correct
11 Correct 1 ms 4952 KB Output is correct
12 Correct 1 ms 4952 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3933 ms 417172 KB Output is correct
21 Correct 2451 ms 416644 KB Output is correct
22 Correct 3318 ms 416632 KB Output is correct
23 Correct 3546 ms 416984 KB Output is correct
24 Correct 3773 ms 416848 KB Output is correct
25 Correct 3892 ms 416940 KB Output is correct
26 Correct 3944 ms 416972 KB Output is correct
27 Correct 3845 ms 416884 KB Output is correct
28 Correct 4086 ms 417012 KB Output is correct
29 Correct 2977 ms 414260 KB Output is correct
30 Correct 3954 ms 417144 KB Output is correct
31 Correct 3941 ms 416940 KB Output is correct
32 Correct 3929 ms 417092 KB Output is correct
33 Correct 3968 ms 416928 KB Output is correct
34 Correct 2394 ms 387076 KB Output is correct
35 Correct 3901 ms 416888 KB Output is correct
36 Correct 1 ms 4956 KB Output is correct
37 Correct 1 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Execution timed out 5095 ms 418468 KB Time limit exceeded
40 Halted 0 ms 0 KB -