This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i,l,r,d) for(int i=(l);i<=(r);i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF=2147483647;
const int MOD=1000000007;
const int mod=998244353;
const double eps=1e-12;
set<int> vis; /// visited edges
int n;
vector<pii> edge[3]; /// <idx,to>
int indeg[3], outdeg[3];
vi res;
/// nodes :
/// 0 : |
/// 1 : <
/// 2 : >
void EulerCircuit(int v){
for(pii i : edge[v]){ /// for all edges from v :
if( vis.find(i.F) == vis.end() ){ /// if i is not visited
/// mark i as visited
vis.insert(i.F);
/// dfs
EulerCircuit(i.S);
res.pb(i.F);
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
/// input
cin>>n;
FOR(i,1,n,1){
int type, idx;
cin>>type>>idx;
if(type==1) edge[1].eb(idx,2);
if(type==2) edge[1].eb(idx,1);
if(type==3) edge[2].eb(idx,2);
if(type==4) edge[2].eb(idx,1);
if(type==5) edge[0].eb(idx,2);
if(type==6) edge[0].eb(idx,1);
if(type==7) edge[1].eb(idx,0);
if(type==8) edge[2].eb(idx,0);
}
/// check if Euler circuit exists
FOR(i,0,2,1){
outdeg[i] = szof(edge[i]);
for(pii j : edge[i]){
indeg[j.S]++;
}
}
FOR(i,0,2,1){
if( indeg[i] != outdeg[i] ){
cout<<-1<<'\n';
return 0;
}
}
/// sort edges
FOR(i,0,2,1){
sort(edge[i].begin(),edge[i].end());
}
/// find lex. smallest Euler circuit
EulerCircuit(0);
/// output
reverse(res.begin(),res.end());
for(int i : res){
cout<<i<<" ";
}
cout<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |