#pragma once #include #define unlikely(x) __builtin_expect(!!(x), 0) struct char_reader { FILE* f; char *buf,*p1,*p2; int size; char_reader(FILE* fin,int bufsize=1<<16) { f=fin; size=bufsize; p1=p2=0; buf=new char[size]; } ~char_reader() { delete []buf; } inline int operator()() { return p1==p2&&(p2=(p1=buf)+fread(buf,1,size,f),p1==p2)?EOF:*p1++; } }; struct char_writer { FILE* f; char *buf,*p,*end; int size; char_writer(FILE* fout,int bufsize=1<<16) { f=fout; size=bufsize; buf=new char[size]; p=buf; end=buf+bufsize; } ~char_writer() { fwrite(buf,p-buf,1,f); delete []buf; } inline char operator()(char ch) { if(unlikely(end==p)) { fwrite(buf,end-buf,1,f); p=buf; } return *p++=ch; } }; char_reader gch(stdin); char_writer wch(stdout); template inline int read(T& t) { bool f=false; int ch; while(ch=gch(),!((ch>='0'&&ch<='9')||ch=='-')) { if(ch==EOF) return 0; } t=0; if(ch=='-') f=true,ch=gch(); t=ch^48; while(ch=gch(),ch>='0'&&ch<='9') t=(t<<3)+(t<<1)+(ch^48); if(f) t=-t; return 1; } inline int read(char &c) { c=0; int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; } c=ch; return 1; } inline int read(char *s) { int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; } *s++=ch; while(ch=gch(),!(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')&&ch!=EOF) *s++=ch; return 1; } inline int read(const char *s) { return read((char*)s); } inline int readline(char *s) { int ch; while(ch=gch(),(ch==' '||ch=='\n'||ch=='\r'||ch=='\t')) { if(ch==EOF) return 0; } *s++=ch; while(ch=gch(),!(ch=='\n'||ch=='\r')&&ch!=EOF) *s++=ch; return 1; } inline int readline(const char *s) { return readline((char*)s); } template inline void write(T t) { int stk[20],cnt=0; if(t==0) { wch('0'); return; } if(t<0) { wch('-'); t=-t; } while(t>0) { stk[cnt++]=t%10; t/=10; } while(cnt) wch(stk[--cnt]+'0'); } inline void write(char t) { wch(t); } inline void write(char *s) { while(*s) wch(*s++); } inline void write(const char *s) { write((char*)s); } #if __cplusplus >= 201103L template inline int read(T& t,Args&... args) { return read(t)+read(args...); } template inline void write(T t,Args... args) { write(t); write(args...); } #else template inline int read(A_t &a,B_t &b) { return read(a)+read(b); } template inline int read(A_t &a,B_t &b,C_t &c) { return read(a)+read(b)+read(c); } template inline int read(A_t &a,B_t &b,C_t &c,D_t &d) { return read(a)+read(b)+read(c)+read(d); } template inline void write(A_t a,B_t b) { write(a); write(b); } template inline void write(A_t a,B_t b,C_t c) { write(a); write(b); write(c); } template inline void write(A_t a,B_t b,C_t c,D_t d) { write(a); write(b); write(c); write(d); } #endif #include #include #include #include #include using namespace std; typedef long long LL; const int maxn=1e5+10; const int maxm=5e5+10; const int maxw=1e5+10; struct DSUType { int fa[maxn],size[maxn]; void init(int n) { for(int i=1;i<=n;i++) { fa[i]=i; size[i]=1; } } inline int GetRoot(int u) { int rt=u,x; while(fa[rt]!=rt) rt=fa[rt]; while(u!=rt) { x=fa[u]; fa[u]=rt; u=x; } return rt; } inline void Merge(int a,int b) { a=GetRoot(a); b=GetRoot(b); if(size[a]b.w; } struct Edge { int v,w; Edge *nxt; }mem[maxm*2],*G[maxn],*ecnt=mem; inline void AddEdge(int u,int v,int w) { ecnt->v=v; ecnt->w=w; ecnt->nxt=G[u]; G[u]=ecnt++; } int n,m; bool vis[maxn]; vector rawG; int root[maxn],dep[maxn],anc[maxn][25],min_up[maxn][25]; void InitData(int p,int fa) { vis[p]=true; dep[p]=dep[fa]+1; for(int i=1;i<25;i++) anc[p][i]=anc[anc[p][i-1]][i-1]; for(int i=1;i<25;i++) min_up[p][i]=min(min_up[p][i-1],min_up[anc[p][i-1]][i-1]); for(Edge *it=G[p];it;it=it->nxt) if(it->v!=fa) { min_up[it->v][0]=it->w; anc[it->v][0]=p; InitData(it->v,p); } } inline int GetLCA(int x,int y) { if(x==y) return x; if(dep[y]>dep[x]) swap(x,y); for(int i=24;i>=0;i--) if(dep[anc[x][i]]>=dep[y]) x=anc[x][i]; assert(dep[x]==dep[y]); if(x==y) return x; for(int i=24;i>=0;i--) if(anc[x][i]!=anc[y][i]) { x=anc[x][i]; y=anc[y][i]; } return anc[x][0]; } inline int GetRoute(int x,int rt) { int res=0x3f3f3f3f,len=dep[x]-dep[rt]; for(int i=24;i>=0;i--) if(len&(1<