-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathB.cpp
114 lines (90 loc) · 2.38 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <bits/stdc++.h>
typedef int ll;
#define get(a) scanf("%lld", &a)
#define repVector(v) for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
vector<ll> adj[3005];
vector<ll> adjR[3005];
ll dist[3005][3005];
ll distR[3005][3005];
set< pair<ll,ll> > nodeR[3005], node[3005];
ll n;
void bfs1( ll p ) {
queue<ll> Q;
Q.push(p);
dist[p][p] = 0;
while(!Q.empty()) {
ll ptr = Q.front();
ll d = dist[p][ptr];
Q.pop();
node[p].insert( { d, ptr } );
while( node[p].size() > 3 )
node[p].erase( node[p].begin() );
for( ll i : adj[ptr] )
if( dist[p][i] > d+1 ) {
Q.push(i);
dist[p][i] = d+1;
}
}
}
void bfs2( ll p ) {
queue<ll> Q;
Q.push(p);
distR[p][p] = 0;
while(!Q.empty()) {
ll ptr = Q.front();
ll d = distR[p][ptr];
Q.pop();
nodeR[p].insert( { d, ptr } );
while( nodeR[p].size() > 3 )
nodeR[p].erase( nodeR[p].begin() );
for( ll i : adjR[ptr] )
if( distR[p][i] > d+1 ) {
Q.push(i);
distR[p][i] = d+1;
}
}
}
int main() {
BOOST;
ll m;
cin >> n >> m;
FOR(i,1,m) {
ll u, v;
cin >> u >> v;
adj[u].pb(v);
adjR[v].pb(u);
}
FOR(i,1,n) FOR(j,1,n) dist[i][j] = distR[i][j] = INT_MAX;
FOR(i,1,n) {
bfs1( i );
bfs2( i );
}
ll ans = -1;
vector<ll> w;
FOR(i,1,n) FOR(j,1,n)
if( i != j && dist[i][j] != INT_MAX ) {
for( auto x : nodeR[i] ) for( auto y : node[j] )
if( x.S != i && y.S != j && x.S != j && y.S != i && y.S != x.S ) {
if( dist[i][j] + x.F + y.F > ans ) {
ans = dist[i][j] + x.F + y.F;
w.clear();
w.pb(x.S); w.pb(i); w.pb(j); w.pb(y.S);
}
}
}
debug(ans);
for( ll x : w )
cout << x << ' ';
}