이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
const int N = 5e4 + 100;
int p[N], rnk[N];
inline void build(int n) {
for (int i = 0; i < n; ++i) {
p[i] = i, rnk[i] = 1;
}
}
inline int par(int v) {
if (p[v] == v) return v;
return (p[v] = par(p[v]));
}
inline void uni(int a, int b) {
a = par(a), b = par(b);
if (a == b) return;
if (rnk[a] > rnk[b]) swap(a, b);
p[a] = b, rnk[b] += rnk[a];
}
int bg = 0, en = 0;
int Q[N];
int used[N];
ve<int> gr[N];
inline int bfs(int v) {
int ans = 0;
used[v] = 1; bg = en = 0;
Q[en++] = v;
while (bg != en) {
int v = Q[bg++];
ans += rnk[v];
for (const auto &to : gr[v]) {
if (used[to]) continue;
used[to] = 1;
Q[en++] = to;
}
}
return ans;
}
constexpr int b = 500;
inline void solve() {
int n, m;
cin >> n >> m;
ve<array<int,3>> e(m);
for (int i = 0; i < m; ++i) {
int v, u, w;
cin >> v >> u >> w, --v, --u;
e[i] = {v, u, -w};
}
int q;
cin >> q;
ve<array<int,3>> op(q);
for (int i = 0; i < q; ++i) {
int t;
cin >> t;
if (t == 1) {
int id, w;
cin >> id >> w;
op[i] = {1, --id, -w};
}
else {
int v, w;
cin >> v >> w;
op[i] = {2, --v, -w};
}
}
int p = 0, pq = 0;
ve<array<int,3>> que(q);
ve<ve<array<int,3>>> edges(q);
ve<array<int,3>> nch(m);
ve<bool> used_in_block(m);
ve<int> answer(q, -1);
for (int l = 0; l < q; l += b) {
build(n);
int r = min(q - 1, l + b - 1);
fill(all(used_in_block), 0);
for (int i = l; i <= r; ++i) {
const auto &[type, a, b] = op[i];
if (type == 1) used_in_block[a] = 1;
}
p = 0;
for (int i = 0; i < m; ++i) {
if (!used_in_block[i]) {
const auto &[v, u, w] = e[i];
nch[p++] = {w, v, u};
}
}
pq = 0;
for (int i = l; i <= r; ++i) {
const auto &[type, a, b] = op[i];
if (type == 2) {
que[pq++] = {b, a, i};
for (int j = l; j <= r; ++j) {
const auto &[tj, aj, bj] = op[j];
if (tj == 1) edges[i].pb(array<int,3>{e[aj][0], e[aj][1], e[aj][2]});
}
}
else {
e[a][2] = b;
}
}
sort(que.begin(), que.begin() + pq);
sort(nch.begin(), nch.begin() + p);
/*
cout << "block " << l + 1 << " " << r + 1 << ":" << '\n';
cout << "not used: " << '\n';
for (int i = 0; i < p; ++i) {
auto &[w, v, u] = nch[i];
cout << w << " " << v + 1 << " " << u + 1 << '\n';
}
cout << "queries: " << '\n';
for (int i = 0; i < pq; ++i) {
auto &[w, v, id] = que[i];
cout << w << " " << v + 1 << " " << id + 1 << '\n';
}
cout << '\n';
*/
int ptr = 0;
for (int id = 0; id < pq; ++id) {
const auto &[w, v, i] = que[id];
while (ptr < p && nch[ptr][0] <= w) {
uni(nch[ptr][1], nch[ptr][2]);
++ptr;
}
for (auto &[a, b, W] : edges[i]) {
if (W > w) continue;
a = par(a), b = par(b);
gr[a].pb(b);
gr[b].pb(a);
}
answer[i] = bfs(par(v));
used[par(v)] = 0;
for (auto &[a, b, w] : edges[i]) {
gr[a].clear(); gr[a].shrink_to_fit();
gr[b].clear(); gr[b].shrink_to_fit();
used[a] = used[b] = 0;
}
}
}
for (int i = 0; i < q; ++i) if (~answer[i]) cout << answer[i] << '\n';
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |