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 pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct dsu{
int n;
vector<int> sz, p;
stack<tuple<int, int, int>> st;
dsu(int nn){
n = nn;
sz.resize(n, 1);
p.resize(n);
for(int i = 0; i < n; i++) p[i] = i;
}
int get(int x){
if(p[x] != x) return get(p[x]);
return p[x];
}
int unite(int a, int b){
a = get(a), b = get(b);
if(a == b) return 0;
if(sz[b] > sz[a]) swap(a, b);
st.push({a, b, sz[b]});
sz[a]+=sz[b];
sz[b] = 0;
p[b] = a;
return 1;
}
void rollback(){
auto [a, b, ss] = st.top(); st.pop();
sz[a]-=ss;
sz[b] = ss;
p[b] = b;
}
};
int sq;
bool comp(tuple<ll, int, int, vector<pair<int, int>>> a, tuple<ll, int, int, vector<pair<int, int>>> b){return get<0>(a) > get<0>(b);}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<tuple<ll, int, int>> edge;
for(int i = 0; i < m; i++){
int a, b; ll w; cin >> a >> b >> w; a--, b--;
edge.pb({w, a, b});
}
int q; cin >> q;
sq = sqrt(q+0.0);
vector<vector<tuple<ll, int, int, int>>> query;
query.pb({});
int inx = 0;
for(int i = 0; i < q; i++){
int tt, s; ll w; cin >> tt >> s >> w; s--;
int in = -1;
if(tt == 2) in = inx++;
if(query.back().size() >= sq) query.pb({});
query.back().pb({tt, s, w, in});
}
vector<int> ans(inx);
for(auto qq: query){
dsu ds(n);
vector<tuple<ll, int, int, vector<pair<int, int>>>> calc;
vector<bool> ucng(m, 1);
vector<tuple<ll, int, int>> edges;
vector<tuple<int, ll, int, int>> cng;
for(auto [tt, s, w, in]: qq) if(tt == 1) ucng[s] = 0;
for(int i = 0; i < m; i++){
if(ucng[i]) edges.pb(edge[i]);
else cng.pb({i, get<0>(edge[i]), get<1>(edge[i]), get<2>(edge[i])});
}
map<int, int> mp;
for(int i = 0; i < cng.size(); i++) mp[get<0>(cng[i])] = i;
for(auto [tt, s, w, in]: qq){
if(tt == 1){
auto &[ss, ww, a, b] = cng[mp[s]];
ww = w;
}
else{
vector<pair<int, int>> upd;
for(auto [inn, ww, a, b]: cng) if(ww >= w) upd.pb({a, b});
calc.pb({w, s, in, upd});
}
}
sort(edges.begin(), edges.end());
sort(calc.begin(), calc.end(), comp);
for(auto [w, s, in, upd]: calc){
while(!edges.empty() && get<0>(edges.back()) >= w){
auto [ww, a, b] = edges.back(); edges.pop_back();
ds.unite(a, b);
}
int k = 0;
for(auto [a, b]: upd) k+=ds.unite(a, b);
ans[in] = ds.sz[ds.get(s)];
while(k--) ds.rollback();
}
for(auto [tt, s, w, in]: qq) if(tt == 1){
get<0>(edge[s]) = w;
}
}
for(int x: ans) cout << x << "\n";
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:59:32: warning: comparison of integer expressions of different signedness: 'std::vector<std::tuple<long long int, int, int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
59 | if(query.back().size() >= sq) query.pb({});
| ~~~~~~~~~~~~~~~~~~~~^~~~~
bridges.cpp:75:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, long long int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < cng.size(); i++) mp[get<0>(cng[i])] = 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... |