题目大意:
给定你一个有n个点m条边的无向图,中间有且仅有一个环。让你求出图上每一个点离这个环的最短距离。
题目分析&解法:
①首先找到环
②从环上的每一个节点出发,求出与其他节点的距离,并求出最小值
写之前就感觉这题很水,n最大是3000,随便过
#AC Code1:
//From yh2021ZJJ
//https://codeforces.com/contest/131/submission/162698268
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> pii;
int n,m,idx[3005];
bool vis[3005];
vector<int> e[3005],go,cir;
bool dfs(int now,int fa){
if(vis[now]){
int pos=0;
while(go[pos]!=now) pos++;
for(int i=pos;i<go.size();i++) cir.push_back(go[i]);
return true;
}
vis[now]=1;
go.push_back(now);
for(int i=0;i<e[now].size();i++) if(e[now][i]!=fa)
if(dfs(e[now][i],now)) return true;
int pos=0;