#include<bits/stdc++.h>
#define block 200
#define maxn 100005
using namespace std;
typedef long long ll;
typedef pair < int, int > II;
int n, m;
int nq;
struct edge{
int u, v, c;
edge(int _u = 0, int _v = 0, int _c = 0) : u(_u), v(_v), c(_c) {};
bool operator < (const edge op) const {
return c > op.c;
}
};
vector < edge > e;
struct query{
int type, a, b;
query(int _type, int _a, int _b) : type(_type), a(_a), b(_b) {};
};
vector < query > q[1005];
int id[maxn], ok[maxn];
II d[maxn];
int ntem;
pair < II*, II > tem[maxn * 100];
int root(int u) {
if (u == d[u].first)
return u;
tem[ntem++] = {d + u, d[u]};
return d[u].first = root(d[u].first);
}
void join(int u, int v) {
u = root(u); v = root(v);
if (u != v) {
tem[ntem++] = {d + u, d[u]};
tem[ntem++] = {d + v, d[v]};
d[u].first = v;
d[v].second += d[u].second;
d[u].second = 0;
}
}
void undo() {
for (int i = ntem - 1; i >= 0; --i)
(*tem[i].first) = tem[i].second;
ntem = 0;
}
int k;
int main(){
#define TASK "ABC"
// freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> m;
set < pair < edge, int > > s;
for (int i = 0; i < m; ++i) {
int u, v, c;
cin >> u >> v >> c;
e.push_back({edge(u, v, c)});
s.insert({e.back(), i});
}
cin >> nq;
for (int i = 0; i < nq; ++i) {
int type, a, b;
cin >> type >> a >> b;
if (type == 1)
a--;
q[i / block].push_back(query(type, a, b));
}
for (k = 0; k <= (nq - 1) / block; ++k) {
for (int i = 1; i <= n; ++i)
d[i] = {i, 1};
ntem = 0;
vector < int > ret;
for (int i = 0; i < q[k].size(); ++i)
if (q[k][i].type == 2)
ret.push_back(i);
sort(ret.begin(), ret.end(), [](int i, int j){
return q[k][i].b > q[k][j].b;
});
vector < int > ans(q[k].size());
auto iter = s.begin();
for (auto x : ret) {
for (int i = 0; i < q[k].size(); ++i)
if (q[k][i].type == 1)
ok[q[k][i].a] = 1;
while (iter != s.end() && (iter -> first).c >= q[k][x].b) {
if (!ok[iter -> second])
join((iter -> first).u, (iter -> first).v);
iter++;
}
ntem = 0;
for (int i = 0; i < q[k].size(); ++i)
if (q[k][i].type == 1)
ok[q[k][i].a] = 0;
for (int i = 0; i < x; ++i)
if (q[k][i].type == 1)
ok[q[k][i].a] = 1;
for (int i = q[k].size() - 1; i >= 0; --i)
if (q[k][i].type == 1) {
if ((i < x && ok[q[k][i].a] && q[k][i].b >= q[k][x].b) || (i >= x && !ok[q[k][i].a] && e[q[k][i].a].c >= q[k][x].b))
join(e[q[k][i].a].u, e[q[k][i].a].v);
if (i >= x && ok[q[k][i].a]) continue;
ok[q[k][i].a] = 0;
}
for (int i = 0; i < q[k].size(); ++i)
if (q[k][i].type == 1)
ok[q[k][i].a] = 0;
ans[x] = d[root(q[k][x].a)].second;
undo();
}
for (int i = 0; i < q[k].size(); ++i)
if (q[k][i].type == 2)
cout << ans[i] << '\n';
else {
s.erase({e[q[k][i].a], q[k][i].a});
e[q[k][i].a].c = q[k][i].b;
s.insert({e[q[k][i].a], q[k][i].a});
}
}
return 0;
}
Compilation message
bridges.cpp: In function 'int main()':
bridges.cpp:99:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < q[k].size(); ++i)
~~^~~~~~~~~~~~~
bridges.cpp:111:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < q[k].size(); ++i)
~~^~~~~~~~~~~~~
bridges.cpp:123:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < q[k].size(); ++i)
~~^~~~~~~~~~~~~
bridges.cpp:139:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < q[k].size(); ++i)
~~^~~~~~~~~~~~~
bridges.cpp:148:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < q[k].size(); ++i)
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
35 ms |
640 KB |
Output is correct |
4 |
Correct |
17 ms |
512 KB |
Output is correct |
5 |
Correct |
32 ms |
512 KB |
Output is correct |
6 |
Correct |
30 ms |
512 KB |
Output is correct |
7 |
Correct |
35 ms |
512 KB |
Output is correct |
8 |
Correct |
32 ms |
512 KB |
Output is correct |
9 |
Correct |
40 ms |
512 KB |
Output is correct |
10 |
Correct |
32 ms |
512 KB |
Output is correct |
11 |
Correct |
31 ms |
512 KB |
Output is correct |
12 |
Correct |
33 ms |
512 KB |
Output is correct |
13 |
Correct |
37 ms |
512 KB |
Output is correct |
14 |
Correct |
35 ms |
512 KB |
Output is correct |
15 |
Correct |
36 ms |
512 KB |
Output is correct |
16 |
Correct |
36 ms |
512 KB |
Output is correct |
17 |
Correct |
34 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1900 ms |
6820 KB |
Output is correct |
2 |
Correct |
1893 ms |
6832 KB |
Output is correct |
3 |
Correct |
1841 ms |
7564 KB |
Output is correct |
4 |
Correct |
1957 ms |
7596 KB |
Output is correct |
5 |
Correct |
1987 ms |
7600 KB |
Output is correct |
6 |
Correct |
2441 ms |
9772 KB |
Output is correct |
7 |
Correct |
2356 ms |
9888 KB |
Output is correct |
8 |
Correct |
2233 ms |
9708 KB |
Output is correct |
9 |
Correct |
139 ms |
2936 KB |
Output is correct |
10 |
Execution timed out |
3076 ms |
8364 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1804 ms |
5368 KB |
Output is correct |
2 |
Correct |
1072 ms |
3064 KB |
Output is correct |
3 |
Correct |
2593 ms |
6196 KB |
Output is correct |
4 |
Correct |
2158 ms |
6580 KB |
Output is correct |
5 |
Correct |
135 ms |
3192 KB |
Output is correct |
6 |
Correct |
2368 ms |
6836 KB |
Output is correct |
7 |
Correct |
2426 ms |
6836 KB |
Output is correct |
8 |
Correct |
2129 ms |
6836 KB |
Output is correct |
9 |
Correct |
2137 ms |
7092 KB |
Output is correct |
10 |
Correct |
1997 ms |
6712 KB |
Output is correct |
11 |
Correct |
2054 ms |
7168 KB |
Output is correct |
12 |
Correct |
1798 ms |
7872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3080 ms |
10904 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1900 ms |
6820 KB |
Output is correct |
2 |
Correct |
1893 ms |
6832 KB |
Output is correct |
3 |
Correct |
1841 ms |
7564 KB |
Output is correct |
4 |
Correct |
1957 ms |
7596 KB |
Output is correct |
5 |
Correct |
1987 ms |
7600 KB |
Output is correct |
6 |
Correct |
2441 ms |
9772 KB |
Output is correct |
7 |
Correct |
2356 ms |
9888 KB |
Output is correct |
8 |
Correct |
2233 ms |
9708 KB |
Output is correct |
9 |
Correct |
139 ms |
2936 KB |
Output is correct |
10 |
Execution timed out |
3076 ms |
8364 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
35 ms |
640 KB |
Output is correct |
4 |
Correct |
17 ms |
512 KB |
Output is correct |
5 |
Correct |
32 ms |
512 KB |
Output is correct |
6 |
Correct |
30 ms |
512 KB |
Output is correct |
7 |
Correct |
35 ms |
512 KB |
Output is correct |
8 |
Correct |
32 ms |
512 KB |
Output is correct |
9 |
Correct |
40 ms |
512 KB |
Output is correct |
10 |
Correct |
32 ms |
512 KB |
Output is correct |
11 |
Correct |
31 ms |
512 KB |
Output is correct |
12 |
Correct |
33 ms |
512 KB |
Output is correct |
13 |
Correct |
37 ms |
512 KB |
Output is correct |
14 |
Correct |
35 ms |
512 KB |
Output is correct |
15 |
Correct |
36 ms |
512 KB |
Output is correct |
16 |
Correct |
36 ms |
512 KB |
Output is correct |
17 |
Correct |
34 ms |
512 KB |
Output is correct |
18 |
Correct |
1900 ms |
6820 KB |
Output is correct |
19 |
Correct |
1893 ms |
6832 KB |
Output is correct |
20 |
Correct |
1841 ms |
7564 KB |
Output is correct |
21 |
Correct |
1957 ms |
7596 KB |
Output is correct |
22 |
Correct |
1987 ms |
7600 KB |
Output is correct |
23 |
Correct |
2441 ms |
9772 KB |
Output is correct |
24 |
Correct |
2356 ms |
9888 KB |
Output is correct |
25 |
Correct |
2233 ms |
9708 KB |
Output is correct |
26 |
Correct |
139 ms |
2936 KB |
Output is correct |
27 |
Execution timed out |
3076 ms |
8364 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |