#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mp make_pair
#define e first
#define w second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
struct edge {
int u, v, w, ind;
bool operator < (const edge &b) const {
if(w == b.w)
return ind > b.ind;
return w > b.w;
}
};
struct query {
int v, w, updatesTillNow, ind;
bool operator < (const query &b) const {
return w > b.w;
}
};
const int block = 400;
const int N = 1e5 + 1;
int n, m, q, ans[N], p[N], sz[N];
vector <int> g[N];
set <edge> edges;
vector <int> was;
edge last[N], back[N];
query qq[N];
pii upd[N];
int szupd, szqq;
edge e[N];
bool used[N], usedupd[N];
int find(int v) {
if(v == p[v])
return v;
return p[v] = find(p[v]);
}
inline void unio(int a, int b) {
a = find(a);
b = find(b);
if(a != b) {
if(sz[a] < sz[b])
swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
void dfs(int v, int ind, int w) {
used[v] = true;
was.pb(v);
ans[ind] += sz[v];
for(int to : g[v])
if(!used[to])
dfs(to, ind, w);
}
inline void solve() {
sort(qq, qq + szqq);
for(int i = 0; i < szupd; ++i)
edges.erase(e[upd[i].e]);
set <edge>::iterator it = edges.end();
if(!edges.empty())
it = edges.begin();
for(int i = 1; i <= n; ++i)
p[i] = i, sz[i] = 1;
for(int k = 0; k < szqq; ++k) {
query cur = qq[k];
while(it != edges.end() && it->w >= cur.w) {
unio(it->v, it->u);
++it;
}
for(int i = cur.updatesTillNow - 1; i >= 0; --i) {
if(!usedupd[upd[i].e]) {
edge x = e[upd[i].e];
x.w = upd[i].w;
int a = find(x.u), b = find(x.v);
if(x.w >= cur.w && a != b) {
g[a].pb(b);
g[b].pb(a);
}
}
usedupd[upd[i].e] = true;
}
for(int i = cur.updatesTillNow; i < szupd; ++i) {
if(!usedupd[upd[i].e]) {
edge x = back[i];
int a = find(x.u), b = find(x.v);
if(x.w >= cur.w && a != b) {
g[a].pb(b);
g[b].pb(a);
}
}
usedupd[upd[i].e] = true;
}
dfs(find(cur.v), cur.ind, cur.w);
for(int i = 0; i < szupd; ++i) {
usedupd[upd[i].e] = false;
g[find(e[upd[i].e].u)].clear();
g[find(e[upd[i].e].v)].clear();
}
while(!was.empty()) {
used[was.back()] = false;
was.ppb();
}
}
for(int i = 0; i < szupd; ++i) {
edges.erase(e[upd[i].e]);
e[upd[i].e].w = upd[i].w;
edges.insert(e[upd[i].e]);
}
szqq = szupd = 0;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
last[i] = e[i];
e[i].ind = i;
edges.insert(e[i]);
}
scanf("%d", &q);
for(int i = 1; i <= q; ++i) {
int c;
scanf("%d", &c);
if(c == 1) {
scanf("%d%d", &upd[szupd].e, &upd[szupd].w);
back[szupd] = last[upd[szupd].e];
last[upd[szupd].e] = e[upd[szupd].e];
last[upd[szupd].e].w = upd[szupd].w;
++szupd;
}
else {
scanf("%d%d", &qq[szqq].v, &qq[szqq].w);
qq[szqq].updatesTillNow = szupd;
qq[szqq].ind = i;
++szqq;
}
if(i % block == 0)
solve();
}
solve();
for(int i = 1; i <= q; ++i)
if(ans[i])
printf("%d\n", ans[i]);
return 0;
}
Compilation message
bridges.cpp: In function 'int main()':
bridges.cpp:141: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:143:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:148:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
bridges.cpp:151:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c);
~~~~~^~~~~~~~~~
bridges.cpp:153:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &upd[szupd].e, &upd[szupd].w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:160:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &qq[szqq].v, &qq[szqq].w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2660 KB |
Output is correct |
3 |
Correct |
24 ms |
2816 KB |
Output is correct |
4 |
Correct |
8 ms |
2816 KB |
Output is correct |
5 |
Correct |
21 ms |
2816 KB |
Output is correct |
6 |
Correct |
19 ms |
2816 KB |
Output is correct |
7 |
Correct |
20 ms |
2816 KB |
Output is correct |
8 |
Correct |
19 ms |
2816 KB |
Output is correct |
9 |
Correct |
22 ms |
2816 KB |
Output is correct |
10 |
Correct |
19 ms |
2816 KB |
Output is correct |
11 |
Correct |
19 ms |
2816 KB |
Output is correct |
12 |
Correct |
19 ms |
2856 KB |
Output is correct |
13 |
Correct |
23 ms |
2816 KB |
Output is correct |
14 |
Correct |
21 ms |
2816 KB |
Output is correct |
15 |
Correct |
23 ms |
2944 KB |
Output is correct |
16 |
Correct |
19 ms |
2964 KB |
Output is correct |
17 |
Correct |
19 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1821 ms |
10028 KB |
Output is correct |
2 |
Correct |
1666 ms |
9864 KB |
Output is correct |
3 |
Correct |
1453 ms |
9880 KB |
Output is correct |
4 |
Correct |
1284 ms |
9836 KB |
Output is correct |
5 |
Correct |
1602 ms |
9960 KB |
Output is correct |
6 |
Correct |
2063 ms |
9100 KB |
Output is correct |
7 |
Correct |
1562 ms |
9032 KB |
Output is correct |
8 |
Correct |
1804 ms |
9032 KB |
Output is correct |
9 |
Correct |
42 ms |
3292 KB |
Output is correct |
10 |
Correct |
1076 ms |
9820 KB |
Output is correct |
11 |
Correct |
1377 ms |
9900 KB |
Output is correct |
12 |
Correct |
1137 ms |
8940 KB |
Output is correct |
13 |
Correct |
1332 ms |
8952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1146 ms |
7644 KB |
Output is correct |
2 |
Correct |
507 ms |
4700 KB |
Output is correct |
3 |
Correct |
1223 ms |
7800 KB |
Output is correct |
4 |
Correct |
1206 ms |
7752 KB |
Output is correct |
5 |
Correct |
44 ms |
3320 KB |
Output is correct |
6 |
Correct |
1046 ms |
7564 KB |
Output is correct |
7 |
Correct |
1010 ms |
7768 KB |
Output is correct |
8 |
Correct |
954 ms |
7684 KB |
Output is correct |
9 |
Correct |
618 ms |
7288 KB |
Output is correct |
10 |
Correct |
606 ms |
7132 KB |
Output is correct |
11 |
Correct |
819 ms |
7564 KB |
Output is correct |
12 |
Correct |
864 ms |
7612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3004 ms |
12832 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1821 ms |
10028 KB |
Output is correct |
2 |
Correct |
1666 ms |
9864 KB |
Output is correct |
3 |
Correct |
1453 ms |
9880 KB |
Output is correct |
4 |
Correct |
1284 ms |
9836 KB |
Output is correct |
5 |
Correct |
1602 ms |
9960 KB |
Output is correct |
6 |
Correct |
2063 ms |
9100 KB |
Output is correct |
7 |
Correct |
1562 ms |
9032 KB |
Output is correct |
8 |
Correct |
1804 ms |
9032 KB |
Output is correct |
9 |
Correct |
42 ms |
3292 KB |
Output is correct |
10 |
Correct |
1076 ms |
9820 KB |
Output is correct |
11 |
Correct |
1377 ms |
9900 KB |
Output is correct |
12 |
Correct |
1137 ms |
8940 KB |
Output is correct |
13 |
Correct |
1332 ms |
8952 KB |
Output is correct |
14 |
Correct |
1146 ms |
7644 KB |
Output is correct |
15 |
Correct |
507 ms |
4700 KB |
Output is correct |
16 |
Correct |
1223 ms |
7800 KB |
Output is correct |
17 |
Correct |
1206 ms |
7752 KB |
Output is correct |
18 |
Correct |
44 ms |
3320 KB |
Output is correct |
19 |
Correct |
1046 ms |
7564 KB |
Output is correct |
20 |
Correct |
1010 ms |
7768 KB |
Output is correct |
21 |
Correct |
954 ms |
7684 KB |
Output is correct |
22 |
Correct |
618 ms |
7288 KB |
Output is correct |
23 |
Correct |
606 ms |
7132 KB |
Output is correct |
24 |
Correct |
819 ms |
7564 KB |
Output is correct |
25 |
Correct |
864 ms |
7612 KB |
Output is correct |
26 |
Correct |
1868 ms |
10108 KB |
Output is correct |
27 |
Correct |
2101 ms |
9820 KB |
Output is correct |
28 |
Correct |
2059 ms |
9944 KB |
Output is correct |
29 |
Correct |
1936 ms |
9632 KB |
Output is correct |
30 |
Correct |
1959 ms |
9772 KB |
Output is correct |
31 |
Correct |
2101 ms |
9760 KB |
Output is correct |
32 |
Correct |
2224 ms |
9820 KB |
Output is correct |
33 |
Correct |
2040 ms |
9800 KB |
Output is correct |
34 |
Correct |
2302 ms |
9788 KB |
Output is correct |
35 |
Correct |
2289 ms |
9848 KB |
Output is correct |
36 |
Correct |
1793 ms |
9760 KB |
Output is correct |
37 |
Correct |
1532 ms |
9672 KB |
Output is correct |
38 |
Correct |
2139 ms |
9720 KB |
Output is correct |
39 |
Correct |
1498 ms |
9084 KB |
Output is correct |
40 |
Correct |
987 ms |
9280 KB |
Output is correct |
41 |
Correct |
1079 ms |
9204 KB |
Output is correct |
42 |
Correct |
1228 ms |
10008 KB |
Output is correct |
43 |
Correct |
2099 ms |
9848 KB |
Output is correct |
44 |
Correct |
1548 ms |
9860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2660 KB |
Output is correct |
3 |
Correct |
24 ms |
2816 KB |
Output is correct |
4 |
Correct |
8 ms |
2816 KB |
Output is correct |
5 |
Correct |
21 ms |
2816 KB |
Output is correct |
6 |
Correct |
19 ms |
2816 KB |
Output is correct |
7 |
Correct |
20 ms |
2816 KB |
Output is correct |
8 |
Correct |
19 ms |
2816 KB |
Output is correct |
9 |
Correct |
22 ms |
2816 KB |
Output is correct |
10 |
Correct |
19 ms |
2816 KB |
Output is correct |
11 |
Correct |
19 ms |
2816 KB |
Output is correct |
12 |
Correct |
19 ms |
2856 KB |
Output is correct |
13 |
Correct |
23 ms |
2816 KB |
Output is correct |
14 |
Correct |
21 ms |
2816 KB |
Output is correct |
15 |
Correct |
23 ms |
2944 KB |
Output is correct |
16 |
Correct |
19 ms |
2964 KB |
Output is correct |
17 |
Correct |
19 ms |
2816 KB |
Output is correct |
18 |
Correct |
1821 ms |
10028 KB |
Output is correct |
19 |
Correct |
1666 ms |
9864 KB |
Output is correct |
20 |
Correct |
1453 ms |
9880 KB |
Output is correct |
21 |
Correct |
1284 ms |
9836 KB |
Output is correct |
22 |
Correct |
1602 ms |
9960 KB |
Output is correct |
23 |
Correct |
2063 ms |
9100 KB |
Output is correct |
24 |
Correct |
1562 ms |
9032 KB |
Output is correct |
25 |
Correct |
1804 ms |
9032 KB |
Output is correct |
26 |
Correct |
42 ms |
3292 KB |
Output is correct |
27 |
Correct |
1076 ms |
9820 KB |
Output is correct |
28 |
Correct |
1377 ms |
9900 KB |
Output is correct |
29 |
Correct |
1137 ms |
8940 KB |
Output is correct |
30 |
Correct |
1332 ms |
8952 KB |
Output is correct |
31 |
Correct |
1146 ms |
7644 KB |
Output is correct |
32 |
Correct |
507 ms |
4700 KB |
Output is correct |
33 |
Correct |
1223 ms |
7800 KB |
Output is correct |
34 |
Correct |
1206 ms |
7752 KB |
Output is correct |
35 |
Correct |
44 ms |
3320 KB |
Output is correct |
36 |
Correct |
1046 ms |
7564 KB |
Output is correct |
37 |
Correct |
1010 ms |
7768 KB |
Output is correct |
38 |
Correct |
954 ms |
7684 KB |
Output is correct |
39 |
Correct |
618 ms |
7288 KB |
Output is correct |
40 |
Correct |
606 ms |
7132 KB |
Output is correct |
41 |
Correct |
819 ms |
7564 KB |
Output is correct |
42 |
Correct |
864 ms |
7612 KB |
Output is correct |
43 |
Execution timed out |
3004 ms |
12832 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |