This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Arayi
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = { 'a', 'e', 'i', 'o', 'u' };
int dx[] = { 0, -1, 0, 1, -1, -1, 1, 1, 0 };
int dy[] = { -1, 0, 1, 0, -1, 1, -1, 1, 0 };
const int N = 1e5 + 30;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = log2(3);
const int T = 1000;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1) ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream& operator<<(ostream& c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template<class T>
void maxi(T& a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T& a, T b)
{
a = min(a, b);
}
int n, m, t;
int dp[N], p[N], pat[N], a[N], b[N], sm[N];
bool col[N];
vector<pir> g[N];
vii f[N];
int gp(int x)
{
if (p[x] == x) return x;
return p[x] = gp(p[x]);
}
int main()
{
fastio;
cin >> n >> m;
for (int i = 1; i <= n; i++) dp[i] = mod, p[i] = i;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
g[a].ad(MP(b, c));
g[b].ad(MP(a, c));
}
priority_queue<pir> q;
cin >> t;
for (itn i = 0; i < t; i++)
{
int a;
cin >> a;
q.push(MP(0, a));
}
vii fp;
while (!q.empty())
{
auto p = q.top();
q.pop();
if (dp[p.sc] != mod) continue;
dp[p.sc] = -p.fr;
fp.ad(p.sc);
for (auto& p1 : g[p.sc])
{
if (dp[p1.fr] != mod) continue;
q.push(MP(p.fr - p1.sc, p1.fr));
}
}
reverse(all(fp));
//for (auto p : fp) cout << p << " " << dp[p] << endl;
cin >> t;
for (int i = 0; i < t; i++)
{
cin >> a[i] >> b[i];
f[a[i]].ad(i);
f[b[i]].ad(i);
sm[i] = a[i] + b[i];
}
for (auto p : fp)
{
col[p] = 1;
for (auto& p1 : g[p])
{
if (!col[p1.fr]) continue;
int l = gp(p), r = gp(p1.fr);
if (l == r) continue;
if (f[r].size() < f[l].size()) swap(l, r);
::p[r] = l;
for (auto x : f[r])
{
if (sm[x] == -1) continue;
else if (sm[x] - r == l) pat[x] = dp[p], sm[x] = -1;
else
{
sm[x] -= r, sm[x] += l;
f[l].ad(x);
}
}
f[r].clear();
}
}
for (int i = 0; i < t; i++) cout << pat[i] << "\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... |