#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 |
1 |
Correct |
8 ms |
10240 KB |
Output is correct |
2 |
Correct |
7 ms |
10240 KB |
Output is correct |
3 |
Correct |
40 ms |
11000 KB |
Output is correct |
4 |
Correct |
34 ms |
10880 KB |
Output is correct |
5 |
Correct |
32 ms |
10752 KB |
Output is correct |
6 |
Correct |
28 ms |
10752 KB |
Output is correct |
7 |
Correct |
27 ms |
10740 KB |
Output is correct |
8 |
Correct |
35 ms |
10872 KB |
Output is correct |
9 |
Correct |
30 ms |
10624 KB |
Output is correct |
10 |
Correct |
32 ms |
10872 KB |
Output is correct |
11 |
Correct |
36 ms |
10872 KB |
Output is correct |
12 |
Correct |
34 ms |
10880 KB |
Output is correct |
13 |
Correct |
35 ms |
10872 KB |
Output is correct |
14 |
Correct |
34 ms |
10880 KB |
Output is correct |
15 |
Correct |
34 ms |
10876 KB |
Output is correct |
16 |
Correct |
29 ms |
10624 KB |
Output is correct |
17 |
Correct |
30 ms |
10624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3031 ms |
21424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1936 ms |
19988 KB |
Output is correct |
2 |
Correct |
735 ms |
17228 KB |
Output is correct |
3 |
Correct |
1878 ms |
19056 KB |
Output is correct |
4 |
Correct |
1742 ms |
20164 KB |
Output is correct |
5 |
Correct |
409 ms |
16884 KB |
Output is correct |
6 |
Correct |
1947 ms |
19952 KB |
Output is correct |
7 |
Correct |
1780 ms |
19920 KB |
Output is correct |
8 |
Correct |
1989 ms |
19916 KB |
Output is correct |
9 |
Correct |
1816 ms |
20200 KB |
Output is correct |
10 |
Correct |
1830 ms |
20080 KB |
Output is correct |
11 |
Correct |
1632 ms |
20044 KB |
Output is correct |
12 |
Correct |
1627 ms |
19820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3073 ms |
22404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3031 ms |
21424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10240 KB |
Output is correct |
2 |
Correct |
7 ms |
10240 KB |
Output is correct |
3 |
Correct |
40 ms |
11000 KB |
Output is correct |
4 |
Correct |
34 ms |
10880 KB |
Output is correct |
5 |
Correct |
32 ms |
10752 KB |
Output is correct |
6 |
Correct |
28 ms |
10752 KB |
Output is correct |
7 |
Correct |
27 ms |
10740 KB |
Output is correct |
8 |
Correct |
35 ms |
10872 KB |
Output is correct |
9 |
Correct |
30 ms |
10624 KB |
Output is correct |
10 |
Correct |
32 ms |
10872 KB |
Output is correct |
11 |
Correct |
36 ms |
10872 KB |
Output is correct |
12 |
Correct |
34 ms |
10880 KB |
Output is correct |
13 |
Correct |
35 ms |
10872 KB |
Output is correct |
14 |
Correct |
34 ms |
10880 KB |
Output is correct |
15 |
Correct |
34 ms |
10876 KB |
Output is correct |
16 |
Correct |
29 ms |
10624 KB |
Output is correct |
17 |
Correct |
30 ms |
10624 KB |
Output is correct |
18 |
Execution timed out |
3031 ms |
21424 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |