이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 , bool compress = 1)
{
if(compress)
return (par[v] < 0? v : par[v] = root(par[v]));
while(par[v] >= 0)
v = par[v];
return v;
}
void merge(int a , int b , bool save = 0 , bool compress = 1)
{
a = root(a , compress) , b = root(b , compress);
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 , 0);
sz++;
}
ans[ind] = -par[root(s[ind] , 0)];
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... |