이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define v first
#define cost second
#define push_back emplace_back
using ii = pair<int, int>;
const int magic = 400;
const int maxn = 50000 + 5;
int n, m, Q;
int l[maxn << 1], r[maxn << 1], w[maxn << 1];
int ww[maxn << 1];
int par[maxn], sz[maxn];
vector<int> op, id, cost;
int in_buffer[maxn << 1];
int ans[maxn];
vector<int> g[maxn];
vector<int> clr;
vector<int> p, q;
vector<int> ord;
struct List {
int lst[maxn << 1];
int ptr;
List() { ptr = 0; }
inline void insert(int x) { lst[ptr++] = x; }
inline void clear() { ptr = 0; }
} xyz;
int vis[maxn];
int find(int u) {
return par[u] == u ? u : par[u] = find(par[u]);
}
void unite(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return;
if(sz[u] > sz[v]) swap(u, v);
sz[v] += sz[u];
par[u] = v;
}
void dfs(int u, int &res) {
vis[u] = 1;
xyz.insert(u);
res += sz[u];
for(int v : g[u]) {
if(!vis[v]) {
dfs(v, res);
}
}
}
void solve() {
for(int i = 0; i < op.size(); i++) {
if(op[i] == 1) in_buffer[id[i]] = 1;
}
for(int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
}
for(int i = 0; i < m; i++) {
if(!in_buffer[ord[i]]) {
p.push_back(ord[i]);
}
}
for(int i = 0; i < op.size(); i++) {
if(op[i] == 1) in_buffer[id[i]] = 0;
else q.push_back(i);
}
sort(q.begin(), q.end(), [&](int x, int y){ return cost[x] > cost[y]; });
int i = p.size() - 1;
for(int j : q) {
while(i >= 0 and w[p[i]] >= cost[j]) {
unite(l[p[i]], r[p[i]]);
i--;
}
for(int k = 0; k < j; k++) if(op[k] == 1) {
ww[id[k]] = cost[k];
}
int u, v;
for(int k = 0; k < j; k++) if(op[k] == 1) {
if(ww[id[k]] >= cost[j]) {
u = l[id[k]];
v = r[id[k]];
u = find(u);
v = find(v);
g[u].push_back(v);
g[v].push_back(u);
clr.push_back(u);
clr.push_back(v);
}
}
for(int k = j + 1; k < op.size(); k++) if(op[k] == 1) {
if(!ww[id[k]]) {
if(w[id[k]] >= cost[j]) {
u = l[id[k]];
v = r[id[k]];
u = find(u);
v = find(v);
g[u].push_back(v);
g[v].push_back(u);
clr.push_back(u);
clr.push_back(v);
}
}
}
ans[j] = 0;
dfs(find(id[j]), ans[j]);
for(int k : clr) g[k].clear();
for(int k = 0; k < xyz.ptr; k++)
vis[xyz.lst[k]] = 0;
clr.clear();
xyz.clear();
for(int k = 0; k < j; k++) if(op[k] == 1)
ww[id[k]] = 0;
}
for(int i = 0; i < op.size(); i++) if(op[i] == 1)
w[id[i]] = cost[i];
q.clear();
for(int i = 0; i < op.size(); i++) if(op[i] == 1)
q.push_back(id[i]);
sort(q.begin(), q.end());
q.erase(unique(q.begin(), q.end()), q.end());
sort(q.begin(), q.end(), [](int x, int y) { return w[x] < w[y]; });
ord.clear();
merge(p.begin(), p.end(), q.begin(), q.end(), back_inserter(ord), [](int x, int y){ return w[x] < w[y]; });
for(int i = 0; i < op.size(); i++) {
if(op[i] == 2) {
printf("%d\n", ans[i]);
}
}
p.clear();
q.clear();
op.clear();
id.clear();
cost.clear();
}
int main(int argc, char const *argv[])
{
// freopen("in", "r", stdin);
scanf("%d %d", &n, &m);
{
ord.reserve((maxn << 1) + 10);
p.reserve((maxn << 1) + 10);
q.reserve((maxn << 1) + 10);
op.reserve((maxn << 1) + 10);
id.reserve((maxn << 1) + 10);
cost.reserve((maxn << 1) + 10);
clr.reserve((maxn << 1) + 10);
}
for(int i = 0; i < m; i++) {
scanf("%d %d %d", &l[i], &r[i], &w[i]);
ord.push_back(i);
}
sort(ord.begin(), ord.end(), [](int x, int y) { return w[x] < w[y]; } );
scanf("%d", &Q);
int done = 0;
while(Q--) {
int op, id, cost;
scanf("%d %d %d", &op, &id, &cost);
if(op == 1) id--;
::op.push_back(op);
::id.push_back(id);
::cost.push_back(cost);
if(::op.size() == magic and !done) {
solve();
}
}
if(op.size()) {
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'void solve()':
bridges.cpp:57:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < op.size(); i++) {
~~^~~~~~~~~~~
bridges.cpp:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < op.size(); i++) {
~~^~~~~~~~~~~
bridges.cpp:97:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = j + 1; k < op.size(); k++) if(op[k] == 1) {
~~^~~~~~~~~~~
bridges.cpp:124:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < op.size(); i++) if(op[i] == 1)
~~^~~~~~~~~~~
bridges.cpp:128:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < op.size(); i++) if(op[i] == 1)
~~^~~~~~~~~~~
bridges.cpp:137:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < op.size(); i++) {
~~^~~~~~~~~~~
bridges.cpp: In function 'int main(int, const char**)':
bridges.cpp:153:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
bridges.cpp:164:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &l[i], &r[i], &w[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:168:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
bridges.cpp:172:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &op, &id, &cost);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |