// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
// consider doing reverse
// pluck the biggest and reachable out
// then update the neighbor to touched and reachable
// priority queue maintain valid points
// reach: can be reached from outside
// touch: has a neighbor plucked
struct DSU {
int n, s; vector<int> f;
DSU(int n = 0) : n(n), s(n), f(n+1) { iota(AI(f), 0); }
int F(int a){ return a == f[a] ? a : f[a] = F(f[a]); }
int M(int a, int b){
a = F(a), b = F(b);
if(a == b) return false;
f[b] = a; --s; return true;
}
int operator() (int a){ return F(a); }
};
struct LISAN : vector<int> {
void done() { sort(AI()); erase(unique(AI()), end()); }
int operator() (int a) { return lower_bound(AI(), a) - begin(); }
};
const int kN = 150051;
const int INF = 1e9 + 7;
const array<pii, 4> d4 = {pii(1, 0), pii(-1, 0), pii(0, 1), pii(0, -1)};
const array<pii, 8> d8 = {pii(1, 1), pii(1, 0), pii(1,-1), pii(0, 1),
pii(0, -1), pii(-1, 1), pii(-1, 0), pii(-1, -1)};
int n, t;
pii a[kN];
map<pii, int> mp;
vector<int> adj[kN];
vector<int> getOuter(){
LISAN lr, lc;
for(int i = 1; i <= n; ++i){ lr.pb(a[i].ff); lc.pb(a[i].ss); }
lr.done(); lc.done();
vector<int> ans;
vector<int> mr(lr.size(), INF), mc(lc.size(), INF);
vector<int> Mr(lr.size(), -INF), Mc(lc.size(), -INF);
for(int i = 1; i <= n; ++i){
auto[r, c] = a[i]; r = lr(r); c = lc(c);
chmin(mr[r], c); chmin(mc[c], r);
chmax(Mr[r], c); chmax(Mc[c], r);
}
for(int i = 0; i < lr.size(); ++i)
if(mr[i] != INF) ans.pb(mp[pii(lr[i], lc[mr[i]])]);
for(int i = 0; i < lr.size(); ++i)
if(Mr[i] != -INF) ans.pb(mp[pii(lr[i], lc[Mr[i]])]);
for(int i = 0; i < lc.size(); ++i)
if(mc[i] != INF) ans.pb(mp[pii(lr[mc[i]], lc[i])]);
for(int i = 0; i < lc.size(); ++i)
if(Mc[i] != -INF) ans.pb(mp[pii(lr[Mc[i]], lc[i])]);
sort(AI(ans)); ans.erase(unique(AI(ans)), end(ans));
debug(mr);
debug(mc);
debug(Mr);
debug(Mc);
debug(ans);
return ans;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> t;
DSU dsu(n);
for(int r, c, i = 1; i <= n; ++i){
cin >> r >> c;
a[i] = pii(r, c);
mp[pii(r, c)] = i;
}
for(int i = 1; i <= n; ++i){
auto [r, c] = a[i];
for(auto [dr, dc]: d8){
pii u(r + dr, c + dc);
if(mp.count(u)){
int v = mp[u];
if(dsu.M(i, v)){
adj[i].pb(v);
adj[v].pb(i);
}
}
}
}
for(int i = 1; i <= n; ++i) debug(i, adj[i]);
if(dsu.s != 1){ cout << "NO"; return 0; }
cout << "YES\n";
vector<int> out = getOuter();
vector<int> ans, vis(n + 1, 0);
priority_queue<int> pq;
for(int i: out){
pq.push(i);
vis[i] = 1;
}
while(!pq.empty()){
int u = pq.top(); pq.pop();
debug(u);
vis[u] = 2; ans.pb(u);
for(int v: adj[u]){
if(vis[v] > 0) continue;
vis[v] = 1; pq.push(v);
}
}
reverse(AI(ans));
for(int i: ans) cout << i << ' ';
return 0;
}
Compilation message
skyscrapers.cpp: In function 'std::vector<int> getOuter()':
skyscrapers.cpp:71:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 0; i < lr.size(); ++i)
| ~~^~~~~~~~~~~
skyscrapers.cpp:73:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < lr.size(); ++i)
| ~~^~~~~~~~~~~
skyscrapers.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < lc.size(); ++i)
| ~~^~~~~~~~~~~
skyscrapers.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 0; i < lc.size(); ++i)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
3 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
3 ms |
3788 KB |
ans=NO N=4 |
4 |
Correct |
2 ms |
3788 KB |
ans=YES N=5 |
5 |
Correct |
3 ms |
3788 KB |
ans=YES N=9 |
6 |
Correct |
3 ms |
3788 KB |
ans=YES N=5 |
7 |
Correct |
3 ms |
3788 KB |
ans=NO N=9 |
8 |
Correct |
3 ms |
3788 KB |
ans=NO N=10 |
9 |
Incorrect |
3 ms |
3788 KB |
Full cells must be connected |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
3 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
3 ms |
3788 KB |
ans=NO N=4 |
4 |
Correct |
2 ms |
3788 KB |
ans=YES N=5 |
5 |
Correct |
3 ms |
3788 KB |
ans=YES N=9 |
6 |
Correct |
3 ms |
3788 KB |
ans=YES N=5 |
7 |
Correct |
3 ms |
3788 KB |
ans=NO N=9 |
8 |
Correct |
3 ms |
3788 KB |
ans=NO N=10 |
9 |
Incorrect |
3 ms |
3788 KB |
Full cells must be connected |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
3 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
3 ms |
3788 KB |
ans=NO N=4 |
4 |
Correct |
2 ms |
3788 KB |
ans=YES N=5 |
5 |
Correct |
3 ms |
3788 KB |
ans=YES N=9 |
6 |
Correct |
3 ms |
3788 KB |
ans=YES N=5 |
7 |
Correct |
3 ms |
3788 KB |
ans=NO N=9 |
8 |
Correct |
3 ms |
3788 KB |
ans=NO N=10 |
9 |
Incorrect |
3 ms |
3788 KB |
Full cells must be connected |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3960 KB |
ans=NO N=1934 |
2 |
Correct |
5 ms |
3984 KB |
ans=NO N=1965 |
3 |
Incorrect |
7 ms |
4036 KB |
Full cells must be connected |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
3 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
3 ms |
3788 KB |
ans=NO N=4 |
4 |
Correct |
2 ms |
3788 KB |
ans=YES N=5 |
5 |
Correct |
3 ms |
3788 KB |
ans=YES N=9 |
6 |
Correct |
3 ms |
3788 KB |
ans=YES N=5 |
7 |
Correct |
3 ms |
3788 KB |
ans=NO N=9 |
8 |
Correct |
3 ms |
3788 KB |
ans=NO N=10 |
9 |
Incorrect |
3 ms |
3788 KB |
Full cells must be connected |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
11336 KB |
ans=NO N=66151 |
2 |
Correct |
144 ms |
9924 KB |
ans=NO N=64333 |
3 |
Incorrect |
285 ms |
13200 KB |
Full cells must be connected |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3960 KB |
ans=NO N=1934 |
2 |
Correct |
5 ms |
3984 KB |
ans=NO N=1965 |
3 |
Incorrect |
7 ms |
4036 KB |
Full cells must be connected |
4 |
Halted |
0 ms |
0 KB |
- |