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 ll int
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 405;
const ll mod = 1e9;
const ll base = 350;
const ll inf = 1e9 ;
ll n, m, t, q, k, ans, a[N], b[N], tong, in[N], out[N], res[N], x[N], y[N], w[N], id[N];
vector<ll> adj[N];
struct DSU
{
ll n;
vector<ll> lab;
vector<pair<pll, pll>> snap;
DSU(ll _n)
{
n = _n;
lab.resize(n+1, -1);
}
ll findp(ll u)
{
return lab[u] < 0 ? u : findp(lab[u]);
}
void hop(ll x, ll y)
{
ll u = findp(x), v = findp(y);
if(u == v)return;
if(lab[u] > lab[v])swap(u, v);
snap.pb({{u, lab[u]}, {v, lab[v]}});
lab[u] += lab[v];
lab[v] = u;
}
void rollback(ll sz)
{
while((int) snap.size() > sz)
{
auto x = snap.back();
snap.pop_back();
lab[x.fi.fi] = x.fi.se;
lab[x.se.fi] = x.se.se;
}
}
};
vector<ll> ve;
bool cmp(const ll& x, const ll& y)
{
return w[x] > w[y];
}
void sol()
{
cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
cin >> x[i] >> y[i] >> w[i];
id[i] = i;
in[i] = i;
out[i] = inf;
ve.pb(i);
}
cin >> q;
for(int i = m+1; i <= m+q; i ++)
{
cin >> x[i] >> y[i] >> w[i];
if(x[i] == 1)
{
out[id[y[i]]] = i-1;
id[y[i]] = i;
in[i] = i;
out[i] = inf;
ve.pb(i);
}
}
//for(int i = 1; i <= m+q; i ++)cout << in[i] <<" "<<out[i] << '\n';
DSU dsu(n);
sort(ve.begin(), ve.end(), cmp);
for(int l = m+1; l <= m+q; l += base)
{
int r = min(m+q, l+base-1);
vector<ll> fixed, query, kq;
for(int i = l; i <= r; i ++)if(x[i] == 2)query.pb(i);
if(query.empty())continue;
sort(query.begin(), query.end(), cmp);
for(int i: ve)
{
if(in[i] <= l && out[i] >= r)fixed.pb(i);
else if(in[i] <= r && out[i] >= l)kq.pb(i);
}
int j = 0;
for(int i: query)
{
//cout << i << " ";
while(j < (int) fixed.size() && w[fixed[j]] >= w[i])
{
//cout << fixed[j] << " ";
if(fixed[j] <= m)dsu.hop(x[fixed[j]], y[fixed[j]]);
else dsu.hop(x[y[fixed[j]]], y[y[fixed[j]]]);
++j;
}
k = dsu.snap.size();
for(int p: kq)
{
if(w[p] >= w[i] && in[p] <= i && i <= out[p])
{
//cout << p << " ";
if(p <= m)dsu.hop(x[p], y[p]);
else dsu.hop(x[y[p]], y[y[p]]);
}
}
//cout << '\n';
res[i] = -dsu.lab[dsu.findp(y[i])];
dsu.rollback(k);
}
dsu.rollback(0);
}
for(int i = m+1; i <= m+q; i ++)if(x[i] == 2)cout << res[i] << '\n';
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:135:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:136:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |