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 OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
struct Query {
int idx,t,x,y;
};
const int MAXN = 1e5+1, BLOCK = 150;
int n,m,q;
int p[MAXN], sz[MAXN], res[MAXN];
vector<pair<int,PII>> e;
vector<Query> qu1, qu2;
US<int> z;
int ans;
int finds(int a) {
if (p[a] != a) p[a] = finds(p[a]);
return p[a];
}
void unite(int a, int b) {
a = finds(a);
b = finds(b);
if (a != b) {
if (sz[a] < sz[b]) swap(a,b);
p[b] = a;
sz[a] += sz[b];
}
}
void dfs(int s, UM<int,US<int>>& v) {
if (z.count(s)) return;
z.insert(s);
ans += sz[s];
FORX(u,v[s]) {
int to = finds(e[u].S.F) == s ? finds(e[u].S.S) : finds(e[u].S.F);
dfs(to,v);
}
}
bool cmp(const Query& a, const Query& b) {
return a.y > b.y;
}
signed main() {
OPTM;
cin >> n >> m;
FOR(i,0,m) {
int a,b,w; cin >> a >> b >> w;
// v[a].PB({b,w});
// v[b].PB({a,w});
e.PB({w,{a,b}});
}
//sort(ALL(e),greater<pair<int,PII>>());
cin >> q;
int i = 0;
while (i < q) {
qu1.clear(); qu2.clear();
FOR(i,1,n+1) {
p[i] = i;
sz[i] = 1;
}
int j = 0;
US<int> vis;
while (j < BLOCK && i < q) {
int t,s,w; cin >> t >> s >> w;
if (t == 1) {
qu1.PB({i,t,s-1,w});
vis.insert(s-1);
}
else qu2.PB({i,t,s,w});
j++; i++;
}
sort(ALL(qu2),cmp);
vector<pair<int,PII>> e1;
FOR(j,0,m) if (!vis.count(j)) e1.PB(e[j]);
sort(ALL(e1),greater<pair<int,PII>>());
int en = e1.size();
// FORX(u,qu2) cout << u.y << ' ';
// cout << ln;
int q1n = qu1.size(), q2n = qu2.size();
int k = 0;
FOR(j,0,q2n) {
while (k < en && e1[k].F >= qu2[j].y) {
unite(e1[k].S.F, e1[k].S.S);
k++;
}
UM<int,US<int>> v;
z.clear();
ans = 0;
FOR(l,0,q1n) {
int a = e[qu1[l].x].S.F, b = e[qu1[l].x].S.S;
if (e[qu1[l].x].F >= qu2[j].y) {
v[finds(a)].insert(qu1[l].x);
v[finds(b)].insert(qu1[l].x);
}
}
FOR(l,0,q1n) {
if (qu1[l].idx > qu2[j].idx) continue;
int a = e[qu1[l].x].S.F, b = e[qu1[l].x].S.S, w = qu1[l].y;
if (w >= qu2[j].y) {
v[finds(a)].insert(qu1[l].x);
v[finds(b)].insert(qu1[l].x);
}
else {
if (v[finds(a)].count(qu1[l].x)) {
v[finds(a)].erase(qu1[l].x);
v[finds(b)].erase(qu1[l].x);
}
}
}
// FORX(u,v) {
// cout << u.F << ": ";
// FORX(y,u.S) cout << y << ' ';
// cout << ln;
// }
dfs(finds(qu2[j].x),v);
res[qu2[j].idx] = ans;
}
// FOR(i,1,n+1) cout << finds(i) << ' ';
// cout << ln;
FOR(i,0,q1n) {
e[qu1[i].x].F = qu1[i].y;
}
}
FOR(i,0,q) if (res[i]) cout << res[i] << ln;
}
# | 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... |