PAT(Advanced Level) Practice(with python)——1013 Battle Over Cities(最后一个样例无能为了,输入太多了python真救不了)

PAT(Advanced Level) Practice(with python)——1013 Battle Over Cities(最后一个样例无能为了,输入太多了python真救不了)_第1张图片

Code

N,M,K = map(int, input().split())

# 连通子图数量

mapper = [[] for i in range(N+1)]
for m in range(M):
    a,b = map(int,input().split())
    mapper[a].append(b)
    mapper[b].append(a)

query = list(map(int,input().split()))
# print(query)
def dfs(cur):
    global q
    vis[cur]=1
    for i in mapper[cur]:
        if vis[i]==0 and i!=q:
            dfs(i)

for qq in query:
    res = 0
    q = qq    
    vis = [0 for i in range(N+1)]
    for i in range(1,N+1):
        if vis[i]==0 and i!=q:
            res+=1
            dfs(i)
    print(res-1)

    

PAT(Advanced Level) Practice(with python)——1013 Battle Over Cities(最后一个样例无能为了,输入太多了python真救不了)_第2张图片

你可能感兴趣的:(PAT,python,开发语言,算法,深度优先)