【NOIP 校内模拟】T1 line(带权并查集)

很无奈 离正解就差一句话

简单的带权并查集 没啥好说的 也可以差分约束

#include<bits/stdc++.h>
#define N 100005
#define M 200005
#define D 10005
using namespace std;
template<class T>
inline void read(T &x)
{
    x=0; int f=1;
    static char ch=getchar();
    while((!isdigit(ch))&&ch!='-')  ch=getchar();
    if(ch=='-') f=-1,ch=getchar();
    while(isdigit(ch))  x=x*10+ch-'0',ch=getchar();
    x*=f;
}
int n,m;
int father[N],to_root[N];
inline int getfather(int x)
{
    if(father[x]==x)    return x;
    int fa=father[x];
    father[x]=getfather(father[x]);
    to_root[x]+=to_root[fa];
    return father[x];
}
int minx[N],maxx[N];
int main()
{
    read(n),read(m);
    for(register int i=1;i<=n;i++)  father[i]=i;
    for(register int i=1;i<=m;i++)
    {
        int x,y,z;
        read(x),read(y),read(z);
        if(z<0) swap(x,y),z=-z;
        int fx=getfather(x); int fy=getfather(y);
        if(fx==fy)
        {
            if(to_root[x]+z!=to_root[y])
            {
                puts("impossible");
                return 0;
            }
        }
        else
        {
            father[fy]=fx;
            to_root[fy]=to_root[x]+z-to_root[y];
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int fa=getfather(i);
        minx[fa]=min(minx[fa],to_root[i]);
        maxx[fa]=max(maxx[fa],to_root[i]);
    }
    for(int i=1;i<=n;i++)   ans=max(ans,maxx[i]-minx[i]);
    cout<<ans;
    return 0;
}

转载于:https://www.cnblogs.com/Patrickpwq/articles/9908798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值