이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
// _vector <int, 3> a(n, m, k);: int a[n][m][k].
// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
template <typename T>
struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
}
const int MAX = 5e4 + 3;
const ll MOD[] = {1000000007, 998244353};
const int blockSize = 1000;
int n, m, q;
int u[MAX << 1], v[MAX << 1], w[MAX << 1];
int type[MAX << 1], a[MAX << 1], b[MAX << 1];
int pa[MAX], sz[MAX];
vector <int> ops;
int findpa(int u) {
return u == pa[u] ? u : pa[u] = findpa(pa[u]);
}
void join(int u, int v) {
u = findpa(u);
v = findpa(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
pa[v] = u;
sz[u] += sz[v];
ops.push_back(v);
}
void rollBack() {
int u = ops.back();
ops.pop_back();
sz[pa[u]] -= sz[u];
pa[u] = u;
}
int ans[MAX << 1];
bool changed[MAX << 1];
vector <int> toJoin[MAX << 1];
void Solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> u[i] >> v[i] >> w[i];
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> type[i] >> a[i] >> b[i];
}
for (int l = 1; l <= q; l += blockSize) {
ops.clear();
fill(sz + 1, sz + n + 1, 1);
fill(changed + 1, changed + m + 1, false);
iota(pa + 1, pa + n + 1, 1);
vector <int> asks;
vector <int> updates;
vector <int> staticEgs;
int r = min(l + blockSize - 1, q);
for (int i = l; i <= r; i++) {
if (type[i] == 1) {
changed[a[i]] = 1;
updates.push_back(a[i]);
} else {
asks.push_back(i);
}
}
for (int i = 1; i <= m; i++) if (!changed[i]) {
staticEgs.push_back(i);
}
for (int i = l; i <= r; i++) {
if (type[i] == 1) {
w[a[i]] = b[i];
} else {
for (int &j : updates) {
if (w[j] >= b[i]) {
toJoin[i].push_back(j);
}
}
}
}
sort(all(asks), [&](const int &i, const int &j) {
return b[i] > b[j];
});
sort(all(staticEgs), [&](const int &i, const int &j) {
return w[i] < w[j];
});
int pointer = 0;
for (int &i : asks) {
while (staticEgs.size() && w[staticEgs.back()] >= b[i]) {
join(u[staticEgs.back()], v[staticEgs.back()]);
staticEgs.pop_back();
}
int preSz = ops.size();
for (int &j : toJoin[i]) {
join(u[j], v[j]);
}
ans[i] = sz[findpa(a[i])];
while ((int)ops.size() > preSz) rollBack();
}
sort(all(asks));
for (int &i : asks) cout << ans[i] << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK ""
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
if (fopen("TASK.INP", "r")) {
freopen("TASK.INP", "r", stdin);
freopen("TASK.OUT", "w", stdout);
}
/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'void Solve()':
bridges.cpp:108:21: warning: unused variable 'pointer' [-Wunused-variable]
108 | int pointer = 0;
| ^~~~~~~
bridges.cpp: In function 'int32_t main()':
bridges.cpp:134:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:135:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:139:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen("TASK.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:140:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("TASK.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |