QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#460665 | #1072. Winter Roads | zxcmeow | 0 | 0ms | 0kb | C++20 | 3.1kb | 2024-07-02 00:03:49 | 2024-07-02 00:03:50 |
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
bool umx (T& a, T b) {
return (a < b ? a = b, 1 : 0);
}
template <typename T>
bool umn (T& a, T b) {
return (a > b ? a = b, 1 : 0);
}
using ll = long long;
using ld = long double;
const int mod = 998244353;
const int N = 2000 + 22;
int n, m, q;
int x[N], y[N], w[N], p[N];
int ans[N];
int get (int x) {
return (x == p[x] ? x : p[x] = get (p[x]));
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif //LOCAL
while (1) {
cin >> n >> m;
if (!n && !m) return 0;
for (int i = 1; i <= m; ++i) {
cin >> x[i] >> y[i] >> w[i];
}
vector<int> id (m);
iota (begin (id), end (id), 1);
sort (begin (id), end (id), [&] (int a, int b) {
return w[a] < w[b];
});
cin >> q;
for (int i = 1; i <= n; ++i) p[i] = i;
vector<array<int,3>> v1;
vector<array<int,4>> v2;
vector<int> type (q + 1);
for (int i = 1; i <= q; ++i) {
char c;
cin >> c;
if (c == 'R' || c == 'B') {
type[i] = 1;
int id, k;
cin >> id >> k;
v1.push_back({i, id, k});
} else {
int a, b, k;
cin >> a >> b >> k;
v2.push_back({i, a, b, k});
}
}
int now = 1, c0 = 0, c1 = 0;
while (now <= q) {
while (now <= q && type[now] == 1) {
w[v1[c0][1]] = v1[c0][2];
++now;
++c0;
}
vector<array<int,3>> ve;
for (int i = 1; i <= n; ++i) p[i] = i;
for (int i = 1; i <= m; ++i) ve.push_back({w[i], x[i], y[i]});
sort (rbegin (ve), rend (ve));
vector<array<int,4>> quer;
while (now <= q && type[now] == 0) {
quer.push_back (v2[c1]);
++now;
++c1;
}
sort (begin (quer), end (quer), [&] (array<int,4> a, array<int,4> b) {
return a[3] > b[3];
});
int ukaz = 0;
for (auto it : quer) {
while (ukaz < (int)ve.size() && ve[ukaz][0] >= it[3]) {
int u = get (ve[ukaz][1]), v = get(ve[ukaz][2]);
if (u != v) p[u] = v;
++ukaz;
}
int u = get (it[1]), v = get (it[2]);
if (u == v) ans[it[0]] = 2;
else ans[it[0]] = 1;
}
}
for (int i = 1; i <= q; ++i) {
if (ans[i]) cout << ans[i] - 1 << '\n';
ans[i] = 0;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
10 14 1 2 3 2 3 5 3 4 7 4 5 1000 5 6 64 6 7 5 7 8 18 8 1 1 1 9 70 2 9 50 8 9 13 9 10 34 7 10 29 5 10 75 25 S 1 1 200 S 1 6 34 S 1 6 35 R 12 35 S 1 6 35 S 1 6 36 B 12 1 S 1 6 1 S 1 6 13 S 1 6 14 R 11 18 S 1 6 18 S 1 6 19 R 7 19 S 1 6 19 R 11 19 S 1 6 19 B 11 18 S 1 6 19 B 7 18 S 1 6 19 R 11 19 S 1 6...