Codeforces 475 B. Strongly Connected City
2014-10-08 15:32
363 查看
floyd or 判断四个角能互相抵达.....
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
output
input
output
Note
The figure above shows street directions in the second sample test case.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10000;
const int INF=0x3f3f3f3f;
int floyd[500][500];
void Add_Edge(int u,int v)
{
floyd[u][v]=1;
}
int n,m;
char heng[100],shu[100];
int main()
{
memset(floyd,63,sizeof(floyd));
scanf("%d%d",&n,&m);
scanf("%s%s",heng,shu);
for(int i=1;i<=n*m;i++) floyd[i][i]=0;
for(int i=1;i<=n;i++)
{
int from=1+(i-1)*m;
if(heng[i-1]=='>')
{
int to=from+1;
for(int j=1;j<m;j++)
{
Add_Edge(from,to);
from=to; to++;
}
}
else if(heng[i-1]=='<')
{
int to=from+1;
for(int j=1;j<m;j++)
{
Add_Edge(to,from);
from=to; to++;
}
}
}
for(int i=1;i<=m;i++)
{
int from=i;
if(shu[i-1]=='v')
{
int to=from+m;
for(int j=1;j<n;j++)
{
Add_Edge(from,to);
from=to; to+=m;
}
}
else if(shu[i-1]=='^')
{
int to=from+m;
for(int j=1;j<n;j++)
{
Add_Edge(to,from);
from=to; to+=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++)
floyd[i][j]=min(floyd[i][j],floyd[i][k]+floyd[k][j]);
}
}
bool flag=true;
for(int i=1;i<=N&&flag;i++)
{
for(int j=1;j<=N&&flag;j++)
{
if(floyd[i][j]>=INF)
{
flag=false;
}
}
}
if(flag) puts("YES");
else puts("NO");
return 0;
}
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10000;
const int INF=0x3f3f3f3f;
int floyd[500][500];
void Add_Edge(int u,int v)
{
floyd[u][v]=1;
}
int n,m;
char heng[100],shu[100];
int main()
{
memset(floyd,63,sizeof(floyd));
scanf("%d%d",&n,&m);
scanf("%s%s",heng,shu);
for(int i=1;i<=n*m;i++) floyd[i][i]=0;
for(int i=1;i<=n;i++)
{
int from=1+(i-1)*m;
if(heng[i-1]=='>')
{
int to=from+1;
for(int j=1;j<m;j++)
{
Add_Edge(from,to);
from=to; to++;
}
}
else if(heng[i-1]=='<')
{
int to=from+1;
for(int j=1;j<m;j++)
{
Add_Edge(to,from);
from=to; to++;
}
}
}
for(int i=1;i<=m;i++)
{
int from=i;
if(shu[i-1]=='v')
{
int to=from+m;
for(int j=1;j<n;j++)
{
Add_Edge(from,to);
from=to; to+=m;
}
}
else if(shu[i-1]=='^')
{
int to=from+m;
for(int j=1;j<n;j++)
{
Add_Edge(to,from);
from=to; to+=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++)
floyd[i][j]=min(floyd[i][j],floyd[i][k]+floyd[k][j]);
}
}
bool flag=true;
for(int i=1;i<=N&&flag;i++)
{
for(int j=1;j<=N&&flag;j++)
{
if(floyd[i][j]>=INF)
{
flag=false;
}
}
}
if(flag) puts("YES");
else puts("NO");
return 0;
}
相关文章推荐
- Codeforces 475 B Strongly Connected City【DFS】
- 【CODEFORCES】 B. Strongly Connected City
- CodeForces 475B —— Strongly Connected City
- D - Strongly Connected City
- codeforces B. Strongly Connected City(dfs水过)
- 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP
- [CF475E]Strongly Connected City 2
- B. Strongly Connected City [强联通判断]
- Codeforces-292D:Connected Components(m个并查集)
- 【CF913F】Strongly Connected Tournament 概率神题
- Codeforces 475 D. CGCDSSQ
- algorithm@ Strongly Connected Component
- [PTA] week12 4-10 Strongly Connected Components
- 6-10 Strongly Connected Components(30 分)
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 4 Assignment: strongly connected component
- codeforces 920 E.Connected Components?
- POJ 2186 -- Popular Cows (Strongly Connected Components)
- 【乱搞】Codeforces EDU37 Connected Components
- [CF913F]Strongly Connected Tournament
- Strongly connected HDU - 4635 强连通