Submission #974744

# Submission time Handle Problem Language Result Execution time Memory
974744 2024-05-03T17:54:51 Z happy_node Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
3726 ms 29116 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<array<int,3>> adj[MX];
 
const int inf=2e9;

pair<int,int> dist[MX];
 
pair<int,int> bfs(int S, int T) {
	queue<int> q;
	q.push(S);

	for(int i=1;i<=N;i++) dist[i]={-1,-1};
	dist[S]={inf,inf};
 
	while(!q.empty()) {
		auto v=q.front(); q.pop();
		if(v==T) break;
		for(auto [u,w,ii]:adj[v]) {
			if(dist[u].first!=-1) continue;
 
			dist[u]=min(make_pair(w,ii),dist[v]);
			q.push(u);
		}
	}
 
	return dist[T];
}
 
pair<int,int> BFS(int S, int T) {
	queue<int> q;
	q.push(S);
	
	for(int i=1;i<=N;i++) dist[i]={-1,-1};
	dist[S]={0,0};
 
	while(!q.empty()) {
		auto v=q.front(); q.pop();
		if(v==T) break;
		for(auto [u,w,ii]:adj[v]) {
			if(dist[u].first!=-1) continue;
 
			dist[u]=max(make_pair(w,ii),dist[v]);
			q.push(u);
		}
	}
 
	return dist[T];
}
 
array<int,3> e[100000+5];
set<array<int,4>> E;
 
int L[100000+5], R[100000+5];
 
