This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ve vector
typedef long long ll;
int C;
ve<ve<pair<int, int>>> G;
ve<int> Comp, D;
void dfs(int u, int d, int c) {
int v, t;
Comp[u] = c;
D[u] = d;
for (pair<int, int> e : G[u]) {
tie(v, t) = e;
if (Comp[v] == 0) dfs(v, d + t, c);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k, n, m, o, a, b, t;
cin >> k >> n >> m >> o;
G = ve<ve<pair<int, int>>>(n);
Comp = ve<int>(n);
D = ve<int>(n);
for (int i = 0; i < m; i++) {
cin >> a >> b >> t;
G[a].push_back({b, t});
}
C = 0;
for (int i = 0; i < n; i++) {
if (G[i].size() == 1) {
if (Comp[i] == 0) {
C++;
dfs(i, 0, C);
}
}
}
for (int i = 0; i < o; i++) {
cin >> a >> b;
if (Comp[a] == Comp[b]) cout << abs(D[a] - D[b]) << endl;
else cout << -1 << endl;
}
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... |