#include<cstdio>
#include<algorithm>
#include<map>
#include<utility>
#include<vector>
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
vi pset; vi prank;
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
void initSet(int N) { pset.assign(N, 0); for(int i = 0; i< N; i++) pset[i] = i; prank.assign(N, 0);}
int findSet(int i) { return (pset[i] == i) ? i : (pset[i] = findSet(pset[i]));}
bool isSameSet(int i, int j) { return findSet(i) == findSet(j);}
void unionSet(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a != b) {
if (prank[a] < prank[b])
swap(a, b);
pset[b] = a;
if (prank[a] == prank[b])
prank[a]++;
}
}
map<ii, int> lookup;
int N, T;
ii scrapers[150010];
vector<ii> edges; // first item is always smaller
int dr[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc[] = {1, -1, 0, 1, -1, 0, 1, -1};
vi adj[150010];
bool vis[150010] = {};
void dfs(int u) {
vis[u] = true;
printf("%d\n", u + 1);
for(auto v: adj[u]) {
if(!vis[v]) {
dfs(v);
}
}
}
int main(){
scanf("%d%d", &N, &T);
for(int i=0;i<N;i++){
int r,c;
scanf("%d%d", &r, &c);
scrapers[i] = ii(r,c);
lookup[ii(r, c)] = i;
}
for(int i=0;i<N;i++){
ii cur = scrapers[i];
int r = cur.first;
int c = cur.second;
for(int j=0;j<8;j++) {
int nr = r + dr[j];
int nc = c + dc[j];
// printf("r c %d %d\n", r,c);
//printf("nr nc %d %d\n", nr, nc);
ii n = ii(nr, nc);
if(lookup.count(n) != 0 && lookup[n] > i) {
edges.push_back(ii(i, lookup[n]));
//adj[i].push_back(lookup[ii]);
//adj[lookup[ii]].push_back(i);
//printf("%d %d\n", i, lookup[n]);
}
}
}
sort(edges.begin(), edges.end());
reverse(edges.begin(), edges.end());
initSet(N);
int chosen = 0;
while(chosen < N - 1) {
if(edges.empty()) {
printf("NO\n");
return 0;
}
ii e = edges.back();edges.pop_back();
int a = e.first; int b = e.second;
if(!isSameSet(a, b)) {
chosen++;
adj[a].push_back(b);
adj[b].push_back(a);
unionSet(a, b);
}
}
for(int i = 0;i< N;i++) {
sort(adj[i].begin(), adj[i].end());
}
printf("YES\n");
dfs(0);
return 0;
}
Compilation message
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &N, &T);
| ~~~~~^~~~~~~~~~~~~~~~
skyscrapers.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d%d", &r, &c);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
2 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
2 ms |
3816 KB |
ans=NO N=4 |
4 |
Correct |
3 ms |
3816 KB |
ans=YES N=5 |
5 |
Incorrect |
2 ms |
3788 KB |
Added cell 8 (1,1) not reachable from infinity |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
2 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
2 ms |
3816 KB |
ans=NO N=4 |
4 |
Correct |
3 ms |
3816 KB |
ans=YES N=5 |
5 |
Incorrect |
2 ms |
3788 KB |
Added cell 8 (1,1) not reachable from infinity |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
2 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
2 ms |
3816 KB |
ans=NO N=4 |
4 |
Correct |
3 ms |
3816 KB |
ans=YES N=5 |
5 |
Incorrect |
2 ms |
3788 KB |
Added cell 8 (1,1) not reachable from infinity |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3960 KB |
ans=NO N=1934 |
2 |
Correct |
5 ms |
4044 KB |
ans=NO N=1965 |
3 |
Incorrect |
6 ms |
4044 KB |
Added cell 1824 (356,209) not reachable from infinity |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
ans=YES N=1 |
2 |
Correct |
2 ms |
3788 KB |
ans=YES N=4 |
3 |
Correct |
2 ms |
3816 KB |
ans=NO N=4 |
4 |
Correct |
3 ms |
3816 KB |
ans=YES N=5 |
5 |
Incorrect |
2 ms |
3788 KB |
Added cell 8 (1,1) not reachable from infinity |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
13060 KB |
ans=NO N=66151 |
2 |
Correct |
118 ms |
10272 KB |
ans=NO N=64333 |
3 |
Incorrect |
250 ms |
14676 KB |
Added cell 69316 (3,-133) not reachable from infinity |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3960 KB |
ans=NO N=1934 |
2 |
Correct |
5 ms |
4044 KB |
ans=NO N=1965 |
3 |
Incorrect |
6 ms |
4044 KB |
Added cell 1824 (356,209) not reachable from infinity |
4 |
Halted |
0 ms |
0 KB |
- |