#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define freo(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
#define FOR(i, a, b) for(int i=(a),_b = (b); i<=_b; i++)
#define FORD(i, a, b) for(int i=(a),_b = (b); i>=_b; i--)
#define bit(x, i) ((x >> i) & 1)
#define oo 1e18
#define pii pair<int, int>
#define debug cout<<"VUDEPTRAI\n";
using namespace std;
// mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
// ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); }
const int N = 5e3 + 5;
int n, m, k;
vector<int> g[N], s;
void dijkstra(int u, vector<int> &d){
priority_queue<pii> pq;
d[u] = 0;
pq.push({0, u});
while(!pq.empty()){
pii top = pq.top();
pq.pop();
int u = top.second;
if(top.first > d[u]) continue;
for(int v:g[u])
if(d[v] > d[u] + 1){
d[v] = d[u] + 1;
pq.push({d[v], v});
}
}
}
void calc(vector<int> d, vector<int> &pa){
for(int u:s)
for(int v:g[u])
if(d[u] == d[v] + 1)
pa[u] += pa[v];
}
void sub6(){
FOR(i, 1, m){
int u, v; cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
cin>>k;
int ans = -1, tmp = INT_MIN;
FOR(i, 1, k){
int a, b; cin>>a>>b;
s.clear();
vector<int> da(N, INT_MAX);
vector<int> db(N, INT_MAX);
vector<int> pa(N, 0);
vector<int> pb(N, 0);
dijkstra(a, da);
dijkstra(b, db);
FOR(u, 0, n)
if(da[u] + db[u] == da[b]) s.pb(u);
pa[a] = 1;
pb[b] = 1;
calc(da, pa);
calc(db, pb);
for(int u:s) if(pa[b] != 0){
int sum = pa[u] * pb[u] / pa[b];
if(sum > tmp){
tmp = sum;
ans = u;
}
}
// for(int u:s[i]) cout<<u<<" ";
// cout<<'\n';
}
cout<<ans;
}
void solve(){
cin>>n>>m;
sub6();
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
// freo("");
solve();
// cerr << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |