Submission #748229

# Submission time Handle Problem Language Result Execution time Memory
748229 2023-05-25T18:53:30 Z arthur_nascimento Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
1760 ms 29868 KB
#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct UnionFind {
	int n;
	std::vector<int> a;
	UnionFind (int n) : n(n), a(n, -1) {}
	int root(int i) { return a[i] < 0 ? i : a[i] = root(a[i]); }
	bool unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (a[x] > a[y]) std::swap(x, y);
		a[x] += a[y];
		a[y] = x;
		return true;
	}
	bool same(int x, int y) { return root(x) == root(y); }
};

int main() {
	int n = ri();
	int m = ri();
	struct Hen {
		int a;
		int b;
		int c;
	};
	std::vector<Hen> hens(m);
	for (auto &i : hens) i.a = ri() - 1, i.b = ri() - 1, i.c = ri();
	
	std::sort(hens.begin(), hens.end(), [] (auto &i, auto &j) { return i.c < j.c; });
	
	// if the overall cost is ax + b, `add` is a list of
	//    {the value of x where the change occurs, {the change in a, the change in b}}
	std::vector<std::pair<int, std::pair<int, int> > > add;
	for (int i = 0; i < m; i++) {
		int cur_cost = hens[i].c;
		
		UnionFind uni(n);
		int j;
		for (j = i + 1; j < m; j++) {
			uni.unite(hens[j].a, hens[j].b);
			if (uni.same(hens[i].a, hens[i].b)) break;
		}
		int r = j;
		int r_value = r == m ? 1000000001 : cur_cost + (hens[r].c - cur_cost + 1) / 2;
		
		uni = UnionFind(n);
		for (j = i - 1; j >= 0; j--) {
			uni.unite(hens[j].a, hens[j].b);
			if (uni.same(hens[i].a, hens[i].b)) break;
		}
		int l = j;
		int l_value = l == -1 ? -1 : cur_cost - (cur_cost - hens[l].c) / 2;
		
		add.push_back({l_value, {-1, hens[i].c}}); // edge starting to be used (cost : 0 -> (hens[i].c - x))
		add.push_back({cur_cost, {2, -2 * hens[i].c}}); // at x == cur_cost (cost : (hens[i].c - x) -> (x - hens[i].c))
		add.push_back({r_value, {-1, hens[i].c}}); // edge ceasing to be used (cost : (x - hens[i].c) -> 0)
	}
	std::sort(add.begin(), add.end());
	
	size_t head = 0;
	int coeff = 0;
	int64_t constant = 0;
	int q = ri();
	for (int i = 0; i < q; i++) {
		int x = ri();
		
		while (head < add.size() && add[head].first <= x) {
			coeff += add[head].second.first;
			constant += add[head].second.second;
			head++;
		}
		
		printf("%" PRId64 "\n", constant + (int64_t) x * coeff);
	}
	
	return 0;
}

Compilation message

