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 820
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];
bitset<MAXN> typ;
pii quer[MAXN];
vector<pii> events;
bitset<MAXN> mentioned;
vi inds;
int dsu[MAXN], siz[MAXN];
vpi szs;
int ans[MAXN];
int get(int u)
{
return (u == dsu[u] ? u : get(dsu[u]));
}
void merge(int u, int v, bool flag)
{
u = get(u);
v = get(v);
if (u == v) return;
if (siz[v] > siz[u]) swap(u, v);
if (flag)
{
szs.PB({v, siz[v]});
szs.PB({u, siz[u]});
}
dsu[v] = u;
siz[u] += siz[v];
siz[v] = 0;
}
void rollback()
{
FORD(i, SZ(szs), 0)
{
dsu[szs[i].fi] = szs[i].fi;
siz[szs[i].fi] = szs[i].se;
}
szs.clear();
}
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)
{
typ[i] = true;
}
quer[i] = {b, c};
}
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 (typ[j]) continue;
mentioned[quer[j].fi] = true;
}
FOR(j, 0, M)
{
if (mentioned[j])
{
inds.PB(j);
}
else
{
events.PB({2 * weight[j] + 1, j});
}
}
for (int j = i; j < i + MAGIC && j < Q; j++)
{
if (!typ[j]) continue;
events.PB({2 * quer[j].se, j});
}
sort(ALL(events), greater<pii>());
for (auto e : events)
{
int idx = e.se;
if (e.fi & 1)
{
merge(edge[idx].fi, edge[idx].se, 0);
}
else
{
for (int x : inds)
{
tmp[x] = weight[x];
}
FOR(j, i, idx)
{
if (typ[j]) continue;
tmp[quer[j].fi] = quer[j].se;
}
for (int x : inds)
{
if (tmp[x] >= quer[idx].se)
{
merge(edge[x].fi, edge[x].se, 1);
}
}
int u = quer[idx].fi;
u = get(u);
ans[idx] = siz[u];
rollback();
}
}
for (int j = i; j < i + MAGIC && j < Q; j++)
{
if (typ[j]) continue;
weight[quer[j].fi] = quer[j].se;
}
}
FOR(i, 0, Q)
{
if (!typ[i]) 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... |