这道题主要是求缩小版的割点,可以使用dfs从两个点中的一个开始遍历,然后达到终点后将经过的所有点次数加一,如果从u到v的路径个数等于x点经过的次数,就说明x是关键点
import collections
n,m = map(int,input().split()) #输入站点数与通道数
seen = [0 for i in range(n+1)]
count = [0 for i in range(n+1)]
map1 = collections.defaultdict(list) #
for i in range(m):
u,v = map(int,input().split())
map1[u].append(v)
map1[v].append(u)
x,y = map(int,input().split())
seen[x]=True
countpath=0
path=[]
def dfs(point):
global countpath,path
if(point==y): #如果找到
countpath+=1
for j in path:
count[j]+=1 #把经过的点遍历一遍
seen[point] = True
for i in map1[point]:
if(not seen[i]):
path.append(i)
dfs(i)
path.pop()
seen[point]=False
dfs(x)
ans=0
for i in range(1,n+1):
if(count[i]==countpath):
ans+=1
print(ans-1)