CodeForces 131D【图特性+BFS】】的更多相关文章

题意: 只有一个环,然后环都是0(环缩点相当于树的根),然后其余的输出到根的距离 思路: 可以从度为1的 开始搜 把那些分支全标记掉,然后再取没有标记掉的,BFS一下搞出距离. 具体这个标记: 倒着搜这样肯定没有多对一,标记掉度等于2的那些点就好了,度>2的要减减,而且环里的点的度不可能搜到度 = 2 因为环=点的度为2,他又连出一条边 度=3. 后话: 这种n个点 n条边/n-1条边的题都是套路了,要仔细考虑图特性:点的度(出度,入度),怎么搜(顺着搜,倒着搜,BFS好写还是DFS好写) 但是…
转载请注明出处:http://blog.csdn.net/ns_code/article/details/19617187 图的存储结构 本文的重点在于图的深度优先搜索(DFS)和广度优先搜索(BFS),因此不再对图的基本概念做过多的介绍,但是要先大致了解下图的几种常见的存储结构. 邻接矩阵 邻接矩阵既可以用来存储无向图,也可以用来存储有向图.该结构实际上就是用一个二维数组(邻接矩阵)来存储顶点的信息和顶点之间的关系(有向图的弧或无向图的边).其描述形式如下: [cpp] view plainc…
题目: 一个网格迷宫由n行m列的单元格组成,每一个单元格要么是空地(用1表示),要么是障碍物(用0来表示).你的任务是找一条从起点到终点的最短移动序列,当中UDLR分别表示往上.下.左.右移动到相邻单元格.不论什么时候都不能在障碍格中.也不能走到迷宫之外.起点和终点保证是空地. 分析:图的BFS. #include <iostream> #include <string> #include <queue> using namespace std; const int M…
这篇文章用来复习使用BFS(Breadth First Search)和DFS(Depth First Search) 并解决一个在旅游时遇到的问题. 关于图的邻接表存储与邻接矩阵的存储,各有优缺点.但是邻接矩阵继承了数组的优点--在索引方面速度相比链表要快的多.由于以前我实现过邻接矩阵的存储方式,这次就用邻接表的方式复习吧. 图的邻接表存储简单示意 0.前提 类中的api以及属性 class CGraph { public: CGraph(void); ~CGraph(void); // 根据…
1128 写的dfs貌似不太对 bfs重写 用bfs将图进行黑白染色 如果有超过一个与自己颜色相同的点 就把该点存入栈中 最后处理栈中的点 判断此点是否合法 不合法 取反 取反后再判断相邻点是否合法 不合法再存入栈中 直到栈为空 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stdlib.h> #include<vect…
图是严蔚敏书上P168的图, 图的邻接表存储,DFS可以看以前写的文章:http://www.cnblogs.com/youxin/archive/2012/07/28/2613362.html ]; void (*visitFunc)(VextexType v); void visit(VextexType v) { cout<<v<<ends; } void BFSTraverse(Graph G,void (*Visit)(VextexType v)) { visitFunc…
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output This is an interactive problem. Vladik has favorite game, in which he plays all his free time. Game field could…
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers…
A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: Ea…
题目大意:一张图,问从起点到终点的最短时间是多少.方向转动也消耗时间. 题目分析:图的广度优先遍历... 代码如下: # include<iostream> # include<cstdio> # include<queue> # include<cstring> # include<algorithm> using namespace std; struct Node { int x,y,f,l,t; Node(int _x,int _y,in…