这么sb吗..floyd即可
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<ctime> 5 #include<iostream> 6 #include<algorithm> 7 #include<queue> 8 #include<set> 9 #define inf (0x3f3f3f3f) 10 #define l(a) ((a)<<1) 11 #define r(a) ((a)<<1|1) 12 #define b(a) (1<<(a)) 13 #define rep(i,a,b) for(int i=a;i<=(b);i++) 14 #define clr(a) memset(a,0,sizeof(a)) 15 typedef long long ll; 16 typedef unsigned long long ull; 17 using namespace std; 18 int readint(){ 19 int t=0,f=1;char c=getchar(); 20 while(!isdigit(c)){ 21 if(c=='-') f=-1; 22 c=getchar(); 23 } 24 while(isdigit(c)){ 25 t=(t<<3)+(t<<1)+c-'0'; 26 c=getchar(); 27 } 28 return t*f; 29 } 30 const int maxn=109; 31 int n,m,d[maxn][maxn]; 32 int main(){ 33 //freopen("#input.txt","r",stdin); 34 //freopen("#output.txt","w",stdout); 35 m=readint();n=readint(); 36 rep(i,1,26) 37 rep(j,1,26) if(i!=j) d[i][j]=inf; 38 rep(i,1,m){ 39 char a=getchar();while(a<'a'||a>'z') a=getchar(); 40 char b=getchar();while(b<'a'||b>'z') b=getchar(); 41 int u=a-'a'+1,v=b-'a'+1; 42 d[u][v]=0; 43 } 44 rep(k,1,26) 45 rep(i,1,26) 46 rep(j,1,26) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); 47 while(n--){ 48 char a[109],b[109]; 49 scanf("%s%s",a,b); 50 if(strlen(a)!=strlen(b)){ 51 puts("no");continue; 52 } 53 int l=strlen(a),A[109],B[109];bool flag=1; 54 rep(i,1,l) A[i]=a[i-1]-'a'+1; 55 rep(i,1,l) B[i]=b[i-1]-'a'+1; 56 rep(i,1,l) if(d[A[i]][B[i]]){ 57 puts("no");flag=0;break; 58 } 59 if(flag) puts("yes"); 60 } 61 //fclose(stdin); 62 //fclose(stdout); 63 return 0; 64 }