Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)

const int N = 1000000;
map<pair<int, int>, vector<int> > mp;
vector<int> g[N + 1];
int n, m, fr[N + 1];
bool vis[N + 1];
vector<int> an;

bool is_main_node(int v){
	return g[v].size() >= 2 &amp;&amp; v;
}

void no(){
	printf("-1\n");
	exit(0);
}

pair<int, int> make_p(int a, int b){
	if (a > b)swap(a, b);
	return make_pair(a, b);
}

void fill_edge(int a, int b){
	for (int t: mp[make_p(a, b)])an.push_back(t);
}

void go(int v){
	vis[v] = true;
	for (int u: g[v])if (!is_main_node(u)){
		fill_edge(v, u);
		vis[u] = true;
	}
	for (int u: g[v])if (is_main_node(u) &amp;&amp; !vis[u]){
		fill_edge(v, u);
		return go(u);
	}
}

int main() {
	scanf("%d%d", &amp;n, &amp;m);

	f(i, 1, n + 1){
		int s, a, b = 0;
		scanf("%d%d", &amp;s, &amp;a);
		if (s == 2)scanf("%d", &amp;b);
		mp[make_p(a, b)].push_back(i);
		g[a].push_back(b);
		g[b].push_back(a);
	}

	f(i, 1, m + 1){
		sort(g[i].begin(), g[i].end());
		g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin());
	}

	f(i, 1, m + 1)for (int u: g[i])if (is_main_node(u))++fr[i];
	f(i, 1, m + 1)if (fr[i]> 2)no();
	f(i, 1, m + 1)if (!vis[i] &amp;&amp; is_main_node(i) &amp;&amp; fr[i] <= 1)go(i);
	f(i, 1, m + 1)if (!vis[i] &amp;&amp; fr[i] == 0)go(i);
	f(i, 1, m + 1)if (!vis[i])no();

	f(i, 0, n)printf("%d%c", an[i], i + 1 < n ? ' ' : '\n');
}
Copy
Gym moathhamudah
GNU G++17
0 ms
0 KB
Compilation Error