QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#77055#5502. Dazzling MountainGolovanov399ML 3127ms168752kbC++201.5kb2023-02-13 00:49:532023-02-13 00:49:54

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-13 00:49:54]
  • 评测
  • 测评结果:ML
  • 用时:3127ms
  • 内存:168752kb
  • [2023-02-13 00:49:53]
  • 提交

answer

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
// #define make_unique(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

inline int nxt() {
	int x;
	// scanf("%d", &x);
	cin >> x;
	return x;
}

const int N = 1011111;
vector<int> a[N];
vector<char> can[N];
int sz[N];

void intersect(vector<char>& where, vector<char>& what) {
	if (where.empty()) {
		where.swap(what);
		return;
	}
	if (where.size() > what.size()) {
		where.swap(what);
	}
	for (int i = 0; i < (int)where.size(); ++i) {
		where[i] &= what[i];
	}
}

void dfs(int v, int p = -1) {
	sz[v] = 1;
	for (int x : a[v]) {
		if (x == p) {
			continue;
		}
		dfs(x, v);
		sz[v] += sz[x];
		intersect(can[v], can[x]);
	}
	can[v].resize(sz[v] + 1);
	can[v][sz[v]] = 1;
	// cerr << v + 1 << ": ";
	// for (int i = 1; i <= sz[v]; ++i) {
	// 	cerr << (int)can[v][i];
	// }
	// cerr << "\n";
}

void solve() {
	int n = nxt();
	for (int i = 0; i < n; ++i) {
		a[i] = {};
		can[i] = {};
	}
	for (int i = 0; i < n - 1; ++i) {
		int u = nxt() - 1, v = nxt() - 1;
		a[u].push_back(v);
		a[v].push_back(u);
	}
	dfs(0);
	int cnt = 0;
	for (int i = 1; i <= n; ++i) {
		if (can[0][i]) {
			++cnt;
		}
	}
	cout << cnt << "\n";
	for (int i = 1; i <= n; ++i) {
		if (can[0][i]) {
			cout << i << " ";
		}
	}
	cout << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = nxt();
	for (int i = 1; i <= t; ++i) {
		solve();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 19ms
memory: 50860kb

input:

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

output:

4
1 3 8 9 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 483ms
memory: 53616kb

input:

10000
906
675 189
555 889
491 97
791 419
175 694
713 842
788 513
159 354
670 815
652 546
253 87
89 278
563 429
522 900
202 657
331 865
35 605
735 532
612 471
657 386
7 886
856 164
224 777
73 534
481 631
711 698
240 465
115 181
191 825
311 155
709 501
207 849
294 546
591 682
96 405
210 696
861 13
781...

output:

63
1 3 4 34 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 
6
1 2 3 4 5 11 
28
1 2 3 4 5 6 15 16 17 18 19 20 21...

result:

ok 20000 lines

Test #3:

score: 0
Accepted
time: 2122ms
memory: 87396kb

input:

10
257056
71485 24974
175037 254578
15503 255561
2268 184070
101954 23776
151400 163190
209539 157934
61908 8578
251032 109510
106012 63219
6393 135129
229530 202665
135202 195080
36226 54716
113653 27375
130515 126621
51348 62149
190321 116509
235895 205631
193944 184367
234172 88847
217084 158554
...

output:

24809
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...

result:

ok 20 lines

Test #4:

score: 0
Accepted
time: 3127ms
memory: 168752kb

input:

3
1000000
929036 746780
508756 963246
215473 613289
951642 391332
605398 514190
932199 548138
439718 525533
976267 837482
522952 131368
536100 143927
866830 627096
995333 784817
209340 817981
278894 972055
340159 479037
505969 156301
962674 345998
328184 514559
522443 340810
671564 2940
233204 19714...

output:

1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

result:

ok 6 lines

Test #5:

score: -100
Memory Limit Exceeded

input:

3
1000000
762755 663578
736521 890486
762558 739995
2696 497271
66774 285301
58082 960305
515605 849755
585841 791747
805356 25220
199034 206010
792516 101430
419916 601668
256275 440770
76325 556632
286424 45286
173087 212370
760515 256873
614983 749308
248450 520646
937026 203197
698482 110785
336...

output:


result: