#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define ii pair<int, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}
const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 1e5 + 10, S = 1e3 + 1e2 + 10;
const long long INF = (long long)4e18 + 11;
/*
/\_/\
(= ._.)
/ >? \>$
*/
int n, m, q;
struct Egde{
int u, v, w;
bool operator < (const Egde &p) const{
return w > p.w;
}
}e[N];
int par[N];
stack<ii> st;
int Find(int v){
while(par[v] > 0) v = par[v];
return v;
}
bool Union(int u, int v){
u = Find(u), v = Find(v);
if(u == v) return false;
if(par[u] > par[v]) swap(u, v);
st.push({v, par[v]});
st.push({u, par[u]});
par[u] += par[v], par[v] = u;
return true;
}
void rollback(int tms){
while(sz(st) > tms){
auto [v, parV] = st.top(); st.pop();
par[v] = parV;
auto [u, parU] = st.top(); st.pop();
par[u] = parU;
}
}
struct Query{
int t, s, w, id;
void init(){
id = 0;
cin >> t >> s >> w;
}
bool operator < (const Query &p) const{
return w > p.w;
}
}Q[N];
vector<int> T, adj[N];
vector<Egde> E;
vector<Query> QQ;
int res[N];
bool mark[N];
void sol(void){
cin >> n >> m;
FOR(i, 1, m){
int u, v, w; cin >> u >> v >> w;
e[i] = {u, v, w};
}
cin >> q;
FOR(i, 1, q) Q[i].init();
FOR(i, 1, n) par[i] = -1;
for(int B = 1; B <= (q+S-1) / S; B++){
int l = (B-1)*S + 1, r = min(B*S, q);
FOR(i, l, r){
if(Q[i].t == 1){
int id = Q[i].s;
mark[id] = true;
adj[id].push_back(i);
}
else QQ.push_back({Q[i].t, Q[i].s, Q[i].w, i});
}
FOR(i, 1, m){
if(mark[i] == true) T.push_back(i);
else E.push_back(e[i]);
}
sort(begin(E), end(E));
sort(begin(QQ), end(QQ));
int j = 0;
for(auto &[t, s, w, id] : QQ){
while(j < sz(E) && E[j].w >= w){
Union(E[j].u, E[j].v);
j++;
}
int old = sz(st);
for(int idx : T){
int p = upper_bound(all(adj[idx]), id) - adj[idx].begin();
auto [u, v, c] = e[idx];
if(p > 0){
p--;
c = Q[adj[idx][p]].w;
}
if(c >= w) Union(u, v);
}
res[id] = -par[Find(s)];
rollback(old);
}
FOR(i, l, r) if(Q[i].t == 1){
int id = Q[i].s;
mark[id] = false;
e[id].w = Q[i].w;
adj[id].clear();
}
T.clear(); E.clear(); QQ.clear();
rollback(0);
}
FOR(i, 1, q) if(Q[i].t == 2) cout << res[i] << '\n';
}
signed main(void){
#define TASK "nhap"
if(fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--) sol();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'int main()':
bridges.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | 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... |