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>
typedef long long int ll;
#define INF 1000000007
#define INF2 998244353
#define N (ll)(1e5+ 5)
using namespace std;
//#define int ll
#define lsb(x) (x & (-x))
const int K = 320;
int n, m, cur=0, g, h, q;
int u[N], v[N], d[N];
int x[N], y[N], ans[N];
bool used[N];
pair<int, int> upd[N], qu[N];
vector<int> lis[N];
int p[N], sz[N];
vector<int> tb;
int get(int x){
while(x != p[x])x = p[x];
return x;
}
void unite(int x, int y){
int a = get(x), b = get(y);
if(a == b)return;
if(sz[a] < sz[b]){
swap(a, b);
}
p[b] = a;
sz[a] += sz[b];
tb.push_back(b);
}
void revert(int x){
while(tb.size() > x){
int t = tb.back();
sz[p[t]] -= sz[t];
p[t] = t;
tb.pop_back();
}
}
bool c1(int x, int y){
return d[x] > d[y];
}
bool c2(int x, int y){
return qu[x].second > qu[y].second;
}
void solve(){
cin >> n >> m;
for(int i=1; i<=m; i++){
cin >> u[i] >> v[i] >> d[i];
}
cin >> q;
for(int i=1; i<=q; i++){
cin >> g;
if(g == 1){
cin >> g >> h;
upd[i] = {g, h};
}
else{
cin >> g >> h;
qu[i] = {g, h};
}
}
for(int i=0; i<= q / K; i++){
int l = max(1, i * K), r = min((i + 1) * K, q + 1);
memset(used, 0, m + 1);
fill(sz + 1, sz + n + 1, 1);
iota(p + 1, p + n + 1, 1);
vector<int> ch, ask, uch;
for(int j=l; j<r; j++){
if(upd[j].first){
used[upd[j].first] = 1;
ch.push_back(j);
}
else{
ask.push_back(j);
}
}
for(int j=l; j<r; j++){
if(qu[j].first){
for(int u : ch){
if(d[upd[u].first] >= qu[j].second){
lis[j].push_back(upd[u].first);
}
}
}
else{
d[upd[j].first] = upd[j].second;
}
}
for(int j=1; j<=m; j++){
if(!used[j])uch.push_back(j);
}
sort(uch.begin(), uch.end(), c1);
sort(ask.begin(), ask.end(), c2);
int p1 = 0;
for(int j : ask){
while(p1 < uch.size() && qu[j].second <= d[uch[p1]]){
unite(u[uch[p1]], v[uch[p1]]);
p1++;
}
int prev = tb.size();
for(int l : lis[j]){
unite(u[l], v[l]);
}
ans[j] = sz[get(qu[j].first)];
revert(prev);
}
}
for(int i=1; i<=q; i++){
if(qu[i].first)cout << ans[i] << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
//cin>>T;
while (T--){
solve();
}
}
Compilation message (stderr)
bridges.cpp: In function 'void revert(int)':
bridges.cpp:38:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
38 | while(tb.size() > x){
| ~~~~~~~~~~^~~
bridges.cpp: In function 'void solve()':
bridges.cpp:111:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while(p1 < uch.size() && qu[j].second <= d[uch[p1]]){
| ~~~^~~~~~~~~~~~
# | 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... |