B. Strongly Connected City [强联通判断]
2018-03-18 11:20
295 查看
B. Strongly Connected Citytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputImagine 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.InputThe 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.OutputIf the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".ExamplesinputCopy
题意: 裸的强联通,难点在建图
#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;
}
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.InputThe 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.OutputIf the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".ExamplesinputCopy
3 3 ><> v^voutput
NOinputCopy
4 6 <><> v^v^v^output
YESNoteThe 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;
}
相关文章推荐
- Codeforces 475 B. Strongly Connected City
- codeforces B. Strongly Connected City(dfs水过)
- [CF475E]Strongly Connected City 2
- D - Strongly Connected City
- CodeForces 475B —— Strongly Connected City
- Codeforces 475 B Strongly Connected City【DFS】
- 【CODEFORCES】 B. Strongly Connected City
- 转 联通近期走势判断
- android代码判断当前手机的手机号码是属于移动、联通、还是电信的
- Programming Question-4-Strongly Connected Components Problem through DFS
- 并查集判断无向图联通
- HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
- 如何判断手机用户是移动,电信,联通?
- 判断手机所属三大运营商 移动、联通、电信
- Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集
- poj 2513 欧拉回路+并查集判断是否联通+Trie树
- JS判断手机号码和联通手机号码
- 使用InternetGetConnectedState这个API判断网络是否连通
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- android 怎么判断手机号是移动还是联通