Cod sursa(job #2679207)

Utilizator Catalina07Encea Catalina Catalina07 Data 29 noiembrie 2020 22:27:20
Problema Lowest Common Ancestor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.43 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define MAXEDGE 100001
#define MAXN 40001
#define MAXM 410 
struct EDGE{int to,v,next;};
struct QUESTION{int to,num,next;};
EDGE a[MAXEDGE];
QUESTION b[MAXM];
int DATA,N,M,edge[MAXN],question[MAXN],tot1=0,tot2=0,ansf[MAXM],anst[MAXM];
int fa[MAXN],LCA[MAXM],dist[MAXN];//dist[i] records the distance from point i to the root node 
bool vis[MAXN];
void add_edge(int x,int y,int value)
{
             a[++tot1].to=y;//the arrival point of this side 
             a[tot1].v=value;//the length of this side 
             a[tot1].next=edge[x];//The last edge number with the same initial point as this edge 
             Edge[x]=tot1;//update number 
}
void add_question(int x,int y,int number)
{
             b[++tot2].to=y;//The point label of this query 
             b[tot2].num=number;//the corresponding number of this query 
      b[tot2].next=question[x];
      question[x]=tot2;
}
int get(int x)
{
      if(fa[x]==x)  return x;
      return fa[x]=get(fa[x]);
}
void Tarjan(int x)
{
             Fa[x]=x;//As the current root node, point its father to himself 
             Vis[x]=true;//mark that the point has passed 
      for(int i=question[x];i;i=b[i].next) 
      {
            int now_to=b[i].to;
                         If(vis[now_to])// If the subtrees of its child nodes and child nodes are all accessed, this step will be entered, judged by vis           
                                     LCA[b[i].num]=get(now_to);//Update LCA value 
      }
      for(int i=edge[x];i;i=a[i].next)
      {
            int now_to=a[i].to;
            if(!vis[now_to])
            {
                                     Dist[now_to]=dist[x]+a[i].v;//Update dist value 
                                     Tarjan (now_to); / / is equivalent to reverting now_to as the root node 
                                     Fa[now_to]=x;//update the father of the child node
            }
      } 
}
int main()
{
      freopen("input.in","r",stdin);
	  freopen("output.out","w",stdout); 
	  scanf("%d",&DATA);
	  for(int now=1;now<=DATA;now++)
      {
            memset(vis,false,sizeof(vis));
            memset(edge,0,sizeof(edge));
            memset(question,0,sizeof(question));
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            memset(dist,0,sizeof(dist));
            memset(fa,0,sizeof(fa));
            tot1=0,tot2=0;
            scanf("%d%d",&N,&M);
            for(int i=1;i<=N;i++)
                                     Fa[i]=i;//First point the father node of all nodes to themselves 
            for(int i=1;i<=N-1;i++)
            {
                  int A,B,C;
                  scanf("%d%d%d",&A,&B,&C); 
                                     Add_edge(A,B,C);//Create undirected edges, store with a linked list 
                  add_edge(B,A,C);
            }
            for(int i=1;i<=M;i++)
            {
                  int A,B;
                  scanf("%d%d",&A,&B);
                                     Add_question(A,B,i);//Use two records here to ensure that you can take care of these two points when updating the LCA. 
                  add_question(B,A,i);
                  ansf[i]=A,anst[i]=B;
            }
            Tarjan(1);
            for(int i=1;i<=M;i++)
                  printf("%d\n",dist[ansf[i]]+dist[anst[i]]-2*dist[LCA[i]]);
      }
	  //system("pause");
      return 0;
}