#include using namespace std; using Int = long long; //BEGIN CUT HERE struct HLDecomposition { int n,pos; vector > G; vector vid, head, heavy, parent, depth, inv, type; HLDecomposition(){} HLDecomposition(int sz): n(sz),pos(0),G(n), vid(n,-1),head(n),heavy(n,-1),parent(n),depth(n),inv(n),type(n){} void add_edge(int u, int v) { G[u].push_back(v); G[v].push_back(u); } void build(vector rs=vector(1,0)) { int c=0; for(int r:rs){ dfs(r, -1); bfs(r, c++); } } typedef tuple T; int dfs(int curr,int prev) { stack st; int result; int sub,max_sub,i,next; ENTRYPOINT: parent[curr] = prev; sub=1; max_sub=0; for(i=0;i<(int)G[curr].size();i++){ next=G[curr][i]; if(next!=prev) { depth[next]=depth[curr]+1; { st.emplace(curr,prev,sub,max_sub,i,next); prev=curr;curr=next; goto ENTRYPOINT; } RETURNPOINT: T t=st.top();st.pop(); curr = get<0>(t); prev = get<1>(t); sub = get<2>(t); max_sub = get<3>(t); i = get<4>(t); next = get<5>(t); int sub_next=result; sub+=sub_next; if(max_sub q({0}); while(!q.empty()){ int h=q.front();q.pop(); for(int i=h;i!=-1;i=heavy[i]) { type[i]=c; vid[i]=k++; inv[vid[i]]=i; head[i]=h; for(int j:G[i]) if(j!=parent[i]&&j!=heavy[i]) q.push(j); } } } // for_each(vertex) // [l,r] <- attention!! inline void for_each(int u, int v, const function& f) { while(1){ if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v]) v=parent[head[v]]; else break; } } // for_each(edge) // [l,r] <- attention!! inline void for_each_edge(Int u,Int v,const function&f){ while(1){ if(vid[u] > vid[v]) swap(u,v); if(head[u] != head[v]){ f(vid[head[v]],vid[v]); v=parent[head[v]]; } else{ if(u!=v) f(vid[u]+1,vid[v]); break; } } } int lca(int u,int v){ if(vid[u]>vid[v]) swap(u,v); if(head[u]==head[v]) return u; return lca(u,parent[head[v]]); } int distance(int u,int v){ return depth[u]+depth[v]-2*depth[lca(u,v)]; } }; //END CUT HERE struct BiconectedGraph{ typedef pair P; int n; vector > G,C,T; vector ord,low,belong; vector

B; BiconectedGraph(){} BiconectedGraph(int sz):n(sz),G(sz),C(sz),T(sz){} void add_edge(int u,int v){ G[u].push_back(v); G[v].push_back(u); } void input(int m,int offset){ ios::sync_with_stdio(0); cin.tie(0); int a,b; for(int i=0;i>a>>b; add_edge(a+offset,b+offset); } } bool is_bridge(int u,int v){ if(ord[u]>ord[v]) swap(u,v); return ord[u]=0){ low[u]=min(low[u],ord[v]); }else{ dfs(v,u,k); low[u]=min(low[u],low[v]); } if(is_bridge(u,v)) B.push_back(P(u,v)); } } void fill_component(int c,int u){ C[c].push_back(u); belong[u]=c; for(int v:G[u]){ if(belong[v]>=0||is_bridge(u,v)) continue; fill_component(c,v); } } void add_component(int u,int &k){ if(belong[u]>=0) return; fill_component(k++,u); } int build(){ int k=0; ord.resize(n); low.resize(n); belong.resize(n); fill(ord.begin(),ord.end(),-1); fill(belong.begin(),belong.end(),-1); for(int u=0;u=0) continue; dfs(u,-1,k); } k=0; for(int i=0;i<(int)B.size();i++){ add_component(B[i].first,k); add_component(B[i].second,k); } for(int u=0;u > dat; RMQ(){} RMQ(int n_){init(n_);} void init(int n_){ n=1; while(n0){ k=(k-1)/2; assert(!dat[k].count(a)); dat[k].insert(a); } } void remove(int k,int a){ //cout<0){ k=(k-1)/2; assert(dat[k].count(a)); dat[k].erase(a); } } int query(int a,int b,int k,int l,int r){ if(r<=a||b<=l) return -1; if(a<=l&&r<=b){ if(dat[k].empty()) return -1; return *--dat[k].end(); } int vl=query(a,b,k*2+1,l,(l+r)/2); int vr=query(a,b,k*2+2,(l+r)/2,r); return max(vl,vr); } int query(int a,int b){ //cout<>n>>e>>q; BiconectedGraph big(n); big.input(e,-1); int E=0,V=big.build(); HLDecomposition hl(V+1000); for(int i=0;i m; int num=0; set as; for(int i=0;i>d; if(d==1){ int u,w; cin>>u>>w; u--; u=big.belong[u]; u=hl.vid[u]; //cout<>s>>t; s--;t--; s=big.belong[s]; t=big.belong[t]; int ans=-1; //cout<