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;
}

  

cf475B Strongly Connected City的更多相关文章

  1. codeforces B. Strongly Connected City(dfs水过)

    题意:有横向和纵向的街道,每个街道只有一个方向,垂直的街道相交会产生一个节点,这样每个节点都有两个方向, 问是否每一个节点都可以由其他的节点到达.... 思路:规律没有想到,直接爆搜!每一个节点dfs ...

  2. Codeforces 475 B Strongly Connected City【DFS】

    题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...

  3. Codeforces-475B Strongly Connected City

    仅仅用推断最外层是不是回路  假设是   则每两个点之间连通 #include<iostream> #include<algorithm> #include<cstdio ...

  4. [CF475E]Strongly Connected City 2

    题目大意:给一张$n(n\leqslant2000)$个点的无向图,给所有边定向,使定向之后存在最多的有序点对$(a,b)$满足从$a$能到$b$ 题解:先把边双缩点,因为这里面的点一定两两可达. 根 ...

  5. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  6. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  7. Strongly connected(hdu4635(强连通分量))

    /* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...

  8. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

  9. HDU 4635 Strongly connected (Tarjan+一点数学分析)

    Strongly connected Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

随机推荐

  1. Tk::Table

    <pre name="code" class="python"># DESCRIPTION # Tk::Table is an all-perl w ...

  2. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  3. linux下添加中文输入法

    一.安装环境 查看linux版本号 [ztteng@ztteng ~]$ lsb_release -aLSB Version:    :core-4.0-ia32:core-4.0-noarch:gr ...

  4. python高级编程之选择好名称:pepe8和命名最佳实践

    # # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' # my_list=['a','b','c','d'] ...

  5. PHP连接Mysql服务器的操作

    我们的数据存储在数据库中以后,要把数据和网页联系起来的话,要通过web服务器的解释器进行读取数据,再传递给客户端网页.如图: 这里,我选择了PHP作为学习的解释器.下面就具体来总结一下PHP连接MYS ...

  6. Git的一些用法(建立新的branch)

    建立新的branch和查看全部的branch(kk的代码是基于现有的branch) 切换到branch kk: 当然我们也能够在android studio里操作: 注意切换的时候代码会丢失,必须先c ...

  7. 文件读写IO

    摘要:本文主要总结了以下有关文件读写的IO,系统调用与库函数. 1.初级IO函数:close,creat,lseek,open,write 文件描述符是一个整型数 1.1close 1.2int cr ...

  8. Working with Numbers in PL/SQL(在PL/SQL中使用数字)

    This article gives you all the information you need in order to begin working with numbers in your P ...

  9. BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类

    本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...

  10. Niagara AX之在Station下显示Home节点

    默认的Station下是没有Home节点的,那么,这个Home节点是怎么添加上去的呢? 注意Home后面的描述(Description):“Navigation tree defined by nav ...