bnuoj 17184 代数 (带权并查集 )

代数

1000ms
65536KB

 

现有N个未知数A[1],A[2],…A[N],以及M个方程,每个方程都是形如A[s]+A[s+1]+A[s+2]+…A[t-1]+A[t]=c。现在求解这个方程组。

Input

 

输入的第一行为两个整数N和M(1<=N,M<=100000)。接下来的M行每行三个整数s,t,c(1 <= s, t <= N, 0 <= c < 10^9)。

Output

 

对于输入的每个方程,若该方程与前面的方程矛盾,则输出"Error!"并忽略这个方程,否则输出"Accepted!"。之后对于每个变量,若能求出这个变量的值,则输出这个变量,否则输出"Unknown!"。

Sample Input

3 5
1 3 3
1 3 2
2 3 2
1 1 0
1 1 1

Sample Output

Accepted!
Error!
Accepted!
Error! 
Accepted!
1
Unknown!
Unknown!

Source




#include<stdio.h>
int fa[100010];long long rank[100010]; int n;
void initial()
{
    int i;
    for(i=1;i<=100000;i++)
    {
        fa[i]=i;  rank[i]=0;
    }
}
int getfather(int x)
{
    if(x==fa[x]) return x;
    int old = fa[x];
    fa[x]=getfather(fa[x]);
    rank[x]=rank[old]+rank[x];
    return fa[x];
}
int istrue(int x,int y,int c)
{
    int fx,fy;
    fx=getfather(x); fy=getfather(y);
    if(fx!=fy)
    {
        return 1;
    }
    else
    {
        if( rank[y] + c == rank[x] )  return 1;
        else  return 0;
    }
}
void unionset(int x,int y,int c)
{
    int fx,fy;
    fx=getfather(x); fy=getfather(y);
    if(fx==fy)  return;
    if(fy>fx)
    {
        fa[fx]=fy;
        rank[fx]= (c+rank[y]-rank[x]);
    }
    else
    {
        fa[fy]=fx;
        rank[fy]= (rank[x]-c-rank[y]);
    }
}
void isknown(int x)
{
    int fx,fy,y;
    y=x+1;
    fx=getfather(x); fy=getfather(y);
    if(fx!=fy)
    {
         printf("Unknown!\n");
    }
    else
    {
         printf("%lld\n",rank[x]-rank[y]);
    }
}
int main()
{
    int m,s,t,c,i,j;
    initial();
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d",&s,&t,&c);
        t++;
        if( istrue(s,t,c) )
        {
            unionset(s,t,c);
            printf("Accepted!\n");
        }
        else
        {
            printf("Error!\n");
        }
    }
    for(i=1;i<=n;i++)
    {
        isknown(i);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值