This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 09.11.2023 22:50:14 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const int BLOCK_SIZE = 320;
struct Edge {
int u, v, w;
Edge(int u, int v, int w): u(u), v(v), w(w) {}
bool operator < (const Edge &other) const { return w > other.w; }
};
struct Query {
int t, u, w;
Query(int t, int u, int w): t(t), u(u), w(w) {}
};
void process(void) {
int n, m, q; cin >> n >> m;
vector <Edge> edges;
REP(i, m) {
int u, v, w; cin >> u >> v >> w; --u; --v;
edges.emplace_back(u, v, w);
}
vector <int> ord(m);
iota(ALL(ord), 0);
sort(ALL(ord), [&] (int a, int b) { return edges[a].w > edges[b].w; });
vector <Query> queries;
cin >> q;
REP(i, q) {
int t, u, w; cin >> t >> u >> w; --u;
queries.emplace_back(t, u, w);
}
vector <int> par(n, -1);
stack <pair <int, int>> his;
function <int(int, bool)> find = [&] (int u, bool save) {
if (par[u] < 0) return u;
if (save) his.emplace(u, par[u]);
return par[u] = find(par[u], save);
};
auto join = [&] (int u, int v, bool save) {
if ((u = find(u, save)) == (v = find(v, save))) return false;
if (par[u] > par[v]) swap(u, v);
if (save) his.emplace(u, par[u]);
if (save) his.emplace(v, par[v]);
par[u] += par[v];
par[v] = u;
return true;
};
auto rollback = [&] {
while (!his.empty()) {
par[his.top().fi] = his.top().se;
his.pop();
}
};
vector <bool> used(m);
vector <int> res(q, -1);
for (int l = 0; l < q; ) {
int r = min(q, l + BLOCK_SIZE);
vector <int> qr;
FOR(i, l, r) {
if (queries[i].t == 1) used[queries[i].u] = true;
else qr.push_back(i);
}
fill(ALL(par), -1);
sort(ALL(qr), [&] (int a, int b) { return queries[a].w > queries[b].w; });
int p = 0;
for (int i: qr) {
auto [t, u, w] = queries[i];
while (p < m && edges[ord[p]].w >= w) {
if (!used[ord[p]]) join(edges[ord[p]].u, edges[ord[p]].v, false);
++p;
}
FORD(j, i, l) if (queries[j].t == 1 && queries[j].w >= w) {
int x = queries[j].u;
if (!used[x]) continue;
used[x] = false;
join(edges[x].u, edges[x].v, true);
}
FOR(j, i + 1, r) if (queries[j].t == 1) {
int x = queries[j].u;
if (!used[x] || edges[x].w < w) continue;
used[x] = false;
join(edges[x].u, edges[x].v, true);
}
FOR(j, l, r) if (queries[j].t == 1) used[queries[j].u] = true;
res[i] = -par[find(u, true)];
rollback();
}
vector <int> nxt;
FORD(i, r, l) if (queries[i].t == 1) {
int x = queries[i].u;
if (!used[x]) continue;
used[x] = false;
edges[x].w = queries[i].w;
nxt.push_back(x);
}
for (int i: nxt) used[i] = true;
sort(ALL(nxt), [&] (int a, int b) { return edges[a].w > edges[b].w; });
int i = 0, j = 0;
vector <int> new_ord;
while (i < m || j < nxt.size()) {
if (i < m && used[ord[i]]) ++i;
else if (i == m || (j < nxt.size() && edges[ord[i]].w <= edges[nxt[j]].w)) new_ord.push_back(nxt[j++]);
else new_ord.push_back(ord[i++]);
}
for (int i: nxt) used[i] = false;
ord = move(new_ord);
l = r;
}
REP(i, q) if (res[i] != -1) cout << res[i] << '\n';
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("bridges");
// int t; cin >> t; while (t--)
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message (stderr)
bridges.cpp: In function 'void process()':
bridges.cpp:142:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | while (i < m || j < nxt.size()) {
| ~~^~~~~~~~~~~~
bridges.cpp:144:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | else if (i == m || (j < nxt.size() && edges[ord[i]].w <= edges[nxt[j]].w)) new_ord.push_back(nxt[j++]);
| ~~^~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:156:2: note: in expansion of macro 'file'
156 | file("bridges");
| ^~~~
bridges.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:156:2: note: in expansion of macro 'file'
156 | file("bridges");
| ^~~~
# | 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... |