This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;
const int MAX_N = 5e4 + 10;
const int MAX_M = 1e5 + 10;
const int MAX_Q = 1e5 + 10;
int parent[MAX_N], sz[MAX_N];
int U[MAX_M], V[MAX_M], D[MAX_M];
int T[MAX_Q], X[MAX_Q], Y[MAX_Q];
bool changed[MAX_M];
int ans[MAX_Q];
vector <int> unchanged, update, query, tojoin[MAX_Q];
stack <int> stk;
int root(int u) {
if(u == parent[u]) {
return u;
}
return root(parent[u]);
}
void merge(int u, int v) {
u = root(u), v = root(v);
if(u == v) {
return;
}
if(sz[u] < sz[v]) {
swap(u, v);
}
sz[u] += sz[v];
parent[v] = u;
stk.push(v);
}
void rollback(int timer) {
while(stk.size() > timer) {
int v = stk.top();
stk.pop();
sz[root(v)] -= sz[v];
parent[v] = v;
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
iota(parent + 1, parent + N + 1, 1);
fill(sz + 1, sz + N + 1, 1);
for(int i = 1; i <= M; i++) {
cin >> U[i] >> V[i] >> D[i];
}
int Q;
cin >> Q;
for(int i = 1; i <= Q; i++) {
cin >> T[i] >> X[i] >> Y[i];
if(T[i] == 1) {
changed[X[i]] = true;
update.push_back(i);
}
else {
query.push_back(i);
}
}
for(int i = 1; i <= M; i++) {
if(changed[i] == false) {
unchanged.push_back(i);
}
}
for(int i = 1; i <= Q; i++) {
if(T[i] == 1) {
D[X[i]] = Y[i];
}
else {
for(auto u : update) {
if(D[X[u]] >= Y[i]) {
tojoin[i].push_back(X[u]);
}
}
}
}
sort(query.begin(), query.end(), [&](const int &a, const int &b) {
return Y[a] > Y[b];
});
sort(unchanged.begin(), unchanged.end(), [&](const int &a, const int &b) {
return D[a] > D[b];
});
int j = 0;
for(auto i : query) {
while(j < unchanged.size() and D[unchanged[j]] >= Y[i]) {
merge(U[unchanged[j]], V[unchanged[j]]);
j++;
}
int timer = stk.size();
for(auto e : tojoin[i]) {
merge(U[e], V[e]);
}
ans[i] = sz[root(X[i])];
rollback(timer);
}
for(int i = 1; i <= Q; i++) {
if(T[i] == 2) {
cout << ans[i] << '\n';
}
}
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'void rollback(int)':
bridges.cpp:39:22: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
39 | while(stk.size() > timer) {
| ~~~~~~~~~~~^~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:101:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | while(j < unchanged.size() and D[unchanged[j]] >= Y[i]) {
| ~~^~~~~~~~~~~~~~~~~~
# | 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... |