Submission #25102518


Source Code Expand

#include "bits/stdc++.h"

#define int long long

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll MOD = 1000000007;

bool seen[100010];
vector<int>G[100010];
vector<int>ans;
void dfs(int v) {
	seen[v] = true;
	ans.push_back(v);
	for (auto nv : G[v]) {
		if (seen[nv])continue;
		dfs(nv);
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N;  cin >> N;
	vector<bool>ok(N, false);
	for (int i = 0; i < N; i++) {
		int A, B; cin >> A >> B; A--; B--;
		if (i == A || i == B) {
			ok[i] = true;
		}
		G[A].push_back(i);
		G[B].push_back(i);
	}
	for (int i = 0; i < N; i++) {
		if (ok[i] && !seen[i])dfs(i);
	}
	int s = ans.size();
	if (s != N) {
		cout << -1 << endl; return 0;
	}
	reverse(ans.begin(), ans.end());
	for (int i = 0; i < N; i++) {
		cout << ans[i] + 1 << endl;
	}
	return 0;
}

Submission Info

Submission Time
Task 062 - Paint All(★6)
User Example
Language C++ (GCC 9.2.1)
Score 6
Code Size 929 Byte
Status AC
Exec Time 198 ms
Memory 14724 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 6 / 6
Status
AC × 5
AC × 50
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
All 01_random_yes_01.txt, 01_random_yes_02.txt, 01_random_yes_03.txt, 01_random_yes_04.txt, 01_random_yes_05.txt, 01_random_yes_06.txt, 01_random_yes_07.txt, 01_random_yes_08.txt, 01_random_yes_09.txt, 01_random_yes_10.txt, 02_random_no_01.txt, 02_random_no_02.txt, 02_random_no_03.txt, 02_random_no_04.txt, 02_random_no_05.txt, 02_random_no_06.txt, 02_random_no_07.txt, 02_random_no_08.txt, 02_random_no_09.txt, 02_random_no_10.txt, 03_large_yes_01.txt, 03_large_yes_02.txt, 03_large_yes_03.txt, 03_large_yes_04.txt, 03_large_yes_05.txt, 04_large_no_01.txt, 04_large_no_02.txt, 04_large_no_03.txt, 04_large_no_04.txt, 04_large_no_05.txt, 05_small_yes_01.txt, 05_small_yes_02.txt, 05_small_yes_03.txt, 05_small_yes_04.txt, 05_small_yes_05.txt, 06_small_no_01.txt, 06_small_no_02.txt, 06_small_no_03.txt, 06_small_no_04.txt, 06_small_no_05.txt, 07_linear_yes_01.txt, 07_linear_yes_02.txt, 07_linear_yes_03.txt, 07_linear_yes_04.txt, 07_linear_yes_05.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
Case Name Status Exec Time Memory
01_random_yes_01.txt AC 64 ms 7876 KiB
01_random_yes_02.txt AC 127 ms 10176 KiB
01_random_yes_03.txt AC 68 ms 7960 KiB
01_random_yes_04.txt AC 99 ms 9192 KiB
01_random_yes_05.txt AC 128 ms 10156 KiB
01_random_yes_06.txt AC 192 ms 12748 KiB
01_random_yes_07.txt AC 54 ms 7512 KiB
01_random_yes_08.txt AC 113 ms 9664 KiB
01_random_yes_09.txt AC 90 ms 8844 KiB
01_random_yes_10.txt AC 58 ms 7784 KiB
02_random_no_01.txt AC 27 ms 8300 KiB
02_random_no_02.txt AC 35 ms 8960 KiB
02_random_no_03.txt AC 12 ms 6792 KiB
02_random_no_04.txt AC 38 ms 9984 KiB
02_random_no_05.txt AC 12 ms 6572 KiB
02_random_no_06.txt AC 36 ms 9480 KiB
02_random_no_07.txt AC 35 ms 9096 KiB
02_random_no_08.txt AC 21 ms 7688 KiB
02_random_no_09.txt AC 40 ms 9684 KiB
02_random_no_10.txt AC 24 ms 8512 KiB
03_large_yes_01.txt AC 184 ms 12332 KiB
03_large_yes_02.txt AC 189 ms 12672 KiB
03_large_yes_03.txt AC 193 ms 12836 KiB
03_large_yes_04.txt AC 190 ms 12696 KiB
03_large_yes_05.txt AC 198 ms 12812 KiB
04_large_no_01.txt AC 47 ms 12688 KiB
04_large_no_02.txt AC 40 ms 9832 KiB
04_large_no_03.txt AC 43 ms 11528 KiB
04_large_no_04.txt AC 42 ms 9612 KiB
04_large_no_05.txt AC 47 ms 11480 KiB
05_small_yes_01.txt AC 8 ms 5852 KiB
05_small_yes_02.txt AC 5 ms 5960 KiB
05_small_yes_03.txt AC 8 ms 5956 KiB
05_small_yes_04.txt AC 3 ms 5848 KiB
05_small_yes_05.txt AC 7 ms 5952 KiB
06_small_no_01.txt AC 11 ms 5908 KiB
06_small_no_02.txt AC 9 ms 5952 KiB
06_small_no_03.txt AC 4 ms 5908 KiB
06_small_no_04.txt AC 5 ms 5896 KiB
06_small_no_05.txt AC 4 ms 5852 KiB
07_linear_yes_01.txt AC 194 ms 14708 KiB
07_linear_yes_02.txt AC 194 ms 14704 KiB
07_linear_yes_03.txt AC 182 ms 14280 KiB
07_linear_yes_04.txt AC 189 ms 14200 KiB
07_linear_yes_05.txt AC 192 ms 14724 KiB
sample_01.txt AC 10 ms 5912 KiB
sample_02.txt AC 5 ms 5944 KiB
sample_03.txt AC 4 ms 5836 KiB
sample_04.txt AC 5 ms 5896 KiB
sample_05.txt AC 5 ms 5912 KiB