# 代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 3e3+5;
const int M = 5e3+5;
const int INF = -1;

struct Gragh {
struct Edge {
int to, nxt;
Edge(int to, int nxt) : to(to), nxt(nxt) {}
Edge() {}
};

int dis[N][N], head[N], fw[N][3], bk[N][3], ec;
Edge e[M];

void addEdge(int from, int to) {
}
void init(int n) {
ec = 0;
for (int i = 0; i <= n; i++) head[i] = -1;
memset(dis, INF, sizeof dis);
}
void bfs(int st) {
queue<int> q;
dis[st][st] = 0;
q.push(st);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = head[cur]; ~i; i = e[i].nxt) {
int to = e[i].to;
if (dis[st][to] == INF) {
dis[st][to] = dis[st][cur] + 1;
q.push(to);
}
}
}
}

void solve(int n) {
for (int i = 0; i < n; i++) {
bfs(i);
}
memset(fw, INF, sizeof fw);
memset(bk, INF, sizeof bk);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || dis[i][j] == INF) continue;
int cur = j;
for (int k = 0; k < 3; k++) {
if (fw[i][k] == INF || dis[i][cur] > dis[i][fw[i][k]]) {
swap(cur, fw[i][k]);
}
}
}
for (int j = 0; j < n; j++) {
if (i == j || dis[j][i] == INF) continue;
int cur = j;
for (int k = 0; k < 3; k++) {
if (bk[i][k] == INF || dis[cur][i] > dis[bk[i][k]][i]) {
swap(cur, bk[i][k]);
}
}
}
}
int ans = -1, res[4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || dis[i][j] == INF) continue;
for (int ii = 0; ii < 3; ii++) {
int k = fw[j][ii];
if (k == INF || dis[j][k] == INF) continue;
if (k == i || k == j) continue;
for (int jj = 0; jj < 3; jj++) {
int l = bk[i][jj];
if (l == INF || dis[l][i] == INF) continue;
if (l == i || l == j || l == k) continue;
int sum = dis[l][i] + dis[i][j] + dis[j][k];
if (ans < sum) {
ans = sum;
res[0] = l, res[1] = i, res[2] = j, res[3] = k;
}
}
}
}
}
for (int i = 0; i < 4; i++) {
printf("%d%c", res[i]+1, i==3 ? '\n' : ' ');
}
}
};

Gragh g;
int main() {
int n, m, u, v;
scanf("%d%d", &n, &m);
g.init(n);
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
}
g.solve(n);
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

ned_chu

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24

11-18 212
02-02 511