#include<bits/stdc++.h>
#define NOT_STONKS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int maxn = 2e5 + 10; //4e6 + 10; //3e5 + 10;
const int mod = 1e9 + 7; //998244353;
typedef long long ll;
int k, n, m, o, arr[maxn], root[maxn];
ll psum[maxn];
vector< pair<int, int> > G[maxn];
bool marked[maxn];
void dfs(int v) {
marked[v] = true;
if (!G[v].empty()) {
root[G[v].back().first] = root[v];
dfs(G[v].back().first);
}
}
int main() {
NOT_STONKS;
cin >> k >> n >> m >> o;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({ v, w });
arr[v] = w;
}
for (int i = 1; i <= n; i++) if (!marked[i]) {
root[i] = i;
dfs(i);
}
for (int i = 1; i <= n; i++) psum[i] = psum[i - 1] + arr[i];
while (o--) {
int a, b;
cin >> a >> b;
if (root[a] != root[b]) {
cout << -1 << endl;
continue;
}
cout << psum[b] - psum[a] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7380 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
7320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7380 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |