#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
//--------------------------------------------------------------------
const int N = 1e5 + 10;
const int BLOCK = 1000;
const int M = 1e5 + 10;
int p[N], sz[N];
vector<int> edges;
int findset(int u)
{
return u == p[u] ? u : u = findset(p[u]);
}
void ghep(int u, int v)
{
u = findset(u);
v = findset(v);
if(u == v) return;
if(sz[u] < sz[v]) {
swap(u, v);
}
edges.push_back(v);
p[v] = u;
sz[u] += sz[v];
}
void rollback() {
int v = edges.back();
sz[p[v]] -= sz[v];
p[v] = v;
edges.pop_back();
}
int n, m, q;
struct ED {
int u, v, d;
} ed[M];
struct QR {
int t, x, y;
} qr[M];
int dx[M];
struct B {
int u, v, d, id;
bool operator < (const B & a) const {
return d > a.d || d == a.d && id < a.id;
}
} b[M + BLOCK];
int ans[M];
vector<pa> vr[N];
void hbmt() {
cin >> n >> m;
FOR(i, 1, m) {
int u, v, d;
cin >> u >> v >> d;
ed[i] = {u, v, d};
}
FOR(i, 1, n) {
p[i] = i;
sz[i] = 1;
}
cin >> q;
int cnt = 0;
vector<int> q1, q2;
FOR(i, 1, q) {
int t, x, y;
cin >> t >> x >> y;
qr[i] = {t, x, y};
cnt++;
if(t == 1) {
q1.push_back(x);
}
else {
q2.push_back(i);
}
if(i % BLOCK == 0 || i == q) {
int o = 0;
int pre = max(1, i - cnt + 1);
cnt = 0;
FOR(j, pre, i) {
if(qr[j].t == 1) {
int s = qr[j].x, w = qr[j].y;
ed[s].d = w;
dx[s] = 1;
}
else {
int u = qr[j].x, w = qr[j].y;
for(auto & e : q1) {
if(ed[e].d >= qr[j].y)
vr[j].push_back({e, ed[e].d});
}
b[++o] = {u, 0, w, j};
}
}
FOR(i, 1, m) {
if(dx[i]) {
dx[i] = 0;
continue;
}
b[++o] = {ed[i].u, ed[i].v, ed[i].d, -1};
}
sort(b + 1, b + o + 1);
FOR(j, 1, o) {
if(b[j].id == -1) {
ghep(b[j].u, b[j].v);
}
else {
int presz = edges.size();
for(auto &e : vr[b[j].id]) {
ghep(ed[e.fi].u, ed[e.fi].v);
}
ans[b[j].id] = sz[findset(b[j].u)];
while(presz < edges.size()) rollback();
}
}
while(edges.size() > 0) rollback();
for(auto &e : q2) {
cout << ans[e] << '\n';
vr[e].clear();
}
q1.clear();
q2.clear();
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
#define NAME "hbmt"
if(fopen(NAME".inp", "r")) {
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
//int t;cin>>t;while(t--)
hbmt();
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen(NAME".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... |