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>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (int i = 0; i < n; i++)
#define rfo(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = a; i >= b; i--)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void dfs(int u, int par);
const int mod = 1'000'000'007;
const int N = 1e5 + 10, M = N;
vi g[N];
int a[N], ans[N], par[N], sz[N];
int n = 0;
stack<int> stck;
void reset()
{
fo(i, n)
{
par[i] = i;
sz[i] = 1;
}
}
int find(int a)
{
while (a != par[a])
{
a = par[a];
}
return a;
}
void onion(int a, int b)
{
a = find(a);
b = find(b);
if (a == b)
return;
if (sz[a] > sz[b])
swap(a, b);
stck.push(a);
sz[b] += sz[a];
par[a] = par[b];
}
void rollback(int siz)
{
while (stck.size() > siz)
{
int k = stck.top();
stck.pop();
sz[par[k]] -= sz[k];
par[k] = k;
}
}
int u[N], v[N], d[N], b[N], r[N], s[N], w[N], ty[N], changed[N];
vi tojoin[N];
const int bs = 1000;
void solve()
{
int m, q;
cin >> n >> m;
fo(i, m)
{
cin >> u[i] >> v[i] >> d[i];
u[i]--;
v[i]--;
}
cin >> q;
fo(i, q)
{
int t;
cin >> t;
if (t == 1)
{
cin >> b[i] >> r[i];
b[i]--;
ty[i] = 1;
}
else
{
cin >> s[i] >> w[i];
s[i]--;
ty[i] = 2;
}
}
for (int j = 0; j < q; j += bs)
{
reset();
vi ask, update, nochange;
fo(i, m) changed[i] = false;
for (int i = j; i < min(j + bs, q); i++)
{
if (ty[i] == 1)
{
changed[b[i]] = true;
update.pb(i);
}
else
{
ask.pb(i);
}
}
fo(k, m) if (!changed[k]) nochange.pb(k);
for (int i = j; i < min(j + bs, q); i++)
{
if (ty[i] == 1)
{
d[b[i]] = r[i];
}
else
{
tojoin[i].clear();
for (auto e : update)
if (d[b[e]] >= w[i])
tojoin[i].pb(b[e]);
}
}
sort(all(ask), [&](int a, int b)
{ return w[a] > w[b]; });
sort(all(nochange), [&](int a, int b)
{ return d[a] > d[b]; });
int ptr = 0;
for (auto e : ask)
{
while (ptr < nochange.size() && d[nochange[ptr]] >= w[e])
{
onion(u[nochange[ptr]], v[nochange[ptr]]);
ptr++;
}
int prv = stck.size();
for (auto f : tojoin[e])
onion(u[f], v[f]);
ans[e] = sz[find(s[e])];
rollback(prv);
}
}
fo(i, q) if (ty[i] == 2) cout << ans[i] << '\n';
return;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
int cs = 0;
while (t - cs)
{
solve();
cs++;
}
return 0;
}
int mpow(int base, int exp)
{
base %= mod;
int result = 1;
while (exp > 0)
{
if (exp & 1)
result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m)
{
int u, v;
while (m--)
{
cin >> u >> v;
u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
void dfs(int u, int par)
{
for (int v : g[u])
{
if (v == par)
continue;
dfs(v, u);
}
}
Compilation message (stderr)
bridges.cpp: In function 'void rollback(int)':
bridges.cpp:82:24: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
82 | while (stck.size() > siz)
| ~~~~~~~~~~~~^~~~~
bridges.cpp: In function 'void solve()':
bridges.cpp:162:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | while (ptr < nochange.size() && d[nochange[ptr]] >= w[e])
| ~~~~^~~~~~~~~~~~~~~~~
# | 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... |