이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 1;
int nra = 1;
int bit[nmax];
int n, m;
int root[nmax], sz[nmax];
int mr[nmax], ms[nmax];
struct DSU {
void init() {
for(int i = 1; i <= n; i++) {
root[i] = i;
sz[i] = 1;
}
}
int find(int a) {
if(root[a] == a) {
return a;
}
return root[a] = find(root[a]);
}
void unite(int a, int b) {
if(a == b) {
return;
}
if(sz[a] > sz[b]) {
swap(a, b);
}
root[a] = b;
sz[b] += sz[a];
}
int mfind(int a) {
if(bit[a] != nra) {
bit[a] = nra;
mr[a] = find(a);
ms[a] = sz[find(a)];
}
if(mr[a] == a) {
return a;
}
return mr[a] = mfind(mr[a]);
}
void munite(int a, int b) {
if(a == b) {
return;
}
if(ms[a] > ms[b]) {
swap(a, b);
}
mr[a] = b;
ms[b] += ms[a];
}
};
struct str {
int a, b, c;
};
struct special {
int t, a, b, c;
};
vector<str> qry, upd, edges;
vector<special> e2;
int ans[nmax], mark[nmax], to[nmax], ult[nmax];
int pas = 1;
bool cmp(special x, special y) {
if(x.c != y.c) {
return x.c > y.c;
}
return x.t < y.t;
}
bool cmp2(str x, str y) {
return x.c < y.c;
}
bool cmp3(str x, str y) {
return x.b > y.b;
}
void solve() {
vector<special> curr, doar;
vector<pair<int, int>> unsafe;
for(int i = 1; i <= m; i++) {
if(mark[i] == pas) {
unsafe.push_back({i, edges[i].c});
}
}
// cout << "-------\n";
sort(qry.begin(), qry.end(), cmp3);
int ie = 0, iq = 0;
while(ie < e2.size() || iq < qry.size()) {
while(ie < e2.size() && mark[e2[ie].t] == pas) {
ie++;
}
if(iq >= qry.size() || (ie < e2.size() && e2[ie].c >= qry[iq].b)) {
curr.push_back({0, e2[ie].a, e2[ie].b, e2[ie].c});
doar.push_back({e2[ie].t, e2[ie].a, e2[ie].b, e2[ie].c});
ie++;
} else {
curr.push_back({1, qry[iq].a, qry[iq].b, qry[iq].c});
iq++;
}
}
DSU ds;
ds.init();
for(auto [t, a, b, c] : curr) {
if(t == 0) {
ds.unite(ds.find(a), ds.find(b));
} else {
for(auto it : upd) {
if(it.c <= c) {
edges[it.a].c = it.b;
}
}
for(auto it : unsafe) {
if(edges[it.first].c >= b) {
ds.munite(ds.mfind(edges[it.first].a), ds.mfind(edges[it.first].b));
}
edges[it.first].c = it.second;
}
ans[c] = ms[ds.mfind(a)];
nra++;
}
}
pas++;
/*
for(auto it : upd) {
edges[it.a].c = it.b;
}
e2.clear();
for(int i = 1; i <= m; i++) {
e2.push_back({i, edges[i].a, edges[i].b, edges[i].c});
}
sort(e2.begin(), e2.end(), cmp);
*/
sort(upd.begin(), upd.end(), cmp2);
reverse(upd.begin(), upd.end());
vector<special> up;
for(auto [a, b, c] : upd) {
if(ult[a] != pas) {
ult[a] = pas;
up.push_back({c, edges[a].a, edges[a].b, b});
// cout << edges[a].a << " " << edges[a].b << " " << b << '\n';
}
}
sort(up.begin(), up.end(), cmp);
int iu = 0, id = 0;
e2.clear();
// cout << upd.size() << " " << doar.size() << '\n';
while(iu < upd.size() || id < doar.size()) {
// cout << up[iu].c << " " << doar[id].c << '\n';
if(id >= doar.size() || (iu < upd.size() && up[iu].c > doar[id].c)) {
e2.push_back(up[iu]);
iu++;
} else {
e2.push_back(doar[id]);
id++;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
edges.push_back({-1, -1, -1});
for(int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
if(a > b) {
swap(a, b);
}
edges.push_back({a, b, c});
e2.push_back({i, a, b, c});
}
sort(e2.begin(), e2.end(), cmp);
for(int i = 0; i < m; i++) {
to[e2[i].t] = i;
}
int q;
cin >> q;
int block = sqrt(q);
for(int i = 1; i <= q; i++) {
int t, a, b;
cin >> t >> a >> b;
if(t == 1) {
// update
mark[a] = pas;
upd.push_back({a, b, i});
} else {
// query
qry.push_back({a, b, i});
}
if(i % block == 0 || i == q) {
solve();
upd.clear();
qry.clear();
}
}
for(int i = 1; i <= q; i++) {
if(ans[i] != 0) {
cout << ans[i] << "\n";
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'void solve()':
bridges.cpp:104:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<special>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | while(ie < e2.size() || iq < qry.size()) {
| ~~~^~~~~~~~~~~
bridges.cpp:104:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<str>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | while(ie < e2.size() || iq < qry.size()) {
| ~~~^~~~~~~~~~~~
bridges.cpp:105:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<special>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | while(ie < e2.size() && mark[e2[ie].t] == pas) {
| ~~~^~~~~~~~~~~
bridges.cpp:108:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<str>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | if(iq >= qry.size() || (ie < e2.size() && e2[ie].c >= qry[iq].b)) {
| ~~~^~~~~~~~~~~~~
bridges.cpp:108:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<special>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | if(iq >= qry.size() || (ie < e2.size() && e2[ie].c >= qry[iq].b)) {
| ~~~^~~~~~~~~~~
bridges.cpp:163:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<str>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | while(iu < upd.size() || id < doar.size()) {
| ~~~^~~~~~~~~~~~
bridges.cpp:163:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<special>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | while(iu < upd.size() || id < doar.size()) {
| ~~~^~~~~~~~~~~~~
bridges.cpp:165:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<special>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
165 | if(id >= doar.size() || (iu < upd.size() && up[iu].c > doar[id].c)) {
| ~~~^~~~~~~~~~~~~~
bridges.cpp:165:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<str>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
165 | if(id >= doar.size() || (iu < upd.size() && up[iu].c > doar[id].c)) {
| ~~~^~~~~~~~~~~~
# | 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... |