#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 = 200;
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], a[N];
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], f[M];
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;
memset(f, -1, sizeof f);
int cnt = 0;
FOR(i, 1, q) {
int t, x, y;
cin >> t >> x >> y;
qr[i] = {t, x, y};
cnt++;
if(i % BLOCK == 0 || i == q) {
int pre = (i - cnt);
cnt = 0;
pre = max(0, pre);
int o = 0, k = 0;
FOR(j, pre + 1, i) {
if(qr[j].t == 1) {
a[++k] = j;
dx[qr[j].x] = 1;
}
else {
b[++o] = {qr[j].x, 0, qr[j].y, j};
}
}
vector<int> vt;
FOR(j, 1, m) {
if(dx[j]) {
vt.push_back(j);
dx[j] = 0;
continue;
}
b[++o] = {ed[j].u, ed[j].v, ed[j].d, -1};
}
sort(b + 1, b + o + 1);
// cout << edges.size() << '\n';
FOR(j, 1, o) {
// cout << b[j].u << ' '<< b[j].v << ' '<< b[j].d << ' ' << b[j].id << ' ' << k<< '\n';
if(b[j].id == -1) {
ghep(b[j].u, b[j].v);
// cout << edges.size() << '\n';
}
else {
int pre_edges_sz = edges.size();
FOR(v, 1, k) {
if(a[v] <= b[j].id) {
int id = qr[a[v]].x;
if(f[id] == -1) {
f[id] = ed[id].d;
}
ed[id].d = qr[a[v]].y;
}
}
for(auto &e : vt) {
if(ed[e].d >= b[j].d) {
ghep(ed[e].u, ed[e].v);
}
if(f[e] != -1) ed[e].d = f[e];
f[e] = -1;
}
ans[b[j].id] = sz[findset(b[j].u)];
while(edges.size() > pre_edges_sz) rollback();
}
}
while(edges.size() > 0) rollback();
FOR(j, pre + 1, i) {
if(qr[j].t == 1) {
ed[qr[j].x].d = qr[j].y;
}
else {
cout << ans[j] << '\n';
}
}
}
}
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'int main()':
bridges.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | 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... |