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.
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.
If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".
3 3 ><> v^v
NO
4 6 <><> v^v^v^
YES
The figure above shows street directions in the second sample test case.
题意: 裸的强联通,难点在建图
#include<bits/stdc++.h>
#define bug cout <<"bug"<<endl;
using namespace std;
typedef long long ll;
const int MAX_N=800;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
vector <int> edge[MAX_N];
int n,cur,cnt,top,mp[MAX_N],DFN[MAX_N],LOW[MAX_N],s[MAX_N];
void init(int a,int b){ //判断横纵坐标再来建图
n=a*b;
cur=cnt=0;
char op[50];
scanf("%s",op+1);
for(int i=1;i<=a;i++){
if(op[i]=='>'){
for(int j=1;j<=b;j++){
for(int k=j+1;k<=b;k++) edge[(i-1)*b+j].push_back((i-1)*b+k);
}
}
if(op[i]=='<'){
for(int j=b;j>=1;j--){
for(int k=j-1;k>=1;k--) edge[(i-1)*b+j].push_back((i-1)*b+k);
}
}
}
scanf("%s",op+1);
for(int j=1;j<=b;j++){
if(op[j]=='v'){
for(int i=1;i<=a;i++){
for(int k=i+1;k<=a;k++) edge[(i-1)*b+j].push_back((k-1)*b+j);
}
}
if(op[j]=='^'){
for(int i=a;i>=1;i--){
for(int k=i-1;k>=1;k--) edge[(i-1)*b+j].push_back((k-1)*b+j);
}
}
}
}
void tarjan(int u){
DFN[u]=LOW[u]=cur++;
s[++top]=u;
for(int i=0;i<(int)edge[u].size();i++){
int v=edge[u][i];
if(!DFN[v]){
tarjan(v);
LOW[u]=min(LOW[u],LOW[v]);
}
else if(DFN[v] && !mp[v]) LOW[u]=min(LOW[u],DFN[v]);
}
if(DFN[u]==LOW[u]){
int v=-1;
cnt++;
while(u!=v){
v=s[top--];
mp[v]=cnt;
}
}
}
int main(void){
int a,b;
cin >>a >> b;
init(a,b);
for(int i=1;i<=n;i++)
if(!DFN[i]) tarjan(i);
if(cnt==1) cout <<"YES"<<endl;
else cout <<"NO"<<endl;
return 0;
}