이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
const int M = 1e5 + 5;
const int bucket_size = 250;
struct edges{
int x, y, w, p, w2, p2;
bool operator < (const edges &aux)const{
if(w != aux.w) return w > aux.w;
return p < aux.p;
}
};
inline bool cmp(edges x, edges y){
return x.p2 > y.p2;
}
struct query{
int x, w, p;
bool operator < (const query &aux)const{
if(w != aux.w) return w > aux.w;
return p < aux.p;
}
};
edges a[M], e[M], b[bucket_size + 5];
query Q[bucket_size + 5];
int n, m, q;
bool uz[M], f[M];
int SZ[N], TT[N];
int tip[M], ans[M];
vector <int> v[N];
inline int find(int x){
int R = x;
while(R != TT[R]) R = TT[R];
while(x != R){
int aux = TT[x];
TT[x] = R;
x = aux;
}
return R;
}
inline void unite(int x, int y){
if(x == y) return ;
if(SZ[x] > SZ[y]) SZ[x] += SZ[y], TT[y] = x;
else SZ[y] += SZ[x], TT[x] = y;
}
bool viz[N];
int dfs(int nod){
int ans = SZ[nod];
viz[nod] = 1;
for(auto it : v[nod]){
if(viz[it]) continue ;
ans += dfs(it);
}
return ans;
}
void bdfs(int nod){
viz[nod] = 0;
for(auto it : v[nod])
if(viz[it]) bdfs(it);
}
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d%d", &n, &m);
for(int i = 1; i <= m ; ++i){
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].w);
a[i].p = i;
e[i] = a[i];
}
scanf("%d", &q);
int x, y, w;
for(int i = 1; i <= q ; i += bucket_size){
memset(uz, 0, sizeof(uz));
sort(a + 1, a + m + 1);
for(int i = 1; i <= n ; ++i) TT[i] = i, SZ[i] = 1;
int j = min(q, i + bucket_size - 1), nr = 0, k = 0;
for(int t = i; t <= j ; ++t){
scanf("%d%d%d", &tip[t], &x, &w);
if(tip[t] == 1){
b[++k] = {e[x].x, e[x].y, w, t, e[x].w, e[x].p2};
uz[x] = 1;
e[x].w = w; e[x].p2 = t;
}
else Q[++nr] = {x, w, t};
}
sort(Q + 1, Q + nr + 1);
j = 1;
for(int i = 1; i <= nr ; ++i){
while(j <= m && Q[i].w <= a[j].w){
if(!uz[a[j].p]) unite(find(a[j].x), find(a[j].y));
++j;
}
for(int t = 1; t <= k ; ++t){
if(b[t].p2 > Q[i].p) continue ;
if((b[t].p > Q[i].p && b[t].w2 >= Q[i].w) || (b[t].p < Q[i].p && b[t].w >= Q[i].w)){
x = find(b[t].x); y = find(b[t].y);
if(x != y){
v[x].push_back(y);
v[y].push_back(x);
}
}
}
ans[Q[i].p] = dfs(find(Q[i].x));
bdfs(find(Q[i].x));
for(int t = 1; t <= k ; ++t){
x = find(b[t].x); y = find(b[t].y);
v[x].clear();
v[y].clear();
}
}
for(int i = 1; i <= m ; ++i){
a[i].w = e[a[i].p].w;
e[i].p2 = 0;
}
}
for(int i = 1; i <= q ; ++i)
if(tip[i] == 2) printf("%d\n", ans[i]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridges.cpp: In function 'int main()':
bridges.cpp:72: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:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
bridges.cpp:90:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &tip[t], &x, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |