This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll 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 = 5005;
int n, m, k;
double sum[N];
vector<int> g[N], s[N];
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(int a, vector<int> d, vector<int> &pa){
for(int u:s[a])
for(int v:g[u])
if(d[v] + 1 == d[u])
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;
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[i].pb(u);
pa[a] = 1;
pb[b] = 1;
calc(i, da, pa);
calc(i, db, pb);
for(int u:s[i]){
sum[u] = pa[u] * pb[u] / pa[b];
if(sum[u] > tmp){
tmp = sum[u];
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;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |