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 taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 5e4 + 10;
const int maxM = 1e5 + 10;
const int maxQ = 1e5 + 10;
const int SQRT = 316;
int n, m, q;
struct TEdge{
int u, v, w, idx;
};
TEdge e[maxM];
struct TQuery{
int t, x, y;
};
TQuery query[maxQ];
struct TDSU{
vector <pii> save;
vector <int> lab;
TDSU(){}
TDSU(int _n){
lab.resize(_n, -1);
}
inline void resize(int _n){
lab.resize(_n, -1);
}
inline int find_set(int u){
if (lab[u] < 0) return u;
return lab[u] = find(lab[u]);
}
inline int find(int u){
if (lab[u] < 0) return u;
return find_set(lab[u]);
}
inline void merge(int u, int v){
if (lab[u] > lab[v]) swap(u, v);
save.pb({v, lab[v]});
lab[u] += lab[v];
lab[v] = u;
}
inline void undo(){
int v = save.back().fi;
int u = lab[v];
lab[v] = save.back().se;
lab[u] -= lab[v];
save.pop_back();
}
inline void clear(){
lab.clear();
save.clear();
}
inline void reset(int _n){
clear();
resize(_n);
}
inline void roll_back(int _n){
while (save.size() > _n) undo();
}
};
TDSU dsu;
bool changed[maxM];
int ans[maxN];
vector <int> join[maxQ];
void Init(){
cin >> n >> m;
for (int i = 1; i <= m; ++i){
cin >> e[i].u >> e[i].v >> e[i].w;
e[i].idx = i;
}
vector <int> block_idx;
ll cnt = 0;
block_idx.pb(0);
cin >> q;
for (int i = 1; i <= q; ++i){
cin >> query[i].t >> query[i].x >> query[i].y;
if (!--query[i].t) ++cnt;
if (cnt >= SQRT){
block_idx.pb(i);
cnt = 0;
}
}
block_idx.pb(q);
for (int _ = 0; _ < block_idx.size() - 1; ++_){
int l = block_idx[_] + 1, r = block_idx[_ + 1];
dsu.reset(n + 1);
vector <int> ask, unchanged, unstable;
for (int i = l; i <= r; ++i){
if (query[i].t) ask.pb(i);
else{
changed[query[i].x] = 1;
unstable.pb(i);
}
}
for (int i = 1; i <= m; ++i){
if (!changed[i]) unchanged.pb(i);
else changed[i] = 0;
}
for (int i = l; i <= r; ++i){
if (!query[i].t) e[query[i].x].w = query[i].y;
else{
join[i - l].clear();
for (int j: unstable){
if (e[query[j].x].w >= query[i].y) join[i - l].pb(query[j].x);
}
}
}
sort(ask.begin(), ask.end(),[&](const int &i, const int &j){
return query[i].y > query[j].y;
});
sort(unchanged.begin(), unchanged.end(),[&](const int &i, const int &j){
return e[i].w > e[j].w;
});
int j = 0;
for (int i: ask){
while (j < unchanged.size() && e[unchanged[j]].w >= query[i].y){
int x = dsu.find(e[unchanged[j]].u), y = dsu.find(e[unchanged[j]].v);
if (x != y){
dsu.merge(x, y);
}
++j;
}
int prev_sz = dsu.save.size();
for (int j: join[i - l]){
int x = dsu.find(e[j].u), y = dsu.find(e[j].v);
if (x != y){
dsu.merge(x, y);
}
}
ans[i] = - dsu.lab[dsu.find(query[i].x)];
dsu.roll_back(prev_sz);
}
}
for (int i = 1; i <= q; ++i){
if (query[i].t) cout << ans[i] << " ";
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message (stderr)
bridges.cpp: In member function 'void TDSU::roll_back(int)':
bridges.cpp:85:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
85 | while (save.size() > _n) undo();
| ~~~~~~~~~~~~^~~~
bridges.cpp: In function 'void Init()':
bridges.cpp:113:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int _ = 0; _ < block_idx.size() - 1; ++_){
| ~~^~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:145:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | while (j < unchanged.size() && e[unchanged[j]].w >= query[i].y){
| ~~^~~~~~~~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
172 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |