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;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 100013
#define MAGIC 1000
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, M, Q;
pii edge[MAXN];
int weight[MAXN], tmp[MAXN];
array<int, 3> quer[MAXN];
vector<array<int, 3> > events;
bitset<MAXN> mentioned, vis;
vi edge1[MAXN];
vi inds;
int dsu[MAXN], siz[MAXN], ans[MAXN];
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void merge(int u, int v)
{
u = get(u);
v = get(v);
if (u == v) return;
if (siz[v] > siz[u]) swap(u, v);
dsu[v] = u;
siz[u] += siz[v];
siz[v] = 0;
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
cin >> N >> M;
FOR(i, 0, M)
{
int u, v, d;
cin >> u >> v >> d;
u--; v--;
if (u > v) swap(u, v);
edge[i] = {u, v};
weight[i] = d;
}
cin >> Q;
FOR(i, 0, Q)
{
int a, b, c;
cin >> a >> b >> c; b--;
if (a == 2)
{
quer[i] = {-1, b, c};
}
else
{
quer[i] = {b, weight[b], c};
weight[b] = c;
}
}
FORD(i, Q, 0)
{
if (quer[i][0] == -1) continue;
int idx = quer[i][0];
weight[idx] = quer[i][1];
}
for (int i = 0; i < Q; i += MAGIC)
{
FOR(j, 0, N)
{
dsu[j] = j;
siz[j] = 1;
}
mentioned.reset();
inds.clear();
events.clear();
for (int j = i; j < i + MAGIC && j < Q; j++)
{
if (quer[j][0] == -1) continue;
mentioned[quer[j][0]] = true;
}
FOR(j, 0, M)
{
if (mentioned[j])
{
inds.PB(j);
}
else
{
events.PB({weight[j], 1, j});
}
}
for (int j = i; j < i + MAGIC && j < Q; j++)
{
if (quer[j][0] != -1) continue;
events.PB({quer[j][2], 0, j});
}
sort(ALL(events), greater<array<int, 3> >());
for (auto e : events)
{
int idx = e[2];
if (e[1] == 1)
{
merge(edge[idx].fi, edge[idx].se);
}
else
{
for (int x : inds)
{
tmp[x] = weight[x];
}
FOR(j, i, idx)
{
if (quer[j][0] == -1) continue;
tmp[quer[j][0]] = quer[j][2];
}
for (int x : inds)
{
if (tmp[x] >= quer[idx][2])
{
int u = get(edge[x].fi), v = get(edge[x].se);
if (u == v) continue;
edge1[u].PB(v);
edge1[v].PB(u);
}
}
int s = get(quer[idx][1]);
vi q; q.PB(s);
vis[s] = true;
while(!q.empty())
{
int u = q.back(); q.pop_back();
ans[idx] += siz[u];
for (int v : edge1[u])
{
if (!vis[v])
{
vis[v] = true;
q.PB(v);
}
}
}
for (int x : inds)
{
if (tmp[x] >= quer[idx][2])
{
int u = get(edge[x].fi), v = get(edge[x].se);
edge1[u].clear();
edge1[v].clear();
vis[u] = false;
vis[v] = false;
}
}
vis[s] = false;
}
}
for (int j = i; j < i + MAGIC && j < Q; j++)
{
if (quer[j][0] == -1) continue;
weight[quer[j][0]] = quer[j][2];
}
}
FOR(i, 0, Q)
{
if (quer[i][0] != -1) continue;
cout << ans[i] << '\n';
}
return 0;
}
# | 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... |