This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define sz size
const int mxN = 1e5;
struct edge {
int a,b,w;
};
edge edges[mxN];
int p[mxN];
int ssize[mxN];
int cnt[mxN];
int gsize;
bool comp(int a, int b){
return (edges[a].w > edges[b].w);
}
void group(int u, int v){
while (p[u] != p[p[u]]) p[u] = p[p[u]];
while (p[v] != p[p[v]]) p[v] = p[p[v]];
u = p[u];
v = p[v];
if (u==v) return;
if (ssize[u] < ssize[v]) swap(u,v);
ssize[u] += ssize[v];
p[v] = u;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(cnt,0,sizeof(cnt));
int n,m;
cin >> n >> m;
for (int i =0; i < m; i++){
cin >> edges[i].a >> edges[i].b >> edges[i].w;
edges[i].a -= 1;
edges[i].b -= 1;
}
int q;
cin >> q;
edge all[q];
for (int i = 0; i < q; i++){
cin >> all[i].a >> all[i].b >> all[i].w;
all[i].b -= 1;
}
gsize = sqrt(q);
vector<int> curre;
vector<int> cvis;
vector<int> ans;
int cidx;
int res[q];
// cout <<"GISZE " << gsize << "\n";
for (int i =0; i < (int) ceil((double) q/gsize); i++){
curre.clear();
ans.clear();
cvis.clear();
/* cout << "W: ";
for (int j = 0; j < m; j++) cout << edges[j].w << " ";
cout << '\n';*/
for (int j = i*gsize; j < min(q,(i+1)*gsize); j++){
if (all[j].a == 1 && cnt[all[j].b] != i+1){
cnt[all[j].b] = i+1;
cvis.pb(j);
}
else{
ans.pb(j);
}
}
for (int j = 0; j < m; j++) if (cnt[j] != i+1) curre.pb(j);
sort(curre.begin(),curre.end(),comp);
sort(ans.begin(),ans.end(),comp);
/* cout << i << ":\n";
for (int j : curre) cout << j << " ";
cout << '\n';
for (int j : ans) cout << j << " ";
cout << "\n";*/
for (int j = 0; j < n; j++){
p[j] = j;
ssize[j] = 1;
}
cidx = 0;
stack<pii> upd;
int u,v;
for (int j = 0; j < ans.sz(); j++){
//Adding on non-changing edges
// cout << j <<" - ";
while (cidx < curre.sz() && edges[curre[cidx]].w >= all[ans[j]].w){
// cout << curre[cidx] << " ";
group(edges[curre[cidx]].a,edges[curre[cidx]].b);
cidx++;
}
for (int k = ans[j]-1; k >= i*gsize; k--){
// cout << all[k].b << " ";
if (all[k].a == 2 || all[k].w < all[ans[j]].w) continue;
if (cnt[all[k].b] != q+i+1){
cnt[all[k].b] = q+i+1;
upd.push({edges[all[k].b].a,edges[all[k].b].b});
group(edges[all[k].b].a,edges[all[k].b].b);
}
}
for (int k : cvis){
if (cnt[all[k].b] == q+i+1){
cnt[all[k].b] = i+1;
continue;
}
cnt[all[k].b] = i+1;
// cout << all[k].b << " ";
upd.push({edges[all[k].b].a,edges[all[k].b].b});
group(edges[all[k].b].a,edges[all[k].b].b);
}
// cout << '\n';
int c = all[ans[j]].b;
int prev = p[c];
while (p[c] != p[p[c]]) p[c] = p[p[c]];
res[ans[j]] = ssize[p[c]];
p[c] = prev;
pii temp;
while (upd.sz() > 0){
temp = upd.top();
upd.pop();
u = temp.f;
v = temp.s;
if (ssize[u] < ssize[v]) swap(u,v);
ssize[u] -= ssize[v];
p[v] = v;
}
}
for (int j : cvis) edges[all[j].b].w = all[j].w;
//sort(curre.begin(),curre.end(),comp);
}
for (int i =0; i < q; i++){
if (all[i].a == 1) continue;
cout << res[i] << "\n";
}
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int j = 0; j < ans.sz(); j++){
| ~~^~~~~~~~~~
bridges.cpp:101:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | while (cidx < curre.sz() && edges[curre[cidx]].w >= all[ans[j]].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... |