This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 1e5 + 5;
const int SQ = 320;
struct Query
{
int a, b, c;
Query(int _a, int _b, int _c) : a(_a), b(_b), c(_c){}
};
vector<Query> Q;
struct Edge
{
int u, v, w;
Edge(){}
}e[NM];
struct DSU
{
int p[NM];
vector<pair<int, int>> c;
DSU() {memset(p, -1, sizeof p);}
void init(int n) {memset(p, -1, (n + 1) * (sizeof p[0])); c.clear();}
int find(int u) {return p[u] < 0 ? u : find(p[u]);}
void join(int u, int v)
{
u = find(u), v = find(v);
c.emplace_back(-1, -1);
if (u == v)
return;
c.emplace_back(u, p[u]);
c.emplace_back(v, p[v]);
if (p[u] > p[v])
swap(u, v);
p[u] += p[v];
p[v] = u;
}
void rollback()
{
while (~c.back().fi)
{
p[c.back().fi] = c.back().se;
c.pop_back();
}
c.pop_back();
}
}dsu;
vector<pair<int, int>> ans;
int cur, n, m, q;
bool o[NM];
void giai()
{
dsu.init(n);
vector<Query> QQ, E;
vector<Edge> T;
for (auto t : Q)
{
if (t.a == 1)
{
o[t.b] = 1;
E.emplace_back(t.c, t.b, ++cur);
}
else
QQ.emplace_back(t.c, t.b, ++cur);
}
for (int i = 1; i <= m; i++)
if (!o[i])
T.emplace_back(e[i]);
sort(QQ.begin(), QQ.end(), [&](auto x, auto y){return x.a > y.a;});
sort (T.begin(), T.end(), [&](auto x, auto y){return x.w > y.w;});
int pt = 0;
for (auto t : QQ)
{
while (pt < T.size() && T[pt].w >= t.a)
{
dsu.join(T[pt].u, T[pt].v);
pt++;
}
int cnt = 0;
map<int, int> mp;
for (auto ee : E)
{
if (ee.c < t.c)
mp[ee.b] = ee.a;
if (mp.find(ee.b) == mp.end())
mp[ee.b] = e[ee.b].w;
}
for (auto ee : mp)
if (ee.se >= t.a)
{
dsu.join(e[ee.fi].u, e[ee.fi].v);
cnt++;
}
ans.emplace_back(t.c, -dsu.p[dsu.find(t.b)]);
while (cnt--)
dsu.rollback();
}
for (auto t : Q)
if (t.a == 1)
{
e[t.b].w = t.c;
o[t.b] = 0;
}
Q.clear();
}
void pre()
{
}
void solve()
{
pre();
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> e[i].u >> e[i].v >> e[i].w;
cin >> q;
for (int i = 1; i <= q; i++)
{
int a, b, c;
cin >> a >> b >> c;
Q.emplace_back(a, b, c);
if (Q.size() == SQ)
giai();
}
if (Q.size())
giai();
sort(ans.begin(), ans.end());
for (auto t : ans)
cout << t.se << endl;
}
signed main()
{
fastIO
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
bridges.cpp: In function 'void giai()':
bridges.cpp:94:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | while (pt < T.size() && T[pt].w >= t.a)
| ~~~^~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |