Task: | Budgetting bridges |
Sender: | TyƤmiesklubi |
Submission time: | 2024-11-16 16:22:35 +0200 |
Language: | C++ (C++20) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.23 s | details |
#7 | ACCEPTED | 0.29 s | details |
#8 | ACCEPTED | 0.29 s | details |
#9 | ACCEPTED | 0.23 s | details |
#10 | ACCEPTED | 0.32 s | details |
#11 | ACCEPTED | 0.51 s | details |
Code
#include <bits/stdc++.h> using namespace std; struct E { int u, v; long c; }; const int N = 1e5+1; int dsu_p[N], dsu_s[N]; int dsu_get(int x) { while (dsu_p[x] != x) x = dsu_p[x]; return x; } struct U { int a, b, p, s; }; U dsu_j(int a, int b) { a = dsu_get(a); b = dsu_get(b); if (a == b) return {0, 0, 0, 0}; // b -> a if (dsu_s[b] > dsu_s[a]) swap(a, b); U r{a, b, dsu_p[b], dsu_s[a]}; dsu_p[b] = a; dsu_s[a] += dsu_s[b]; return r; } void do_u(U x) { if (x.a == 0) return; dsu_p[x.b] = x.p; dsu_s[x.a] = x.s; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; ++i) dsu_p[i] = i, dsu_s[i] = 1; E e[m]; int ord[m]; map<pair<int, int>, int> vtoi; for (int i = 0; i < m; ++i) { ord[i] = i; cin >> e[i].u >> e[i].v >> e[i].c; vtoi[{e[i].u, e[i].v}] = i; } sort(ord, ord+m, [&](int a, int b) {return e[a].c < e[b].c;}); map<long, map<int, vector<int>>> ctoe; bool ans[k]; for (int i = 0; i < k; ++i) { ans[i] = 1; int b; cin >> b; for (int j = 0; j < b; ++j) { int u, v; cin >> u >> v; int ei = vtoi[{u, v}]; long c = e[ei].c; ctoe[c][i].push_back(ei); } } for (int ei = 0; ei < m; ) { long cc = e[ord[ei]].c; for (const auto& [pi, es] : ctoe[cc]) { vector<U> undos; for (int pei : es) { int a = dsu_get(e[pei].u), b = dsu_get(e[pei].v); if (a == b) { ans[pi] = false; break; } undos.push_back(dsu_j(a, b)); } for (auto& x : undos) do_u(x); } for (; ei < m && e[ord[ei]].c == cc; ++ei) { int a = dsu_get(e[ord[ei]].u), b = dsu_get(e[ord[ei]].v); dsu_j(a, b); } } for (int i = 0; i < k; ++i) { if (ans[i]) cout << "YES\n"; else cout << "NO\n"; } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
3 3 2 1 2 1 1 3 1 2 3 2 1 ... |
correct output |
---|
YES NO |
user output |
---|
YES NO |
Test 2
Verdict: ACCEPTED
input |
---|
3 3 2 1 2 1 1 3 1 2 3 1 3 ... |
correct output |
---|
NO YES |
user output |
---|
NO YES |
Test 3
Verdict: ACCEPTED
input |
---|
10 20 10 5 10 1 10 1 1 4 10 1 3 1 1 ... |
correct output |
---|
YES YES YES YES YES ... |
user output |
---|
YES YES YES YES YES ... |
Test 4
Verdict: ACCEPTED
input |
---|
10 20 10 5 2 1 10 6 3 5 4 1 2 3 3 ... |
correct output |
---|
NO NO YES NO NO ... |
user output |
---|
NO NO YES NO NO ... |
Test 5
Verdict: ACCEPTED
input |
---|
10 20 10 6 1 3 9 3 1 9 5 4 1 2 1 ... |
correct output |
---|
YES YES NO NO NO ... |
user output |
---|
YES YES NO NO NO ... |
Test 6
Verdict: ACCEPTED
input |
---|
100000 200000 50 96703 90063 1 17270 97269 1 71482 60904 1 59756 21609 1 ... |
correct output |
---|
YES YES YES YES YES ... |
user output |
---|
YES YES YES YES YES ... |
Test 7
Verdict: ACCEPTED
input |
---|
100000 200000 30000 22200 5519 1 83133 20672 1 91862 97945 1 8983 61175 1 ... |
correct output |
---|
YES YES YES YES YES ... |
user output |
---|
YES YES YES YES YES ... |
Test 8
Verdict: ACCEPTED
input |
---|
100000 200000 5000 22200 5519 9 83133 20672 4 91862 97945 5 8983 61175 4 ... |
correct output |
---|
NO NO NO NO NO ... |
user output |
---|
NO NO NO NO NO ... |
Test 9
Verdict: ACCEPTED
input |
---|
100000 200000 5000 22200 5519 9 83133 20672 4 91862 97945 5 8983 61175 4 ... |
correct output |
---|
YES NO NO YES YES ... |
user output |
---|
YES NO NO YES YES ... |
Test 10
Verdict: ACCEPTED
input |
---|
100000 200000 2000 22200 5519 870732304 83133 20672 363736896 91862 97945 488411190 8983 61175 396736607 ... |
correct output |
---|
NO NO NO NO NO ... |
user output |
---|
NO NO NO NO NO ... |
Test 11
Verdict: ACCEPTED
input |
---|
100000 200000 60000 22200 5519 9 83133 20672 4 91862 97945 5 8983 61175 4 ... |
correct output |
---|
NO NO YES NO NO ... |
user output |
---|
NO NO YES NO NO ... |