-
Notifications
You must be signed in to change notification settings - Fork 1
/
BAJ.cpp
executable file
·106 lines (76 loc) · 1.63 KB
/
BAJ.cpp
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#define FOR(i,a,b) for(long long i = a ; i < b ; ++i )
#define PB push_back
using namespace std;
struct edge{ long long p; long long k; long long w; int pos;};
long long fau[7001];
long long ilosc[7001];
int result[ 300001 ];
void makeset( long long n ){
FOR( i, 1 , n+1 ){
fau[ i ] = i;
ilosc[ i ] = 1;
}
}
long long find( long long a ){
return fau[ a ] == a ? a : fau[ a ] = find( fau[ a ] );
}
bool unionn( long long a , long long b){
long long fa = find(a);
long long fb = find(b);
if( fa == fb ) return false;
if(min( ilosc[fa], ilosc[ fb ] ) == ilosc[ fa ]) swap( fa, fb );
ilosc[ fa ] += ilosc[ fb ];
fau[ fb ] = fa;
return true;
}
bool comparator( edge a, edge b){
return a.w < b.w ;
}
vector < edge > ms;
int main(){
long long t;
long long n;
long long m;
long long surf; //surface
ios_base::sync_with_stdio(false);
cin >> n >> m;
FOR( i , 0 , m ){
edge x;
cin >> x.p >> x.k >> x.w ;
x.pos = i;
ms.PB( x );
}
makeset( n );
sort( ms.begin(), ms.end() , comparator );
long long prev = -1;
for( long long i = 0 ; i < m ; ++i ){
if( ms[ i ].w != prev ){
long long j = i ;
while( j < m and ms[ j ].w == ms[ i ].w ){
if( find( ms[ j ].p) != find( ms[ j ].k ) ){
result[ ms[ j ].pos ] = 1;
}
j++;
}
}
if( find( ms[ i ].p ) != find( ms[ i ].k ) ){
unionn( ms[ i ].p, ms[ i ].k );
}
prev = ms[ i ].w;
}
for( int i = 0 ; i < m ; ++i ){
if( result[ i ] == 1 ){
cout << "TAK\n";
}
else
cout << "NIE\n";
}
return 0 ;
}