QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#216357#2879. Kaleidoscopic Routekaruna#TL 52ms14604kbC++172.8kb2023-10-15 17:43:012023-10-15 17:43:01

Judging History

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

  • [2023-10-15 17:43:01]
  • 评测
  • 测评结果:TL
  • 用时:52ms
  • 内存:14604kb
  • [2023-10-15 17:43:01]
  • 提交

answer

#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 101010;
const int INF = 1e9 + 10;

int n, m, d[2][MAXN];
vector<pair<int, int>> g[MAXN];

int back_mn[2][MAXN];
int back_mx[2][MAXN];
int mn[2][MAXN];
int mx[2][MAXN];
int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int u, v, c; cin >> u >> v >> c;
		g[u].push_back({c, v});
		g[v].push_back({c, u});
	}
	for (int t = 0; t < 2; t++) {
		queue<int> q;
		for (int i = 1; i <= n; i++) d[t][i] = -1;
		if (t == 0) q.push(1), d[t][1] = 0;
		else q.push(n), d[t][n] = 0;
		while (!q.empty()) {
			int v = q.front();
			q.pop();
			for (auto [w, x] : g[v]) {
				if (d[t][x] == -1) {
					d[t][x] = d[t][v] + 1;
					q.push(x);
				}
			}
		}
	}
	int D = d[0][n];
	// cout << "distance " << D << "?\n";
	for (int t = 0; t < 2; t++) {
		for (int i = 1; i <= n; i++) mn[t][i] = INF;
		for (int i = 1; i <= n; i++) mx[t][i] = -1;
	}
	for (int t = 0; t < 2; t++) {
		queue<int> q;
		if (t == 0) q.push(1);
		else q.push(n);

		while (!q.empty()) {
			int v = q.front();
			q.pop();
			for (auto [w, x] : g[v]) {
				if (d[t][x] == d[t][v] + 1 && d[t][v] + d[t ^ 1][x] + 1 == D) { // need to check?
					if (mn[t][x] > min(mn[t][v], w)) {
						mn[t][x] = min(mn[t][v], w);
						back_mn[t][x] = v;
					}
					if (mx[t][x] < max(mx[t][v], w)) {
						mx[t][x] = max(mx[t][v], w);
						back_mx[t][x] = v;
					}
					q.push(x);
				}
			}
		}
	}
	// for (int i = 1; i <= n; i++) {
	// 	cout << mn[0][i] << ' ' << mx[0][i] << "?\n";
	// }
	int cs = 1;
	int vn = 1;
	int val = 0;
	for (int i = 2; i < n; i++) {
		if (d[0][i] + d[1][i] != D) continue;
		// case 1 : min first
		if (val < mx[1][i] - mn[0][i]) {
			val = mx[1][i] - mn[0][i];
			vn = i;
			cs = 1;
		}
		// case 2 : max first
		if (val < mx[0][i] - mn[1][i]) {
			val = mx[0][i] - mn[1][i];
			vn = i;
			cs = 2;
		}
	}
	vector<int> pre, suf;
	if (cs == 1) {
		for (int v = vn; v != 1; v = back_mn[0][v]) {
			assert(1 <= v && v <= n);
			pre.push_back(v);
		}
		pre.push_back(1);
		for (int v = vn; v != n; v = back_mx[1][v]) {
			assert(1 <= v && v <= n);
			if (v != vn) suf.push_back(v);
		}
		suf.push_back(n);
	}
	else {
		for (int v = vn; v != 1; v = back_mx[0][v]) {
			assert(1 <= v && v <= n);
			pre.push_back(v);
		}
		pre.push_back(1);
		for (int v = vn; v != n; v = back_mn[1][v]) {
			assert(1 <= v && v <= n);
			if (v != vn) suf.push_back(v);
		}
		suf.push_back(n);
	}
	reverse(pre.begin(), pre.end());
	for (int x : suf) pre.push_back(x);

	cout << (int)pre.size() - 1 << '\n';
	for (int x : pre) cout << x << ' ';
	cout << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8112kb

input:

6 8
1 5 2
5 2 5
3 5 4
1 3 10
3 4 6
4 5 7
4 6 8
2 6 1

output:

3
1 5 4 6 

result:

ok 3 6

Test #2:

score: 0
Accepted
time: 0ms
memory: 8672kb

input:

10 20
1 9 34
6 3 80
7 3 15
5 4 73
4 1 42
8 6 92
2 10 25
4 3 8
9 3 79
3 1 11
9 2 74
10 1 83
3 2 6
10 3 38
10 4 48
1 5 38
6 2 43
4 2 55
8 7 90
3 5 4

output:

1
1 10 

result:

ok 1 0

Test #3:

score: 0
Accepted
time: 1ms
memory: 7740kb

input:

24 65
22 11 519
2 4 585
8 17 647
1 11 464
13 4 596
12 10 733
24 21 106
1 14 345
23 5 430
2 3 137
16 7 891
16 4 807
14 3 465
4 22 532
16 9 346
21 8 859
18 15 631
9 5 914
19 17 729
1 2 546
5 15 43
2 12 349
19 9 642
13 3 441
20 15 359
21 11 645
9 14 992
4 23 9
23 9 34
24 16 117
16 18 349
10 22 285
22 5...

output:

2
1 11 24 

result:

ok 2 208

Test #4:

score: 0
Accepted
time: 1ms
memory: 7780kb

input:

53 123
43 38 999
7 24 718
6 33 393
16 15 734
21 39 604
27 15 828
45 31 747
23 4 538
14 41 224
38 9 756
12 36 574
12 13 624
7 34 649
17 53 309
38 33 825
1 36 273
38 21 81
36 11 680
14 8 649
12 24 346
21 49 921
1 47 612
11 39 648
41 35 271
15 24 36
29 3 980
29 28 295
15 21 236
27 8 853
22 6 532
3 14 9...

output:

2
1 52 53 

result:

ok 2 482

Test #5:

score: 0
Accepted
time: 1ms
memory: 7740kb

input:

2 1
2 1 33

output:

1
1 2 

result:

ok 1 0

Test #6:

score: 0
Accepted
time: 1ms
memory: 8380kb

input:

123 1032
49 28 6306
70 79 6579
24 47 3306
102 114 7381
78 34 4078
31 92 2591
114 65 7836
92 59 7583
110 51 4384
84 99 439
78 48 77
23 77 6225
96 69 2939
55 112 45
48 29 4735
27 18 4242
117 97 395
71 64 2979
102 36 9531
20 86 7795
11 5 3142
114 107 1188
62 106 6321
6 71 2974
102 96 4240
82 13 7488
95...

output:

2
1 93 123 

result:

ok 2 5258

Test #7:

score: 0
Accepted
time: 5ms
memory: 10456kb

input:

1342 23134
757 512 65447
281 782 35017
179 633 86323
707 635 20673
1225 962 76138
986 59 62123
29 482 39870
90 265 65928
572 1028 18186
810 568 89715
469 998 95978
583 641 63660
261 208 37031
775 1131 33707
161 702 33733
1340 726 67611
548 583 9224
1100 208 82846
227 486 60094
1070 565 49959
1285 11...

output:

3
1 669 616 1342 

result:

ok 3 93825

Test #8:

score: 0
Accepted
time: 16ms
memory: 10788kb

input:

5313 87123
3239 37 964433
3078 247 435023
1830 493 489819
4478 1661 914186
750 2703 353434
3853 1072 285309
2117 1377 641750
1254 4366 734040
2222 2191 362794
3461 2267 359296
2514 913 627160
878 1006 709645
1767 3791 250153
4845 4041 339269
3270 2061 26968
2421 660 798388
262 1399 974636
5120 4171 ...

output:

3
1 3463 3294 5313 

result:

ok 3 878139

Test #9:

score: 0
Accepted
time: 34ms
memory: 13544kb

input:

12313 173254
4620 6513 380927
4465 6551 243296
615 9825 794381
338 11211 368032
4571 9567 634707
3731 7430 87826
8106 7718 998064
4146 7848 922789
3966 1263 331881
9179 646 948846
11893 5020 883102
5612 10513 898068
3451 3435 451139
9640 1947 1353
12271 9668 283149
9742 5631 884826
6837 2449 309976
...

output:

3
1 1402 3350 12313 

result:

ok 3 497061

Test #10:

score: 0
Accepted
time: 41ms
memory: 14104kb

input:

23532 200000
21979 22532 52608855
17378 1428 55313979
12897 6506 2960978
21412 2936 26201355
1327 21400 71454522
6940 9953 7548221
15846 15164 69193724
22756 8774 49306072
10793 7975 94773768
15722 221 88298496
274 22685 29319966
21564 10196 30589888
2895 9073 93905131
8310 22526 13817682
1565 2616 ...

output:

4
1 18111 9089 18380 23532 

result:

ok 4 93767115

Test #11:

score: 0
Accepted
time: 52ms
memory: 14604kb

input:

56043 200000
8956 48459 891115255
34699 29270 626119743
15612 49268 833711278
35090 40312 12951690
27544 40981 509660868
32984 29979 910590843
54449 18153 383011987
36405 52382 871189321
22735 40079 205544352
1579 34250 703531988
1733 44402 554010888
11931 21887 65967217
7150 29324 904513650
55402 4...

output:

6
1 27483 27883 37006 9501 26651 56043 

result:

ok 6 960310393

Test #12:

score: 0
Accepted
time: 32ms
memory: 13944kb

input:

100000 99999
16259 89189 677093403
78250 8073 458212862
57547 91624 275388547
61506 43428 213190761
19202 93628 867133431
39385 53296 668727622
16499 67293 611076654
41619 33235 854029781
13362 34905 113954233
52118 36368 107607827
10583 42580 534654035
86638 25330 709240484
52574 8418 111555367
767...

output:

13
1 32645 7845 51316 29812 10607 72877 5776 79952 16297 43702 9206 88402 100000 

result:

ok 13 861759213

Test #13:

score: -100
Time Limit Exceeded

input:

80000 200000
58330 43217 297444461
16896 51153 140513289
9385 49460 443143629
69429 43934 531903821
26079 74229 736987435
51036 31562 159062344
41578 13401 929067646
55379 23507 690768049
30121 23885 877254400
35892 23458 465046769
34835 48337 668330315
18408 46981 120331309
44840 17333 120558928
54...

output:


result: