https://vjudge.net/contest/279463#problem/D
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
map<string,int>ma;
string s,s1;
int m,kk,ff,i,j,k,l,n;
double d[50][50],f;
int main()
{
while (scanf("%d",&n))
{
if (n==0) break;
ma.clear();
memset(d,0,sizeof(d));
for (i=1; i<=n; i++)
{
cin>>s;
ma[s]=i;
}
scanf("%d",&m);
for (i=1; i<=n; i++) d[i][i]=1;
for (i=1; i<=m; i++)
{
cin>>s>>f>>s1;
d[ma[s]][ma[s1]]=f;
}
for (k=1; k<=n; k++)
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
d[i][j]=max(d[i][j],d[i][k]*d[k][j]);
kk++;
ff=0;
for (i=1; i<=n; i++)
if (d[i][i]>1)
{
ff=1;
break;
}
printf("Case %d: ",kk);
if (ff)
printf("Yes\n");
else printf("No\n");
}
}