#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
const vector<pii > dir = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
const vector<pii > dir_2 = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
int n, type;
vector<pair<int, int> > v;
unordered_map<int, unordered_map<int, int> > which;
vector<int> ans;
set<int> doable;
bool has_neighbour(pii cur) {
for(pii d : dir) {
pii nei = mp(cur.fi + d.fi, cur.se + d.se);
int nei_idx = which[nei.fi][nei.se];
if(nei_idx != 0) {
return true;
}
}
return false;
}
int count_bfs(int start, int miss) {
queue<int> q;
vector<bool> vis(1 + n, false);
q.push(start);
vis[start] = true;
while(!q.empty()) {
int cur = q.front();
q.pop();
for(pii d : dir) {
pii nei = mp(v[cur].fi + d.fi, v[cur].se + d.se);
int nei_idx = which[nei.fi][nei.se];
if(nei_idx != 0 && !vis[nei_idx] && nei_idx != miss) {
q.push(nei_idx);
vis[nei_idx] = true;
}
}
}
int res = 0;
for(int i = 1; i <= n; i++) {
if(vis[i]) {
res++;
}
}
return res;
}
queue<pii> q;
unordered_map<int, unordered_map<int, bool> > seen;
void upd_bfs() {
while(!q.empty()) {
pii cur = q.front();
q.pop();
for(pii d : dir_2) {
pii nei = mp(cur.fi + d.fi, cur.se + d.se);
int nei_idx = which[nei.fi][nei.se];
if(nei_idx != 0) {
doable.insert(nei_idx);
} else {
if(!seen[nei.fi][nei.se] && has_neighbour(nei)) {
q.push(nei);
seen[nei.fi][nei.se] = true;
}
}
}
}
}
void solve() {
cin >> n >> type;
v.assign(1 + n, mp(0, 0));
int bottom_left = 1;
for(int i = 1; i <= n; i++) {
cin >> v[i].fi >> v[i].se;
which[v[i].fi][v[i].se] = i;
if(v[i].fi < v[bottom_left].fi) {
bottom_left = i;
}
}
if(count_bfs(1, -1) < n) {
cout << "NO\n";
return;
}
cout << "YES\n";
set<int> rem;
for(int i = 1; i <= n; i++) {
rem.insert(i);
}
pii start = mp(v[bottom_left].fi - 1, v[bottom_left].se);
q.push(start);
seen[start.fi][start.se] = true;
upd_bfs();
while(!doable.empty()) {
int to_rem = -1;
for(int cur : doable) {
int st = *rem.begin();
if(st == cur) {
st = *rem.rbegin();
}
if(count_bfs(st, cur) >= (int) rem.size() - 1) {
// cur can be removed
to_rem = cur;
break;
}
}
ans.pb(to_rem);
rem.erase(to_rem);
doable.erase(to_rem);
q.push(v[to_rem]);
which[v[to_rem].fi][v[to_rem].se] = 0;
upd_bfs();
}
reverse(ans.begin(), ans.end());
for(int cur : ans) {
cout << cur << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
212 KB |
ans=YES N=4 |
3 |
Correct |
0 ms |
212 KB |
ans=NO N=4 |
4 |
Correct |
0 ms |
328 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
212 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
1 ms |
212 KB |
ans=NO N=9 |
8 |
Correct |
0 ms |
212 KB |
ans=NO N=10 |
9 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
10 |
Correct |
1 ms |
320 KB |
ans=YES N=10 |
11 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
12 |
Correct |
1 ms |
324 KB |
ans=YES N=9 |
13 |
Correct |
1 ms |
340 KB |
ans=YES N=9 |
14 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
15 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
16 |
Correct |
1 ms |
212 KB |
ans=NO N=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
212 KB |
ans=YES N=4 |
3 |
Correct |
0 ms |
212 KB |
ans=NO N=4 |
4 |
Correct |
0 ms |
328 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
212 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
1 ms |
212 KB |
ans=NO N=9 |
8 |
Correct |
0 ms |
212 KB |
ans=NO N=10 |
9 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
10 |
Correct |
1 ms |
320 KB |
ans=YES N=10 |
11 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
12 |
Correct |
1 ms |
324 KB |
ans=YES N=9 |
13 |
Correct |
1 ms |
340 KB |
ans=YES N=9 |
14 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
15 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
16 |
Correct |
1 ms |
212 KB |
ans=NO N=2 |
17 |
Correct |
1 ms |
212 KB |
ans=YES N=17 |
18 |
Correct |
1 ms |
340 KB |
ans=YES N=25 |
19 |
Correct |
6 ms |
320 KB |
ans=YES N=100 |
20 |
Correct |
30 ms |
340 KB |
ans=YES N=185 |
21 |
Correct |
1 ms |
340 KB |
ans=NO N=174 |
22 |
Correct |
4 ms |
340 KB |
ans=YES N=90 |
23 |
Correct |
5 ms |
364 KB |
ans=YES N=63 |
24 |
Correct |
5 ms |
340 KB |
ans=YES N=87 |
25 |
Correct |
21 ms |
372 KB |
ans=YES N=183 |
26 |
Correct |
30 ms |
340 KB |
ans=YES N=188 |
27 |
Correct |
53 ms |
372 KB |
ans=YES N=183 |
28 |
Correct |
24 ms |
376 KB |
ans=YES N=189 |
29 |
Correct |
21 ms |
340 KB |
ans=YES N=200 |
30 |
Correct |
99 ms |
328 KB |
ans=YES N=190 |
31 |
Correct |
24 ms |
340 KB |
ans=YES N=187 |
32 |
Correct |
58 ms |
396 KB |
ans=YES N=187 |
33 |
Correct |
102 ms |
340 KB |
ans=YES N=182 |
34 |
Correct |
99 ms |
400 KB |
ans=YES N=184 |
35 |
Correct |
130 ms |
340 KB |
ans=YES N=188 |
36 |
Correct |
114 ms |
416 KB |
ans=YES N=181 |
37 |
Correct |
158 ms |
432 KB |
ans=YES N=188 |
38 |
Correct |
182 ms |
436 KB |
ans=YES N=191 |
39 |
Correct |
51 ms |
376 KB |
ans=YES N=196 |
40 |
Correct |
57 ms |
396 KB |
ans=YES N=196 |
41 |
Correct |
54 ms |
424 KB |
ans=YES N=196 |
42 |
Correct |
48 ms |
340 KB |
ans=YES N=196 |
43 |
Correct |
145 ms |
328 KB |
ans=YES N=195 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
212 KB |
ans=YES N=4 |
3 |
Correct |
0 ms |
212 KB |
ans=NO N=4 |
4 |
Correct |
0 ms |
328 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
212 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
1 ms |
212 KB |
ans=NO N=9 |
8 |
Correct |
0 ms |
212 KB |
ans=NO N=10 |
9 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
10 |
Correct |
1 ms |
320 KB |
ans=YES N=10 |
11 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
12 |
Correct |
1 ms |
324 KB |
ans=YES N=9 |
13 |
Correct |
1 ms |
340 KB |
ans=YES N=9 |
14 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
15 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
16 |
Correct |
1 ms |
212 KB |
ans=NO N=2 |
17 |
Correct |
1 ms |
212 KB |
ans=YES N=17 |
18 |
Correct |
1 ms |
340 KB |
ans=YES N=25 |
19 |
Correct |
6 ms |
320 KB |
ans=YES N=100 |
20 |
Correct |
30 ms |
340 KB |
ans=YES N=185 |
21 |
Correct |
1 ms |
340 KB |
ans=NO N=174 |
22 |
Correct |
4 ms |
340 KB |
ans=YES N=90 |
23 |
Correct |
5 ms |
364 KB |
ans=YES N=63 |
24 |
Correct |
5 ms |
340 KB |
ans=YES N=87 |
25 |
Correct |
21 ms |
372 KB |
ans=YES N=183 |
26 |
Correct |
30 ms |
340 KB |
ans=YES N=188 |
27 |
Correct |
53 ms |
372 KB |
ans=YES N=183 |
28 |
Correct |
24 ms |
376 KB |
ans=YES N=189 |
29 |
Correct |
21 ms |
340 KB |
ans=YES N=200 |
30 |
Correct |
99 ms |
328 KB |
ans=YES N=190 |
31 |
Correct |
24 ms |
340 KB |
ans=YES N=187 |
32 |
Correct |
58 ms |
396 KB |
ans=YES N=187 |
33 |
Correct |
102 ms |
340 KB |
ans=YES N=182 |
34 |
Correct |
99 ms |
400 KB |
ans=YES N=184 |
35 |
Correct |
130 ms |
340 KB |
ans=YES N=188 |
36 |
Correct |
114 ms |
416 KB |
ans=YES N=181 |
37 |
Correct |
158 ms |
432 KB |
ans=YES N=188 |
38 |
Correct |
182 ms |
436 KB |
ans=YES N=191 |
39 |
Correct |
51 ms |
376 KB |
ans=YES N=196 |
40 |
Correct |
57 ms |
396 KB |
ans=YES N=196 |
41 |
Correct |
54 ms |
424 KB |
ans=YES N=196 |
42 |
Correct |
48 ms |
340 KB |
ans=YES N=196 |
43 |
Correct |
145 ms |
328 KB |
ans=YES N=195 |
44 |
Correct |
2 ms |
724 KB |
ans=NO N=1934 |
45 |
Correct |
1 ms |
468 KB |
ans=NO N=1965 |
46 |
Execution timed out |
3592 ms |
712 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
ans=NO N=1934 |
2 |
Correct |
1 ms |
460 KB |
ans=NO N=1965 |
3 |
Execution timed out |
3589 ms |
596 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
ans=YES N=1 |
2 |
Correct |
1 ms |
212 KB |
ans=YES N=4 |
3 |
Correct |
0 ms |
212 KB |
ans=NO N=4 |
4 |
Correct |
0 ms |
328 KB |
ans=YES N=5 |
5 |
Correct |
1 ms |
212 KB |
ans=YES N=9 |
6 |
Correct |
1 ms |
212 KB |
ans=YES N=5 |
7 |
Correct |
1 ms |
212 KB |
ans=NO N=9 |
8 |
Correct |
0 ms |
212 KB |
ans=NO N=10 |
9 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
10 |
Correct |
1 ms |
320 KB |
ans=YES N=10 |
11 |
Correct |
0 ms |
212 KB |
ans=YES N=10 |
12 |
Correct |
1 ms |
324 KB |
ans=YES N=9 |
13 |
Correct |
1 ms |
340 KB |
ans=YES N=9 |
14 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
15 |
Correct |
1 ms |
212 KB |
ans=YES N=8 |
16 |
Correct |
1 ms |
212 KB |
ans=NO N=2 |
17 |
Correct |
1 ms |
212 KB |
ans=YES N=17 |
18 |
Correct |
1 ms |
340 KB |
ans=YES N=25 |
19 |
Correct |
6 ms |
320 KB |
ans=YES N=100 |
20 |
Correct |
30 ms |
340 KB |
ans=YES N=185 |
21 |
Correct |
1 ms |
340 KB |
ans=NO N=174 |
22 |
Correct |
4 ms |
340 KB |
ans=YES N=90 |
23 |
Correct |
5 ms |
364 KB |
ans=YES N=63 |
24 |
Correct |
5 ms |
340 KB |
ans=YES N=87 |
25 |
Correct |
21 ms |
372 KB |
ans=YES N=183 |
26 |
Correct |
30 ms |
340 KB |
ans=YES N=188 |
27 |
Correct |
53 ms |
372 KB |
ans=YES N=183 |
28 |
Correct |
24 ms |
376 KB |
ans=YES N=189 |
29 |
Correct |
21 ms |
340 KB |
ans=YES N=200 |
30 |
Correct |
99 ms |
328 KB |
ans=YES N=190 |
31 |
Correct |
24 ms |
340 KB |
ans=YES N=187 |
32 |
Correct |
58 ms |
396 KB |
ans=YES N=187 |
33 |
Correct |
102 ms |
340 KB |
ans=YES N=182 |
34 |
Correct |
99 ms |
400 KB |
ans=YES N=184 |
35 |
Correct |
130 ms |
340 KB |
ans=YES N=188 |
36 |
Correct |
114 ms |
416 KB |
ans=YES N=181 |
37 |
Correct |
158 ms |
432 KB |
ans=YES N=188 |
38 |
Correct |
182 ms |
436 KB |
ans=YES N=191 |
39 |
Correct |
51 ms |
376 KB |
ans=YES N=196 |
40 |
Correct |
57 ms |
396 KB |
ans=YES N=196 |
41 |
Correct |
54 ms |
424 KB |
ans=YES N=196 |
42 |
Correct |
48 ms |
340 KB |
ans=YES N=196 |
43 |
Correct |
145 ms |
328 KB |
ans=YES N=195 |
44 |
Correct |
2 ms |
724 KB |
ans=NO N=1934 |
45 |
Correct |
1 ms |
468 KB |
ans=NO N=1965 |
46 |
Execution timed out |
3592 ms |
712 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
5980 KB |
ans=NO N=66151 |
2 |
Correct |
23 ms |
4304 KB |
ans=NO N=64333 |
3 |
Execution timed out |
3594 ms |
8112 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
ans=NO N=1934 |
2 |
Correct |
1 ms |
460 KB |
ans=NO N=1965 |
3 |
Execution timed out |
3589 ms |
596 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |