DFS。
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
vector<vector<int>> vec;
int vis[10003];
int main() {
int n,m,k;
cin>>n>>m>>k;
vec.resize(n+10);
while(m--) {
int a,b;
cin>>a>>b;
vec[a].push_back(b);
vec[b].push_back(a);
}
for(int i=1; i<=n; i++) {
sort(vec[i].begin(),vec[i].end());
}
while(k--) {
int t;
cin>>t;
int output = 0;
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(t);
vis[t]=1;
while(!q.empty()) {
int ssize = q.size();
int ans = inf;//这一层中的最小
for(int i=0; i<ssize; i++) { //q当中的节点
int ele = q.front();
q.pop();
for(int j=0; j<vec[ele].size(); j++) {
if(vis[vec[ele][j]]==0) {
q.push(vec[ele][j]);
vis[vec[ele][j]]=1;
ans=min(ans,vec[ele][j]);
}
}
}
if(ans != inf) output = ans;
}
cout << output << '\n';
}
return 0;
}
标签:cin,int,喊山,back,vis,vec,push,008,L3
From: https://www.cnblogs.com/chengyiyuki/p/18119824