90302024-02-12 12:16:56IgnácParti (75 pont)cpp17Wrong answer 72/7570ms7320 KiB
// Source: https://usaco.guide/general/io

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

int main() {
	int n;
	cin >> n;
	if (n < 3) {
		cout << 0;
		return 0;
	}
	vector<int> db(n + 1);
	vector<pair<int, int>> to(n + 1);
	for (int i = 1; i <= n; i++) {
		int a, b;
		cin >> a >> b;
		db[a]++;
		db[b]++;
		to[i].first = a;
		to[i].second = b;
	}

	queue<int> out;
	for (int i = 1; i <= n; i++) {
		if (db[i] < 2) {
			out.push(i);
			db[i] = -1;
		}
	}

	while (out.size() > 0) {
		//cout << out.front() << "\n";
		db[to[out.front()].first]--;
		db[to[out.front()].second]--;
		if (db[to[out.front()].first] < 2 && db[to[out.front()].first] > -1) {
			out.push(to[out.front()].first);
			db[to[out.front()].first] = -1;
		}
		if (db[to[out.front()].second] < 2 && db[to[out.front()].second] > -1) {
			out.push(to[out.front()].second);
			db[to[out.front()].second] = -1;
		}
		out.pop();
	}

	string ans;
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (db[i] > -1) {
			ans += to_string(i) + " ";
			cnt++;
		}
	}

	if (cnt > 0) cout << cnt << "\n" << ans;
	else cout << 0;
}
SubtaskSumTestVerdictTimeMemory
base72/75
1Accepted0/03ms1812 KiB
2Accepted0/035ms3800 KiB
3Accepted3/33ms2216 KiB
4Accepted3/33ms2460 KiB
5Wrong answer0/33ms2668 KiB
6Accepted3/33ms2728 KiB
7Accepted3/33ms2876 KiB
8Accepted4/43ms2996 KiB
9Accepted4/43ms3104 KiB
10Accepted4/44ms3228 KiB
11Accepted4/43ms3360 KiB
12Accepted4/44ms3460 KiB
13Accepted4/44ms3484 KiB
14Accepted4/44ms3760 KiB
15Accepted4/435ms5300 KiB
16Accepted4/443ms6036 KiB
17Accepted4/448ms6212 KiB
18Accepted4/456ms6752 KiB
19Accepted4/463ms6904 KiB
20Accepted4/470ms7228 KiB
21Accepted4/470ms7320 KiB
22Accepted4/43ms4064 KiB