QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#138879#6520. Classic ProblemthenymphsofdelphiWA 2267ms48204kbC++206.2kb2023-08-12 12:35:582023-08-12 12:35:59

Judging History

你现在查看的是最新测评结果

  • [2023-08-12 12:35:59]
  • 评测
  • 测评结果:WA
  • 用时:2267ms
  • 内存:48204kb
  • [2023-08-12 12:35:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())

using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;

template <class T>
using min_heap = priority_queue <T, vector <T>, greater <T>>;

const int N = 2e5 + 5, LN = 17;
const int inf = 1e9 + 7;

struct edge{
	int u, v, w;

	friend bool operator< (const edge& lhs, const edge& rhs){
		return tuple{lhs.w, lhs.u, lhs.v} < tuple{rhs.w, rhs.u, rhs.v};
	}
};

struct node{
	int u, l, r;

	friend bool operator< (const node& lhs, const node& rhs){
		return tuple{lhs.u, lhs.l, lhs.r} < tuple{rhs.u, rhs.l, rhs.r};
	}

	friend bool operator== (const node& lhs, const node& rhs){
		return tuple{lhs.u, lhs.l, lhs.r} == tuple{rhs.u, rhs.l, rhs.r};
	}
};

struct disjoint_set_union{
	int par[N];

	void reset(int sz = N){
		fill(par, par + sz, -1);
	}

	disjoint_set_union(){
		reset();
	}

	int root(int x){
		return par[x] < 0 ? x : (par[x] = root(par[x]));
	}

	bool merge(int x, int y){
		if ((x = root(x)) == (y = root(y))){
			return false;
		}
		if (par[x] > par[y]){
			swap(x, y);
		}
		par[x] += par[y];
		par[y] = x;
		return true;
	}
} dsu;

int n, m;
edge a[N];
set <pair <int, int>> stt_edge;
vector <node> vertex;
int cnt_node;

vector <vector <int>> components;
int idx_component[N];

int pref_same[N], suff_same[N]; // farthest index having the same value of idx_component to the left and right

edge nearest[N];
ll ans;

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	// freopen("KEK.inp", "r", stdin);
	// freopen("KEK.out", "w", stdout);
int tests; cin >> tests; while (tests--){
	stt_edge.clear();
	vertex.clear();

	cin >> n >> m;
	if (m == 0){
		cout << n - 1 << endl;
		continue;
	}
	ForE(i, 1, m){
		int u, v, w; cin >> u >> v >> w;
		a[i] = edge{u, v, w};
		stt_edge.emplace(u, v);
		vertex.emplace_back(u, 0, 0);
		vertex.emplace_back(v, 0, 0);
	}

	sort(bend(vertex));
	vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
	cnt_node = isz(vertex);
	For(x, 0, cnt_node){
		if (x == 0){
			vertex[x].l = 1;
		}
		else{
			vertex[x].l = vertex[x - 1].r + 1;
		}
		if (x == cnt_node - 1){
			vertex[x].r = n;
		}
		else{
			vertex[x].r = vertex[x + 1].u - 1;
		}
	}
	ans = n - cnt_node;

	components.clear();
	For(i, 0, cnt_node){
		vi component = {i};
		components.emplace_back(component);
	}

	while (isz(components) != 1){
		int k = isz(components);
		dsu.reset(k);
		For(i, 0, k){
			Fora(x, components[i]){
				idx_component[x] = i;
			}
		}
		// For(i, 0, k){
		// 	cout << "component " << i << ' ';
		// 	Fora(x, components[i]){
		// 		cout << x << ' ';
		// 	}
		// 	cout << endl;
		// }
		auto find_component = [&](int u){
			int x = lower_bound(bend(vertex), node{u, 0, 0}) - vertex.begin();
			if (x == cnt_node or vertex[x].l > u){
				x--;
			}
			return x;
		};
		For(i, 0, cnt_node){
			pref_same[i] = (i == 0 ? 0 : (idx_component[i - 1] == idx_component[i] ? pref_same[i - 1] : i));
		}
		FordE(i, cnt_node - 1, 0){
			suff_same[i] = (i == cnt_node - 1 ? cnt_node - 1 : (idx_component[i + 1] == idx_component[i] ? suff_same[i + 1] : i));
		}
		For(i, 0, k){
			nearest[i] = edge{-1, -1, inf};
		}

		ForE(i, 1, m){
			auto [u, v, w] = a[i];
			int x = find_component(u), y = find_component(v);
			if (idx_component[x] == idx_component[y]){
				continue;
			}
			nearest[idx_component[x]] = min(nearest[idx_component[x]], a[i]);
			nearest[idx_component[y]] = min(nearest[idx_component[y]], a[i]);
		}
		auto move_down = [&](int x, int u){
			// cout << "move_down " << x << ' ' << u << endl;
			int v = u;
			while (true){
				v--;
				if (v == 0){
					break;
				}
				int y = find_component(v);
				if (idx_component[y] == idx_component[x]){
					v = vertex[pref_same[y]].l - 1;
				}
				if (v == 0){
					break;
				}
				if (not stt_edge.count(pair{v, u})){
					// cout << "add edge " << v << ' ' << u << endl;
					nearest[idx_component[x]] = min(nearest[idx_component[x]], edge{v, u, u - v});
					break;
				}
			}
		};
		auto move_up = [&](int x, int u){
			// cout << "move_up " << x << ' ' << u << endl;
			int v = u;
			while (true){
				v++;
				if (v == n + 1){
					break;
				}
				int y = find_component(v);
				if (idx_component[y] == idx_component[x]){
					v = vertex[suff_same[y]].r + 1;
				}
				if (v == n + 1){
					break;
				}
				if (not stt_edge.count(pair{u, v})){
					// cout << "add edge " << u << ' ' << v << endl;
					nearest[idx_component[x]] = min(nearest[idx_component[x]], edge{u, v, v - u});
					break;
				}
			}
		};
		For(x, 0, cnt_node){
			move_down(x, vertex[x].l);
			move_down(x, vertex[x].u);
		}
		For(x, 0, cnt_node){
			move_up(x, vertex[x].u);
			move_up(x, vertex[x].r);
		}

		set <edge> chosen_edge;
		For(i, 0, k){
			chosen_edge.emplace(nearest[i]);
			dsu.merge(i, i ^ idx_component[find_component(nearest[i].u)] ^ idx_component[find_component(nearest[i].v)]);
		}
		for (auto &[u, v, w]: chosen_edge){
			ans += w;
		}
		vector <vector <int>> new_components(k);
		For(x, 0, cnt_node){
			new_components[dsu.root(idx_component[x])].emplace_back(x);
		}
		components.clear();
		For(i, 0, k){
			if (not new_components[i].empty()){
				components.emplace_back(new_components[i]);
			}
		}
	}
	cout << ans << endl;
}
}

