#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct hashtable{ int mo,wildcard,n,len; int w[2100],ne[2100]; int fir[110000]; void init(){ memset(fir,0x00,sizeof fir); sort(w+1,w+len+1); len=unique(w+1,w+len+1)-w-1; int premax=len+1,premo=0; for (int t=1;t<=100;t++){ int hit=0; mo=50000+rand()%50000; for (int i=1;i<=len;i++){ int k=w[i]%mo; if (fir[k]) hit++; fir[k]=1; } if (hit<premax){ premax=hit; premo=mo; } for (int i=1;i<=len;i++) fir[w[i]%mo]=0; } //cerr<<len<<" "<<premax<<endl; mo=premo; for (int i=1;i<=len;i++){ int k=w[i]%mo; ne[i]=fir[k]; fir[k]=i; } } int find(long long k1){ int k=k1%mo; for (int i=fir[k];i;i=ne[i]) if (w[i]==k1) return i; return 0; } }X,Y; struct point{ int x,y,where; long long L; void scan(){ scanf("%d%d",&x,&y); } }x[2100]; int n,ans[2100]; int compare(point k1,point k2){ return k1.x<k2.x||(k1.x==k2.x&&k1.y<k2.y); } int overlap(long long l1,long long r1,long long l2,long long r2){ if (r1<=l2||l1>=r2) return 0; return 1; } int overlap(point k1,point k2){ return overlap(k1.x,1ll*k1.x+k1.L,k2.x,k2.x+k2.L)&&overlap(k1.y,k1.y+k1.L,k2.y,k2.y+k2.L); } int check(){ for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) if (overlap(x[i],x[j])){ return 0; } long long L=1e9,R=0,U=0,D=1e9,tot=0; for (int i=1;i<=n;i++){ tot+=1ll*x[i].L*x[i].L; L=min(L,1ll*x[i].x); R=max(R,1ll*x[i].x+x[i].L); D=min(D,1ll*x[i].y); U=max(U,1ll*x[i].y+x[i].L); } if ((R-L)*(U-D)!=tot) return 0; return 1; } void print(){ if (check()==0){ printf("NIE\n"); return; } printf("TAK"); for (int i=1;i<=n;i++) ans[x[i].where]=x[i].L; for (int i=1;i<=n;i++) printf(" %d",ans[i]); printf("\n"); } int A[2100],s[2100],head,N,M; int loc[2100][2100],downloc[2100][2100]; struct atom{ int l,r; }; vector<int>id[2100]; int compare2(int k1,int k2){ return x[k1].x<x[k2].x; } int getnext(int y,int l,int r){ for (int i=l;i<=r;i++) if (x[id[y][i]].L==0) return 0; for (int i=l+1;i<=r;i++){ int k1=id[y][i-1],k2=id[y][i]; if (1ll*x[k1].x+x[k1].L>x[k2].x) return 0; } long long Lx=x[id[y][l]].x,Rx=1ll*x[id[y][r]].x+x[id[y][r]].L; //cout<<"getnext "<<y<<" "<<l<<" "<<r<<" "<<Lx<<" "<<Rx<<endl; long long nowx=Lx; while (1){ if (nowx>Rx) return 0; if (nowx==Rx) return 1; int now=X.find(nowx); if (now==0||downloc[now][y]==0) return 0; int where=downloc[now][y]; x[where].L=x[id[y][l]].y-x[where].y; //cout<<"put "<<where<<" "<<x[where].L<<endl; nowx+=x[where].L; } } int check(int Ulim,int where){ //cout<<"find "<<Ulim<<" "<<where<<endl; for (int i=1;i<=n;i++) x[i].L=0; for (int i=1;i<=n;i++) if (i!=where&&x[i].y>=Ulim) return 0; long long Rlim=x[1].x; while (1){ int now=X.find(Rlim); if (now==0||downloc[now][M+1]==0) break; int where=downloc[now][M+1]; x[where].L=Ulim-x[where].y; //cout<<"put "<<where<<" "<<x[where].L<<endl; Rlim+=x[where].L; } //cout<<Rlim<<endl; //for (int i=1;i<=n;i++) cout<<x[i].L<<" "; cout<<endl; for (int i=1;i<=n;i++) if (x[i].x>=Rlim) return 0; for (int nowy=M;nowy>1;nowy--){ if (id[nowy].size()==0) continue; int pre=0; for (int i=1;i<id[nowy].size();i++){ int k1=id[nowy][i-1],k2=id[nowy][i]; if (x[k2].x>1ll*x[k1].x+x[k1].L){ if (getnext(nowy,pre,i-1)==0) return 0; pre=i; } } if (getnext(nowy,pre,id[nowy].size()-1)==0) return 0; } if (where){ x[where].L=Rlim-x[where].x; } for (int i=1;i<=n;i++) if (x[i].L==0) return 0; return 1; } void solve(){ scanf("%d",&n); X.wildcard=Y.wildcard=-1; for (int i=0;i<=n+1;i++){ id[i].clear(); for (int j=0;j<=n+1;j++) loc[i][j]=0; } for (int i=1;i<=n;i++) x[i].scan(),x[i].where=i; if (n==1){ x[1].L=1; print(); return; } for (int i=1;i<=n;i++) X.w[i]=x[i].x,Y.w[i]=x[i].y; X.len=n; Y.len=n; X.init(); Y.init(); sort(x+1,x+n+1,compare); N=X.len; M=Y.len; for (int i=1;i<=n;i++){ loc[X.find(x[i].x)][Y.find(x[i].y)]=i; id[Y.find(x[i].y)].push_back(i); } for (int i=1;i<=M;i++) sort(id[i].begin(),id[i].end(),compare2); for (int i=1;i<=N;i++) for (int j=1;j<=M+1;j++) if (loc[i][j-1]) downloc[i][j]=loc[i][j-1]; else downloc[i][j]=downloc[i][j-1]; int where=1; while (where<n&&x[where].x==x[where+1].x) where++; //cout<<"asd"<<endl; //for (int i=1;i<=n;i++) cout<<x[i].x<<" "<<x[i].y<<endl; for (int i=where+1;i<=n;i++) if (check(x[where].y+x[i].x-x[where].x,0)){ print(); return; } Y.len--; M--; if (check(x[where].y,where)){ print(); return; } printf("NIE\n"); } int main(){ int t; scanf("%d",&t); for (;t;t--) solve(); return 0; } /* 5 16 9 9 5 9 8 5 11 12 7 1 5 1 3 1 1 1 1 2 1 9 8 1 2 1 9 12 4 1 6 1 10 12 */
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct hashtable{ int mo,wildcard,n,len; int w[2100],ne[2100]; int fir[110000]; void init(){ memset(fir,0x00,sizeof fir); sort(w+1,w+len+1); len=unique(w+1,w+len+1)-w-1; int premax=len+1,premo=0; for (int t=1;t<=100;t++){ int hit=0; mo=50000+rand()%50000; for (int i=1;i<=len;i++){ int k=w[i]%mo; if (fir[k]) hit++; fir[k]=1; } if (hit<premax){ premax=hit; premo=mo; } for (int i=1;i<=len;i++) fir[w[i]%mo]=0; } //cerr<<len<<" "<<premax<<endl; mo=premo; for (int i=1;i<=len;i++){ int k=w[i]%mo; ne[i]=fir[k]; fir[k]=i; } } int find(long long k1){ int k=k1%mo; for (int i=fir[k];i;i=ne[i]) if (w[i]==k1) return i; return 0; } }X,Y; struct point{ int x,y,where; long long L; void scan(){ scanf("%d%d",&x,&y); } }x[2100]; int n,ans[2100]; int compare(point k1,point k2){ return k1.x<k2.x||(k1.x==k2.x&&k1.y<k2.y); } int overlap(long long l1,long long r1,long long l2,long long r2){ if (r1<=l2||l1>=r2) return 0; return 1; } int overlap(point k1,point k2){ return overlap(k1.x,1ll*k1.x+k1.L,k2.x,k2.x+k2.L)&&overlap(k1.y,k1.y+k1.L,k2.y,k2.y+k2.L); } int check(){ for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) if (overlap(x[i],x[j])){ return 0; } long long L=1e9,R=0,U=0,D=1e9,tot=0; for (int i=1;i<=n;i++){ tot+=1ll*x[i].L*x[i].L; L=min(L,1ll*x[i].x); R=max(R,1ll*x[i].x+x[i].L); D=min(D,1ll*x[i].y); U=max(U,1ll*x[i].y+x[i].L); } if ((R-L)*(U-D)!=tot) return 0; return 1; } void print(){ if (check()==0){ printf("NIE\n"); return; } printf("TAK"); for (int i=1;i<=n;i++) ans[x[i].where]=x[i].L; for (int i=1;i<=n;i++) printf(" %d",ans[i]); printf("\n"); } int A[2100],s[2100],head,N,M; int loc[2100][2100],downloc[2100][2100]; struct atom{ int l,r; }; vector<int>id[2100]; int compare2(int k1,int k2){ return x[k1].x<x[k2].x; } int getnext(int y,int l,int r){ for (int i=l;i<=r;i++) if (x[id[y][i]].L==0) return 0; for (int i=l+1;i<=r;i++){ int k1=id[y][i-1],k2=id[y][i]; if (1ll*x[k1].x+x[k1].L>x[k2].x) return 0; } long long Lx=x[id[y][l]].x,Rx=1ll*x[id[y][r]].x+x[id[y][r]].L; //cout<<"getnext "<<y<<" "<<l<<" "<<r<<" "<<Lx<<" "<<Rx<<endl; long long nowx=Lx; while (1){ if (nowx>Rx) return 0; if (nowx==Rx) return 1; int now=X.find(nowx); if (now==0||downloc[now][y]==0) return 0; int where=downloc[now][y]; x[where].L=x[id[y][l]].y-x[where].y; //cout<<"put "<<where<<" "<<x[where].L<<endl; nowx+=x[where].L; } } int check(int Ulim,int where){ //cout<<"find "<<Ulim<<" "<<where<<endl; for (int i=1;i<=n;i++) x[i].L=0; for (int i=1;i<=n;i++) if (i!=where&&x[i].y>=Ulim) return 0; long long Rlim=x[1].x; while (1){ int now=X.find(Rlim); if (now==0||downloc[now][M+1]==0) break; int where=downloc[now][M+1]; x[where].L=Ulim-x[where].y; //cout<<"put "<<where<<" "<<x[where].L<<endl; Rlim+=x[where].L; } //cout<<Rlim<<endl; //for (int i=1;i<=n;i++) cout<<x[i].L<<" "; cout<<endl; for (int i=1;i<=n;i++) if (x[i].x>=Rlim) return 0; for (int nowy=M;nowy>1;nowy--){ if (id[nowy].size()==0) continue; int pre=0; for (int i=1;i<id[nowy].size();i++){ int k1=id[nowy][i-1],k2=id[nowy][i]; if (x[k2].x>1ll*x[k1].x+x[k1].L){ if (getnext(nowy,pre,i-1)==0) return 0; pre=i; } } if (getnext(nowy,pre,id[nowy].size()-1)==0) return 0; } if (where){ x[where].L=Rlim-x[where].x; } for (int i=1;i<=n;i++) if (x[i].L==0) return 0; return 1; } void solve(){ scanf("%d",&n); X.wildcard=Y.wildcard=-1; for (int i=0;i<=n+1;i++){ id[i].clear(); for (int j=0;j<=n+1;j++) loc[i][j]=0; } for (int i=1;i<=n;i++) x[i].scan(),x[i].where=i; if (n==1){ x[1].L=1; print(); return; } for (int i=1;i<=n;i++) X.w[i]=x[i].x,Y.w[i]=x[i].y; X.len=n; Y.len=n; X.init(); Y.init(); sort(x+1,x+n+1,compare); N=X.len; M=Y.len; for (int i=1;i<=n;i++){ loc[X.find(x[i].x)][Y.find(x[i].y)]=i; id[Y.find(x[i].y)].push_back(i); } for (int i=1;i<=M;i++) sort(id[i].begin(),id[i].end(),compare2); for (int i=1;i<=N;i++) for (int j=1;j<=M+1;j++) if (loc[i][j-1]) downloc[i][j]=loc[i][j-1]; else downloc[i][j]=downloc[i][j-1]; int where=1; while (where<n&&x[where].x==x[where+1].x) where++; //cout<<"asd"<<endl; //for (int i=1;i<=n;i++) cout<<x[i].x<<" "<<x[i].y<<endl; for (int i=where+1;i<=n;i++) if (check(x[where].y+x[i].x-x[where].x,0)){ print(); return; } Y.len--; M--; if (check(x[where].y,where)){ print(); return; } printf("NIE\n"); } int main(){ int t; scanf("%d",&t); for (;t;t--) solve(); return 0; } /* 5 16 9 9 5 9 8 5 11 12 7 1 5 1 3 1 1 1 1 2 1 9 8 1 2 1 9 12 4 1 6 1 10 12 */ |