[Blue Bridge Cup] [2013 fourth Zhenti] risk factor (violence + dfs)

Title Description
Problem Description
Anti-Japanese War, Jizhong plain of tunnel warfare has played an important role.
Authentic among multiple sites with a channel connected to form a large network. But there are risks, when the enemy found a site, among other sites may therefore be lost contact.
We define a risk factor DF (x, y):
For two sites x and y (! X = y), if you can find a site z, z is destroyed when the enemy, x and y are none, then we say z is the key point about x, y a. Accordingly, for any pair of sites x and y, risk factor DF (x, y) represents the number of key points on these two points is between.
The task of this question is: Given the network structure, seeking risk coefficient between the two sites.
Input
Input data of the first line contains two integers n (2 <= n <= 1000), m (0 <= m <= 2000), representing the number of sites, the number of channels;
Next m lines of two integers u, v (1 <= u , v <= n; u = v!) representative of a channel;
last line, two numbers u, v, representative of interrogation risk factor DF (u, v) between two points.
Output
an integer, if the query is not in communication two outputs -1.
Sample input
. 7. 6
. 1. 3
2. 3
. 3. 4
. 3. 5
. 4. 5
. 5. 6
. 1. 6
sample output
2
Ideas: Find the cut point between the two points, emmmm began to think of a graph is seeking a cut point, but it seems not any matter much. The amount of data of 1000, we will set every point cut point, see if you can come to v from u. Such complexity is O (n ^ 2) time will not time out. All paths deep online search + go back, look for u-> v chiefs with time complexity should be about.
code show as below:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e3+100;
struct edge{
	int to,next;
}e[maxx<<1];
int head[maxx<<1],vis[maxx];
int n,m,tot;
int u,v;

inline void init()
{
	memset(head,-1,sizeof(head));
	tot=0;
}

inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void dfs(int u)
{
	vis[u]=1;
	if(u==v) return ;//一个小小的剪枝
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(vis[to]) continue;
		dfs(to);
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	init();
	int x,y;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	scanf("%d%d",&u,&v);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		if(i==u||i==v) continue;
		for(int j=1;j<=n;j++) vis[j]=0;
		vis[i]=1;
		dfs(u);
		if(vis[v]==0) ans++;
	}
	printf("%d\n",ans);
	return 0;
}

To refuel a ah, ( O ) / ~

Published 598 original articles · won praise 49 · views 40000 +

Guess you like

Origin blog.csdn.net/starlet_kiss/article/details/105219331