Portal
Test Instructions: Chinese topic, not to say.
I don't know how much each subsidiary's office is at the time of the game, but I don't think it's much, so I tried it boldly, and it was a surprise.
Ask two subsidiaries the shortest circuit, because it is a tree, so is the length of the path between two points, we only know where each subsidiary's office is, I am enumerating the distance between every two offices, the output of the smallest pair of distance, because the given tree is definitely irregular, so I asked for a tree's center of gravity, The height of the tree is lowered, and then the nearest common ancestor is used to solve the distance, because the tree height is low, so the time complexity of the public ancestor has also been reduced. (Just at this point I found that there seems to be no optimization, the recent public ancestor time complexity is equal to the unit distance between two points, so I do not seek the center of gravity of the tree, not optimized, handed over, found that the center of gravity optimization is more than 6,000 MS, without optimization is more than 8,000 ms)
That's kinda interesting, Explain that optimizing time will be faster.
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace
Std
typedef long Long LL;
Const double EPS = 1e-8;
Const LL MOD = 1e9+7;
const int INF=1E9+10;
const int maxn=100005;
int room[maxn][100];
int n;
struct node {int to,next,w;} edge[maxn*2];
int Head[maxn],tot;
void Addedge (int u,int v,int W) {edge[tot].to=v;
Edge[tot].w=w;
Edge[tot].next=head[u];
head[u]=tot++;
} int root,num,min_root;
int VIS[MAXN],SON[MAXN],MAX_SON[MAXN];
void Init () {tot=0;
memset (head,-1,sizeof (head));
memset (vis,0,sizeof (VIS));
memset (Room,0,sizeof ());
} void dfs_size (int u,int fa) {int v;
Son[u]=1;
max_son[u]=0;
for (int i=head[u]; i!=-1; i=edge[i].next) {v=edge[i].to; if (vis[v]| |
V==FA) continue;
Dfs_size (V,u);
SON[U]+=SON[V];
if (Max_son[u]<son[v]) max_son[u]=son[v]; }} void Dfs_root (int r,int u,int FA) {iNT V;
Max_son[u]=max (Max_son[u],son[r]-son[u]);
if (max_son[u]<min_root) {min_root=max_son[u];
Root=u;
} for (int i=head[u]; i!=-1; i=edge[i].next) {v=edge[i].to; if (vis[v]| |
V==FA) continue;
Dfs_root (R,v,u);
}} void solve (int u) {min_root=n;
Dfs_size (u,-1);
Dfs_root (u,u,-1);
} int DEP[MAXN],FA_W[MAXN],FA[MAXN];
void get_dep (int u,int d,int pre) {int V;
Dep[u]=d;
for (int i=head[u];i!=-1;i=edge[i].next) {v=edge[i].to;
if (v==pre) continue;
Fa[v]=u;
FA_W[V]=EDGE[I].W;
GET_DEP (V,d+1,u);
}} int Get_dis (int x,int y) {/////Based on depth, depth goes up, resulting in X y depth equal to int ans=0;
while (Dep[x]>dep[y]) {ans+=fa_w[x];
X=FA[X];
} while (Dep[y]>dep[x]) {ans+=fa_w[y];
Y=fa[y];
}///When two points are simultaneously indicated to have found the nearest public ancestor.
while (x!=y) {ans+=fa_w[x];
Ans+=fa_w[y]; x=FA[X];
Y=fa[y];
} return ans;
} int main () {int m,u,v,w;
int T;
int q;
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &n,&m);
Init ();
for (int i=0; i<n-1; i++) {scanf ("%d%d%d", &u,&v,&w);
Addedge (U,V,W);
Addedge (V,U,W);
} memset (Fa_w,0,sizeof (fa_w));
Solve (1);///The center of gravity of the number, and later found not to seek the center of gravity can also be too.
Fa[root]=root; GET_DEP (ROOT,1,-1)//////For each point, by the way, the length of each point and the edge of its parent//point is recorded, and the parent node of each node is recorded for (int i=1;i<=m
; i++) {scanf ("%d", &room[i][0]);
int temp=room[i][0];
for (int j=1;j<=temp;j++) scanf ("%d", &room[i][j]);
Sort (room[i]+1,room[i]+1+temp);
Manual to re-room[i][0]=0; for (int j=1;j<=temp;j++) {if (room[i][j]!=room[i][j-1]) room[i][++room[i][0]]=r OOM[I][J];
}} int s,t;
scanf ("%d", &q);
while (q--) {int ans=inf;
scanf ("%d%d", &s,&t);
for (int i=1;i<=room[s][0];i++) {for (int j=1;j<=room[t][0];j++) {
int Temp=get_dis (ROOM[S][I],ROOM[T][J]),///////two points between the if (Temp<ans)
Ans=temp;
}} printf ("%d\n", ans);
}} return 0;
}/** 1 3 3 1 2 1 2 3 1 2 1 1 2 2 3 * *