挺水的题,顶多橙色。。。
40 pts 解法
直接跑 Floyd。
时间复杂度:
空间复杂度:
100 pts 解法
观察发现每条边边权都是相同的,所以可以跑 N 遍 bfs ,每次记录最大值。
时间复杂度:
空间复杂度:
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define int long long
using namespace std;
const int N = 2e3+10, M = 4e3+10;
int n, m, h[N], tot;
struct edge{int to, nxt;}e[M];
void add(int u, int v){e[++tot] = {v, h[u]}, h[u] = tot;}
bool vis[N];int dis[N], mx[N];
void bfs(int s){
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
queue<int> q;
q.push(s);dis[s] = 0, vis[s] = 1;
while(!q.empty()){
int u = q.front();q.pop();
for(int i = h[u]; i; i = e[i].nxt){
int v = e[i].to;
if(vis[v]) continue;
dis[v] = dis[u]+1;vis[v] = 1;
q.push(v);
}
}
for(int i = 1; i <= n; ++i) mx[s] = max(mx[s], dis[i]);
}
signed main(){
scanf("%d%d", &n, &m);
memset(mx, -1, sizeof(mx));
for(int i = 1, u, v; i <= m; ++i){
scanf("%d%d", &u, &v);
add(u, v);add(v, u);
}
for(int i = 1; i <= n; ++i) bfs(i);
int ans = 0, nw = 2e9;
for(int i = 1; i <= n; ++i)
if(mx[i] < nw) nw = mx[i], ans = i;
cout << ans;
return 0;
}