• cf475B Strongly Connected City


    B. Strongly Connected City
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Imagine a city with n horizontal streets crossing m vertical streets, forming an (n - 1) × (m - 1) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traffic moves only from north to south or only from south to north in each vertical street. It is possible to enter a horizontal street from a vertical street, or vice versa, at their intersection.

    The mayor has received some street direction patterns. Your task is to check whether it is possible to reach any junction from any other junction in the proposed street direction pattern.

    Input

    The first line of input contains two integers n and m, (2 ≤ n, m ≤ 20), denoting the number of horizontal streets and the number of vertical streets.

    The second line contains a string of length n, made of characters '<' and '>', denoting direction of each horizontal street. If the i-th character is equal to '<', the street is directed from east to west otherwise, the street is directed from west to east. Streets are listed in order from north to south.

    The third line contains a string of length m, made of characters '^' and 'v', denoting direction of each vertical street. If the i-th character is equal to '^', the street is directed from south to north, otherwise the street is directed from north to south. Streets are listed in order from west to east.

    Output

    If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".

    Sample test(s)
    input
    3 3
    ><>
    v^v
    
    output
    NO
    
    input
    4 6
    <><>
    v^v^v^
    
    output
    YES
    
    Note

    The figure above shows street directions in the second sample test case.

    sb模拟题

    处理输入之后floyd乱搞

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<deque>
    #include<set>
    #include<map>
    #include<ctime>
    #define LL long long
    #define inf 0x7ffffff
    #define pa pair<int,int>
    #define pi 3.1415926535897932384626433832795028841971
    using namespace std;
    inline LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m;
    bool mark[500][500];
    inline int g(int x,int y)
    {
    	return (x-1)*m+y;
    }
    int main()
    {
    	n=read();m=read();
    	for (int i=1;i<=n;i++)
    	  {
    	  	char ch=getchar();
    	  	while (ch!='>'&&ch!='<')ch=getchar();
    	  	for (int j=1;j<m;j++)
    	  	  if (ch=='>') mark[g(i,j)][g(i,j+1)]=1;
    	  	  else mark[g(i,j+1)][g(i,j)]=1;
    	  }
    	for (int i=1;i<=m;i++)
    	  {
    	  	char ch=getchar();
    	  	while (ch!='^'&&ch!='v')ch=getchar();
    	  	for (int j=2;j<=n;j++)
    	  	  if (ch=='^') mark[g(j,i)][g(j-1,i)]=1;
    	  	  else mark[g(j-1,i)][g(j,i)]=1;
    	  }
    	int tot=n*m;
    	for(int k=1;k<=tot;k++)
    	  for(int i=1;i<=tot;i++)
    	    for (int j=1;j<=tot;j++)
    	      mark[i][j]=mark[i][j]||(mark[i][k]&&mark[k][j]);
    	for (int i=1;i<=tot;i++)
    	  for (int j=1;j<=tot;j++)
    	    if (i!=j&&!mark[i][j])
    	    {
    	    	printf("NO");
    	    	return 0;
    	    }
    	printf("YES");
    	return 0;
    }
    

      

    ——by zhber,转载请注明来源
  • 相关阅读:
    CentOS 6.3 安装 phpmyadmin
    mysql 常用命令
    用PHP将Unicode 转化为UTF-8
    Angularjs通过$http与服务器通信
    话说Angularjs的$resource模块
    AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)
    Angular-ui-router + oclazyload + requirejs实现资源随route懒加载
    移动前端开发之viewport的深入理解
    AngularJS的ng-class切换class
    AngularJS中如何对Controller与Service进行分层设计与编码
  • 原文地址:https://www.cnblogs.com/zhber/p/4035887.html
Copyright © 2020-2023  润新知