Key VertexTime
limit:10000/5000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1347 Accepted Submission (s): 305
Problem descriptionyou need walking from vertex S to vertex T in a graph. If you remove one vertex which stops the from walking from S to T, that's vertex we call as key vertex. Now is given a directed graph, S and T, and you should tell us how many key vertexes is there in the graph.
Notice that s and T is key vertexes and if S cannot walking to T by the directed edge in the initial graph then Al L vertexes becomes to key vertexes.
Inputthe input consists of multiply test cases. The first line of all test case contains-integers, n (0 <= n <= 100000), m (0 <= M <= 300000), which is th E Number of vertexes and the number of edge. Each of the next m lines consists of the integers, U, v (0 <= u, v < n; u! = v), indicating there exists an edge from Vertex u to vertex v. There might is multiple edges but no loops. The last line of all test case contains-integers, s, t (0 <= S, t < n, S! = t).
Outputoutput the number of the key vertexes in a, single, and the test case.
Sample Input
6 60 11 21 32 43 44 50 5
Sample Output
4
Authormomodi
Sourcehdoj Monthly contest–2010.02.06
RECOMMENDWXL | We have carefully selected several similar problems for you:3251 3310 3311 3314 3376
Test instructions: N point m edge of the graph, asked how many points to remove this point and the connected side after the beginning and end of the no longer connect.
Ideas: It is easy to think of Tarjan algorithm to cut points, but a little thought will know that the wrong, because the Tarjan algorithm is the entire plot of the cut point, and here the topic only requires to make the starting point and the endpoint is not connected points. Then we first use SPFA to find a shortest path, then the required "cut point" must be in this path, think carefully will know. After finding the shortest path from the starting point DFS, until you find the furthest distance from start and point V on the shortest path, then V is a cut point, then update start, make Start=v, repeat the above DFS until the end point. Why did you do it? You can draw a picture on the paper and you will understand it quickly.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define PI ACOs ( -1.0) #define EPS 1e-6# Define Lson rt<<1,l,mid#define Rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE ( I,A,B) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i-- ) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;const int MAXN = 100010;const int MAXM = 300010;constint N = 1005;int n,m;struct edge{int u,v,next;} Edge[maxm];int head[maxn],dist[maxn],pre[maxn];bool inq[maxn],mark[maxn],vis[maxn];int num,start,End;void init () { num=0; MEM (head,-1);} void Addedge (int u,int v) {edge[num].u=u; Edge[num].v=v; Edge[num].next=head[u]; head[u]=num++;} BOOL SPFA (int s,int t) {mem (inq,false); MEM (Mark,false); MEM (Dist,inf); MEM (pre,-1); dist[s]=0; Inq[s]=true; queue<int>q; Q.push (s); while (! Q.empty ()) {int U=q.front (); Q.pop (); Inq[u]=false; for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].v; if (dist[v]>dist[u]+1) {dist[v]=dist[u]+1; Pre[v]=u; if (!inq[v]) {inq[v]=true; Q.push (v); }}}} if (Dist[t]>=inf) return false; int x=t; MEM (Mark,false); while (x!=-1) {mark[x]=tRue X=PRE[X]; } return true; void Dfs (int u) {if (Vis[u]) return; Vis[u]=true; for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].v; if (Mark[v]&&dist[v]>=dist[start])//Because there are heavy edges, so must add equal, pit me for a long time =-= {start=v; Continue } dfs (v); } return; void Dfs (int u)//Another notation//{//for (int i=head[u];~i;i=edge[i].next)//{//int v=edge[i].v;//if (vi S[V]) continue;//vis[v]=true;//if (Mark[v]&&dist[v]>dist[start])//{//start= v;//continue;//}//dfs (v);//}//return;//}int main () {#ifndef Online_judge freopen ("C :/users/lyf/desktop/in.txt "," R ", stdin); #endif int i,j,u,v; while (~SFF (n,m)) {init (); for (i=0;i<m;i++) {SFF (u,v); Addedge (U,V); } SFF (Start,end); if (! SPFA (start,end)) {PF ("%d\n", N); COntinue; } int ans=0; MEM (Vis,false); while (start!=end) {//printf ("++%d\n", start); DFS (start);//printf ("--%d\n", start); ans++; } printf ("%d\n", ans+1); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Key Vertex (hdu 3313 Spfa+dfs The cut point from the start to the end path)