이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
const int N = (int)5e4 + 5, M = (int)1e5 + 5, Q = M, NGU = 320;
int n, m, q, ans[Q];
vector<int> gr[N], contain;
bool vis[N];
struct Edge {
int u, v, d;
Edge (int _u = 0, int _v = 0, int _d = 0) : u(_u), v(_v), d(_d) {}
bool operator< (const Edge &_) const { return d > _.d; }
} edge[M], edge_[M];
struct Query {
int t, b, r;
Query (int _t = 0, int _b = 0, int _r = 0) : t(_t), b(_b), r(_r) {}
bool operator< (const Query &_) const { return r > _.r; }
} query[Q];
struct Dsu {
int pSet[N], szSet[N];
void init () {
for (int i = 1; i <= n; ++i) pSet[i] = i, szSet[i] = 1;
}
int findSet (int i) { return i == pSet[i] ? i : pSet[i] = findSet(pSet[i]); }
void unionSet (int i, int j) {
i = findSet(i); j = findSet(j);
if (i == j) return ;
if (szSet[i] > szSet[j]) swap(i, j);
szSet[j] += szSet[i];
pSet[i] = j;
}
} dsu;
void dfs (int u) {
if (vis[u]) return ;
vis[u] = 1; contain.pb(u);
for (int v : gr[u]) dfs(v);
}
int main () {
// freopen("test.INP", "r", stdin);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v, d; scanf("%d %d %d", &u, &v, &d);
edge_[i] = edge[i] = Edge(u, v, d);
}
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
int t, b, r; scanf("%d %d %d", &t, &b, &r);
query[i] = Query(t, b, r);
}
for (int i = 1; i <= q; i += NGU) {
dsu.init();
vector< pair<Query, int> > vec;
vector<bool> exist(N);
for (int j = i; j <= min(q, i + NGU - 1); ++j) {
if (query[j].t == 2) vec.pb( { query[j], j } );
else exist[ query[j].b ] = 1;
}
sort(vec.begin(), vec.end() );
vector<Edge> e;
for (int j = 1; j <= m; ++j) if (!exist[j]) e.pb(edge[j]);
sort(e.begin(), e.end() );
int iE = 0;
for (auto _ : vec) {
Query _query = _.fi;
// cout << _.se << "*********\n";
for (; iE < (int)e.size() && e[iE].d >= _query.r; ++iE) dsu.unionSet(e[iE].u, e[iE].v);
for (int j = i; j <= _.se; ++j) if (query[j].t == 1) edge_[ query[j].b ].d = query[j].r;
for (int j = i; j <= min(q, i + NGU - 1); ++j) if (query[j].t == 1 && edge_[ query[j].b ].d >= _query.r) {
gr[ dsu.findSet(edge[ query[j].b ].u) ].pb(dsu.findSet(edge[ query[j].b ].v) );
gr[ dsu.findSet(edge[ query[j].b ].v) ].pb(dsu.findSet(edge[ query[j].b ].u) );
}
dfs(dsu.findSet(_query.b) );
for (int __ : contain) {
ans[_.se] += dsu.szSet[__];
vis[__] = 0;
}
contain.clear();
for (int j = i; j <= min(q, i + NGU - 1); ++j) if (query[j].t == 1 && edge[ query[j].b ].d >= _query.r) {
gr[ dsu.findSet(edge[ query[j].b ].u) ].clear();
gr[ dsu.findSet(edge[ query[j].b ].v) ].clear();
}
for (int j = i; j <= _.se; ++j) if (query[j].t == 1) edge_[ query[j].b ].d = edge[ query[j].b ].d;
}
for (int j = i; j <= min(q, i + NGU - 1); ++j) if (query[j].t == 1) edge_[ query[j].b ].d = edge[ query[j].b ].d = query[j].r;
}
for (int i = 1; i <= q; ++i) if (query[i].t == 2) printf("%d\n", ans[i]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'int main()':
bridges.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
bridges.cpp:51:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v, d; scanf("%d %d %d", &u, &v, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
bridges.cpp:57:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int t, b, r; scanf("%d %d %d", &t, &b, &r);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |