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>
#pragma GCC target("avx2")
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
int u[100000], v[100000], d[100000], par[50000], siz[50000];
int t[100000], s[100000], w[100000], vis[100000], res[100000], tmp[100000];
struct UnionFind {
void build(int n) {
for (int i = 0; i < n; i++) {
par[i] = i, siz[i] = 1;
}
}
inline int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
inline void unite(int u, int v) {
u = find(u), v = find(v);
if (u == v)
return;
if (siz[u] < siz[v])
swap(u, v);
par[v] = u;
siz[u] += siz[v];
}
bool same(int u, int v) { return find(u) == find(v); }
};
stack<int> G[50000];
int ok[50000];
stack<int> vv;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
const int b = 500;
int n, m;
cin >> n >> m;
vector<P> es(m);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> d[i];
--u[i];
--v[i];
}
int q;
cin >> q;
fill(res, res + q, -1);
fill(tmp, tmp + m, -1);
priority_queue<pair<P, int>> qs;
UnionFind uf;
for (int i = 0; i < q; i++) {
cin >> t[i] >> s[i] >> w[i];
--s[i];
}
vector<int> os;
int ot = 0;
for (int i = 0; i < q; i++) {
if (t[i] == 1) {
vis[s[i]] = 1;
os.emplace_back(i);
} else {
qs.push(make_pair(P(w[i], s[i]), i));
}
if (i % b < b - 1 && i != q - 1) {
continue;
}
for (int j = 0; j < m; j++) {
es[j] = P(d[j], j);
}
sort(all(es), greater<P>());
int id = 0;
uf.build(n);
int fs = (i / b) * b;
while (!qs.empty()) {
int ww = qs.top().first.first, st = qs.top().first.second,
idx = qs.top().second;
qs.pop();
while (id < m && es[id].first >= ww) {
if (!vis[es[id].second]) {
uf.unite(u[es[id].second], v[es[id].second]);
}
id++;
}
for (int k = ot; k < os.size(); k++) {
if (os[k] >= idx) {
break;
}
tmp[s[os[k]]] = w[os[k]];
}
int to = INF;
for (int k = ot; k < os.size(); k++) {
if (os[k] >= idx) {
to = k;
break;
}
if (w[os[k]] == tmp[s[os[k]]] && w[os[k]] >= ww) {
int uu = uf.find(u[s[os[k]]]), vv = uf.find(v[s[os[k]]]);
G[uu].push(uf.find(vv));
G[vv].push(uf.find(uu));
}
}
for (int k = to; k < os.size(); k++) {
if ((tmp[s[os[k]]] == -1 ? d[s[os[k]]] : tmp[s[os[k]]]) >= ww) {
int uu = uf.find(u[s[os[k]]]), vv = uf.find(v[s[os[k]]]);
G[uu].push(uf.find(vv));
G[vv].push(uf.find(uu));
}
}
for (int k = ot; k < os.size(); k++) {
if (os[k] >= idx) {
break;
}
tmp[s[os[k]]] = -1;
}
int p = uf.find(st);
stack<int> ss;
ss.push(p);
while (!ss.empty()) {
int e = ss.top();
ss.pop();
ok[e] = 1;
vv.push(e);
while (!G[e].empty()) {
if (!ok[G[e].top()]) {
ok[G[e].top()] = 1;
ss.push(G[e].top());
}
G[e].pop();
}
}
res[idx] = 0;
while (!vv.empty()) {
res[idx] += siz[uf.find(vv.top())];
ok[uf.find(vv.top())] = 0;
vv.pop();
}
for (int k = ot; k < os.size(); k++) {
int uu = uf.find(u[s[os[k]]]), vv = uf.find(v[s[os[k]]]);
while (!G[uu].empty()) {
G[uu].pop();
}
while (!G[vv].empty()) {
G[vv].pop();
}
}
}
for (int j = (int)os.size() - 1; j >= ot; j--) {
if (vis[s[os[j]]]) {
d[s[os[j]]] = w[os[j]];
vis[s[os[j]]] = 0;
}
}
ot = os.size();
}
for (int i = 0; i < q; i++) {
if (res[i] != -1) {
cout << res[i] << '\n';
}
}
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:92:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = ot; k < os.size(); k++) {
~~^~~~~~~~~~~
bridges.cpp:99:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = ot; k < os.size(); k++) {
~~^~~~~~~~~~~
bridges.cpp:110:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = to; k < os.size(); k++) {
~~^~~~~~~~~~~
bridges.cpp:117:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = ot; k < os.size(); k++) {
~~^~~~~~~~~~~
bridges.cpp:145:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = ot; k < os.size(); k++) {
~~^~~~~~~~~~~
bridges.cpp:81:13: warning: unused variable 'fs' [-Wunused-variable]
int fs = (i / b) * b;
^~
# | 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... |