下雪了

Kattis secretchamber – Secret Chamber at Mount Rushmore(弗洛伊德求闭包/弗洛伊德模板)

WF的题,放弃好了。

emmm,一开始是想用字典树什么的,就没去看。后来发现,我们要把一个点与另一个点连起来,那就想到如果把它们看成26*26的图,说不定就好了呢?题目范围给的很小,那么n^3是绝对不会超时的,所以就用Floyd来做,果然不出所料。

AC代码:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<stack>
#include<cmath>
#include<ctime>
#include<queue>
#include<deque>
#include<list>
#include<map>
#define int long long
#define ffor(i, a, b) for(ll i = a; i <= b; i++)
#define rfor(i, a, b) for(ll i = a; i >= b; i--)
#define mes(a,b) memset(a, b, sizeof(a))
#define cos(x) cos(x*PI/180.0)
#define sin(x) sin(x*PI/180.0)
#define stop system("pause")
#define see(s,x) cout<<(s)<<'='<<(x)<<endl
#define IMAX 0x7fffffff
#define PI 3.141592654
#define INF 0x3f3f3f3f
#define eps 1e-6
#define lowbit(x) (x&(-x))
typedef long long ll;
ll mod;
ll max(ll a, ll b) { return(a > b) ? a : b; }
ll min(ll a, ll b) { return(a < b) ? a : b; }
using namespace std;

int mp[30][30];

int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        getchar();
        ffor(i, 0, 26)
        {
            ffor(k, 0, 26)
                mp[i][k] = 99;
            mp[i][i] = 0;
        }
        ffor(i, 1, n)
        {
            char x, y;
            scanf("%c %c%*c", &x, &y);
            mp[x - 'a'][y - 'a'] = 0;
        }
        ffor(j, 0, 26)
        {
            ffor(i, 0, 26)
            {
                ffor(k, 0, 26)
                    mp[i][k] = min(mp[i][k], mp[i][j] + mp[j][k]);
            }
        }
        ffor(i, 1, m)
        {
            char s1[60], s2[60];
            cin >> s1 >> s2;
            int l1 = strlen(s1);
            int l2 = strlen(s2);
            bool ok = true;
            if (l1 == l2)
            {
                ffor(i, 0, l1 - 1)
                {
                    int x = s1[i] - 'a';
                    int y = s2[i] - 'a';
                    if (mp[x][y] != 0)
                    {
                        ok = false;
                        break;
                    }
                }
            }
            else ok = false;
            if (ok) cout << "yes" << endl;
            else cout << "no" << endl;
        }
    }
}

 


Add Your Comment

* Indicates Required Field

Your email address will not be published.

*