You have been struck with inspiration, and are designing a beautiful new art sculpture for the foyer of your local museum. For highly important artistic reasons, you are designing it using very specific materials. However, you are not sure if physics is on your side. Will your sculpture actually stand up?
The sculpture will be composed of various ball joints weighing 1 kilogram each, and various rods (of negligible weight) connecting the joints. Rods cannot be stretched or compressed, and they can never detach from a joint. However, they are free to rotate around the joints in any direction. The joints that lie on the ground are glued in place; all others are free to move. For simplicity, you may ignore the effects of intersections of rods; each rod exerts force only on the 2 joints connected to it. Also, any joint that is in the air will have at least one rod coming out that is not parallel to the ground. This prevents the degenerate case where a ball is supported only horizontally by a rigid structure. In real life, it would sag just a little.
Write a program to determine whether your structure is static (that is, will not immediately move from the effects of gravity). Note that each rod can transmit an arbitrarily large tensional force along its length, and that ``being static" means that the tensional forces at each joint balance the weight of the joint.
If the structure is static, you must also determine whether it is stable (that is, will not move if perturbed slightly by pulling its joints).
Input
The input contains several sculpture descriptions. Every description begins with a line containing integersJ and R, the number of joints and rods in the structure, respectively. Joints are numbered from 1 to J. The description continues with J lines, one per joint, each containing 3 floating point numbers giving the x, y,z coordinates of that joint. Following are R lines, one per rod, with 2 distinct integers indicating the joints connected by that rod.
Each rod is exactly the right length to connect its joints. The z coordinates will always be non-negative; az coordinate of 0 indicates that the joint is on the ground and fixed in place. There are at most 100 joints and 100 rods.
The last description is followed by a line containing two zeroes.
Output
For each sculpture description, output `NON-STATIC', `UNSTABLE', or `STABLE', as shown in the sample output.
Sample Input
4 3 0 0 0 -1.0 -0.5 1.0 1.0 -0.5 1.0 0 1.0 1.0 1 2 1 3 1 4 4 6 0 0 0 -1.0 -0.5 1.0 1.0 -0.5 1.0 0 1.0 1.0 1 2 1 3 1 4 2 3 2 4 3 4 7 9 0 0 0 -1.0 -0.5 1.0 1.0 -0.5 1.0 0 1.0 1.0 -1.0 -0.5 0 1.0 0.5 0 0 1.0 0 1 2 1 3 1 4 2 3 2 4 3 4 2 5 3 6 4 7 0 0
Sample Output
Sculpture 1: NON-STATIC Sculpture 2: UNSTABLE Sculpture 3: STABLE
Claimer: The data used in this problem is unofficial data prepared by Derek Kisman. So any mistake here does not imply mistake in the offcial judge data. Only Derek Kisman is responsible for the mistakes. Report mistakes to dkisman@acm.org
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
double x[101],y[101],z[101],a[300][101],b[300][101],eps,c;
int n,m,r,i,j,k,l,t;
bool ok()
{
l=0;
for(i=m;i;i--)
{
k=l;
for(j=l+1;j<r;j++)
if(fabs(b[j][i])>fabs(b[k][i]))
k=j;
if(fabs(b[k][i])<eps)
continue;
memcpy(x,b[l],sizeof(x));
memcpy(b[l],b[k],sizeof(x));
memcpy(b[k],x,sizeof(x));
for(j=l+1;j<r;j++)
{
c=b[j][i]/b[l][i];
for(k=0;k<i;k++)
b[j][k]-=c*b[l][k];
b[j][i]=0;
}
l++;
}
for(i=l;i<r;i++)
if(fabs(b[i][0])>eps)
return false;
return true;
}
int main()
{
eps=0.1;
while(scanf("%d%d",&n,&m)&&n)
{
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
for(i=1;i<=m;i++)
{
scanf("%d%d",&j,&k);
j--,k--;
if(z[j])
{
a[3*j][i]=x[k]-x[j];
a[3*j+1][i]=y[k]-y[j];
a[3*j+2][i]=z[k]-z[j];
}
if(z[k])
{
a[3*k][i]=x[j]-x[k];
a[3*k+1][i]=y[j]-y[k];
a[3*k+2][i]=z[j]-z[k];
}
}
r=0;
for(i=0;i<n;i++)
if(z[i]>0)
{
memcpy(b[r++],a[i*3],sizeof(x));
memcpy(b[r++],a[i*3+1],sizeof(y));
memcpy(b[r++],a[i*3+2],sizeof(z));
b[r-1][0]=1;
}
printf("Sculpture %d: ",++t);
if(!ok())
{
puts("NON-STATIC");
continue;
}
r=0;
for(i=0;i<n;i++)
if(z[i]>0)
{
memcpy(b[r++],a[i*3],sizeof(x));
memcpy(b[r++],a[i*3+1],sizeof(y));
memcpy(b[r++],a[i*3+2],sizeof(z));
b[r-1][0]=sin(double(i*3+1));
b[r-2][0]=sin(double(i*3+2));
b[r-3][0]=sin(double(i*3+3));
}
if(!ok())
{
puts("UNSTABLE");
continue;
}
puts("STABLE");
}
return 0;
}