Navigation Nightmare-- weighted disjoint-set (multi-weight)

Topic Link

Meaning of the questions:

Gives the n farms, then time sequentially gives information about the relative position of the m the farm, will be given later asked, Q at time t, the number of x to y Manhattan distance Yes.

answer:

dx [i] maintains the root node to the abscissa i dy [i] from the root node to maintain a distance ordinate i

Disjoint-set and efficient place is that all nodes when using Find (x) function to find the parent node of x will descend along the way to directly connect to the parent, so that the next query can find a direct parent, indicates that the code f [x] = Find (f [x]);

Prior to contacting the bare are relatively disjoint-set, but the problem like this, when disjoint-set maintenance required to maintain the distance to the point of its parent node (dx [x] + = dx [f [x]]; ), and then constructed Find (x) function when you have to consider at the same time maintaining the distance, maintenance is likely to lead the way once right from wrong.

Maintenance nodes within communication distance blocks to the abscissa and ordinate from the parent node, determines whether the first query if the two points belong to the horizontal axis will be the distance from the longitudinal axis of the parent node and to a subtraction request the same set you can get from Manhattan

How focused is a collection of merger

 

Let  fx fy directed i.e.  f [fy] = fx   then distributed discussions

如果 d==W  dx【fx】= dx【a[i].f2】 - dx【a[i].f1】- a【i】.l   dy【fx】=dy【a[i].f2】-dy【a[i].f1】

如果 d==E  dx【fx】= dx【a[i].f2】 - dx【a[i].f1】+ a【i】.l   dy[fx]=dy【a[i].f2】-dy【a[i].f1】

如果 d==S  dy【fx】= dy【a[i].f2】 - dy【a[i].f1】+ a【i】.l   dx[fx]=dx【a[i].f2】-dx【a[i].f1】

如果 d==N  dy【fx】= dy【a[i].f2】 - dy【a[i].f1】- a【i】.l    dx[fx]=dx【a[i].f2】-dx【a[i].f1】

Then merge the above four cases

 

Code:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5+7;
int f[maxn],dx[maxn],dy[maxn];
struct node
{
    int f1,f2,l;
    char d;
} a[maxn];
int n,m,k;
int Find(int x)
{
    if(f[x]==x)return x;
    int root=Find(f[x]);
    dx[x]+=dx[f[x]];
    dy[x]+=dy[f[x]];
    return f[x]=root;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++)f[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a[i].f1,&a[i].f2,&a[i].l);
        cin>>a[i].d;
    }
    scanf("%d",&k);

    int i=1;
    while(k--)
    {
        int F1,F2,L;
        scanf("%d%d%d",&F1,&F2,&L);
        while(i<=L)
        {
            int fx=Find(a[i].f1);
            int fy=Find(a[i].f2);
            if(fx!=fy)
            {
                f[fx]=fy;

                dx[fx]=dx[a[i].f2]-dx[a[i].f1];
                dy[fx]=dy[a[i].f2]-dy[a[i].f1];

                if(a[i].d=='W') dx[fx]-=a[i].l;
                else if(a[i].d=='E')dx[fx]+=a[i].l;
                else if(a[i].d=='S')dy[fx]+=a[i].l;
                else dy[fx]-=a[i].l;
            }
            i++;
        }
        int fx=Find(F1);
        int fy=Find(F2);
        if(fx!=fy)printf("-1\n");
        else
        {
            int ans=abs(dx[F1]-dx[F2])+abs(dy[F1]-dy[F2]);
            printf("%d\n",ans);
        }

    }

    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/j666/p/11615378.html