Source Code
/*
      ___           ___           ___           ___           ___           ___           ___          _____          ___           ___           ___
     /  /\         /__/\         /  /\         /__/\         /  /\         /__/\         /  /\        /  /::\        /  /\         /  /\         /  /\
    /  /::\       |  |::\       /  /::\        \  \:\       /  /::\       |  |::\       /  /::\      /  /:/\:\      /  /::\       /  /::\       /  /::\
   /  /:/\:\      |  |:|:\     /  /:/\:\        \__\:\     /  /:/\:\      |  |:|:\     /  /:/\:\    /  /:/  \:\    /  /:/\:\     /  /:/\:\     /  /:/\:\
  /  /:/  \:\   __|__|:|\:\   /  /:/  \:\   ___ /  /::\   /  /:/~/::\   __|__|:|\:\   /  /:/~/::\  /__/:/ \__\:|  /  /:/  \:\   /  /:/  \:\   /  /:/  \:\
 /__/:/ \__\:\ /__/::::| \:\ /__/:/ \__\:\ /__/\  /:/\:\ /__/:/ /:/\:\ /__/::::| \:\ /__/:/ /:/\:\ \  \:\ /  /:/ /__/:/ \__\:\ /__/:/ \__\:\ /__/:/ \__\:\
 \  \:\ /  /:/ \  \:\~~\__\/ \  \:\ /  /:/ \  \:\/:/__\/ \  \:\/:/__\/ \  \:\~~\__\/ \  \:\/:/__\/  \  \:\  /:/  \  \:\ /  /:/ \  \:\ /  /:/ \  \:\ /  /:/
  \  \:\  /:/   \  \:\        \  \:\  /:/   \  \::/       \  \::/       \  \:\        \  \::/        \  \:\/:/    \  \:\  /:/   \  \:\  /:/   \  \:\  /:/
   \  \:\/:/     \  \:\        \  \:\/:/     \  \:\        \  \:\        \  \:\        \  \:\         \  \::/      \  \:\/:/     \  \:\/:/     \  \:\/:/
    \  \::/       \  \:\        \  \::/       \  \:\        \  \:\        \  \:\        \  \:\         \__\/        \  \::/       \  \::/       \  \::/
     \__\/         \__\/         \__\/         \__\/         \__\/         \__\/         \__\/                       \__\/         \__\/         \__\/

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll,ll >
#define F first
#define S second
#define ld long double

using namespace std;
using namespace __gnu_pbds;

//typedef tree<ll , null_type , less_equal<ll> ,rb_tree_tag ,tree_order_statistics_node_update >ordered_set;

const int MOD=1e9+7;
const int  N=2e5+7;
const ll INF= 1e18+10;

long long po(ll x,ll y)
{
    ll ans=1;
    while(y){
        if(y & 1) ans=(ans*x);//%MOD;
        y/=2;
        x=(x*x);//%MOD;
    }
    return ans;
}

ll n,m;
ll a[N];
ll ctr[N];
bool vis[N];
map<ll,pii>mp;
map<pii,ll>mp2;
vector<ll>v[N];
vector<ll>ans;
bool don[N];
vector<ll>all[N];
bool no[N];

void dfs(ll x,ll p)
{
    if(vis[x]) return;
    vis[x]=1;
    if(x==p){
        if(v[x].size()==0){
            ans.pb(x);
            for(ll i=0;i<all[x].size();i++) ans.pb(all[x][i]);
            return;
        }
        else if(v[x].size()==1){
            dfs(v[x][0],x);
            ans.pb(x);
            for(ll i=0;i<all[x].size();i++) ans.pb(all[x][i]);
        }
        else{
            dfs(v[x][0],x);
            ans.pb(x);
            for(ll i=0;i<all[x].size();i++) ans.pb(all[x][i]);
            dfs(v[x][1],x);
        }
    }
    else{
        if(v[x].size()==1){
            ans.pb(x);
            for(ll i=0;i<all[x].size();i++) ans.pb(all[x][i]);
            return;
        }
        else{
            bool k=0;
            for(ll i=0;i<v[x].size();i++){
                if(v[x][i]!=p){
                    if(!k){
                        dfs(v[x][i],x);
                        ans.pb(x);
                        for(ll j=0;j<all[x].size();j++) ans.pb(all[x][i]);
                        k=1;
                    }
                    else{
                        dfs(v[x][i],x);
                        return;
                    }
                }
            }
        }
    }
}

void solve()
{
    cin>>n>>m;
    bool f=0;
    ll curr=1;
    while(n--){
        ll x,y,z;
        cin>>x;
        if(x==1){
            cin>>y;
            if(mp2[{y,0}]){
                all[mp2[{y,0}]].pb(curr);
            }
            else{
                if(a[y]){
                    v[curr].pb(a[y]);
                    v[a[y]].pb(curr);
                }
                a[y]=curr;
                mp2[{y,0}]=curr;
                mp[curr]={y,0};
                don[curr]=1;
            }
                curr++;
        }
        else{
            cin>>y>>z;
            if(y>z) swap(y,z);
            if(mp2[{y,z}]){
                all[mp2[{y,z}]].pb(curr);
            }
            else{
                if(1 == 2){
                    f=1;
                }
                else{
                    if(a[y]){
                        v[a[y]].pb(curr);
                        v[curr].pb(a[y]);
                    }
                    else if(a[z]){
                        v[a[z]].pb(curr);
                        v[curr].pb(a[z]);
                    }
                    a[y]=curr;
                    a[z]=curr;
                    mp2[{y,z}]=curr;
                    mp[curr]={y,z};
                }
                don[curr]=1;
                curr++;
            }
        }
    }
    for(ll i=1;i<=m;i++){
        if(!vis[i] && don[i]){
            dfs(i,i);
        }
    }
    ll fi,se;
    for(ll i=0;i<ans.size();i++){
        if(!ans[i]){
            cout<<-1<<endl;
            return;
        }
        if(!i){
            no[mp[ans[i]].F]=1;
            if(mp[ans[i]].S) no[mp[ans[i]].S]=1;
            fi=mp[ans[i]].F;
            se=mp[ans[i]].S;
        }
        else{
            if(no[mp[ans[i]].F]){
                if(mp[ans[i]].F!=fi && mp[ans[i]].F!=se){
                    cout<<-1<<endl;
                    return;
                }

            }
            no[mp[ans[i]].F]=1;
            if(mp[ans[i]].S){
                if(no[mp[ans[i]].S]){
                    if(mp[ans[i]].S!=fi && mp[ans[i]].S!=se){
                        cout<<-1<<endl;
                        return;
                    }
                }
                no[mp[ans[i]].S]=1;
            }
            fi=mp[ans[i]].F;
            se=mp[ans[i]].S;
        }
    }
    for(ll i=0;i<ans.size();i++) cout<<ans[i]<<' ';
    cout<<endl;




}

int main()
{
    //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("262144.in","r",stdin);freopen("262144.out","w",stdout);
    int t=1;
    //cin>>t;
    while(t--){ solve();}
    return 0;
}

Copy
Gym omohamadooo
GNU G++17
8 ms
10.0 MB
Wrong Answer