题目: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;
}

相关文章:

  • 2021-06-09
  • 2021-07-15
  • 2022-12-23
  • 2021-06-01
  • 2021-08-04
  • 2021-10-13
  • 2022-12-23
  • 2021-11-12
猜你喜欢
  • 2021-11-16
  • 2021-07-28
  • 2022-12-23
  • 2021-08-27
相关资源
相似解决方案