Submission #255976

#TimeUsernameProblemLanguageResultExecution timeMemory
255976AM1648Bridges (APIO19_bridges)C++17
100 / 100
2873 ms81784 KiB
/* be name khoda */ // #define long_enable #include <iostream> #include <algorithm> #include <cstring> #include <numeric> #include <vector> #include <fstream> #include <set> #include <map> using namespace std; #ifdef long_enable typedef long long int ll; #else typedef int ll; #endif typedef pair<ll, ll> pii; typedef pair<pii, ll> ppi; typedef pair<ll, pii> pip; typedef vector<ll> vi; typedef vector<pii> vpii; #define forifrom(i, s, n) for (ll i = s; i < n; ++i) #define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i) #define fori(i, n) forifrom (i, 0, n) #define forir(i, n) forirto (i, n, 0) #define F first #define S second #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define smin(a, b) a = min(a, (b)) #define smax(a, b) a = max(a, (b)) #define debug(x) cout << #x << " -> " << (x) << endl #define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl #define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl #define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl #define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl #define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl const ll MOD = 1000000007; const ll INF = 2000000000; const long long BIG = 1446803456761533460LL; #define XL (x << 1) #define XR (XL | 1) #define MD ((l + r) >> 1) #define Add(a, b) a = ((a) + (b)) % MOD #define Mul(a, b) a = (1LL * (a) * (b)) % MOD // ----------------------------------------------------------------------- const ll maxn = 50010; const ll maxm = 100010; const ll SQ = 400; ll n, m, qn, qm, cm; pip es[maxm]; ppi qs[maxm], cs[maxm]; ll g[maxn][SQ], deg[maxn]; ll rt[maxn], sz[maxn], qt[maxm], es_sort[maxm], lst[maxm]; ll vis[maxn]; bool chg[maxm]; ll res[maxm]; ll root(ll x) { return rt[x] == x ? x : rt[x] = root(rt[x]); } ll dfs(ll x) { ll s = 0; fori (yi, deg[x]) if (vis[g[x][yi]] != vis[x]) vis[g[x][yi]] = vis[x], s += dfs(g[x][yi]); return s + sz[x]; } ll search(ll s, ll w, ll t) { forir (i, cm) if (cs[i].S < t && lst[cs[i].F.F] != t + 1) { if (cs[i].F.S >= w) { auto [a, b] = es[cs[i].F.F].S; a = root(a), b = root(b); if (a != b) g[a][deg[a]++] = b, g[b][deg[b]++] = a; } lst[cs[i].F.F] = t + 1; } fori (i, cm) if (lst[cs[i].F.F] != t + 1 && es[cs[i].F.F].F >= w) { auto [a, b] = es[cs[i].F.F].S; a = root(a), b = root(b); if (a != b) g[a][deg[a]++] = b, g[b][deg[b]++] = a; } vis[root(s)] = t + 1; ll r = dfs(rt[s]); fori (i, cm) { auto [a, b] = es[cs[i].F.F].S; a = root(a), b = root(b); deg[rt[a]] = deg[rt[b]] = 0; } return r; } void calc() { sort(es_sort, es_sort + m, [](const ll a, const ll b) { return es[a].F > es[b].F; }); sort(qs, qs + qm, greater<ppi>()); iota(rt, rt + n, 0); fill(sz, sz + n, 1); fori (i, cm) chg[cs[i].F.F] = 1; ll ptr = 0; fori (i, m) { if (chg[es_sort[i]]) continue; while (ptr < qm && qs[ptr].F.F > es[es_sort[i]].F) { res[qs[ptr].S] = search(qs[ptr].F.S, qs[ptr].F.F, qs[ptr].S); ++ptr; } auto [a, b] = es[es_sort[i]].S; if (root(a) != root(b)) { if (sz[rt[a]] > sz[rt[b]]) swap(a, b); sz[rt[b]] += sz[rt[a]]; rt[rt[a]] = rt[b]; } } while (ptr < qm) { res[qs[ptr].S] = search(qs[ptr].F.S, qs[ptr].F.F, qs[ptr].S); ++ptr; } fori (i, cm) { chg[cs[i].F.F] = 0; es[cs[i].F.F].F = cs[i].F.S; } qm = cm = 0; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; fori (i, m) { ll a, b, w; cin >> a >> b >> w; --a, --b; es[i] = {w, {a, b}}; } iota(es_sort, es_sort + m, 0); cin >> qn; fori (i, qn) { ll t, a, b; cin >> t >> a >> b; --a; qt[i] = t; if (t == 1) { cs[cm++] = {{a, b}, i}; } else { qs[qm++] = {{b, a}, i}; } if ((cm + 1) % SQ == 0) calc(); } calc(); fori (i, qn) if (qt[i] == 2) cout << res[i] << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...