Submission #554292

# Submission time Handle Problem Language Result Execution time Memory
554292 2022-04-28T06:34:19 Z Arvin Reconstruction Project (JOI22_reconstruction) C++11
7 / 100
5000 ms 8836 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int maxN = 505;

struct DSU {
	int parent[maxN], sz[maxN];
	
	void reset(){
		for(int x=0;x<maxN;x++){
			parent[x] = x;
			sz[x] = 1;
		}
	}
	
	int getParent(int x){
		if(parent[x] == x) return x;
		return parent[x] = getParent(parent[x]);
	}
	
	bool merge(int x, int y){
		int a = getParent(x);
		int b = getParent(y);
		
		if(a != b){
			if(sz[a] < sz[b]) swap(a, b);
			
			parent[b] = a;
			sz[a] += sz[b];
			return true;
		}
		return false;
	}
};

DSU dsu;

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	int n, m;
	cin >> n >> m;
	
	vector<array<ll, 3>> edge;
	for(int x=0;x<m;x++){
		ll a, b, w;
		cin >> a >> b >> w;
		
		a--; b--;
		edge.push_back({w, a, b});
	}
	
	sort(edge.begin(), edge.end());
	
	int q;
	cin >> q;
	
	int pos = -1;
	while(q--){
		ll val;
		cin >> val;
		
		while(pos+1 < m && edge[pos+1][0] <= val){
			pos++;
		}
		
		dsu.reset();
		int l = pos, r = pos+1;
		
		ll ans = 0;
		int cnt = n;
		while(cnt > 1){
			if(l >= 0 && r < m){
				if(val-edge[l][0] <= edge[r][0]-val){
					if(dsu.merge(edge[l][1], edge[l][2])){
						cnt--;
						ans += val-edge[l][0];
					}
					l--;
				} else {
					if(dsu.merge(edge[r][1], edge[r][2])){
						cnt--;
						ans += edge[r][0]-val;
					}
					r++;
				}
			} else if(l >= 0){
				if(dsu.merge(edge[l][1], edge[l][2])){
					cnt--;
					ans += val-edge[l][0];
				}
				l--;
			} else if(r < m){
				if(dsu.merge(edge[r][1], edge[r][2])){
					cnt--;
					ans += edge[r][0]-val;
				}
				r++;
			} else {
				assert(false);
			}
		}
		
		cout << ans << "\n";
	}
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 272 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 272 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 324 KB Output is correct
20 Correct 33 ms 4672 KB Output is correct
21 Correct 39 ms 3528 KB Output is correct
22 Correct 36 ms 3528 KB Output is correct
23 Correct 33 ms 3432 KB Output is correct
24 Correct 33 ms 3488 KB Output is correct
25 Correct 33 ms 3548 KB Output is correct
26 Correct 35 ms 4736 KB Output is correct
27 Correct 34 ms 4672 KB Output is correct
28 Correct 38 ms 4668 KB Output is correct
29 Correct 39 ms 4704 KB Output is correct
30 Correct 36 ms 4804 KB Output is correct
31 Correct 36 ms 4672 KB Output is correct
32 Correct 35 ms 4728 KB Output is correct
33 Correct 36 ms 3448 KB Output is correct
34 Correct 46 ms 4804 KB Output is correct
35 Correct 37 ms 4716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Execution timed out 5069 ms 5192 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 272 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Execution timed out 5078 ms 8836 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 272 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 324 KB Output is correct
20 Correct 33 ms 4672 KB Output is correct
21 Correct 39 ms 3528 KB Output is correct
22 Correct 36 ms 3528 KB Output is correct
23 Correct 33 ms 3432 KB Output is correct
24 Correct 33 ms 3488 KB Output is correct
25 Correct 33 ms 3548 KB Output is correct
26 Correct 35 ms 4736 KB Output is correct
27 Correct 34 ms 4672 KB Output is correct
28 Correct 38 ms 4668 KB Output is correct
29 Correct 39 ms 4704 KB Output is correct
30 Correct 36 ms 4804 KB Output is correct
31 Correct 36 ms 4672 KB Output is correct
32 Correct 35 ms 4728 KB Output is correct
33 Correct 36 ms 3448 KB Output is correct
34 Correct 46 ms 4804 KB Output is correct
35 Correct 37 ms 4716 KB Output is correct
36 Correct 551 ms 4808 KB Output is correct
37 Execution timed out 5064 ms 4964 KB Time limit exceeded
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 272 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 324 KB Output is correct
20 Correct 33 ms 4672 KB Output is correct
21 Correct 39 ms 3528 KB Output is correct
22 Correct 36 ms 3528 KB Output is correct
23 Correct 33 ms 3432 KB Output is correct
24 Correct 33 ms 3488 KB Output is correct
25 Correct 33 ms 3548 KB Output is correct
26 Correct 35 ms 4736 KB Output is correct
27 Correct 34 ms 4672 KB Output is correct
28 Correct 38 ms 4668 KB Output is correct
29 Correct 39 ms 4704 KB Output is correct
30 Correct 36 ms 4804 KB Output is correct
31 Correct 36 ms 4672 KB Output is correct
32 Correct 35 ms 4728 KB Output is correct
33 Correct 36 ms 3448 KB Output is correct
34 Correct 46 ms 4804 KB Output is correct
35 Correct 37 ms 4716 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Execution timed out 5069 ms 5192 KB Time limit exceeded
40 Halted 0 ms 0 KB -