#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC optimize("Ofast,unroll-loops,inline-functions,no-stack-protector")
#pragma GCC target("avx2,fma,tune=native")
#pragma clang loop vectorize(enable)
#pragma clang loop interleave(enable)
#endif
#define int long long
#define sz(x) (int)x.size()
#define F first
#define S second
#define pb push_back
#define nl '\n'
#define o_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;
void file( string s = "" )
{
if( s.empty() )
return;
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int N = 2e5 + 2;
const int N1 = 1e6 + 2;
const int inf = 1e9 + 2;
const int INF = 1e18 + 2;
const int mod = 1e9 + 7;
int T = 1, n, m, q, p[N], sz[N], up[N][20], mx[N][20], tin[N], tout[N], timer, us[N];
vector< pii > g[N];
vector< array<int, 3> > qr[N];
string ans[N];
int root( int v )
{
return v == p[v] ? v : p[v] = root(p[v]);
}
int merge( int u, int v )
{
u = root(u);
v = root(v);
if( u == v )
return 0;
if( sz[u] < sz[v] )
swap(u, v);
p[v] = u;
sz[u] += sz[v];
return 1;
}
void calc( int v, int pr, int lw )
{
us[v] = 1;
tin[v] = ++timer;
up[v][0] = pr;
mx[v][0] = lw;
for( int k = 1; k < 20; ++k )
{
up[v][k] = up[up[v][k - 1]][k - 1];
mx[v][k] = max(mx[v][k - 1], mx[up[v][k - 1]][k - 1]);
}
for( auto [to, w]: g[v] )
if( to != pr )
calc(to, v, w);
tout[v] = timer;
}
int ch( int u, int v )
{
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca( int u, int v )
{
if( ch(u, v) )
return u;
if( ch(v, u) )
return v;
for( int i = 19; i >= 0; --i )
if( !ch(up[u][i], v) )
u = up[u][i];
return up[u][0];
}
int fmx( int u, int v )
{
if( u == v )
return 0;
int res = 0;
for( int i = 19; i >= 0; --i )
if( !ch(up[v][i], u) )
{
res = max(res, mx[v][i]);
v = up[v][i];
}
return max(res, mx[v][0]);
}
void dfs( int v, int pr )
{
for( auto [u, w, id]: qr[v] )
if( sz(ans[id]) == 0 )
{
if( root(u) != root(v) )
ans[id] = "NE";
else
{
int l = lca(u, v);
ans[id] = (max(fmx(l, u), fmx(l, v)) <= w ? "TAIP" : "NE");
}
}
for( auto [to, w]: g[v] )
if( to != pr )
dfs(to, v);
}
void solve()
{
cin >>n >>m >>q;
for( int i = 1; i <= n; ++i )
p[i] = i, sz[i] = 1;
vector< array<int, 3> > vec;
for( int i = 1, u, v, w; i <= m; ++i )
{
cin >>u >>v >>w;
vec.pb({w, u, v});
}
sort(vec.begin(), vec.end());
for( auto [w, u, v]: vec )
if( merge(u, v) )
{
g[u].pb({v, w});
g[v].pb({u, w});
}
for( int i = 1, u, v, w; i <= q; ++i )
{
ans[i] = "";
cin >>u >>v >>w;
qr[u].pb({v, w, i});
qr[v].pb({u, w, i});
}
for( int i = 1; i <= n; ++i )
if( !us[i] )
{
calc(i, i, 0);
dfs(i, i);
}
for( int i = 1; i <= q; ++i )
cout <<ans[i] <<nl;
}
signed main()
{
file("");
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// cin >>T;
while( T-- )
solve();
}
Compilation message (stderr)
Main.cpp: In function 'void file(std::string)':
Main.cpp:29:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:30:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |