제출 #1290195

#제출 시각아이디문제언어결과실행 시간메모리
1290195ghammazhassanGrapevine (NOI22_grapevine)C++20
6 / 100
127 ms872 KiB
// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n"
#define fi first
#define se second
const int M=1e9+7;
const int inf = 1e14;
const int LOG=18;
const int N=2e3+5;
int n , m , c , w , k , t=1 , q=1 , x , y , z , l , r;
vector<int> a[N];
map<pair<int,int>,int>d;
int on[N];
void solve(){
    cin >> n >> q;
    for (int i=0;i<n-1;i++){
        cin >> x >> y >> w;
        a[x].push_back(y);
        a[y].push_back(x);
        d[{x,y}]=w;
        d[{y,x}]=w;
    }
    for (int p=0;p<q;p++){
        cin >> x;
        if (x==3){
            cin >> x >> y >> w;
            d[{x,y}]=w;
            d[{y,x}]=w;
        }
        else if (x==2){
            cin >> x;
            on[x]^=1;
        }
        else{
            cin >> x;
            queue<int>o;
            vector<int>vi(n+1);
            vector<int>di(n+1);
            vi[x]=1;
            o.push(x);
            int ans=inf;
            while (o.size()){
                int h=o.front();
                o.pop();
                if (on[h]){
                    ans=min(ans,di[h]);
                }
                for (int i:a[h]){
                    if (vi[i])continue;
                    vi[i]=1;
                    o.push(i);
                    di[i]=di[h]+d[{i,h}];
                }
            }
            if (ans==inf){
                cout << -1 << endl;
            }
            else{
                cout << ans << endl;
            }
        }
    }
}
signed main()    
{   

    ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
    cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
    cout << fixed << setprecision(9);
    srand(time(0));
    // int t=1;
    // cin >> t;
    for (int _=1;_<=t;_++){
        solve();
        q++;
    }
} 
















#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...