【Strongly Connected City】【CodeForces - 475B】(floyd)

题目:

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".

Examples

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.

 

解题报告:题目说的是比较繁琐,不容易看懂,需要去细细理解他的说法,这个题目的题意就是咱们去判定一下任意两点之间的连通性,但是他给的是每行每列的运行方向,所以咱们需要将这个图存起来,彼此每两个结点的关系。因为floyd是用于解决点与点之间的关系的,所以每行每列的交点都是作为结点来处理的,按照行优先的顺序将所有的顶点进行编号,咱们就会得到n*m个点,之后就是根据每行每列的方向将这些点之间的连通起来,最后再三重for循环floyd将图的点的连通性计算出来,最后只要判断这些顶点中是否有不可达的两个顶点就可以了。!!

 

ac代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;

int n,m;
char hang[100],lie[100];
int mp[500][500];
const int inf=0x3f3f3f3f;

int main()
{
	memset(mp,inf,sizeof(mp));
	scanf("%d%d",&n,&m);
	scanf("%s%s",hang,lie);
	for(int i=1;i<=n*m;i++)
		mp[i][i]=0;
	for(int i=1;i<=n;i++)
	{
		int from=1+(i-1)*m;
		if(hang[i-1]=='>')
		{
			int to=from+1;
			for(int j=1;j<m;j++)
			{
				mp[from][to]=1;
				from=to;
				to=from+1;
			}
		}
		else if(hang[i-1]=='<')
		{
			int to=1+from;
			for(int j=1;j<m;j++)
			{
				mp[to][from]=1;
				from=to;
				to=from+1;
			}
		}
	}
	for(int i=1;i<=m;i++)
	{
		int from=i;
		if(lie[i-1]=='v')
		{
			int to=from+m;
			for(int j=1;j<n;j++)
			{
				mp[from][to]=1;
				from=to;
				to=from+m;
			}
		}
		else if(lie[i-1]=='^')
		{
			int to=from+m;
			for(int j=1;j<n;j++)
			{
				mp[to][from]=1;
				from=to;
				to=from+m;
			}
		}
	}
	int N=n*m;
	for(int k=1;k<=N;k++)
		for(int i=1;i<=N;i++)
			for(int j=1;j<=N;j++)
			{
				mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
			}
	int flag=1;
	for(int i=1;i<=N&&flag;i++)
		for(int j=1;j<=N&&flag;j++)
		{
			if(mp[i][j]>=inf)
				flag=0;
		}
	if(!flag)
		printf("NO\n");
	else
		printf("YES\n");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值