#include <bits/stdc++.h>
#define TASK "asdjaksjdkasjd"
#define INT_LIM (int) 2147483647
#define LL_LIM (long long) 9223372036854775807
#define endl '\n'
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define BIT(i,x) (((i)>>(x))&1)
#define FOR(i,a,b) for(int i = (a); i<=(b); i++)
#define FORD(i,a,b) for(int i = (a); i>=(b); i--)
#define ll long long
#define pii pair<int,int>
using namespace std;
///------------------------------------------///
struct DSU{
int par[50005];
int sz[50005];
vector<pii> upd;
void init(int n)
{
upd.clear();
FOR(i, 1, n)
{
par[i] = i; sz[i] = 1;
}
}
int get(int u)
{
return (par[u]==u)?u:get(par[u]);
}
bool merge(int u, int v)
{
u = get(u); v = get(v);
upd.pb(mp(u, sz[u]));
upd.pb(mp(v, sz[v]));
if (u==v) return false;
if (sz[u]<sz[v]) swap(u,v);
par[v] = u;
sz[u]+= sz[v];
return true;
}
void rollback()
{
pii P = upd.back(); upd.pop_back();
par[P.fi] = P.fi; sz[P.fi] = P.se;
P = upd.back(); upd.pop_back();
par[P.fi] = P.fi; sz[P.fi] = P.se;
}
} dsu;
int n,m,q;
const int B = 317;
pair<int,pii> edge[100005];
pii f[100005];
void inp()
{
cin >> n >> m;
FOR(i, 1, m)
{
int u,v,d;
cin >> u >> v >> d;
edge[i] = mp(d, mp(u,v));
}
cin >> q;
FOR(i, 1, q)
{
int type;
cin >> type;
cin >> f[i].fi >> f[i].se;
if (type==2)
{
f[i].fi*=-1; f[i].se*=-1;
}
}
}
int dd[100005];
struct item{
int type, weight, id;
};
vector<item> sweep;
vector<int> T[100005];
bool cmp(item x, item y)
{
if (x.weight!=y.weight) return x.weight>y.weight;
return x.type<y.type;
}
int ans[100005];
void solve()
{
for (int b = 1; b<=q; b+=B)
{
sweep.clear();
FOR(i, 1, m) dd[i] = 0;
FOR(i, b, min(b+B-1, q))
{
if (f[i].fi>0)
{
dd[f[i].fi] = f[i].se;
}
else
{
sweep.pb({2, -f[i].se, i});
FOR(j, b, min(b+B-1,q)) if (f[j].fi>0)
{
if (dd[f[j].fi]==0 && edge[f[j].fi].fi>=-f[i].se) T[i].pb(f[j].fi);
else if (dd[f[j].fi]>=-f[i].se) T[i].pb(f[j].fi);
}
}
}
FOR(i, 1, m) if (!dd[i]) sweep.pb({1,edge[i].fi, i});
sort(sweep.begin(), sweep.end(), cmp);
dsu.init(n);
for (auto V:sweep)
{
if (V.type==1)
{
int i = V.id;
dsu.merge(edge[i].se.fi, edge[i].se.se);
}
else
{
// cout << V.id << endl;
for (auto v:T[V.id]) dsu.merge(edge[v].se.fi, edge[v].se.se);
ans[V.id] = dsu.sz[dsu.get(-f[V.id].fi)];
for (auto v:T[V.id]) dsu.rollback();
}
}
FOR(i, 1, m) if (dd[i]>0) edge[i].fi = dd[i];
}
FOR(i, 1, q) if (f[i].fi<0) cout << ans[i] << endl;
}
signed main()
{
///--------------------------///
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
if (fopen(TASK".INP","r")!=NULL)
{
freopen(TASK".INP","r",stdin);
freopen(TASK".OUT","w",stdout);
}
///--------------------------///
int NTEST = 1;
//cin >> NTEST;
while (NTEST--)
{
inp();
solve();
}
return 0;
}
///------------------------------------------///
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'int main()':
bridges.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | freopen(TASK".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | 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... |