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 int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, m;
int block_size = 500;
int mark[maxN];
int lab[maxN];
int res[maxN];
vector<pair<int, int>> vc;
int Findset(int u)
{
return lab[u] < 0 ? u : Findset(lab[u]);
}
void Unite(int u, int v)
{
int r = Findset(u), s = Findset(v);
if(r == s)
return;
if(lab[r] > lab[s])
swap(r, s);
vc.pb({r, lab[r]});
vc.pb({s, lab[s]});
lab[r] += lab[s];
lab[s] = r;
}
void roll_back(int sz)
{
while(vc.size() > sz)
{
pair<int, int> tmp = vc.back();
vc.pop_back();
lab[tmp.fi] = tmp.se;
}
}
struct TEdge
{
int u, v, w;
} e[maxN];
int q;
struct TQuery
{
int type;
int s, w;
} qu[maxN];
vector<int> upd[maxN];
void ReadInput()
{
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int u, v, w;
cin >> u >> v >> w;
e[i] = {u, v, w};
}
cin >> q;
for(int i=1; i<=q; i++)
{
cin >> qu[i].type >> qu[i].s >> qu[i].w;
}
}
void Solve()
{
for(int i=1; i<=block_size; i++)
{
memset(lab, -1, sizeof lab);
vc.clear();
int l = block_size * (i - 1) + 1, r = min(q, block_size * i);
if(l > q)
break;
for(int j=1; j<=m; j++)
mark[j] = 0;
for(int j=l; j<=r; j++)
{
if(qu[j].type == 1)
{
mark[qu[j].s] = 1;
}
}
vector<int> unchanged, query, changed;
for(int j=1; j<=m; j++)
{
if(!mark[j])
unchanged.pb(j);
else
changed.pb(j);
}
for(int j=l; j<=r; j++)
if(qu[j].type == 2)
query.pb(j);
for(int j=l; j<=r; j++)
{
if(qu[j].type == 1)
e[qu[j].s].w = qu[j].w;
else
{
for(int v : changed)
if(e[v].w >= qu[j].w)
upd[j].pb(v);
}
}
sort(unchanged.begin(), unchanged.end(), [](int i, int j)
{
return e[i].w > e[j].w;
});
sort(query.begin(), query.end(), [](int i, int j)
{
return qu[i].w > qu[j].w;
});
int p = 0;
for(int j : query)
{
while(p < unchanged.size() && e[unchanged[p]].w >= qu[j].w)
{
Unite(e[unchanged[p]].u, e[unchanged[p]].v);
p++;
}
int sz = vc.size();
for(int v : upd[j])
{
// if(j == 5) cout << v << " ";
Unite(e[v].u, e[v].v);
}
res[j] = -lab[Findset(qu[j].s)];
roll_back(sz);
}
}
for(int i=1; i<=q; i++)
{
if(qu[i].type == 2)
cout << res[i] << '\n';
}
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
Compilation message (stderr)
bridges.cpp: In function 'void roll_back(long long int)':
bridges.cpp:39:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
39 | while(vc.size() > sz)
| ~~~~~~~~~~^~~~
bridges.cpp: In function 'void Solve()':
bridges.cpp:123:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | while(p < unchanged.size() && e[unchanged[p]].w >= qu[j].w)
| ~~^~~~~~~~~~~~~~~~~~
# | 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... |