BZOJ4690: Never Wait for Weights

Posted Star_Feel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ4690: Never Wait for Weights相关的知识,希望对你有一定的参考价值。

【传送门:BZOJ4690


简要题意:

  有n个物品,m个操作,两种操作:

  ! a b w表示第a个物品比第b个物品重量轻w

  ? a b求出a比b轻多少,如果无法求出答案则输出UNKNOWN


题解:

  带权并查集,对于!操作,默认编号小的在前面,如果要交换则w=-w

  然后直接做就好了


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int fa[110000],v[110000];
int findfa(int x)
{
    if(fa[x]==x) return x;
    int f=fa[x];
    fa[x]=findfa(fa[x]);
    v[x]+=v[f];
    return fa[x];
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        memset(v,0,sizeof(v));
        for(int i=0;i<=n;i++) fa[i]=i;
        bool bk=true;char st[3];
        for(int i=1;i<=m;i++)
        {
            int x,y,k;
            scanf("%s%d%d",st+1,&x,&y);
            if(st[1]==!)
            {
                scanf("%d",&k);
                if(x>y) k=-k,swap(x,y);
                int fx=findfa(x),fy=findfa(y);
                if(fx!=fy)
                {
                    fa[fy]=fx;
                    v[fy]=k-v[y]+v[x];
                }
            }
            else
            {
                int fx=findfa(x),fy=findfa(y);
                if(fx!=fy) printf("UNKNOWN\n");
                else printf("%d\n",v[y]-v[x]);
            }
        }
    }   
    return 0;
}

 

以上是关于BZOJ4690: Never Wait for Weights的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ-4690Never Wait For Weights 带权并查集

bzoj4690 Never Wait for Weights

bzoj4690: Never Wait for Weights 并查集

BZOJ4690: Never Wait for Weights

bzoj 4690&&4602

[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking