#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef vector<LL> VLL; typedef pair<int,int> PI; typedef pair<LL,LL> PLL; typedef unsigned long long ULL; typedef pair<double,double> PD; #define FOR(x, b, e) for(int x = b; x<= (e); x++) #define FORD(x, b, e) for(int x = b; x>= (e); x--) #define REP(x, n) for(int x = 0; x<(n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXS 1000010 double zl[MAXS]; int n; void erxit(vector<VLL> &tab){ REP(i,n) FOR(j,1,2) zl[tab[i][j]]=0; cout<<"NIE\n"; } void go(){ //Wth, co to za zadanie B drugiego dnia cin>>n; vector<VLL>tab(n,VLL(3)); LL s1=0,s2=0; REP(i,n){ REP(j,3) cin>>tab[i][j]; s1+=tab[i][0]*tab[i][1]; s2+=tab[i][0]*tab[i][2]; zl[tab[i][1]]+=tab[i][0]; zl[tab[i][2]]-=tab[i][0]; } if(s1!=s2){ erxit(tab); return; } VI pt; REP(i,n){ FOR(j,1,2){ if(zl[tab[i][j]]) pt.PB(tab[i][j]); } } sort(ALL(pt)); pt.erase(unique(ALL(pt)),pt.end()); stack<int> sp; deque<int> sn; for(auto x:pt){ if(zl[x]>0){ while((!sn.empty())&&zl[x]>0){ if(sp.empty()) erxit(tab); if(sn.front()==sn.back()){ if(zl[sn.front()]/2<=min(zl[x],zl[sp.top()])){ zl[x]-=zl[sn.front()]/2; zl[sp.top()]-=zl[sn.front()]/2; zl[sn.front()]=0; } else{ zl[x]-=min(zl[x],zl[sp.top()]); zl[sp.top()]-=min(zl[x],zl[sp.top()]); zl[sn.front()]-=2*min(zl[x],zl[sp.top()]); } } else{ int mn1=min({abs(zl[sn.front()]),abs(zl[sn.back()]),abs(zl[sp.top()]),abs(zl[x])}); zl[sp.top()]-=mn1; zl[x]-=mn1; if(abs(sp.top()-sn.front())<abs(x-sn.back())){ //sp jest blizej poczatku kolejki zl[sn.front()]+=mn1; int c1=x-abs(sp.top()-sn.front()); zl[c1]+=mn1; x=c1; } else if(abs(sp.top()-sn.front())==abs(x-sn.back())){ zl[sn.back()]+=mn1; zl[sn.front()]+=mn1; } else{ zl[sn.back()]+=mn1; if(zl[sp.top()]==0) sp.pop(); int c1=sp.top()+abs(x-sn.back()); zl[c1]+=mn1; sp.push(c1); } } while(!sn.empty()&&zl[sn.front()]==0) sn.pop_front(); while(!sn.empty()&&zl[sn.back()]==0) sn.pop_back(); while(!sp.empty()&&zl[sp.top()]==0) sp.pop(); } if(zl[x]>0) sp.push(x); } else if(zl[x]<0){ sn.PB(x); if(sp.empty()){ erxit(tab); return; } } } if(sn.empty()){ REP(i,n) FOR(j,1,2) zl[tab[i][j]]=0; cout<<"TAK\n"; return; } else erxit(tab); } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) go(); return 0; } /* 54 212 17728 17479 561 18021 18044 592 17597 17485 118 17352 17672 341 17717 17561 583 17739 17546 53 17680 17985 827 17530 17638 1 1 17128 385 17403 18045 159 18104 17145 798 17532 17800 665 17464 17999 158 17402 17780 827 17251 18110 414 17860 17503 679 17593 17856 740 18050 17314 998 17460 17790 66 17782 17380 800 17566 17813 365 17586 17732 925 17762 17933 880 17146 17545 680 17589 17964 153 17433 17226 557 17775 17637 767 17164 17270 891 17317 17531 457 17899 17315 16 17861 17939 819 17291 17514 629 18054 17627 439 18011 17276 840 17760 17357 936 17498 18068 293 18123 17515 504 17206 17285 473 17371 17353 44 17287 17339 711 17952 17412 541 18030 17388 251 17863 17550 789 17543 18103 40 17673 17590 526 17203 17677 433 17764 17810 451 17782 17994 894 18093 18000 828 17796 17144 983 17802 17610 997 17729 17243 235 17919 17858 859 17804 17397 5 2 2 1 4 2 5 2 2 1 4 3 1 5 4 2 1 5 7 1 7 5 2 1 4 1 1 2 5 3 2 6 4 1 2 3 3 4 5 */
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef vector<LL> VLL; typedef pair<int,int> PI; typedef pair<LL,LL> PLL; typedef unsigned long long ULL; typedef pair<double,double> PD; #define FOR(x, b, e) for(int x = b; x<= (e); x++) #define FORD(x, b, e) for(int x = b; x>= (e); x--) #define REP(x, n) for(int x = 0; x<(n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXS 1000010 double zl[MAXS]; int n; void erxit(vector<VLL> &tab){ REP(i,n) FOR(j,1,2) zl[tab[i][j]]=0; cout<<"NIE\n"; } void go(){ //Wth, co to za zadanie B drugiego dnia cin>>n; vector<VLL>tab(n,VLL(3)); LL s1=0,s2=0; REP(i,n){ REP(j,3) cin>>tab[i][j]; s1+=tab[i][0]*tab[i][1]; s2+=tab[i][0]*tab[i][2]; zl[tab[i][1]]+=tab[i][0]; zl[tab[i][2]]-=tab[i][0]; } if(s1!=s2){ erxit(tab); return; } VI pt; REP(i,n){ FOR(j,1,2){ if(zl[tab[i][j]]) pt.PB(tab[i][j]); } } sort(ALL(pt)); pt.erase(unique(ALL(pt)),pt.end()); stack<int> sp; deque<int> sn; for(auto x:pt){ if(zl[x]>0){ while((!sn.empty())&&zl[x]>0){ if(sp.empty()) erxit(tab); if(sn.front()==sn.back()){ if(zl[sn.front()]/2<=min(zl[x],zl[sp.top()])){ zl[x]-=zl[sn.front()]/2; zl[sp.top()]-=zl[sn.front()]/2; zl[sn.front()]=0; } else{ zl[x]-=min(zl[x],zl[sp.top()]); zl[sp.top()]-=min(zl[x],zl[sp.top()]); zl[sn.front()]-=2*min(zl[x],zl[sp.top()]); } } else{ int mn1=min({abs(zl[sn.front()]),abs(zl[sn.back()]),abs(zl[sp.top()]),abs(zl[x])}); zl[sp.top()]-=mn1; zl[x]-=mn1; if(abs(sp.top()-sn.front())<abs(x-sn.back())){ //sp jest blizej poczatku kolejki zl[sn.front()]+=mn1; int c1=x-abs(sp.top()-sn.front()); zl[c1]+=mn1; x=c1; } else if(abs(sp.top()-sn.front())==abs(x-sn.back())){ zl[sn.back()]+=mn1; zl[sn.front()]+=mn1; } else{ zl[sn.back()]+=mn1; if(zl[sp.top()]==0) sp.pop(); int c1=sp.top()+abs(x-sn.back()); zl[c1]+=mn1; sp.push(c1); } } while(!sn.empty()&&zl[sn.front()]==0) sn.pop_front(); while(!sn.empty()&&zl[sn.back()]==0) sn.pop_back(); while(!sp.empty()&&zl[sp.top()]==0) sp.pop(); } if(zl[x]>0) sp.push(x); } else if(zl[x]<0){ sn.PB(x); if(sp.empty()){ erxit(tab); return; } } } if(sn.empty()){ REP(i,n) FOR(j,1,2) zl[tab[i][j]]=0; cout<<"TAK\n"; return; } else erxit(tab); } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) go(); return 0; } /* 54 212 17728 17479 561 18021 18044 592 17597 17485 118 17352 17672 341 17717 17561 583 17739 17546 53 17680 17985 827 17530 17638 1 1 17128 385 17403 18045 159 18104 17145 798 17532 17800 665 17464 17999 158 17402 17780 827 17251 18110 414 17860 17503 679 17593 17856 740 18050 17314 998 17460 17790 66 17782 17380 800 17566 17813 365 17586 17732 925 17762 17933 880 17146 17545 680 17589 17964 153 17433 17226 557 17775 17637 767 17164 17270 891 17317 17531 457 17899 17315 16 17861 17939 819 17291 17514 629 18054 17627 439 18011 17276 840 17760 17357 936 17498 18068 293 18123 17515 504 17206 17285 473 17371 17353 44 17287 17339 711 17952 17412 541 18030 17388 251 17863 17550 789 17543 18103 40 17673 17590 526 17203 17677 433 17764 17810 451 17782 17994 894 18093 18000 828 17796 17144 983 17802 17610 997 17729 17243 235 17919 17858 859 17804 17397 5 2 2 1 4 2 5 2 2 1 4 3 1 5 4 2 1 5 7 1 7 5 2 1 4 1 1 2 5 3 2 6 4 1 2 3 3 4 5 */ |