1986 Distance Queries //LCA+VECTOR 贼慢

Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 3709   Accepted: 1316
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart.

Source

USACO 2004 February

 

 

#include<stdio.h>

#include<vector>

using namespace std;

const int MAX=40005;

struct node

{

    node(int vv=0,int vval=0):v(vv),val(vval) {}

    int v,val;

};

 

int f[MAX],vis[MAX];

 

vector<node> tree[2*MAX],Qes[2*MAX];

int d[MAX],res[MAX];

 

void init(int n)

{

    for(int i=1; i<=n; i++)

    {

        f[i]=i;//用于并查集记录父亲节点

        vis[i]=0;//是否被访问

        tree[i].clear();//清空

        Qes[i].clear();

        res[i]=0;

    }

}

 

int find(int n)   //查找函数,并压缩路径

{

    if(n!=f[n]) f[n]=find(f[n]);

    return f[n];

}

int Union(int x,int y)  //合并函数,如果属于同一分支则返回0,成功合并返回1

{

    int a=find(x),b=find(y);

    f[y]=x;

    return b;

}

 

void LCA(int u,int dis)

{

    d[u]=dis;

    vis[u]=1;

    int size=tree[u].size();

    for(int i=0; i<size; i++)

    if(!vis[tree[u][i].v])

    {

        LCA(tree[u][i].v,dis+tree[u][i].val);

        Union(u,tree[u][i].v);

    }

 

    size=Qes[u].size();

    for(int i=0; i<size; i++)  //如果已经访问了问题节点,就可以返回结果了.

    {

        if(vis[Qes[u][i].v])

        {

            int p=find(Qes[u][i].v);

            res[Qes[u][i].val]=dis+d[Qes[u][i].v]-2*d[p];

        }

    }

}

int main()

{

    int n,m;

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        init(n);

        int s,t,c;

        char ch;

        for(int i=1; i<=m; i++)

        {

            scanf(" %d %d %d %c",&s,&t,&c,&ch);

            tree[s].push_back(node(t,c));

            tree[t].push_back(node(s,c));

        }

        int k;

        scanf("%d",&k);

        for(int i=1;i<=k;i++)

        {

            scanf("%d%d",&s,&t);//这里可以输入多组询问

            Qes[s].push_back(node(t,i));//相当于询问两次

            Qes[t].push_back(node(s,i));

        }

        LCA(1,0);

        for(int i=1;i<=k;i++)  printf("%d/n",res[i]);

    }

    return 0;

}

你可能感兴趣的:(vector,tree,Integer,input,output,distance)