代数
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;
}