HDU 3313 Key Vertex(BFS+BFS) 求S点到T点路径的关键点
来源:互联网 发布:centos一键安装lnmp 编辑:程序博客网 时间:2024/06/06 12:45
Key Vertex
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1349 Accepted Submission(s): 307
Problem Description
You need walking from vertex S to vertex T in a graph. If you remove one vertex which stops you from walking from S to T, that vertex we call as key vertex. Now you are given a directed graph, S and T, and you should tell us how many key vertexes are there in the graph.
Please notice that S and T are key vertexes and if S cannot walking to T by the directed edge in the initial graph then all vertexes becomes to key vertexes.
Please notice that S and T are key vertexes and if S cannot walking to T by the directed edge in the initial graph then all vertexes becomes to key vertexes.
Input
The input consists of multiply test cases. The first line of each test case contains two integers, n(0 <= n <= 100000), m(0 <= m <= 300000), which are the number of vertexes and the number of edge. Each of the next m lines consists of two integers, u, v(0 <= u, v < n; u != v), indicating there exists an edge from vertex u to vertex v. There might be multiple edges but no loops. The last line of each test case contains two integers, S, T(0 <= S, T < n, S != T).
Output
Output the number of key vertexes in a single line for each test case.
Sample Input
6 60 11 21 32 43 44 50 5
Sample Output
4
Author
momodi
Source
HDOJ Monthly Contest – 2010.02.06
Recommend
wxl | We have carefully selected several similar problems for you: 3251 3310 3314 3306 3307
题意:给出一个有向无环图,问从S点到T点有多少个关键点(即每条路都必过的点),如果没有路从S到T,那么所有的点都是关键点。
解题:因为要求关键点,那么我们可以求一条从S到T的最短路,那么所有的关键点必然在这一条最短路上,我们把这条最短路上所有的点按经过的倒至顺序从0都标一个号,其他的点都标为-1,每一次第一个加入队列里面的都是必须点。再找的过程中更新必须点的指向。
参考详解链接
#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define MAXN 100005#define MAXM 300005struct EDG{ int to,next;}edg[MAXM];int eid,head[MAXN];void init(){ eid=0; memset(head,-1,sizeof(head));}void addedg(int u,int v){ edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;}int vist[MAXN],flagMark[MAXN];bool bfs1(int s,int t){ memset(vist,-1,sizeof(vist)); vist[s]=-2; queue<int>q; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i!=-1; i=edg[i].next) { int v=edg[i].to; if(vist[v]==-1) vist[v]=u,q.push(v); if(v==t) { memset(flagMark,-1,sizeof(flagMark)); int mark=0; while(v!=-2) { flagMark[v]=mark; mark++; v=vist[v]; } return 1; } } } return 0;}int bfs2(int s,int t,int n){ bool flag=bfs1(s,t); if(flag==0) return n; queue<int>q; memset(vist,0,sizeof(vist)); int mark=flagMark[s]; int mustnode=s; int ans=2; vist[s]=1; q.push(s); while(!q.empty()) { while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i!=-1; i=edg[i].next) { int v=edg[i].to; if(flagMark[v]==-1) { if(vist[v]==0) vist[v]=1,q.push(v); } else if(flagMark[v]<mark) //更新必须点的指向 { mark=flagMark[v]; mustnode=v; if(mustnode==t) return ans; } } } ans++; q.push(mustnode); } return ans;}int main(){ int n,m,u,v; int s,t; while(scanf("%d%d",&n,&m)>0) { init(); while(m--) { scanf("%d%d",&u,&v); addedg(u,v); } scanf("%d%d",&s,&t); printf("%d\n",bfs2(s,t,n)); }}
0 0
- HDU 3313 Key Vertex(BFS+BFS) 求S点到T点路径的关键点
- Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)
- HDU 3313 Key Vertex(dfs + bfs)
- HDU 3313 Key Vertex (bfs)
- hdu 3313 Key Vertex BFS应用
- hdu 4771 求一点遍历所有给定点的最短路(bfs+dfs)
- 图结构练习——BFS——从起始点到目标点的最短步数(bfs)
- 图结构练习——BFS——从起始点到目标点的最短步数(邻接矩阵+BFS)
- 图结构练习——BFS——从起始点到目标点的最短步数(邻接表+BFS)
- SDUT OJ 2138 BFS 判断可达性 2139 BFS 从起始点到目标点的最短步数
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- HDU 1300 POJ 1260 Pearls (DP)
- Java:String和Date、Timestamp之间的转换
- Qt入门
- 如何用好AWS上的每一分钱
- Learning Python Day2
- HDU 3313 Key Vertex(BFS+BFS) 求S点到T点路径的关键点
- 单链表复习篇
- 接口的显示实现和隐式实现一点笔记
- 强制获取序列下一个值/当前值(oracle函数)
- Hello CSDN!!!
- Spring的ApplicationEvent的使用
- Mybatis中批量添加、修改、删除
- ThreadLocal类
- 安卓主界面UI不能实现下载