# hihocoder 1381 Little Y's Tree

http://www.elijahqi.win/archives/3110
Time Limit:24000ms
Case Time Limit:4000ms
Memory Limit:512MB
Description
Little Y has a tree of n nodes with each edge having a positive weight.

Little J has q queries, each time he will delete k edges in the tree.

Then the tree is splitted into k+1 connected componets.

Little J wants to know the sum of the distance of farthest pair of points in each connected components.

(0 when one component only has one node.)

Each query is independent with each other. This means each query is based on the original tree.

Input
First line with an integer n, indicating the number of nodes.

Then following n-1 lines, the i-th line with three integers ui, vi, wi, indicating the i-th edge weighted wi connects ui and vi.

Then following one line with an integer q, indicating the number of queries.

For each query, the first line with an integer k, then following one line with k integers, indicating the id of the deleted edges.

1<=n,q,Σk<=105,1<=w<=109

Output
For each query, output one line with an integer indicating the sum.

Sample Input
5
1 2 2
2 3 3
2 4 4
4 5 2
3
4 1 2 3 4
1 4
2 2 3
Sample Output
0
7
4

#include<cstdio>
#include<cctype>
#include<algorithm>
#define lc (x<<1)
#define rc (x<<1|1)
#define ll long long
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
return *S++;
}
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
const int N=1e5+20;
struct node{
int y,next,z;
}data[N<<1];
struct node1{
int x,y;ll dis;
node1(){dis=-1;}
node1(int _x,int _y,ll _dis){x=_x;y=_y;dis=_dis;}
}tree[N<<2];
int mn[N<<1][20],Log[N<<1],n,num=1,cnt,dep[N],pos[N],h[N],in[N],out[N],b[N<<1],k,a[N],dfn[N],now;
ll dis[N],ans;
inline void dfs(int x,int fa){
mn[++num][0]=x;pos[x]=num;in[x]=++cnt;dfn[cnt]=x;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;if (y==fa) continue;dep[y]=dep[x]+1;
dis[y]=dis[x]+data[i].z;b[i>>1]=y;dfs(y,x);mn[++num][0]=x;
}out[x]=cnt;
}
inline void init(){
for (int j=1;j<=Log[num];++j)
for (int i=1;i+(1<<j)-1<=num;++i)
mn[i][j]=dep[mn[i][j-1]]<dep[mn[i+(1<<j-1)][j-1]]?mn[i][j-1]:mn[i+(1<<j-1)][j-1];
}
inline int lca(int x,int y){
x=pos[x];y=pos[y];if (x>y) swap(x,y);int t=Log[y-x+1];
return dep[mn[x][t]]<dep[mn[y-(1<<t)+1][t]]?mn[x][t]:mn[y-(1<<t)+1][t];
}
inline ll calc(int x,int y){
return dis[x]+dis[y]-(dis[lca(x,y)]<<1);
}
inline node1 merge(const node1 &ls,const node1 &rs){
if (ls.dis==-1) return rs;if (rs.dis==-1) return ls;
static int nx,ny,lx,ly,rx,ry;static ll tmp1,tmp2,tmp3,tmp4,mx;mx=0;
lx=ls.x;ly=ls.y;rx=rs.x,ry=rs.y;
if (ls.dis>mx) nx=lx,ny=ly,mx=ls.dis;
if (rs.dis>mx) nx=rx,ny=ry,mx=rs.dis;
tmp1=calc(lx,rx);tmp2=calc(lx,ry);tmp3=calc(ly,rx);tmp4=calc(ly,ry);
if (tmp1>mx) nx=lx,ny=rx,mx=tmp1;if (tmp2>mx) nx=lx,ny=ry,mx=tmp2;
if (tmp3>mx) nx=ly,ny=rx,mx=tmp3;if (tmp4>mx) nx=ly,ny=ry,mx=tmp4;
return (node1){nx,ny,mx};
}
inline void build(int x,int l,int r){
if (l==r) {tree[x].x=tree[x].y=dfn[l];tree[x].dis=0;return;}int mid=l+r>>1;
build(lc,l,mid);build(rc,mid+1,r);tree[x]=merge(tree[lc],tree[rc]);
}
inline node1 query(int x,int l,int r,int l1,int r1){
if(l1<=l&&r1>=r) return tree[x];int mid=l+r>>1;node1 tmp;
if (l1<=mid) tmp=merge(tmp,query(lc,l,mid,l1,r1));
if (r1>mid) tmp=merge(tmp,query(rc,mid+1,r,l1,r1));return tmp;
}
inline bool cmp(const int &a,const int &b){return in[a]<in[b];}
inline void solve(int l,int r){node1 tmp;int L=l;
while(now<=k&&in[a[now]]>=l&&out[a[now]]<=r){int t;
t=now++;solve(in[a[t]],out[a[t]]);
if(L<=in[a[t]]-1) tmp=merge(tmp,query(1,1,n,L,in[a[t]]-1));
L=out[a[t]]+1;
}if (L<=r) tmp=merge(tmp,query(1,1,n,L,r));ans+=tmp.dis;
}
inline void print(int x,int l,int r){
if (l==r) {printf("%d %d %d %d %d\n",l,r,tree[x].x,tree[x].y,tree[x].dis);return;}
int mid=l+r>>1;print(lc,l,mid);
printf("%d %d %d %d %lld\n",l,r,tree[x].x,tree[x].y,tree[x].dis);
print(rc,mid+1,r);
}
int main(){
//  freopen("hiho1381.in","r",stdin);
//  freopen("12.out","w",stdout);
for (int i=1;i<=n<<1;++i) Log[i]=Log[i>>1]+1;
for (int i=1;i<n;++i){static int x,y,z;
}