reconstruction.cpp: In function 'int ri()':
reconstruction.cpp:5:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1501 ms 9500 KB Output is correct
21 Correct 1088 ms 9464 KB Output is correct
22 Correct 1126 ms 9544 KB Output is correct
23 Correct 1235 ms 9400 KB Output is correct
24 Correct 1436 ms 9516 KB Output is correct
25 Correct 1480 ms 9480 KB Output is correct
26 Correct 1508 ms 9612 KB Output is correct
27 Correct 1518 ms 9528 KB Output is correct
28 Correct 1529 ms 9428 KB Output is correct
29 Correct 1487 ms 9596 KB Output is correct
30 Correct 1550 ms 9580 KB Output is correct
31 Correct 1500 ms 9696 KB Output is correct
32 Correct 1504 ms 9636 KB Output is correct
33 Correct 1524 ms 9396 KB Output is correct
34 Correct 1510 ms 9660 KB Output is correct
35 Correct 1530 ms 9396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1534 ms 27440 KB Output is correct
5 Correct 1464 ms 27512 KB Output is correct
6 Correct 1478 ms 27464 KB Output is correct
7 Correct 842 ms 29592 KB Output is correct
8 Correct 669 ms 29868 KB Output is correct
9 Correct 611 ms 29540 KB Output is correct
10 Correct 1487 ms 27668 KB Output is correct
11 Correct 664 ms 29612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 279 ms 22208 KB Output is correct
21 Correct 261 ms 22368 KB Output is correct
22 Correct 265 ms 22272 KB Output is correct
23 Correct 272 ms 22452 KB Output is correct
24 Correct 274 ms 22220 KB Output is correct
25 Correct 271 ms 22256 KB Output is correct
26 Correct 268 ms 22220 KB Output is correct
27 Correct 272 ms 22320 KB Output is correct
28 Correct 264 ms 22524 KB Output is correct
29 Correct 267 ms 22476 KB Output is correct
30 Correct 273 ms 22392 KB Output is correct
31 Correct 271 ms 22232 KB Output is correct
32 Correct 272 ms 22900 KB Output is correct
33 Correct 266 ms 22132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1501 ms 9500 KB Output is correct
21 Correct 1088 ms 9464 KB Output is correct
22 Correct 1126 ms 9544 KB Output is correct
23 Correct 1235 ms 9400 KB Output is correct
24 Correct 1436 ms 9516 KB Output is correct
25 Correct 1480 ms 9480 KB Output is correct
26 Correct 1508 ms 9612 KB Output is correct
27 Correct 1518 ms 9528 KB Output is correct
28 Correct 1529 ms 9428 KB Output is correct
29 Correct 1487 ms 9596 KB Output is correct
30 Correct 1550 ms 9580 KB Output is correct
31 Correct 1500 ms 9696 KB Output is correct
32 Correct 1504 ms 9636 KB Output is correct
33 Correct 1524 ms 9396 KB Output is correct
34 Correct 1510 ms 9660 KB Output is correct
35 Correct 1530 ms 9396 KB Output is correct
36 Correct 1528 ms 9920 KB Output is correct
37 Correct 1069 ms 9656 KB Output is correct
38 Correct 1134 ms 9648 KB Output is correct
39 Correct 1254 ms 9724 KB Output is correct
40 Correct 1445 ms 9660 KB Output is correct
41 Correct 1475 ms 9748 KB Output is correct
42 Correct 1519 ms 9660 KB Output is correct
43 Correct 1529 ms 9616 KB Output is correct
44 Correct 1527 ms 9652 KB Output is correct
45 Correct 1481 ms 9912 KB Output is correct
46 Correct 1521 ms 9716 KB Output is correct
47 Correct 1502 ms 9624 KB Output is correct
48 Correct 1516 ms 9784 KB Output is correct
49 Correct 1487 ms 9720 KB Output is correct
50 Correct 1489 ms 9892 KB Output is correct
51 Correct 1490 ms 9912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1501 ms 9500 KB Output is correct
21 Correct 1088 ms 9464 KB Output is correct
22 Correct 1126 ms 9544 KB Output is correct
23 Correct 1235 ms 9400 KB Output is correct
24 Correct 1436 ms 9516 KB Output is correct
25 Correct 1480 ms 9480 KB Output is correct
26 Correct 1508 ms 9612 KB Output is correct
27 Correct 1518 ms 9528 KB Output is correct
28 Correct 1529 ms 9428 KB Output is correct
29 Correct 1487 ms 9596 KB Output is correct
30 Correct 1550 ms 9580 KB Output is correct
31 Correct 1500 ms 9696 KB Output is correct
32 Correct 1504 ms 9636 KB Output is correct
33 Correct 1524 ms 9396 KB Output is correct
34 Correct 1510 ms 9660 KB Output is correct
35 Correct 1530 ms 9396 KB Output is correct
36 Correct 1 ms 300 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1534 ms 27440 KB Output is correct
40 Correct 1464 ms 27512 KB Output is correct
41 Correct 1478 ms 27464 KB Output is correct
42 Correct 842 ms 29592 KB Output is correct
43 Correct 669 ms 29868 KB Output is correct
44 Correct 611 ms 29540 KB Output is correct
45 Correct 1487 ms 27668 KB Output is correct
46 Correct 664 ms 29612 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 279 ms 22208 KB Output is correct
49 Correct 261 ms 22368 KB Output is correct
50 Correct 265 ms 22272 KB Output is correct
51 Correct 272 ms 22452 KB Output is correct
52 Correct 274 ms 22220 KB Output is correct
53 Correct 271 ms 22256 KB Output is correct
54 Correct 268 ms 22220 KB Output is correct
55 Correct 272 ms 22320 KB Output is correct
56 Correct 264 ms 22524 KB Output is correct
57 Correct 267 ms 22476 KB Output is correct
58 Correct 273 ms 22392 KB Output is correct
59 Correct 271 ms 22232 KB Output is correct
60 Correct 272 ms 22900 KB Output is correct
61 Correct 266 ms 22132 KB Output is correct
62 Correct 1528 ms 9920 KB Output is correct
63 Correct 1069 ms 9656 KB Output is correct
64 Correct 1134 ms 9648 KB Output is correct
65 Correct 1254 ms 9724 KB Output is correct
66 Correct 1445 ms 9660 KB Output is correct
67 Correct 1475 ms 9748 KB Output is correct
68 Correct 1519 ms 9660 KB Output is correct
69 Correct 1529 ms 9616 KB Output is correct
70 Correct 1527 ms 9652 KB Output is correct
71 Correct 1481 ms 9912 KB Output is correct
72 Correct 1521 ms 9716 KB Output is correct
73 Correct 1502 ms 9624 KB Output is correct
74 Correct 1516 ms 9784 KB Output is correct
75 Correct 1487 ms 9720 KB Output is correct
76 Correct 1489 ms 9892 KB Output is correct
77 Correct 1490 ms 9912 KB Output is correct
78 Correct 1731 ms 26664 KB Output is correct
79 Correct 1269 ms 28596 KB Output is correct
80 Correct 1361 ms 27796 KB Output is correct
81 Correct 1489 ms 27528 KB Output is correct
82 Correct 1711 ms 26828 KB Output is correct
83 Correct 1711 ms 26644 KB Output is correct
84 Correct 1758 ms 26692 KB Output is correct
85 Correct 1743 ms 26664 KB Output is correct
86 Correct 1743 ms 26792 KB Output is correct
87 Correct 1743 ms 28228 KB Output is correct
88 Correct 1746 ms 26736 KB Output is correct
89 Correct 1760 ms 26588 KB Output is correct
90 Correct 1738 ms 26684 KB Output is correct
91 Correct 1758 ms 26636 KB Output is correct
92 Correct 1736 ms 29452 KB Output is correct
93 Correct 1751 ms 27720 KB Output is correct