#include<bits/stdc++.h> using namespace std; #define mp make_pair #define st first #define nd second #define pb push_back typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<long long, long long> pll; #define Q 1000000007 #define inf 1000000003 #define maxn 1000003 const ld eps=0.00000001; int T; int n, c; ld A[maxn], B[maxn]; ll asum, bsum; vector<int> v; bool ok, noc; void ini(){ scanf("%d", &n); ok=1; noc=0; asum=0; bsum=0; v.clear(); for(int i=1; i<=n; i++){ int l, a, b; scanf("%d%d%d", &l, &a, &b); A[a]+=l; B[b]+=l; v.pb(a); v.pb(b); asum+=(ll)a*(ll)l; bsum+=(ll)b*(ll)l; } if(asum!=bsum) ok=0; sort(v.begin(), v.end()); auto it=unique(v.begin(), v.end()); v.resize(distance(v.begin(), it)); for(int i=0; i<v.size(); i++){ int w=v[i]; if(A[w]>=B[w]){ A[w]-=B[w]; B[w]=0; } else{ B[w]-=A[w]; A[w]=0; } } } bool equals(ld a, ld b){ if(abs(a-b)<eps) return 1; return 0; } void find_c(int x){ for(int i=x; i<v.size(); i++){ if(!equals(A[v[i]], 0)){ c=i; break; } } if(c<x) noc=1; } void solve(){ c=-1; vector<int> pre; for(int i=0; i<v.size(); i++){ int w=v[i]; //cout<<fixed<<A[w]<<" "<<B[w]<<"\n"; if(!equals(B[w], 0)){ if(c<=i) find_c(i+1); while(!equals(B[w], 0)){ if(pre.size()==0 || noc){ ok=0; break; } int a=pre.back(), b=v[c]; ld xa=((ld)w-b)/((ld)a-b); ld xb=1-xa; ld y=min((ld)B[w], min(A[a]/xa, A[b]/xb)); B[w]-=y; A[a]-=xa*y; A[b]-=xb*y; //printf("aaaa%d %d %Lf\n", a, b, y); if(equals(A[a], 0)) pre.pop_back(); if(equals(A[b], 0)) find_c(c+1); } } else if(!equals(A[w], 0)){ pre.pb(w); } } } int main(){ scanf("%d", &T); for(int test=1; test<=T; test++){ ini(); if(ok) solve(); for(int i=0; i<v.size(); i++){ if(!(equals(A[v[i]], 0) && equals(B[v[i]], 0))) ok=0; A[v[i]]=0; B[v[i]]=0; } if(ok) printf("TAK\n"); else printf("NIE\n"); } }
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 | #include<bits/stdc++.h> using namespace std; #define mp make_pair #define st first #define nd second #define pb push_back typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<long long, long long> pll; #define Q 1000000007 #define inf 1000000003 #define maxn 1000003 const ld eps=0.00000001; int T; int n, c; ld A[maxn], B[maxn]; ll asum, bsum; vector<int> v; bool ok, noc; void ini(){ scanf("%d", &n); ok=1; noc=0; asum=0; bsum=0; v.clear(); for(int i=1; i<=n; i++){ int l, a, b; scanf("%d%d%d", &l, &a, &b); A[a]+=l; B[b]+=l; v.pb(a); v.pb(b); asum+=(ll)a*(ll)l; bsum+=(ll)b*(ll)l; } if(asum!=bsum) ok=0; sort(v.begin(), v.end()); auto it=unique(v.begin(), v.end()); v.resize(distance(v.begin(), it)); for(int i=0; i<v.size(); i++){ int w=v[i]; if(A[w]>=B[w]){ A[w]-=B[w]; B[w]=0; } else{ B[w]-=A[w]; A[w]=0; } } } bool equals(ld a, ld b){ if(abs(a-b)<eps) return 1; return 0; } void find_c(int x){ for(int i=x; i<v.size(); i++){ if(!equals(A[v[i]], 0)){ c=i; break; } } if(c<x) noc=1; } void solve(){ c=-1; vector<int> pre; for(int i=0; i<v.size(); i++){ int w=v[i]; //cout<<fixed<<A[w]<<" "<<B[w]<<"\n"; if(!equals(B[w], 0)){ if(c<=i) find_c(i+1); while(!equals(B[w], 0)){ if(pre.size()==0 || noc){ ok=0; break; } int a=pre.back(), b=v[c]; ld xa=((ld)w-b)/((ld)a-b); ld xb=1-xa; ld y=min((ld)B[w], min(A[a]/xa, A[b]/xb)); B[w]-=y; A[a]-=xa*y; A[b]-=xb*y; //printf("aaaa%d %d %Lf\n", a, b, y); if(equals(A[a], 0)) pre.pop_back(); if(equals(A[b], 0)) find_c(c+1); } } else if(!equals(A[w], 0)){ pre.pb(w); } } } int main(){ scanf("%d", &T); for(int test=1; test<=T; test++){ ini(); if(ok) solve(); for(int i=0; i<v.size(); i++){ if(!(equals(A[v[i]], 0) && equals(B[v[i]], 0))) ok=0; A[v[i]]=0; B[v[i]]=0; } if(ok) printf("TAK\n"); else printf("NIE\n"); } } |