跳过正文
  1. Posts/

codeforces 475 B. Strongly Connected City

·2 分钟

http://codeforces.com/problemset/status 题意:n行m列的道路网络。共n*m条道路。每条道路都是单向的.问从任何一个路口出发能否到达其他的任何一个路口。

思路:需要注意的是。我从A点能到达B点,不代表B也能到达A.也就是说,某些点满足可以遍历所有点是不够的,只有当所有点都满足才可以。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月07日 星期一 08时55分48秒
  4File Name :code/cf/problem/475B.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25
 26
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=25;
 34int n,m;
 35bool vis[N][N];
 36char hor[N],ver[N];
 37
 38bool direrror (char ch,int d)
 39{
 40    if (ch=='v'&&d==3) return true;
 41    if (ch=='^'&&d==0) return true;
 42
 43    if (ch=='<'&&d==2) return true;
 44    if (ch=='>'&&d==1) return true;
 45
 46	return false;
 47}
 48
 49bool ok ( int x,int y)
 50{
 51    if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]) return true;
 52    return false;
 53}
 54
 55
 56void dfs ( int x,int y)
 57{
 58    vis[x][y] = true;
 59   // cout<<"x:"<<x<<" y:"<<y<<endl;
 60    for ( int i = 0 ; i < 4 ; i++)
 61    {
 62	if (direrror(hor[x],i)) continue;
 63	if (direrror(ver[y],i)) continue;
 64
 65	int nx = x + dx4[i];
 66	int ny = y + dy4[i];
 67	if (ok(nx,ny))
 68	{
 69	    dfs(nx,ny);
 70	}
 71    }
 72}
 73
 74bool solve ( int x,int y)
 75{
 76    ms(vis,false);
 77    dfs(x,y);
 78    for ( int i = 0 ; i < n ; i++)
 79	for ( int j = 0 ; j < m; j ++)
 80	    if (!vis[i][j]) return false;
 81    return true;
 82
 83}
 84int main()
 85{
 86	#ifndef  ONLINE_JUDGE
 87	freopen("code/in.txt","r",stdin);
 88  #endif
 89
 90
 91	cin>>n>>m;                      //注意题目要求是从任意一点都可以到达其他任何点。
 92	for ( int i = 0 ; i < n ; i ++) cin>>hor[i];
 93	for ( int i = 0 ; i < m ; i ++) cin>>ver[i];
 94
 95	int cnt = 0 ;
 96	bool ok = true;
 97	for ( int i = 0 ; i< n ; i++)
 98	    for ( int j = 0 ;j < m;  j++)
 99		if (!solve(i,j))
100		{
101		    ok = false;
102		    break;
103		}
104//	cout<<"cnt:"<<cnt<<endl;
105	if (ok)
106	{
107	    puts("YES");
108	}
109	else
110	{
111	    puts("NO");
112	}
113
114  #ifndef ONLINE_JUDGE
115  fclose(stdin);
116  #endif
117    return 0;
118}

相关文章

codeforces 510 B. Fox And Two Dots

·2 分钟
http://codeforces.com/contest/510/problem/B 题意:给定一个maze,用不同的字母代表不同的颜色。问能否找到一个颜色相同的环(失少四个点组成) 思路:dfs一遍,如果遇到之前已经访问过的点,说明成环。需要注意的是,要注意由一个点向某方向移动,然后由反方向移动到该点所造成的误判。所以dfs除了要知道当前的坐标x,y,还要记录之前的坐标px,py.

codeforces 277 A. Learning Languages

·2 分钟
http://codeforces.com/contest/277/problem/A 题意:有n个人,每个人会一定数目的语言(可能为0),一个人学一门语言的代价为1,人和人之间沟通可以通过任意个中间人翻译。问最少的代价使得这n个人可以相互沟通。

codeforces 217A ice skating

·1 分钟
http://codeforces.com/problemset/problem/217/A 题意:有n个雪漂(那是啥?,从某个雪漂出发走直线,只有到达另一个雪飘才能停下来。问最少需要添加多少个雪漂,才能使得可以到达任何一个雪漂。 思路:横坐标相同或者纵坐标相同的两个点之间是可以到达的。先O(N2)扫一遍建图。记录这个森林中数的个数为cnt,cnt-1即为答案。因为对于任意两个不能相互到达的点。我们只需要再来一个雪漂就可以使得这两个点相互到达。

codeforces 445 B. DZY Loves Chemistry

·2 分钟
http://codeforces.com/contest/445/problem/B 题意:一共有n种化学药品。m对关系,每对关系表示为x,y表示x和y相互反应。初始容器的danger值为1,当向容器中加入一个化学药品A,如果容器中存在化学药品和A反应,那么容器的danger值翻倍。否则不变。问一个最优的放置药品的顺序。

codeforces 522 A. Reposts

·1 分钟
http://codeforces.com/problemset/problem/522/A 题意:给定某条消息的传播路径。问最远传播的距离。。 思路:其实就是问树的深度。。直接dfs就行了。。