1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<map> #include<set> #define N 2000010 #define fi first #define se second using namespace std; struct node{ int x,y; node(int x=0,int y=0):x(x),y(y){} bool operator <(const node a)const{return x==a.x?y<a.y:x<a.x;} }p[N]; node operator +(const node &a,const node &b){return node(a.x+b.x,a.y+b.y);} node P[]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; int n,f[N],col[N],deg[N],tt,rt; map<node,int>mp; int id(const node &a){if(mp.count(a)) return mp[a];return 0;} int find(int x){return x==f[x]?f[x]:(f[x]=find(f[x]));} set<int>su; bool check(int u) { if(deg[u]<=1) return true; bool hv=false; for(int i=0;i<4;i++) if(find(id(p[u]+P[i]))==rt){hv=true;break;} if(!hv) return false; static int Id[3][3]; int x=p[u].x,y=p[u].y; for(int i=0;i<=2;i++) for(int j=0;j<=2;j++) Id[i][j]=id({x+i-1,y+j-1}); auto id=[&](int x0,int y0){return Id[x0-x+1][y0-y+1];}; for(int i=-1;i<=1;i+=2) for(int j=-1;j<=1;j+=2) if(col[id(x+i,y+j)]==1) { int vx=id(x+i,y),vy=id(x,y+j); if(vx && vy && find(vx)==find(vy)) return false; } if(col[id(x+1,y)]==2 && col[id(x-1,y)]==2 && find(id(x+1,y))==find(id(x-1,y))) { int hv=0; for(int i=-1;i<=1;i++) if(col[id(x+i,y-1)]==1){hv++;break;} for(int i=-1;i<=1;i++) if(col[id(x+i,y+1)]==1){hv++;break;} if(hv==2) return false; } if(col[id(x,y+1)]==2 && col[id(x,y-1)]==2 && find(id(x,y+1))==find(id(x,y-1))) { int hv=0; for(int i=-1;i<=1;i++) if(col[id(x-1,y+i)]==1){hv++;break;} for(int i=-1;i<=1;i++) if(col[id(x+1,y+i)]==1){hv++;break;} if(hv==2) return false; } return true; } bool vis[N]; void dfs(int u) { vis[u]=true; for(int j=0;j<4;j++) { int y=id(p[u]+P[j]); if(col[y]==1){if(check(y)) su.insert(y);else su.erase(y);} if(col[y]==2 && !vis[y]) dfs(y); } } void del(int u) { col[u]=2,vis[u]=true;f[u]=rt; for(int i=0;i<8;i++) { int v=id(p[u]+P[i]); if(col[v]==1) { deg[v]--; if(check(v)) su.insert(v); else su.erase(v); } } for(int i=0;i<4;i++) { int x=id(p[u]+P[i]);if(col[x]!=2) continue; x=find(x); if(x!=rt) f[x]=rt,dfs(x); } } int ans[N],at; int main() { scanf("%d%*d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y); for(int i=1;i<=n;i++) mp[{p[i].x,p[i].y}]=++tt; for(int i=1;i<=n;i++) for(int j=0;j<8;j++) if(!mp.count(p[i]+P[j])) mp[p[i]+P[j]]=++tt,p[tt]=p[i]+P[j]; for(int i=1;i<=tt;i++) col[i]=i<=n?1:2; for(int i=1;i<=tt;i++) f[i]=i; for(int i=1;i<=n;i++) for(int j=0;j<8;j++) { int u=id(p[i]+P[j]); if(col[u]==1) f[find(u)]=find(i); } for(int i=1;i<n;i++) if(find(i)!=find(i+1)){puts("NO");return 0;} for(int i=1;i<=tt;i++) f[i]=i; for(int i=n+1;i<=tt;i++) for(int j=0;j<4;j++) { int u=id(p[i]+P[j]);if(col[u]!=2) continue; f[find(i)]=find(u); } rt=find(mp.begin()->second); for(int i=1;i<=n;i++) for(int j=0;j<8;j++) if(col[id(p[i]+P[j])]==1) deg[i]++; for(int i=n+1;i<=tt;i++) if(find(i)==rt) vis[i]=true; for(int i=1;i<=n;i++) for(int j=0;j<4;j++) { int u=id(p[i]+P[j]); if(col[u]==2 && find(u)==rt && check(i)){su.insert(i);break;} } while(!su.empty()){int u=*su.rbegin();su.erase(u),ans[++at]=u,del(u);} puts("YES"); for(int i=at;i;i--) printf("%d\n",ans[i]); return 0; }
|