bzoj4690: Never Wait for Weights

一眼LCT什么鬼。。。

带权并查集瞎搞搞AC

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

int fa[110000];LL d[110000];
int findfa(int x)
{
    if(fa[x]==x)return fa[x];
    int FA=findfa(fa[x]);
    d[x]+=d[fa[x]];fa[x]=FA;
    return fa[x];
}

char ss[10];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        
        for(int i=1;i<=n;i++)fa[i]=i,d[i]=0LL;
        
        int x,y;LL c;
        for(int i=1;i<=m;i++)
        {
            scanf("%s",ss+1);
            if(ss[1]=='!')
            {
                scanf("%d%d%lld",&x,&y,&c);
                int fx=findfa(x),fy=findfa(y);
                if(fx!=fy)
                {
                    d[fx]=d[y]+c-d[x];
                    fa[fx]=fy;
                }
            }
            else
            {
                scanf("%d%d",&x,&y);
                int fx=findfa(x),fy=findfa(y);
                if(fx!=fy)printf("UNKNOWN\n");
                else printf("%lld\n",d[x]-d[y]);
            }
        }
    }
    return 0;
}

 

 

posted @ 2018-04-11 14:36  AKCqhzdy  阅读(120)  评论(0编辑  收藏  举报