#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REP(i, a, b) for(int i = (a); i >= (b); --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
const int mod = 1e9 + 7;
inline bool maximize(int &u, int v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimize(int &u, int v){
if(v < u){
u = v;
return true;
}
return false;
}
inline bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return true;
}
return false;
}
inline int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
inline void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 1e5 + 5;
vector<pair<int, int>> adj[maxN];
int n, q, type[maxN], s[maxN], a[maxN], b[maxN], w[maxN];
const long long infll = 1e18;
const int inf = 2e9;
namespace LCA{
int depth[maxN], p[maxN], lg[maxN << 1], min_depth[18][maxN << 1], tour[maxN << 1], cnt;
void dfs(int u = 1, int pre = 0){
tour[++cnt] = u;
p[u] = cnt;
for(auto[v, w] : adj[u]){
if(v != pre){
depth[v] = depth[u] + 1;
dfs(v, u);
tour[++cnt] = u;
}
}
}
int getMin(int u, int v){
return depth[u] < depth[v] ? u : v;
}
int get(int u, int v){
int l = p[u], r = p[v];
if(l > r)swap(l, r);
int k = lg[r - l + 1];
return getMin(min_depth[k][l], min_depth[k][r - (1 << k) + 1]);
}
void build(){
dfs();
FOR(i, 2, cnt)lg[i] = lg[i >> 1] + 1;
FOR(i, 1, cnt)min_depth[0][i] = tour[i];
FOR(j, 1, lg[cnt])FOR(i, 1, cnt - (1 << j) + 1)min_depth[j][i] = getMin(min_depth[j - 1][i], min_depth[j - 1][i + (1 << (j - 1))]);
}
}
const long long lim = 1e14;
namespace subtask1{
bool check(){
return max(n, q) <= 2000;
}
const int maxN = 2e3 + 1;
int val[maxN], par[maxN];
long long d[maxN];
bool red[maxN];
int in[maxN], out[maxN], id[maxN], timeDfs;
void dfs(int u = 1, int pre = 0){
in[u] = ++timeDfs;
id[timeDfs] = u;
for(auto[v, w] : adj[u]){
if(v != pre){
par[v] = u;
val[v] = w;
d[v] = d[u] + w;
dfs(v, u);
}
}
out[u] = timeDfs;
}
void solve(){
LCA :: build();
dfs();
FOR(i, 1, q){
if(type[i] == 1){
long long answer = infll;
int u = s[i];
FOR(v, 1, n){
int p = LCA :: get(u, v);
if(red[v])minimizell(answer, d[u] + d[v] - 2 * d[LCA :: get(u, v)]);
}
cout << (answer > lim ? -1 : answer) << "\n";
}else if(type[i] == 2){
red[s[i]] ^= 1;
}else{
int u = a[i];
if(u == par[b[i]]) u = b[i];
FOR(idx, in[u], out[u])d[id[idx]] += w[i] - val[u];
val[u] = w[i];
}
}
}
}
void process(){
cin >> n >> q;
FOR(i, 2, n){
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
FOR(i, 1, q){
cin >> type[i];
if(type[i] == 1 || type[i] == 2){
cin >> s[i];
}else{
cin >> a[i] >> b[i] >> w[i];
}
}
if(subtask1 :: check()) return subtask1 :: solve();
}
#define NAME "Grapevine"
int main(){
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
/*
1 5 2 5
1 3 2 5
1 2 2 5
1 1 2 5
2 2 2 5
3 3 2 5
4 5 2 5
*/
/*
7 11
1 2 2
2 3 4
5 6 1
5 3 6
3 7 6
2 4 9
2 6
2 4
2 7
1 1
3 2 3 0
1 1
3 6 5 0
1 1
3 3 5 0
3 2 4 0
1 1
*/
/*
6 11
1 2 3
1 3 4
2 4 1
2 5 4
3 6 6
2 3
1 2
2 4
3 1 3 2
1 1
2 3
3 2 1 2
2 4
1 3
2 2
1 3
*/
/*
7 8
1 2 2
2 3 3
3 6 2
4 6 1
5 6 4
6 7 3
2 3
1 4
2 3
2 5
1 1
3 6 7 4
1 5
1 7
*/
//
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | freopen(NAME".out", "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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |