Submission #714772

#TimeUsernameProblemLanguageResultExecution timeMemory
714772KINGToll (BOI17_toll)C++14
0 / 100
102 ms7380 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...