#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
struct Query{
int type, a, b, idx;
};
const int sq = 750;
int n, m, q;
int p[50010], sz[50010];
pii edges[100010];
vector<int> eg;
int w[100010];
Query qry[100010];
int ans[100010];
void init(){
iota(p, p + n + 1, 0);
fill(sz, sz + n + 1, 1);
}
int findR(int x){
return p[x] == x ? x : p[x] = findR(p[x]);
}
void join(int a, int b){
a = findR(a), b = findR(b);
if(a == b) return;
if(sz[a] > sz[b]) swap(a, b);
p[a] = b;
sz[b] += sz[a];
}
bool cmp(Query a, Query b){
return a.b > b.b;
}
bool egCmp(int a, int b){
return w[a] > w[b];
}
vector<int> adj[50010];
bitset<100010> dead, vis, visited;
vector<int> added;
void add_edge(int idx){
int u, v;
u = findR(edges[idx].fi), v = findR(edges[idx].se);
if(u == v) return;
added.emplace_back(idx);
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
void remove(){
for(auto &k : added){
int u = findR(edges[k].fi), v = findR(edges[k].se);
adj[u].clear();
adj[v].clear();
visited[u] = visited[v] = false;
}
added.clear();
}
int dfs(int u){
visited[u] = true;
int ret = sz[u];
for(auto &v : adj[u]){
if(!visited[v]) ret += dfs(v);
}
return ret;
}
void Process(int l, int r){
vector<Query> updates, queries;
for(int i = l; i <= r; i++){
if(qry[i].type == 1) updates.emplace_back(qry[i]);
else queries.emplace_back(qry[i]);
}
sort(queries.begin(), queries.end(), cmp);
sort(eg.begin(), eg.end(), egCmp);
//We process from rightmost to leftmost, getting the most updated weight of edge
reverse(updates.begin(), updates.end());
for(auto &k : updates) dead[k.a] = true;
int id = 0;
init();
for(auto &k : queries){
while(id < m && w[eg[id]] >= k.b){
if(!dead[eg[id]]) join(edges[eg[id]].fi, edges[eg[id]].se);
id++;
}
for(auto &ed : updates){
if(ed.idx < k.idx){
if(vis[ed.a]) continue;
vis[ed.a] = true;
if(ed.b >= k.b) add_edge(ed.a);
}
}
for(auto &ed : updates){
if(ed.idx < k.idx) break;
if(!vis[ed.a] && w[ed.a] >= k.b) add_edge(ed.a);
}
ans[k.idx] = dfs(findR(k.a));
visited[findR(k.a)] = false;
for(auto &ed : updates){
vis[ed.a] = false;
}
remove();
}
for(auto &k : updates){
if(!dead[k.a]) continue;
dead[k.a] = false;
w[k.a] = k.b;
}
}
int main(){
// cerr << sq << "\n";
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &edges[i].fi, &edges[i].se, &w[i]);
}
scanf("%d", &q);
for(int i = 1; i <= q; i++){
scanf("%d%d%d", &qry[i].type, &qry[i].a, &qry[i].b);
qry[i].idx = i;
}
memset(ans, -1, sizeof(ans));
eg.resize(m);
iota(eg.begin(), eg.end(), 1);
for(int i = 0; i * sq + 1 <= q; i++){
Process(i * sq + 1, min(q, (i + 1) * sq));
}
for(int i = 1; i <= q; i++){
if(ans[i] == -1) continue;
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message
bridges.cpp: In function 'int main()':
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:156:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &edges[i].fi, &edges[i].se, &w[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:159:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
bridges.cpp:162:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &qry[i].type, &qry[i].a, &qry[i].b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
33 ms |
2168 KB |
Output is correct |
4 |
Correct |
7 ms |
2176 KB |
Output is correct |
5 |
Correct |
36 ms |
2168 KB |
Output is correct |
6 |
Correct |
34 ms |
2176 KB |
Output is correct |
7 |
Correct |
40 ms |
2160 KB |
Output is correct |
8 |
Correct |
34 ms |
2168 KB |
Output is correct |
9 |
Correct |
47 ms |
2168 KB |
Output is correct |
10 |
Correct |
36 ms |
2168 KB |
Output is correct |
11 |
Correct |
35 ms |
2168 KB |
Output is correct |
12 |
Correct |
38 ms |
2168 KB |
Output is correct |
13 |
Correct |
44 ms |
2168 KB |
Output is correct |
14 |
Correct |
40 ms |
2168 KB |
Output is correct |
15 |
Correct |
41 ms |
2156 KB |
Output is correct |
16 |
Correct |
40 ms |
2168 KB |
Output is correct |
17 |
Correct |
39 ms |
2168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1797 ms |
6392 KB |
Output is correct |
2 |
Correct |
1778 ms |
6264 KB |
Output is correct |
3 |
Correct |
1803 ms |
6328 KB |
Output is correct |
4 |
Correct |
1857 ms |
6244 KB |
Output is correct |
5 |
Correct |
1850 ms |
6436 KB |
Output is correct |
6 |
Correct |
2659 ms |
5356 KB |
Output is correct |
7 |
Correct |
2530 ms |
5436 KB |
Output is correct |
8 |
Correct |
2485 ms |
5516 KB |
Output is correct |
9 |
Correct |
49 ms |
3704 KB |
Output is correct |
10 |
Correct |
1856 ms |
6212 KB |
Output is correct |
11 |
Correct |
1692 ms |
6392 KB |
Output is correct |
12 |
Correct |
1451 ms |
5628 KB |
Output is correct |
13 |
Correct |
1449 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1489 ms |
5780 KB |
Output is correct |
2 |
Correct |
1335 ms |
4316 KB |
Output is correct |
3 |
Correct |
1870 ms |
5696 KB |
Output is correct |
4 |
Correct |
1486 ms |
5624 KB |
Output is correct |
5 |
Correct |
49 ms |
3704 KB |
Output is correct |
6 |
Correct |
1691 ms |
5724 KB |
Output is correct |
7 |
Correct |
1412 ms |
5488 KB |
Output is correct |
8 |
Correct |
1175 ms |
5496 KB |
Output is correct |
9 |
Correct |
967 ms |
5272 KB |
Output is correct |
10 |
Correct |
850 ms |
4984 KB |
Output is correct |
11 |
Correct |
990 ms |
5580 KB |
Output is correct |
12 |
Correct |
842 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1529 ms |
6000 KB |
Output is correct |
2 |
Correct |
50 ms |
3704 KB |
Output is correct |
3 |
Correct |
169 ms |
3708 KB |
Output is correct |
4 |
Correct |
108 ms |
3832 KB |
Output is correct |
5 |
Correct |
1040 ms |
6008 KB |
Output is correct |
6 |
Correct |
1454 ms |
5892 KB |
Output is correct |
7 |
Correct |
1016 ms |
6136 KB |
Output is correct |
8 |
Correct |
715 ms |
4984 KB |
Output is correct |
9 |
Correct |
716 ms |
4988 KB |
Output is correct |
10 |
Correct |
715 ms |
5240 KB |
Output is correct |
11 |
Correct |
1168 ms |
5496 KB |
Output is correct |
12 |
Correct |
1132 ms |
5424 KB |
Output is correct |
13 |
Correct |
1175 ms |
5636 KB |
Output is correct |
14 |
Correct |
1022 ms |
6136 KB |
Output is correct |
15 |
Correct |
994 ms |
6136 KB |
Output is correct |
16 |
Correct |
1500 ms |
5888 KB |
Output is correct |
17 |
Correct |
1498 ms |
6008 KB |
Output is correct |
18 |
Correct |
1560 ms |
5880 KB |
Output is correct |
19 |
Correct |
1542 ms |
5880 KB |
Output is correct |
20 |
Correct |
1421 ms |
5752 KB |
Output is correct |
21 |
Correct |
1343 ms |
5880 KB |
Output is correct |
22 |
Correct |
1467 ms |
6008 KB |
Output is correct |
23 |
Correct |
1004 ms |
5680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1797 ms |
6392 KB |
Output is correct |
2 |
Correct |
1778 ms |
6264 KB |
Output is correct |
3 |
Correct |
1803 ms |
6328 KB |
Output is correct |
4 |
Correct |
1857 ms |
6244 KB |
Output is correct |
5 |
Correct |
1850 ms |
6436 KB |
Output is correct |
6 |
Correct |
2659 ms |
5356 KB |
Output is correct |
7 |
Correct |
2530 ms |
5436 KB |
Output is correct |
8 |
Correct |
2485 ms |
5516 KB |
Output is correct |
9 |
Correct |
49 ms |
3704 KB |
Output is correct |
10 |
Correct |
1856 ms |
6212 KB |
Output is correct |
11 |
Correct |
1692 ms |
6392 KB |
Output is correct |
12 |
Correct |
1451 ms |
5628 KB |
Output is correct |
13 |
Correct |
1449 ms |
5460 KB |
Output is correct |
14 |
Correct |
1489 ms |
5780 KB |
Output is correct |
15 |
Correct |
1335 ms |
4316 KB |
Output is correct |
16 |
Correct |
1870 ms |
5696 KB |
Output is correct |
17 |
Correct |
1486 ms |
5624 KB |
Output is correct |
18 |
Correct |
49 ms |
3704 KB |
Output is correct |
19 |
Correct |
1691 ms |
5724 KB |
Output is correct |
20 |
Correct |
1412 ms |
5488 KB |
Output is correct |
21 |
Correct |
1175 ms |
5496 KB |
Output is correct |
22 |
Correct |
967 ms |
5272 KB |
Output is correct |
23 |
Correct |
850 ms |
4984 KB |
Output is correct |
24 |
Correct |
990 ms |
5580 KB |
Output is correct |
25 |
Correct |
842 ms |
5624 KB |
Output is correct |
26 |
Correct |
1911 ms |
6200 KB |
Output is correct |
27 |
Correct |
2305 ms |
6268 KB |
Output is correct |
28 |
Correct |
1883 ms |
6040 KB |
Output is correct |
29 |
Correct |
1376 ms |
6136 KB |
Output is correct |
30 |
Correct |
2237 ms |
6264 KB |
Output is correct |
31 |
Correct |
2310 ms |
6164 KB |
Output is correct |
32 |
Correct |
2263 ms |
6396 KB |
Output is correct |
33 |
Correct |
1887 ms |
6356 KB |
Output is correct |
34 |
Correct |
1893 ms |
6344 KB |
Output is correct |
35 |
Correct |
2009 ms |
6392 KB |
Output is correct |
36 |
Correct |
1455 ms |
6092 KB |
Output is correct |
37 |
Correct |
1445 ms |
6264 KB |
Output is correct |
38 |
Correct |
1416 ms |
6204 KB |
Output is correct |
39 |
Correct |
1198 ms |
5608 KB |
Output is correct |
40 |
Correct |
1186 ms |
5624 KB |
Output is correct |
41 |
Correct |
1176 ms |
5540 KB |
Output is correct |
42 |
Correct |
1095 ms |
6264 KB |
Output is correct |
43 |
Correct |
1127 ms |
6300 KB |
Output is correct |
44 |
Correct |
1081 ms |
6140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
1920 KB |
Output is correct |
3 |
Correct |
33 ms |
2168 KB |
Output is correct |
4 |
Correct |
7 ms |
2176 KB |
Output is correct |
5 |
Correct |
36 ms |
2168 KB |
Output is correct |
6 |
Correct |
34 ms |
2176 KB |
Output is correct |
7 |
Correct |
40 ms |
2160 KB |
Output is correct |
8 |
Correct |
34 ms |
2168 KB |
Output is correct |
9 |
Correct |
47 ms |
2168 KB |
Output is correct |
10 |
Correct |
36 ms |
2168 KB |
Output is correct |
11 |
Correct |
35 ms |
2168 KB |
Output is correct |
12 |
Correct |
38 ms |
2168 KB |
Output is correct |
13 |
Correct |
44 ms |
2168 KB |
Output is correct |
14 |
Correct |
40 ms |
2168 KB |
Output is correct |
15 |
Correct |
41 ms |
2156 KB |
Output is correct |
16 |
Correct |
40 ms |
2168 KB |
Output is correct |
17 |
Correct |
39 ms |
2168 KB |
Output is correct |
18 |
Correct |
1797 ms |
6392 KB |
Output is correct |
19 |
Correct |
1778 ms |
6264 KB |
Output is correct |
20 |
Correct |
1803 ms |
6328 KB |
Output is correct |
21 |
Correct |
1857 ms |
6244 KB |
Output is correct |
22 |
Correct |
1850 ms |
6436 KB |
Output is correct |
23 |
Correct |
2659 ms |
5356 KB |
Output is correct |
24 |
Correct |
2530 ms |
5436 KB |
Output is correct |
25 |
Correct |
2485 ms |
5516 KB |
Output is correct |
26 |
Correct |
49 ms |
3704 KB |
Output is correct |
27 |
Correct |
1856 ms |
6212 KB |
Output is correct |
28 |
Correct |
1692 ms |
6392 KB |
Output is correct |
29 |
Correct |
1451 ms |
5628 KB |
Output is correct |
30 |
Correct |
1449 ms |
5460 KB |
Output is correct |
31 |
Correct |
1489 ms |
5780 KB |
Output is correct |
32 |
Correct |
1335 ms |
4316 KB |
Output is correct |
33 |
Correct |
1870 ms |
5696 KB |
Output is correct |
34 |
Correct |
1486 ms |
5624 KB |
Output is correct |
35 |
Correct |
49 ms |
3704 KB |
Output is correct |
36 |
Correct |
1691 ms |
5724 KB |
Output is correct |
37 |
Correct |
1412 ms |
5488 KB |
Output is correct |
38 |
Correct |
1175 ms |
5496 KB |
Output is correct |
39 |
Correct |
967 ms |
5272 KB |
Output is correct |
40 |
Correct |
850 ms |
4984 KB |
Output is correct |
41 |
Correct |
990 ms |
5580 KB |
Output is correct |
42 |
Correct |
842 ms |
5624 KB |
Output is correct |
43 |
Correct |
1529 ms |
6000 KB |
Output is correct |
44 |
Correct |
50 ms |
3704 KB |
Output is correct |
45 |
Correct |
169 ms |
3708 KB |
Output is correct |
46 |
Correct |
108 ms |
3832 KB |
Output is correct |
47 |
Correct |
1040 ms |
6008 KB |
Output is correct |
48 |
Correct |
1454 ms |
5892 KB |
Output is correct |
49 |
Correct |
1016 ms |
6136 KB |
Output is correct |
50 |
Correct |
715 ms |
4984 KB |
Output is correct |
51 |
Correct |
716 ms |
4988 KB |
Output is correct |
52 |
Correct |
715 ms |
5240 KB |
Output is correct |
53 |
Correct |
1168 ms |
5496 KB |
Output is correct |
54 |
Correct |
1132 ms |
5424 KB |
Output is correct |
55 |
Correct |
1175 ms |
5636 KB |
Output is correct |
56 |
Correct |
1022 ms |
6136 KB |
Output is correct |
57 |
Correct |
994 ms |
6136 KB |
Output is correct |
58 |
Correct |
1500 ms |
5888 KB |
Output is correct |
59 |
Correct |
1498 ms |
6008 KB |
Output is correct |
60 |
Correct |
1560 ms |
5880 KB |
Output is correct |
61 |
Correct |
1542 ms |
5880 KB |
Output is correct |
62 |
Correct |
1421 ms |
5752 KB |
Output is correct |
63 |
Correct |
1343 ms |
5880 KB |
Output is correct |
64 |
Correct |
1467 ms |
6008 KB |
Output is correct |
65 |
Correct |
1004 ms |
5680 KB |
Output is correct |
66 |
Correct |
1911 ms |
6200 KB |
Output is correct |
67 |
Correct |
2305 ms |
6268 KB |
Output is correct |
68 |
Correct |
1883 ms |
6040 KB |
Output is correct |
69 |
Correct |
1376 ms |
6136 KB |
Output is correct |
70 |
Correct |
2237 ms |
6264 KB |
Output is correct |
71 |
Correct |
2310 ms |
6164 KB |
Output is correct |
72 |
Correct |
2263 ms |
6396 KB |
Output is correct |
73 |
Correct |
1887 ms |
6356 KB |
Output is correct |
74 |
Correct |
1893 ms |
6344 KB |
Output is correct |
75 |
Correct |
2009 ms |
6392 KB |
Output is correct |
76 |
Correct |
1455 ms |
6092 KB |
Output is correct |
77 |
Correct |
1445 ms |
6264 KB |
Output is correct |
78 |
Correct |
1416 ms |
6204 KB |
Output is correct |
79 |
Correct |
1198 ms |
5608 KB |
Output is correct |
80 |
Correct |
1186 ms |
5624 KB |
Output is correct |
81 |
Correct |
1176 ms |
5540 KB |
Output is correct |
82 |
Correct |
1095 ms |
6264 KB |
Output is correct |
83 |
Correct |
1127 ms |
6300 KB |
Output is correct |
84 |
Correct |
1081 ms |
6140 KB |
Output is correct |
85 |
Correct |
2542 ms |
6832 KB |
Output is correct |
86 |
Correct |
252 ms |
3832 KB |
Output is correct |
87 |
Correct |
149 ms |
3704 KB |
Output is correct |
88 |
Correct |
1578 ms |
6648 KB |
Output is correct |
89 |
Correct |
2635 ms |
7040 KB |
Output is correct |
90 |
Correct |
1595 ms |
6484 KB |
Output is correct |
91 |
Correct |
1888 ms |
6388 KB |
Output is correct |
92 |
Correct |
1881 ms |
6392 KB |
Output is correct |
93 |
Correct |
2481 ms |
5620 KB |
Output is correct |
94 |
Correct |
2101 ms |
6548 KB |
Output is correct |
95 |
Correct |
2194 ms |
6520 KB |
Output is correct |
96 |
Correct |
2017 ms |
5752 KB |
Output is correct |
97 |
Correct |
1225 ms |
6136 KB |
Output is correct |
98 |
Correct |
1334 ms |
6392 KB |
Output is correct |
99 |
Correct |
2547 ms |
6848 KB |
Output is correct |
100 |
Correct |
2500 ms |
6756 KB |
Output is correct |
101 |
Correct |
2714 ms |
7064 KB |
Output is correct |
102 |
Correct |
2674 ms |
6868 KB |
Output is correct |
103 |
Correct |
2210 ms |
5880 KB |
Output is correct |
104 |
Correct |
2238 ms |
5676 KB |
Output is correct |
105 |
Correct |
2277 ms |
5496 KB |
Output is correct |
106 |
Correct |
1559 ms |
5368 KB |
Output is correct |
107 |
Correct |
1958 ms |
5880 KB |
Output is correct |
108 |
Correct |
2488 ms |
6052 KB |
Output is correct |
109 |
Correct |
1492 ms |
5512 KB |
Output is correct |