#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
constexpr int INF = (int)2e9;
constexpr int N = (int)7e4 + 111;
constexpr int md = (int)998244353;
constexpr int mdPower = (int)1e9+7 - 1;
constexpr double eps = 1e-7;
typedef __int128 _uint;
typedef long long ll;
mt19937_64 rnd(time(nullptr));
void add(int& a,int b){
a += b; if(a >= md) a -= md;
}
void sub(int& a,int b){
a -= b; while(a < 0) a += md;
}
void add(__int128& a,int b){
a += b;
}
void sub(__int128& a,int b){
a -= b;
}
int bpow(int a,int b){
if(b == 0)
return 1;
if(b % 2 == 0){
int t = bpow(a,b>>1);
return 1ll*t*t%md;
}
return 1ll * a*bpow(a,b-1) % md;
}
int inv(int a){
return bpow(a,md-2);
}
//int fac[N],invfac[N];
//
//void init(){
// fac[0] = 1;
// for(int i = 1; i < N; i++){
// fac[i] = (fac[i-1] * i) % md;
// }
// invfac[N-1] = inv(fac[N-1]);
// for(int i = N-2; i >= 0; i--){
// invfac[i] = (invfac[i+1] * (i+1))%md;
// }
// return;
//}
//
//int C(int n,int k){
// if(k > n)
// return 0;
// return fac[n] * invfac[k] % md * invfac[n-k] % md;
//}
//
//int A(int n,int k){
// if(k > n)
// return 0;
// return fac[n] * invfac[n-k] % md;
//
vector<int> g[N];
int p[N];
int up[20][N];
int tin[N],tout[N];
int timer = 0;
int d[N];
void dfs(int v,int pr){
p[v] = pr;
tin[v] = timer++;
up[0][v] = pr;
for(int i = 1; i < 20; i++){
up[i][v] = up[i-1][up[i-1][v]];
}
for(auto& to : g[v]){
if(to == pr)
continue;
d[to] = d[v] + 1;
dfs(to,v);
}
tout[v] = timer++;
return;
}
bool upper(int a,int b){
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a,int b){
if(upper(a,b)) return a;
if(upper(b,a)) return b;
for(int i = 19; i >= 0; i--){
if(!upper(up[i][a],b))
a = up[i][a];
}
return up[0][a];
}
int L[N],R[N];
void solve(){
int n;
cin >> n;
for(int i = 1; i < n; i++){
int a,b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i = 1; i <= n; i++){
L[i] = -1;
R[i] = INF;
}
dfs(1,1);
int q;
cin >> q;
for(int i = 0; i < q; i++){
char ch;
cin >> ch;
int a,b;
cin >> a >> b;
int z;
cin >> z;
int LCA = lca(a,b);
while(a != LCA){
if(ch == 'M'){
R[a] = min(R[a],z);
} else {
L[a] = min(L[a],z);
}
a = p[a];
}
while(b != LCA){
if(ch == 'M'){
R[b] = min(R[b],z);
} else {
L[b] = min(L[b],z);
}
b = p[b];
}
}
vector<pair<int,int>> ans;
for(int i = 2; i <= n; i++){
int x;
if(L[i] == -1 && R[i] == INF)
x = 0;
else if(L[i] == -1)
x = R[i];
else if(R[i] == INF)
x = L[i];
else
x = (d[i] % 2 == 0 ? L[i] : R[i]);
cout << i << " " << p[i] << " " << x << "\n";
}
return;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1; test <= tests; test++){
// cerr << "test = " << test << "\n";
solve();
}
return 0;
}
/**
x2 x1 x0
[x2+2*x1+x0] [x1+x0] [x0]
1 0 0
2 1 0
1 1 1
1 0 0
-2 1 0
1 -1 1
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
25024 KB |
Output is correct |
2 |
Correct |
155 ms |
22476 KB |
Output is correct |
3 |
Execution timed out |
1090 ms |
21348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
93 ms |
21120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |