UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#199974#535. 生成树tkswls1003895ms33340kbC++111.4kb2023-12-24 11:45:472023-12-24 12:20:24

answer

#include<bits/stdc++.h>
using namespace std;
int n, m, root, b[500005];
long long x, y, z, c[500005];
map<pair<int, int>, int> ma;
stack<int> st;
inline int get(int p, int q) {
	if (ma.find(make_pair(p, q)) != ma.end()) return ma[make_pair(p, q)];
	if (p > q) swap(p, q);
	return (((p * x + q * y) % z) >= (c[p] + c[q]));
}
inline int calc(int p) {
	return (p - 1) % n + 1;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> x >> y >> z;
		ma[make_pair(x, y)] = ma[make_pair(y, x)] = z;
	}
	cin >> x >> y >> z;
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
	}
	root = 1;
	st.push(1);
	for (int qwe = 0; qwe <= 1; qwe++) {
		for (int i = 2; i <= 4 * n; i++) {
			if (calc(i) == root) {
				for (int j = 1; j <= n; j++) {
					if (j == root) continue;
					cout << j << ' ' << b[j] << "\n";
				}
				return 0;
			}
			bool flg = 0;
			while (st.size()) {
				if (get(st.top(), calc(i)) == qwe) {
					flg = 1;
					break;
				}
				st.pop();
			}
			if (!flg) {
				for (int j = root; j < i; j++) {
					b[calc(i)] = 0;
				}
				root = calc(i);
				st.push(calc(i));
			} else {
				b[calc(i)] = st.top();
				st.push(calc(i));
			}
		}
		memset(b, 0, sizeof(b));
		while (st.size()) {
			st.pop();
		}
		root = 1;
		st.push(1);
	}
	cout << "No solution";
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 3236kb

input:

9
22
3 9 1
1 4 0
8 3 0
1 8 1
4 5 1
8 2 1
5 1 1
7 3 1
2 7 1
6 7 0
9 6 0
9 8 1
7 1 1
4 7 1
5 3 1
6 2 1...

output:

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

result:

ok The ans is correct.

Test #2:

score: 10
Accepted
time: 0ms
memory: 1280kb

input:

10
10
5 10 1
1 4 1
6 9 0
3 5 1
8 9 0
1 8 0
8 6 0
8 4 0
9 4 0
1 6 0
67367148504 85443699707 759160493...

output:

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

result:

ok The ans is correct.

Test #3:

score: 10
Accepted
time: 0ms
memory: 3232kb

input:

10
12
8 9 0
10 1 0
6 1 1
3 1 0
2 5 0
4 10 1
4 2 1
10 5 0
8 6 0
8 4 1
9 3 1
3 10 1
85041138719 545241...

output:

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

result:

ok The ans is correct.

Test #4:

score: 10
Accepted
time: 0ms
memory: 1428kb

input:

50
1149
27 47 1
3 50 0
32 30 0
41 2 1
24 32 1
9 32 1
2 19 0
18 11 0
42 12 0
29 26 1
14 25 1
28 8 0
1...

output:

1 50
2 1
3 1
4 3
5 49
6 48
7 6
8 48
9 48
10 9
11 10
12 10
13 12
14 10
15 9
16 48
17 48
18 48
19 46
2...

result:

ok The ans is correct.

Test #5:

score: 10
Accepted
time: 2ms
memory: 3372kb

input:

50
1059
32 43 0
33 24 1
47 39 1
40 4 1
15 33 0
8 46 0
22 15 1
46 30 0
49 31 1
50 46 0
9 16 0
39 28 1...

output:

1 28
2 26
3 2
4 2
5 22
6 22
7 22
9 8
10 9
11 9
12 8
13 12
14 13
15 14
16 13
17 16
18 12
19 18
20 19
...

result:

ok The ans is correct.

Test #6:

score: 10
Accepted
time: 332ms
memory: 28252kb

input:

1000
200000
104 769 1
135 155 0
731 930 0
373 930 1
224 621 0
757 177 1
203 568 0
206 518 1
326 271 ...

output:

1 999
2 1
3 999
4 3
5 4
6 3
7 999
8 7
9 8
10 9
11 9
12 11
13 12
14 13
15 13
16 7
17 16
18 999
19 997...

result:

ok The ans is correct.

Test #7:

score: 10
Accepted
time: 266ms
memory: 25808kb

input:

1000
180474
871 645 1
473 556 1
374 11 1
180 554 0
665 140 1
797 701 1
184 942 1
401 192 0
196 851 0...

output:

1 1000
2 1000
3 2
5 4
6 5
7 5
8 7
9 7
10 7
11 10
12 11
13 12
14 12
15 14
16 14
17 14
18 14
19 14
20 ...

result:

ok The ans is correct.

Test #8:

score: 10
Accepted
time: 1036ms
memory: 33340kb

input:

500000
200000
73482 120629 0
67087 24151 1
232270 490907 1
124168 14906 0
59630 90530 0
332365 32630...

output:

2 1
3 2
4 3
5 3
6 5
7 6
8 6
9 6
10 9
11 10
12 10
13 12
14 13
15 13
16 15
17 16
18 17
19 17
20 19
21 ...

result:

ok The ans is correct.

Test #9:

score: 10
Accepted
time: 1270ms
memory: 33196kb

input:

500000
200000
3210 286973 0
128616 345042 0
12028 311071 1
98462 476017 0
26460 171740 0
191421 1367...

output:

1 500000
2 1
3 499999
4 3
6 5
7 6
8 6
9 8
10 8
11 10
12 10
13 12
14 13
15 14
16 15
17 15
18 17
19 17...

result:

ok The ans is correct.

Test #10:

score: 10
Accepted
time: 989ms
memory: 32728kb

input:

500000
200000
130346 240791 1
417939 148105 0
70793 104973 0
274118 368445 0
233579 192994 1
9349 28...

output:

1 500000
2 1
4 3
5 4
6 4
7 6
8 6
9 8
10 9
11 9
12 9
13 12
14 13
15 14
16 13
17 16
18 17
19 17
20 17
...

result:

ok The ans is correct.

Extra Test:

score: 0
Extra Test Passed