Source Code
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;
int n, m ;
vector<pair<int,int> > adj[N] ; 
vector<int> cnt[N] ; 
vector<int> ord ; 
bool vis[N] ;

struct dsu
{
      int fat[N];
      dsu()
      {
             iota(fat, fat + N, 0);
      }
      int find(int x) { return fat[x] = (x == fat[x] ? x : find(fat[x])); }
      void link(int u, int v)
      {
             u = find(u), v = find(v);
             fat[u] = v;
      }
      bool same(int u, int v)
      {
             return find(u) == find(v);
      }
} du ; 

void dfs(int x){
       vis[x] = 1; 
       for(auto u : cnt[x]){
              ord.push_back(u) ; 
       }
       for(auto u: adj[x]){
              if(vis[u.first])continue; 
              ord.push_back(u.second) ; 
              dfs(u.first) ; 
       }
}

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
       cin >> n >> m; 
       bool flag = 1 ;
       for(int i = 0 ;i < n; ++ i){
              int x ; cin >> x; 
              if(x == 1){
                     int u; cin >> u ; cnt[u].push_back(i) ;  
              }else{
                     int u, v; cin >> u >> v; 
                     if( du.same(u, v)) {
                            flag = 0 ;
                     }
                     du.link(u, v) ; 
                     adj[u].push_back({v,i}) ; 
                     adj[v].push_back({u,i}) ; 
              }
       }      
       if(flag == 0){
              cout << -1 ; 
              return 0; 
       }
       for(int i = 1;i <= m; ++ i){
              if(!vis[i]){
                     dfs(i) ; 
              }
       }
       for(auto u: ord)cout << ++u <<" " ;
       return 0;
}
Copy
Gym Mohamed.Sobhy
GNU G++17
6 ms
10.6 MB
Wrong Answer