90292024-02-12 12:15:08IgnácParti (75 pont)cpp17Wrong answer 72/7574ms7404 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++;
		}
	}

	cout << cnt << "\n" << ans;
}
SubtaskSumTestVerdictTimeMemory
base72/75
1Accepted0/03ms1812 KiB
2Accepted0/037ms3976 KiB
3Accepted3/33ms2436 KiB
4Accepted3/33ms2644 KiB
5Wrong answer0/33ms2828 KiB
6Accepted3/33ms2720 KiB
7Accepted3/33ms2740 KiB
8Accepted4/43ms2980 KiB
9Accepted4/43ms3076 KiB
10Accepted4/44ms3292 KiB
11Accepted4/43ms3260 KiB
12Accepted4/44ms3280 KiB
13Accepted4/44ms3552 KiB
14Accepted4/44ms3632 KiB
15Accepted4/437ms5224 KiB
16Accepted4/443ms5716 KiB
17Accepted4/452ms6312 KiB
18Accepted4/459ms6328 KiB
19Accepted4/465ms6676 KiB
20Accepted4/471ms7264 KiB
21Accepted4/474ms7404 KiB
22Accepted4/43ms4140 KiB