int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
 
	cin>>N>>M;
 
	for(int i=0;i<M;i++) {
		int u,v,w;
		cin>>u>>v>>w;
		e[i]={w,u,v};
	}

	sort(e,e+M);
 
	for(int i=0;i<M;i++) {
		auto [w,u,v]=e[i];

		auto [ww,ii]=bfs(u,v);
		auto [zz,uu,vv]=e[ii];

		if(ww!=-1) {
			adj[uu].erase({vv,ww,ii});
			adj[vv].erase({uu,ww,ii});
			E.erase({ww,uu,vv,ii});
		}

		adj[u].insert({v,w,i});
		adj[v].insert({u,w,i});
		E.insert({w,u,v,i});
 
		if(ww!=-1)
			L[i]=(w+ww),assert(ww<=w);
		else
			L[i]=0;
	}
 
	E.clear();
	for(int i=1;i<=N;i++) adj[i].clear();
 
	for(int i=M-1;i>=0;i--) {
		auto [w,u,v]=e[i];

		auto [ww,ii]=BFS(u,v);
		auto [zz,uu,vv]=e[ii];

		if(ww!=-1) {
			adj[uu].erase({vv,ww,ii});
			adj[vv].erase({uu,ww,ii});
			E.erase({ww,uu,vv,ii});
		}
		adj[u].insert({v,w,i});
		adj[v].insert({u,w,i});
		E.insert({w,u,v,i});

		if(ww!=-1)
			R[i]=(w+ww),assert(ww>=w);
		else
			R[i]=2e9+1;
	}
 
	vector<pair<int,int>> add, del;
 
	for(int i=0;i<M;i++) {
		add.push_back({L[i],i});
		del.push_back({R[i],i});
	}
 
	sort(add.begin(),add.end());
	sort(del.begin(),del.end());
 
	int pa=-1,pd=-1;
	set<int> curEdges;
 
	cin>>Q;
	for(int i=0;i<Q;i++) {
		ll x;
		cin>>x;
 
		while(pa+1<add.size() && add[pa+1].first<=2*x) {
			curEdges.insert(add[pa+1].second);
			pa++;
		} 
 
		while(pd+1<del.size() && del[pd+1].first<=2*x) {
			curEdges.erase(del[pd+1].second);
			pd++;
		}
 
		ll res=0;
		for(auto y:curEdges) res+=abs(e[y][0]-x);
 
		cout<<res<<'\n';
	}
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:160:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |   while(pa+1<add.size() && add[pa+1].first<=2*x) {
      |         ~~~~^~~~~~~~~~~
reconstruction.cpp:165:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |   while(pd+1<del.size() && del[pd+1].first<=2*x) {
      |         ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 408 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 408 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1454 ms 5044 KB Output is correct
21 Correct 224 ms 5808 KB Output is correct
22 Correct 231 ms 5052 KB Output is correct
23 Correct 289 ms 6084 KB Output is correct
24 Correct 625 ms 5688 KB Output is correct
25 Correct 1455 ms 6172 KB Output is correct
26 Correct 1436 ms 5256 KB Output is correct
27 Correct 1459 ms 4956 KB Output is correct
28 Correct 1570 ms 5860 KB Output is correct
29 Correct 909 ms 6220 KB Output is correct
30 Correct 1435 ms 5536 KB Output is correct
31 Correct 1445 ms 5528 KB Output is correct
32 Correct 1446 ms 6472 KB Output is correct
33 Correct 1448 ms 4604 KB Output is correct
34 Correct 856 ms 4292 KB Output is correct
35 Correct 1513 ms 6380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 2431 ms 15160 KB Output is correct
5 Correct 2401 ms 15208 KB Output is correct
6 Correct 2360 ms 15312 KB Output is correct
7 Correct 2378 ms 17232 KB Output is correct
8 Correct 2428 ms 17348 KB Output is correct
9 Correct 2288 ms 17248 KB Output is correct
10 Correct 2777 ms 17304 KB Output is correct
11 Correct 2419 ms 19568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 408 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2041 ms 12756 KB Output is correct
21 Correct 2037 ms 12988 KB Output is correct
22 Correct 2059 ms 13088 KB Output is correct
23 Correct 2033 ms 12988 KB Output is correct
24 Correct 2083 ms 12680 KB Output is correct
25 Correct 2090 ms 12752 KB Output is correct
26 Correct 2108 ms 12960 KB Output is correct
27 Correct 2095 ms 12760 KB Output is correct
28 Correct 2106 ms 12836 KB Output is correct
29 Correct 2037 ms 12880 KB Output is correct
30 Correct 2089 ms 12688 KB Output is correct
31 Correct 2135 ms 13080 KB Output is correct
32 Correct 2188 ms 13524 KB Output is correct
33 Correct 2106 ms 12588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 408 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1454 ms 5044 KB Output is correct
21 Correct 224 ms 5808 KB Output is correct
22 Correct 231 ms 5052 KB Output is correct
23 Correct 289 ms 6084 KB Output is correct
24 Correct 625 ms 5688 KB Output is correct
25 Correct 1455 ms 6172 KB Output is correct
26 Correct 1436 ms 5256 KB Output is correct
27 Correct 1459 ms 4956 KB Output is correct
28 Correct 1570 ms 5860 KB Output is correct
29 Correct 909 ms 6220 KB Output is correct
30 Correct 1435 ms 5536 KB Output is correct
31 Correct 1445 ms 5528 KB Output is correct
32 Correct 1446 ms 6472 KB Output is correct
33 Correct 1448 ms 4604 KB Output is correct
34 Correct 856 ms 4292 KB Output is correct
35 Correct 1513 ms 6380 KB Output is correct
36 Correct 1487 ms 4524 KB Output is correct
37 Correct 278 ms 4524 KB Output is correct
38 Correct 288 ms 4292 KB Output is correct
39 Correct 348 ms 4544 KB Output is correct
40 Correct 720 ms 5040 KB Output is correct
41 Correct 1461 ms 4420 KB Output is correct
42 Correct 1510 ms 4540 KB Output is correct
43 Correct 1513 ms 4352 KB Output is correct
44 Correct 1659 ms 4292 KB Output is correct
45 Correct 1016 ms 4776 KB Output is correct
46 Correct 1493 ms 4808 KB Output is correct
47 Correct 1536 ms 4320 KB Output is correct
48 Correct 1500 ms 6532 KB Output is correct
49 Correct 1536 ms 4356 KB Output is correct
50 Correct 898 ms 4648 KB Output is correct
51 Correct 1499 ms 6940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 408 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1454 ms 5044 KB Output is correct
21 Correct 224 ms 5808 KB Output is correct
22 Correct 231 ms 5052 KB Output is correct
23 Correct 289 ms 6084 KB Output is correct
24 Correct 625 ms 5688 KB Output is correct
25 Correct 1455 ms 6172 KB Output is correct
26 Correct 1436 ms 5256 KB Output is correct
27 Correct 1459 ms 4956 KB Output is correct
28 Correct 1570 ms 5860 KB Output is correct
29 Correct 909 ms 6220 KB Output is correct
30 Correct 1435 ms 5536 KB Output is correct
31 Correct 1445 ms 5528 KB Output is correct
32 Correct 1446 ms 6472 KB Output is correct
33 Correct 1448 ms 4604 KB Output is correct
34 Correct 856 ms 4292 KB Output is correct
35 Correct 1513 ms 6380 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 2431 ms 15160 KB Output is correct
40 Correct 2401 ms 15208 KB Output is correct
41 Correct 2360 ms 15312 KB Output is correct
42 Correct 2378 ms 17232 KB Output is correct
43 Correct 2428 ms 17348 KB Output is correct
44 Correct 2288 ms 17248 KB Output is correct
45 Correct 2777 ms 17304 KB Output is correct
46 Correct 2419 ms 19568 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 2041 ms 12756 KB Output is correct
49 Correct 2037 ms 12988 KB Output is correct
50 Correct 2059 ms 13088 KB Output is correct
51 Correct 2033 ms 12988 KB Output is correct
52 Correct 2083 ms 12680 KB Output is correct
53 Correct 2090 ms 12752 KB Output is correct
54 Correct 2108 ms 12960 KB Output is correct
55 Correct 2095 ms 12760 KB Output is correct
56 Correct 2106 ms 12836 KB Output is correct
57 Correct 2037 ms 12880 KB Output is correct
58 Correct 2089 ms 12688 KB Output is correct
59 Correct 2135 ms 13080 KB Output is correct
60 Correct 2188 ms 13524 KB Output is correct
61 Correct 2106 ms 12588 KB Output is correct
62 Correct 1487 ms 4524 KB Output is correct
63 Correct 278 ms 4524 KB Output is correct
64 Correct 288 ms 4292 KB Output is correct
65 Correct 348 ms 4544 KB Output is correct
66 Correct 720 ms 5040 KB Output is correct
67 Correct 1461 ms 4420 KB Output is correct
68 Correct 1510 ms 4540 KB Output is correct
69 Correct 1513 ms 4352 KB Output is correct
70 Correct 1659 ms 4292 KB Output is correct
71 Correct 1016 ms 4776 KB Output is correct
72 Correct 1493 ms 4808 KB Output is correct
73 Correct 1536 ms 4320 KB Output is correct
74 Correct 1500 ms 6532 KB Output is correct
75 Correct 1536 ms 4356 KB Output is correct
76 Correct 898 ms 4648 KB Output is correct
77 Correct 1499 ms 6940 KB Output is correct
78 Correct 3573 ms 14252 KB Output is correct
79 Correct 2417 ms 27616 KB Output is correct
80 Correct 2430 ms 26524 KB Output is correct
81 Correct 2439 ms 26644 KB Output is correct
82 Correct 2740 ms 25892 KB Output is correct
83 Correct 3527 ms 25532 KB Output is correct
84 Correct 3558 ms 25820 KB Output is correct
85 Correct 3506 ms 25680 KB Output is correct
86 Correct 3676 ms 25580 KB Output is correct
87 Correct 3723 ms 27612 KB Output is correct
88 Correct 3540 ms 25808 KB Output is correct
89 Correct 3536 ms 25824 KB Output is correct
90 Correct 3633 ms 27892 KB Output is correct
91 Correct 3629 ms 25436 KB Output is correct
92 Correct 2956 ms 28496 KB Output is correct
93 Correct 3726 ms 29116 KB Output is correct