이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
* Divide queries into blocks of sqrt(m).
* For each block, at max sqrt(m) edges will be changed.
* For the edges which do not change, maintain a set of nodes
*/
#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
class UFDS {
public:
vi p, rank, sz; int count = 0;
void build(int n) {
p.assign(n, 0); rank.assign(n, 0); sz.assign(n, 1);
for_(i, 0, n) p[i] = i;
count = n;
}
int getParent(int i) {
return (p[i] == i) ? i : (p[i] = getParent(p[i]));
}
void join(int i, int j) {
int a = getParent(i), b = getParent(j);
if (a == b) return;
count -= 1;
if (rank[a] > rank[b]) {
p[b] = a;
sz[a] += sz[b];
} else {
p[a] = b;
if (rank[a] == rank[b]) rank[b] += 1;
sz[b] += sz[a];
}
}
};
const int bs = 800;
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<array<int, 2>> edge(m); // {a, b, w}
vector<int> wt(m);
for_(i, 0, m) {
cin >> edge[i][0] >> edge[i][1] >> wt[i];
edge[i][0] -= 1; edge[i][1] -= 1;
}
int q; cin >> q;
vector<array<int, 3>> qr(q); // {for car: {type (2), weight, node}
for_(i, 0, q) {
cin >> qr[i][0] >> qr[i][1] >> qr[i][2];
qr[i][1] -= 1;
if (qr[i][0] == 2) swap(qr[i][1], qr[i][2]);
}
vector<bool> change(m, false);
UFDS ufds;
vector<array<int, 2>> same;
int pt = 0;
vector<vi> adj(n);
vi adjChange(n), changed, lastSeen(n, -1);
vector<int> currWt(m);
vector<vector<array<int, 2>>> carQr;
for_(qq, 0, q) {
if ((qq % bs) == 0) carQr.push_back({});
if (qr[qq][0] == 2) carQr[carQr.size()-1].push_back({qr[qq][1], qq});
}
for_(i, 0, carQr.size()) sort(carQr[i].begin(), carQr[i].end(), greater<>());
int bpt = 0;
vi fin(q, -1);
for (auto &b: carQr) {
int bStart = bpt*bs;
if (b.size()) {
pt = 0;
ufds.build(n); same.clear(); changed.clear(); change.assign(m, false);
for_(i, bStart, min(q, bStart+bs)) if (qr[i][0] == 1) change[qr[i][1]] = true;
for_(i, 0, m) {
if (!change[i]) same.push_back({wt[i], i});
else changed.push_back(i);
}
sort(same.begin(), same.end(), greater<>());
for (auto &car: b) {
int qq = car[1];
while (pt < same.size() and same[pt][0] >= qr[qq][1]) {
ufds.join(edge[same[pt][1]][0], edge[same[pt][1]][1]);
pt++;
}
for (auto &e: changed) currWt[e] = wt[e];
for_(i, bStart, qq) if (qr[i][0] == 1) currWt[qr[i][1]] = qr[i][2];
for (auto &e: changed) {
if (currWt[e] < qr[qq][1]) continue;
int c1 = ufds.getParent(edge[e][0]), c2 = ufds.getParent(edge[e][1]);
if (c1 == c2) continue;
if (adjChange[c1] != qq) {
adjChange[c1] = qq;
adj[c1].clear();
}
if (adjChange[c2] != qq) {
adjChange[c2] = qq;
adj[c2].clear();
}
adj[c1].push_back(c2); adj[c2].push_back(c1);
}
int cp = ufds.getParent(qr[qq][2]);
int ans = ufds.sz[cp];
queue<int> qu;
lastSeen[cp] = qq;
qu.push(cp);
while (qu.size()) {
int p = qu.front(); qu.pop();
if (adjChange[p] == qq) for (auto i: adj[p]) {
if (lastSeen[i] == qq) continue;
lastSeen[i] = qq;
qu.push(i);
ans += ufds.sz[i];
}
}
fin[qq] = ans;
}
}
for_(i, bStart, min((bpt+1)*bs, q)) if (qr[i][0] == 1) wt[qr[i][1]] = qr[i][2];
bpt++;
}
for (auto i: fin) if (i != -1) cout << i << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'int main()':
bridges.cpp:100:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | while (pt < same.size() and same[pt][0] >= qr[qq][1]) {
| ~~~^~~~~~~~~~~~~
# | 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... |