이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int block = 1000;
const int mx = 100000;
int parent[5 + mx], sz[5 + mx];
stack<int> st;
void init(int n) {
iota(parent, parent + n, 0);
fill(sz, sz + n, 1);
}
int find(int x) {
while (x != parent[x]) {
x = parent[x];
}
return x;
}
void unite_set(int x, int y) {
x = find(x), y = find(y);
if (x == y) {
return ;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
st.emplace(y);
parent[y] = x;
sz[x] += sz[y];
}
void roll_back(int x) {
while (st.size() > x) {
int y = st.top();
st.pop();
sz[parent[y]] -= sz[y];
parent[y] = y;
}
}
int n, m, q;
int u[5 + mx], v[5 + mx], w[5 + mx];
int t[5 + mx], x[5 + mx], y[5 + mx];
bool changed[5 + mx];
vector<int> to_join[block];
int result[5 + mx];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> w[i];
u[i]--, v[i]--;
}
cin >> q;
for (int i = 0; i < q; i++) {
cin >> t[i] >> x[i] >> y[i];
x[i]--;
}
fill(changed, changed + m, false);
for (int l = 0; l < q; l += block) {
int r = min(l + block, q);
init(n);
vector<int> ask, upd, unchanged;
for (int i = l; i < r; i++) {
if (t[i] == 1) {
changed[x[i]] = true;
upd.emplace_back(i);
}
else {
ask.emplace_back(i);
}
}
for (int i = 0; i < m; i++) {
if (!changed[i]) {
unchanged.emplace_back(i);
}
else {
changed[i] = false;
}
}
for (int i = l; i < r; i++) {
if (t[i] == 1) {
w[x[i]] = y[i];
}
else {
to_join[i - l].clear();
for (auto& u: upd) {
if (w[x[u]] >= y[i]) {
to_join[i - l].emplace_back(x[u]);
}
}
}
}
sort(begin(ask), end(ask), [&](int const& a, int const& b) {
return y[a] > y[b];
});
sort(begin(unchanged), end(unchanged), [&](int const& a, int const& b) {
return w[a] > w[b];
});
int p = 0;
for (int& i: ask) {
while (p < unchanged.size() && w[unchanged[p]] >= y[i]) {
unite_set(u[unchanged[p]], v[unchanged[p]]);
p++;
}
int pre_sz = st.size();
for (int& j: to_join[i - l]) {
unite_set(u[j], v[j]);
}
result[i] = sz[find(x[i])];
roll_back(pre_sz);
}
}
for (int i = 0; i < q; i++) {
if (t[i] == 2) {
cout << result[i] << "\n";
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'void roll_back(int)':
bridges.cpp:37:26: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | while (st.size() > x) {
| ~~~~~~~~~~^~~
bridges.cpp: In function 'int main()':
bridges.cpp:115:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | while (p < unchanged.size() && w[unchanged[p]] >= y[i]) {
| ~~^~~~~~~~~~~~~~~~~~
# | 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... |