Codeforces 475B Strongly Connected City(强连通,随机数做法)

Codeforces七题场的一道B题,题目大意就是给你每条边所能通往的唯一方向,问此图是否是强连通图。

此题解法颇多:

1.裸tarjan,【表示不会,回头学学】

2.能够环绕一周的即可【智商做法,此题正解】

3.大暴搜

我这里用的是搜索的做法,唯一值得我总结的就是一种不错的采用随机数的方法(虽然此题数据量小,纯粹暴搜也可过)。

首先弄一个随机数种子,然后取两个随机点A(x1,y1),B(x2,y2)。用BFS验证A是否能到达B。然后随机取多次A,B两点,如果A均能到达B点,那么我们就认为该图是强连通的;反之,倘若有一组A,B不成立,那么就可以否定该图的连通性。

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 <queue>
#include <cstdlib>
#include <time.h>
using namespace std;
int n, m;
string map[3];
int vis[25][25];
struct node {
    int x, y;
    node(int x, int y) : x(x), y(y){}
    node() {}
};
bool BFS(node a, node b) {
    memset(vis, 0, sizeof(vis));
    queue<node> q;
    q.push(a);
    vis[a.x][a.y] = 1;
    while(!q.empty()) {
        node u = q.front(); q.pop();
        int x = u.x, y = u.y;
        if(x == b.x && y == b.y) return true;
        if(map[1][x] == '>') {
            for(int i = y + 1; i < m; i++) {
                if(!vis[x][i]) { vis[x][i] = 1; q.push(node(x, i));}
            }
        } else if(map[1][x] == '<') {
            for(int i = y - 1; i >= 0; i--) {
                if(!vis[x][i]) { vis[x][i] = 1; q.push(node(x, i));}
            }
        }
        if(map[2][y] == '^') {
            for(int i = x - 1; i >= 0; i--) {
                if(!vis[i][y]) { vis[i][y] = 1; q.push(node(i, y));}
            }
        } else if(map[2][y] == 'v') {
            for(int i = x + 1; i < n; i++) {
                if(!vis[i][y]) { vis[i][y] = 1; q.push(node(i, y));}
            }
        }
    }
    return false;
}
int main() {
    srand(time(NULL));
    scanf("%d%d", &n, &m);
    cin>>map[1]>>map[2];
    int times = 10000;
    while(times--) {
        node a = node(rand() % n, rand() % m);
        node b = node(rand() % n, rand() % m);
        while((a.x == b.x) && (a.y == b.y))
            b = node(rand() % n, rand() % m);
        if(!BFS(a, b)) {cout<<"NO"<<endl; goto loop;}
    }
    cout<<"YES"<<endl;
    loop:;
    return 0;
}
【此方法感谢于喵神家的内位,为我提供了一种不错的思路,想必会有用处】

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值