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 ll long long
#define pb push_back
#define lb(a) ((a)&(-(a)))
const int maxn = 1e5 + 20;
const int sq = 320;
int from[maxn] , to[maxn] , w[maxn];
bool has[maxn];
int e[maxn] , nw[maxn] , s[maxn] , tmpw[maxn] , ans[maxn] , type[maxn];
vector<int> edges[maxn * 2] , query[maxn * 2];
vector<pair<int , int> > history;
int par[maxn];
int root(int v)
{
while(par[v] >= 0)
v = par[v];
return v;
}
void merge(int a , int b , bool save = 0)
{
a = root(a) , b = root(b);
if(a == b)
{
if(save)
history.pb({-1 , -1});
return;
}
if(-par[a] < -par[b])
swap(a , b);
if(save)
history.pb({par[b] , b});
par[a] += par[b];
par[b] = a;
}
void undo()
{
auto tmp = history.back();
history.pop_back();
if(tmp.second == -1)
return;
int b = tmp.second;
int a = par[b];
par[b] = tmp.first;
par[a] -= par[b];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n , m;
cin >> n >> m;
vector<int> cmp;
for(int i = 0; i < m; i++)
{
int a , b;
cin >> a >> b >> w[i];
a-- , b--;
from[i] = a , to[i] = b;
cmp.pb(w[i]);
}
int q;
cin >> q;
for(int i = 0; i < q; i++)
{
cin >> type[i];
if(type[i] == 1)
{
cin >> e[i] >> nw[i];
e[i]--;
}
else
{
cin >> s[i] >> nw[i];
s[i]--;
}
cmp.pb(nw[i]);
}
sort(cmp.begin() , cmp.end());
cmp.resize(unique(cmp.begin() , cmp.end()) - cmp.begin());
for(int i = 0; i < m; i++)
w[i] = lower_bound(cmp.begin() , cmp.end() , w[i]) - cmp.begin();
for(int i = 0; i < q; i++)
nw[i] = lower_bound(cmp.begin() , cmp.end() , nw[i]) - cmp.begin();
for(int l = 0; l < q; l += sq)
{
memset(has , 0 , sizeof has);
memset(par , -1 , sizeof par);
int sz = min(sq , q - l);
vector<int> tmp;
for(int i = l; i < l + sz; i++)
{
if(type[i] == 1)
has[e[i]] = 1 , tmp.pb(e[i]);
else
query[nw[i]].pb(i);
}
for(int i = 0; i < m; i++)
if(!has[i])
edges[w[i]].pb(i);
sort(tmp.begin() , tmp.end());
tmp.resize(unique(tmp.begin() , tmp.end()) - tmp.begin());
for(int i = (int)cmp.size() - 1; i >= 0; i--)
{
for(auto e : edges[i])
merge(from[e] , to[e]);
for(auto ind : query[i])
{
for(auto e : tmp)
tmpw[e] = w[e];
for(int j = l; j < ind; j++)
if(type[j] == 1)
tmpw[e[j]] = nw[j];
int sz = 0;
for(auto e : tmp)
if(tmpw[e] >= i)
{
merge(from[e] , to[e] , 1);
sz++;
}
ans[ind] = -par[root(s[ind])];
while(sz--)
undo();
}
edges[i].clear();
query[i].clear();
}
for(int i = l; i < l + sz; i++)
if(type[i] == 1)
w[e[i]] = nw[i];
}
for(int i = 0; i < q; i++)
if(type[i] == 2)
cout << ans[i] << endl;
}
# | 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... |