HDU 1181 变形课 (用传递闭包做)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1181

题意:转化成有向图,是否存在从b到m的通路

传递闭包代码:

#include <iostream>
#include <cstring>
using namespace std;

const int M = 50;

int g[M][M];


int main()
{
    char str[100];
    int n = 26;

    while (~scanf("%s", str))
    {

        if (str[0] != '0')
        {
            
            g[str[0] - 'a' + 1][str[strlen(str) - 1] - 'a' + 1] = 1;
        }
        else
        {


            for (int k = 1; k <= n; k++)
            {
                for (int i = 1; i <= n; i++)
                {
                    if (g[i][k])
                    {
                        for (int j = 1; j <= n; j++)
                        {
                            if (g[k][j])
                            {
                                g[i][j] = 1;
                            }
                        }
                    }
                }
            }


            if (g[2][13])
            {
                puts("Yes.");
            }
            else
            {
                puts("No.");
            }
            memset(g, 0, sizeof(g));
        }


    }
    return 0;
}
原文地址:https://www.cnblogs.com/qiufeihai/p/2639051.html