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 <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 201010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define B 1000
struct dsu {
vector<map<ll, ll>> p, num;
map<ll, set<ll>> chk;
ll qnum = 0;
dsu(ll N) {
p.resize(N + 1);
num.resize(N + 1);
ll i;
for (i = 1; i <= N; i++) p[i][0] = i, num[i][0] = 1;
}
ll find(ll v) {
if ((--p[v].end())->second == v) return v;
else {
ll res = find((--p[v].end())->second);
return res;
}
}
ll get(ll v) { return (--num[find(v)].end())->second; }
void uni(ll a, ll b) {
a = find(a);
b = find(b);
qnum++;
if (a == b) return;
if (get(b) < get(a)) swap(a, b);
ll x = get(b) + get(a);
p[a][qnum] = b;
chk[qnum].insert(a);
num[b][qnum] = x;
chk[qnum].insert(b);
}
void rb() {
for (auto x : chk[qnum]) {
auto it1 = p[x].find(qnum);
if (it1 != p[x].end()) p[x].erase(it1);
auto it2 = num[x].find(qnum);
if (it2 != num[x].end()) num[x].erase(it2);
}
chk[qnum].clear();
qnum--;
}
void rb(ll t) {
ll i;
for (i = 1; i <= t; i++) rb();
}
};
struct query {
ll t;
ll b, r, s, w;
ll ind;
query(ll t, ll x, ll y, ll ind) :ind(ind) {
query::t = t;
if (t == 1) b = x, r = y;
else s = x, w = y;
}
query() {}
};
vector<pll> edge;
ll val[MAX];
vector<query> qv[2000];
query qarr[MAX];
ll ans[MAX];
ll chk[MAX];
ll nchk[MAX];
bool cmp(query& a, query& b) {
return a.w > b.w;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
ll N, M;
cin >> N >> M;
ll i;
edge.resize(M + 1);
for (i = 1; i <= M; i++) {
ll a, b;
cin >> a >> b >> val[i];
edge[i] = { a, b };
}
ll Q;
cin >> Q;
for (i = 1; i <= Q; i++) {
ll a, b, c;
cin >> a >> b >> c;
qarr[i] = query(a, b, c, i);
qv[(i - 1) / B].push_back(qarr[i]);
}
ll st, en;
set<pll> est;
for (i = 1; i <= M; i++) est.emplace(-val[i], i);
for (i = 0; ; i++) {
st = 1 + i * B;
en = (1 + i) * B;
en = min(en, Q);
if (st > en) break;
ll j;
for (j = st; j <= en; j++) if (qarr[j].t == 1) chk[qarr[j].b] = 1;
vector<query> qq;
for (auto& q : qv[i]) if (q.t == 2) qq.push_back(q);
if (qq.size()) sort(qq.begin(), qq.end(), cmp);
dsu d(N);
set<pll>::iterator it;
it = est.begin();
for (ll j = 0; j < qq.size(); j++) {
for (; it != est.end(); it++) {
if (chk[it->second]) continue;
if (qq[j].w > -it->first) break;
else d.uni(edge[it->second].first, edge[it->second].second);
}
ll cnt = 0;
for (ll k = qq[j].ind; k >= st; k--) {
if (qarr[k].t == 1) {
if (qarr[k].r >= qq[j].w && !nchk[qarr[k].b]) {
d.uni(edge[qarr[k].b].first, edge[qarr[k].b].second);
cnt++;
nchk[qarr[k].b] = 1;
}
}
}
for (ll k = en; k > qq[j].ind; k--) {
if (qarr[k].t == 1) {
if (!nchk[qarr[k].b] && val[qarr[k].b] >= qq[j].w) {
d.uni(edge[qarr[k].b].first, edge[qarr[k].b].second);
cnt++;
nchk[qarr[k].b] = 1;
}
}
}
for (ll k = en; k >= st; k--) if (qarr[k].t == 1) nchk[qarr[k].b] = 0;
ans[qq[j].ind] = d.get(qq[j].s);
d.rb(cnt);
}
for (j = st; j <= en; j++) {
if (qarr[j].t == 1) {
chk[qarr[j].b] = 0;
est.erase({ -val[qarr[j].b], qarr[j].b });
est.emplace(-qarr[j].r, qarr[j].b);
val[qarr[j].b] = qarr[j].r;
}
}
}
for (i = 1; i <= Q; i++) if (ans[i]) cout << ans[i] << ln;
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:117:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (ll j = 0; j < qq.size(); j++) {
| ~~^~~~~~~~~~~
# | 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... |