/*
==================================================+
INPUT                                             |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
OUTPUT                                            |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9876kb

input:

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: 0
Accepted
time: 1520ms
memory: 46400kb

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:

999999999
446000000000
732256441
999999680
999899999
999966830
127
4
2186
1562
1176
5100
5
503
679
4

result:

ok 16 numbers

Test #3:

score: 0
Accepted
time: 898ms
memory: 48204kb

input:

5
1000000000 100000
158260500 877914550 822889311
24979400 861648750 548830120
433933400 476190600 342858071
211047200 971407750 480845266
731963950 822804750 449656269
430302150 982631900 545923679
880895700 923078500 816372580
189330700 910286900 135599877
303238500 404539650 605997004
492686550 7...

output:

999999999
999999999
999999999
999999999
999999999

result:

ok 5 number(s): "999999999 999999999 999999999 999999999 999999999"

Test #4:

score: 0
Accepted
time: 1547ms
memory: 44096kb

input:

5
2000 100000
1873 1874 822889311
1290 1291 548830120
1162 1163 342858071
814 815 480845266
742 743 449656269
1609 1610 545923679
1431 1432 816372580
1143 1144 135599877
414 415 605997004
1117 1118 921749002
121 122 786119025
1672 1673 655702582
38 39 211428413
1639 1640 393671555
922 923 501983447
...

output:

4097
20020
198635
2099999
1000099999

result:

ok 5 number(s): "4097 20020 198635 2099999 1000099999"

Test #5:

score: 0
Accepted
time: 841ms
memory: 31524kb

input:

5
100000 100000
36426 60522 446755913
60522 90081 181424399
3497 60522 625711486
60522 94325 647639160
60522 68417 160714711
35902 60522 61020590
23857 60522 626006012
29211 60522 547865075
60522 63340 561330066
60016 60522 650693494
24593 60522 849028504
60522 90310 285323416
11431 60522 990607689
...

output:

100024
150003
200003
250000
999999999

result:

ok 5 number(s): "100024 150003 200003 250000 999999999"

Test #6:

score: 0
Accepted
time: 751ms
memory: 18956kb

input:

5
4000 100000
694 696 619136615
1611 1614 829153748
2551 2552 370724298
3034 3037 49559541
98 105 443754249
822 827 735959328
878 885 201346483
2729 2731 304071225
3961 3965 557890416
1631 1637 430215474
3195 3205 212882505
503 507 937363346
3141 3150 47574456
577 583 727402691
3673 3675 279029853
3...

output:

43935
6913
4336
18350
12678

result:

ok 5 number(s): "43935 6913 4336 18350 12678"

Test #7:

score: -100
Wrong Answer
time: 2267ms
memory: 46504kb

input:

5
100000 100000
73979 73980 5250107
1946 1947 757506029
87433 87434 443673136
64352 64354 338125488
69103 69104 235256717
60843 60845 20769731
23601 23602 214534313
92417 92419 669411181
57364 57365 519766962
42029 42031 827237806
98524 98525 593643533
71482 71483 662414293
6709 6710 745687608
36460...

output:

168735
128189
110353
1000033413
1000009975

result:

wrong answer 1st numbers differ - expected: '166767', found: '168735'