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>
struct DSU {
std::vector<int> e;
void init(int n) {
e = std::vector<int>(n, -1);
}
int get(int x) {
return e[x] < 0 ? x : e[x] = get(e[x]);
}
bool same_set(int a, int b) {
return get(a) == get(b);
}
int size(int x) {
return -e[get(x)];
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) std::swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
}
};
int main() {
const int B = 800;
const int INF = 1e9;
using namespace std;
ios_base::sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<vector<int>> adj(n);
vector<array<int, 3>> ed;
vector<int> id(m);
iota(id.begin(), id.end(), 0);
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
u--, v--;
ed.push_back({u, v, w});
}
vector<int> mod(m);
int q; cin >> q;
vector<array<int, 3>> modify;
vector<array<int, 3>> queries;
vector<int> vis(n);
vector<int> rem;
vector<array<int, 2>> ans;
DSU D;
for (int i = 0; i < q; i++) {
int x, y, z; cin >> x >> y >> z;
y--;
if (x == 1)
modify.push_back({y, z, i});
else
queries.push_back({y, z, i});
if (i == q - 1 || int(modify.size()) + int(queries.size()) == B) {
sort(id.begin(), id.end(), [&ed](int& x, int & y) {
return ed[x][2] < ed[y][2];
});
D.init(n);
sort(queries.begin(), queries.end(), [](array<int, 3>& a, array<int, 3>& b) {
return a[1] > b[1];
});
for (auto& mm : modify)
mod[mm[0]] = 1;
int it = m;
for (auto& qq : queries) {
int weight = qq[1];
int ti = qq[2];
while (it && ed[id[it - 1]][2] >= weight) {
it--;
if (!mod[id[it]])
D.unite(ed[id[it]][0], ed[id[it]][1]);
}
for (auto& mm : modify) {
bool check = mm[2] < ti && mm[1] >= weight || mm[2] > ti && ed[mm[0]][2] >= weight;
if (check) {
int eid = mm[0];
int u = D.get(ed[eid][0]);
int v = D.get(ed[eid][1]);
adj[u].push_back(v);
adj[v].push_back(u);
}
}
int res = 0;
function<void(int)> dfs = [&](int src) {
res += D.size(src);
rem.push_back(src);
vis[src] = 1;
for (int nxt : adj[src]) {
if (vis[nxt])
continue;
dfs(nxt);
}
};
dfs(D.get(qq[0]));
ans.push_back({ti, res});
for (int r : rem)
vis[r] = 0;
rem.clear();
for (auto& mm : modify) {
bool check = mm[2] < ti && mm[1] >= weight || mm[2] > ti && ed[mm[0]][2] >= weight;
if (check) {
int eid = mm[0];
int u = D.get(ed[eid][0]);
int v = D.get(ed[eid][1]);
adj[u].pop_back();
adj[v].pop_back();
}
}
}
for (auto& mm : modify)
mod[mm[0]] = 0, ed[mm[0]][2] = mm[1];
queries.clear();
modify.clear();
}
}
sort(ans.begin(), ans.end());
for (auto& a : ans)
cout << a[1] << '\n';
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:77:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
77 | bool check = mm[2] < ti && mm[1] >= weight || mm[2] > ti && ed[mm[0]][2] >= weight;
bridges.cpp:103:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
103 | bool check = mm[2] < ti && mm[1] >= weight || mm[2] > ti && ed[mm[0]][2] >= weight;
bridges.cpp:28:15: warning: unused variable 'INF' [-Wunused-variable]
28 | const int INF = 1e9;
| ^~~
# | 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... |