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. GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&tip=0

    GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&am ...

  2. 【转】【Android】HAL分析

    原文网址:http://www.cnblogs.com/lcw/p/3335505.html HAL概述 以下是基于android4.0.3,对应其他低版本的代码,可能有所差异,但基本大同小异. An ...

  3. bzoj3721 [PA2014 Final] Bazarek

    Description 有n件商品,选出其中的k个,要求它们的总价为奇数,求最大可能的总价. Input 第一行一个整数n(1<=n<=1000000),表示商品数量.接下来一行有n个整数 ...

  4. Android面试题07

    62. 说说mvc模式的原理,它在android中的运用. MVC英文即Model-View-Controller,即把一个应用的输入.处理.输出流程按照Model.View.Controller的方 ...

  5. JAX-WS 学习二:基于WEB容器,发布WebService

    WebService 的发布通过调用 Endpoint.publish() 方法来启动一个java内嵌的WEB容器来实现的,如果要将WebService部署到一个WEB容器中去,需要使用jax-ws提 ...

  6. ecshop 管理员不需要旧密码

  7. Unity Navigation自动寻路

    NavMesh(导航网格) 是3D游戏世界中主动寻路的一种技术,如果你想让游戏人物能自动绕开障碍物到达目的地.那你就来学习下 Navigation导航技术吧O(∩_∩)O~ 首先创建一个项目创建Pan ...

  8. UILabel+Create

    #import <UIKit/UIKit.h> @interface UILabel (Create) /** * 创建普通Label * * @param frame frame * @ ...

  9. 用dTree组件生成无限级导航树

     在做管理系统时不可避免要用到导航树,这种东西只要一次做好,就可以随处运行,目前比较好的组件是dTree,原则上可以达到无限级,当然实际运行中4,5级就已经很多了,dTree的速度还是不错的,而且是J ...

  10. NFinal 视图—用户控件

    自定义控件 定义控件 以Label控件为例: 1.首先在Common文件夹下添加Label.cs文件,其中代码如下: //a.control的实体类必须继承NFinal.UserControl类 pu ...