#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define ll int
#define db double
#define pb push_back
#define pf push_front
#define newl "\n"
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define f first
#define s second
#define MOD 1000000007
using namespace std;
#pragma GCC diagnostic ignored "-Wunused-result"
void setIO(string name = "") {
ios_base::sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(15);
if (name.size()) {
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
map <pair <ll, ll>, bool> occ;
map <ll, vector <ll>> fx, fy;
bool C(ll x, ll y)
{
if (occ[{x + 1, y}]) return 1;
else if (occ[{x - 1, y}]) return 1;
else if (occ[{x, y + 1}]) return 1;
else if (occ[{x, y - 1}]) return 1;
if (occ[{x + 1, y + 1}]) return 1;
else if (occ[{x + 1, y - 1}]) return 1;
else if (occ[{x - 1, y + 1}]) return 1;
else if (occ[{x - 1, y - 1}]) return 1;
return 0;
}
bool cyc;
map <ll, map <ll, bool>> vis;
void dfs(ll px, ll py, ll x, ll y)
{
if (occ[{x + 1, y}])
{
if (!vis[x + 1][y])
{
vis[x + 1][y] = true;
dfs(x, y, x + 1, y);
}
else if (px != x + 1 && py != y) cyc = true;
}
if (occ[{x - 1, y}])
{
if (!vis[x - 1][y])
{
vis[x - 1][y] = true;
dfs(x, y, x - 1, y);
}
else if (px != x - 1 && py != y) cyc = true;
}
if (occ[{x, y + 1}])
{
if (!vis[x][y + 1])
{
vis[x][y + 1] = true;
dfs(x, y, x, y + 1);
}
else if (px != x && py != y + 1) cyc = true;
}
if (occ[{x, y - 1}])
{
if (!vis[x][y - 1])
{
vis[x][y - 1] = true;
dfs(x, y, x, y - 1);
}
else if (px != x && py != y - 1) cyc = true;
}
}
int main()
{
fast
//setIO("");
//freopen("filename.in", "r", stdin);
//freopen("filename.out", "w", stdout);
ll n, t; cin >> n >> t;
vector <pair <ll, ll>> vec(n);
vector <ll> per(n);
for (ll i = 0; i < n; i++)
{
cin >> vec[i].f >> vec[i].s;
per[i] = i;
}
if (n == 1)
{
cout << "YES" << newl << 1;
return 0;
}
do
{
if (occ.size()) occ.clear();
if (fx.size()) fx.clear();
if (fy.size()) fy.clear();
bool works = true;
ll c = 0;
for (auto idx : per)
{
cyc = 0;
if (vis.size()) vis.clear();
ll x = vec[idx].f, y = vec[idx].s;
c++;
occ[vec[idx]] = true;
fx[x].pb(y);
fy[y].pb(x);
if (!C(x, y) && c != 1)
{
works = false;
break;
}
ll yy = upper_bound(all(fx[x]), y) - fx[x].begin();
ll xx = upper_bound(all(fy[y]), x) - fy[y].begin();
if (yy != fx[x].size()) dfs(x, fx[x][yy], x, fx[x][yy]);
else if (xx != fy[y].size()) dfs(fy[y][xx], y, fy[y][xx], y);
if (cyc)
{
works = false;
break;
}
}
if (works)
{
cout << "YES" << newl;
for (auto v : per) cout << v + 1 << newl;
return 0;
}
} while(next_permutation(all(per)));
cout << "NO";
}
Compilation message
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:142:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | if (yy != fx[x].size()) dfs(x, fx[x][yy], x, fx[x][yy]);
| ~~~^~~~~~~~~~~~~~~
skyscrapers.cpp:143:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
143 | else if (xx != fy[y].size()) dfs(fy[y][xx], y, fy[y][xx], y);
| ~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
316 KB |
ans=YES N=4 |
3 |
Correct |
1 ms |
224 KB |
ans=NO N=4 |
4 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
320 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
382 ms |
212 KB |
ans=NO N=9 |
8 |
Execution timed out |
3552 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
316 KB |
ans=YES N=4 |
3 |
Correct |
1 ms |
224 KB |
ans=NO N=4 |
4 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
320 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
382 ms |
212 KB |
ans=NO N=9 |
8 |
Execution timed out |
3552 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
316 KB |
ans=YES N=4 |
3 |
Correct |
1 ms |
224 KB |
ans=NO N=4 |
4 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
320 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
382 ms |
212 KB |
ans=NO N=9 |
8 |
Execution timed out |
3552 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3562 ms |
340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
316 KB |
ans=YES N=4 |
3 |
Correct |
1 ms |
224 KB |
ans=NO N=4 |
4 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
320 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
382 ms |
212 KB |
ans=NO N=9 |
8 |
Execution timed out |
3552 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3578 ms |
1108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3562 ms |
340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |