1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=1.5e6+10; const int Inf=0x3f3f3f3f; int N,Q; struct G { int next,to; }Edge[MAXN<<1]; int Head[MAXN],Total; inline void addEdge(int u,int v) { Edge[++Total]=(G){Head[u],v};Head[u]=Total; Edge[++Total]=(G){Head[v],u};Head[v]=Total; } int Fa[MAXN],Siz[MAXN],Dep[MAXN],Son[MAXN],Dist[MAXN]; void dfsTree(int x,int last) { Fa[x]=last,Dep[x]=Dep[last]+1,Siz[x]=1; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; Dist[v]=Dist[x]+1; dfsTree(v,x); Siz[x]+=Siz[v]; if(!Son[x]||Siz[v]>Siz[Son[x]]) Son[x]=v; } } int Top[MAXN],Dfn[MAXN],Tot; void dfsChain(int x,int topf) { Top[x]=topf,Dfn[x]=++Tot; if(!Son[x]) return ; dfsChain(Son[x],topf); for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==Fa[x]||v==Son[x]) continue; dfsChain(v,v); } } inline int getLca(int x,int y) { while(Top[x]!=Top[y]) { if(Dep[Top[x]]<Dep[Top[y]]) std::swap(x,y); x=Fa[Top[x]]; } return Dep[x]>Dep[y]?y:x; } inline bool cmp(int a,int b){ return Dfn[a]<Dfn[b]; } std::vector<int>Ki; bool Pot[MAXN]; int Cnt[MAXN],ansx,ansn,Fx[MAXN],Fn[MAXN]; ll anss; void dpTree(int x,int last) { if(Pot[x]) Cnt[x]=1,Fx[x]=Fn[x]=Dist[x]; else Cnt[x]=0,Fx[x]=-Inf,Fn[x]=Inf; int px1=-Inf,px2=-Inf,pn1=Inf,pn2=Inf; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; dpTree(v,x); checkMax(Fx[x],Fx[v]),checkMin(Fn[x],Fn[v]); if(Fx[v]>=px1) px2=px1,px1=Fx[v]; else checkMax(px2,Fx[v]); if(Fn[v]<=pn1) pn2=pn1,pn1=Fn[v]; else checkMin(pn2,Fn[v]); anss+=1ll*Cnt[v]*((ll)Ki.size()-Cnt[v])*(Dist[v]-Dist[x]); Cnt[x]+=Cnt[v]; } checkMin(ansn,pn1+pn2-2*Dist[x]); if(Pot[x]) checkMin(ansn,pn1-Dist[x]); checkMax(ansx,px1+px2-2*Dist[x]); if(Pot[x]) checkMax(ansx,px1-Dist[x]); Head[x]=0; } inline void build(std::vector<int>a) { std::sort(a.begin(),a.end(),cmp); std::vector<int>h; for(int i=1;i<(int)a.size();++i) { h.push_back(a[i-1]); h.push_back(getLca(a[i-1],a[i])); } h.push_back(a[(int)a.size()-1]),h.push_back(1); std::sort(h.begin(),h.end(),cmp); h.erase(std::unique(h.begin(),h.end()),h.end()); for(int x:a) Pot[x]=1; for(int i=1;i<(int)h.size();++i) { int lc=getLca(h[i-1],h[i]); addEdge(lc,h[i]); } } int main() { read(N),Dist[1]=1; for(int i=2,u,v;i<=N;++i) read(u,v),addEdge(u,v); dfsTree(1,0),dfsChain(1,1); std::memset(Head,0,sizeof(Head)),Total=0; read(Q); for(int ki;Q--;) { read(ki),Ki.clear(); for(int i=1,x;i<=ki;++i) read(x),Ki.push_back(x); build(Ki); ansn=Inf,ansx=-Inf,anss=0; dpTree(1,0); write(anss,' ',ansn,' ',ansx,'\n'); for(int x:Ki) Pot[x]=0; } return 0; }
|