#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
const int maxn = 1e5 + 100;
inline int read(){
int x = 0;char c = getchar();
while(!isdigit(c))c = getchar();
while(isdigit(c))x = x * 10 + c - '0',c = getchar();
return x;
}
vector<int> G[16],vec[16];
int n,beg,end,cnt[16],pos[16],nxt[maxn][4];
clock_t begt;
inline int get(int x){
return pos[x] == vec[x].size() ? 0x7fffffff : vec[x][pos[x]];
}
inline void msort(int id){
if(get(nxt[id][1]) < get(nxt[id][0]))swap(nxt[id][1],nxt[id][0]);
if(get(nxt[id][2]) < get(nxt[id][0]))swap(nxt[id][2],nxt[id][0]);
if(get(nxt[id][2]) < get(nxt[id][1]))swap(nxt[id][1],nxt[id][2]);
}
inline void add(int u,int v){G[u].push_back(v);}
inline void init(){
add(1,3);add(1,4);add(1,8);
add(2,1);add(2,2);add(2,7);
add(3,3);add(3,4);add(3,8);
add(4,1);add(4,2);add(4,7);
add(5,3);add(5,4);add(5,8);
add(6,1);add(6,2);add(6,7);
}
int ans[maxn];
inline void dfs(int step,int lst){
if((clock() - begt) * 1000 / CLOCKS_PER_SEC >= 900){
puts("-1");
exit(0);
}
if(step == n){
for(int i = 1;i <= n;i++)printf("%d ",ans[i]);
puts("");
exit(0);
}
for(int i = 0;i < G[lst].size();i++)nxt[step][i] = G[lst][i];
msort(step);
for(int i = 0;i < G[lst].size();i++){
int v = nxt[step][i];
if(!cnt[v])continue;
ans[step + 1] = vec[v][pos[v]];
pos[v]++;
cnt[v]--;
dfs(step + 1,v);
pos[v]--;
cnt[v]++;
}
}
int main(){
// freopen("slagalica.in","r",stdin);
// freopen("slagalica.out","w",stdout);
begt = clock();
n = read();
for(int x,i = 1;i <= n;i++){
int a = read(),b = read();
cnt[a]++;
vec[a].push_back(b);
}
for(int i = 1;i <= 8;i++)sort(vec[i].begin(),vec[i].end());
init();
if(cnt[5])ans[1] = vec[5][0],dfs(1,5);
else if(cnt[6])ans[1] = vec[6][0],dfs(1,6);
puts("-1");
fclose(stdin);
fclose(stdout);
return 0;
}
Compilation message
slagalica.cpp: In function 'int get(int)':
slagalica.cpp:18:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | return pos[x] == vec[x].size() ? 0x7fffffff : vec[x][pos[x]];
| ~~~~~~~^~~~~~~~~~~~~~~~
slagalica.cpp: In function 'void dfs(int, int)':
slagalica.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0;i < G[lst].size();i++)nxt[step][i] = G[lst][i];
| ~~^~~~~~~~~~~~~~~
slagalica.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0;i < G[lst].size();i++){
| ~~^~~~~~~~~~~~~~~
slagalica.cpp: In function 'int main()':
slagalica.cpp:63:10: warning: unused variable 'x' [-Wunused-variable]
63 | for(int x,i = 1;i <= n;i++){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
8992 KB |
Output is correct |
2 |
Correct |
96 ms |
9660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
8496 KB |
Output is correct |
2 |
Correct |
89 ms |
9072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
5108 KB |
Output is correct |
2 |
Incorrect |
901 ms |
7636 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
4388 KB |
Output is correct |
2 |
Correct |
92 ms |
8300 KB |
Output is correct |
3 |
Incorrect |
901 ms |
7964 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
901 ms |
4884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
8560 KB |
Output is correct |
2 |
Correct |
71 ms |
5620 KB |
Output is correct |
3 |
Incorrect |
901 ms |
7800 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
901 ms |
7800 KB |
Output is correct |
2 |
Correct |
86 ms |
8568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
10104 KB |
Output is correct |
2 |
Correct |
901 ms |
8768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
8440 KB |
Output is correct |
2 |
Correct |
901 ms |
9080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
9932 KB |
Output is correct |
2 |
Correct |
901 ms |
9868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
8440 KB |
Output is correct |
2 |
Correct |
901 ms |
9208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
8524 KB |
Output is correct |
2 |
Correct |
901 ms |
9696 KB |
Output is correct |