Submission #992564

#TimeUsernameProblemLanguageResultExecution timeMemory
992564Neco_arcBridges (APIO19_bridges)C++17
100 / 100
1896 ms20048 KiB
#include <bits/stdc++.h> //#include <bits/debug.hpp> #define ll long long #define all(x) x.begin(), x.end() #define Neco "Bridges" #define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin()) #define getbit(x,i) ((x >> i)&1) #define _left id * 2, l, mid #define _right id * 2 + 1, mid + 1, r #define cntbit(x) __builtin_popcountll(x) #define fi(i, a, b) for(int i = a; i <= b; i++) #define fid(i, a, b) for(int i = a; i >= b; i--) #define maxn (int) 2e5 + 7 using namespace std; const ll mod = 1e9 + 7; //972663749 const ll base = 911382323; const int Sqrt = 372; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll GetRandom(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rng); } int n, m, q, DEL; int W[maxn], del[maxn], vis[maxn], used[maxn]; int ans[maxn], E[maxn], tmp[maxn]; vector<int> edges[maxn]; struct edge { int u, v; } Ed[maxn]; struct dl { int type, x, w; } qr[maxn];; struct DSU { int lab[maxn]; void init() { fi(i, 1, n) lab[i] = -1; } int get_rt(int u) { return lab[u] < 0 ? u : lab[u] = get_rt(lab[u]); } void uni(int u, int v) { int p = get_rt(u), q = get_rt(v); if(p == q) return; if(lab[p] > lab[q]) swap(p, q); lab[p] += lab[q], lab[q] = p; } } Dsu; int dfs(int u, int del) { vis[u] = del; int res = Dsu.lab[u]; for(int v : edges[u]) if(vis[v] != del) { res += dfs(v, del); } return res; } void Add(int i) { int u = Dsu.get_rt(Ed[qr[i].x].u), v = Dsu.get_rt(Ed[qr[i].x].v); edges[u].push_back(v); edges[v].push_back(u); } void clear(int i) { int u = Dsu.get_rt(Ed[qr[i].x].u), v = Dsu.get_rt(Ed[qr[i].x].v); edges[u].clear(); edges[v].clear(); } void Gen() { n = GetRandom(3, 7), m = GetRandom(3, 9); fi(i, 1, m) { int u, v; u = GetRandom(1, n), v = GetRandom(1, n), W[i] = GetRandom(1, 10); E[i] = i; Ed[i] = {u, v}; } q = GetRandom(1, 5); fi(i, 1, q) { qr[i].type = GetRandom(1, 2); qr[i].x = GetRandom(1, n); qr[i].w = GetRandom(5, 12); } cout << n << " " << m << '\n'; fi(i, 1, m) cout << Ed[i].u << " " << Ed[i].v << " " << W[i] << '\n'; cout << q << '\n'; fi(i, 1, q) cout << qr[i].type << " " << qr[i].x << " " << qr[i].w << '\n'; cout << "--------------\n"; } void Input() { cin >> n >> m; fi(i, 1, m) { int u, v; cin >> u >> v >> W[i]; E[i] = i; Ed[i] = {u, v}; } cin >> q; fi(i, 1, q) cin >> qr[i].type >> qr[i].x >> qr[i].w; } void solve() { Input(); // Gen(); sort(E + 1, E + 1 + m, [](int x, int y) { return W[x] > W[y]; }); vector<dl> P; for(int T = 1; T <= (q - 1) / Sqrt + 1; ++T) { int L = (T - 1) * Sqrt + 1, R = min(q, Sqrt * T); fi(i, L, R) { if(qr[i].type == 1) del[qr[i].x] = T; else P.push_back({qr[i].x, qr[i].w, i}); } Dsu.init(); sort(all(P), [](dl x, dl y) { return x.x > y.x; }); int j = 1; for(auto [x, w, id] : P) { ++DEL; while(W[E[j]] >= w) { if(del[E[j]] != T) Dsu.uni(Ed[E[j]].u, Ed[E[j]].v); ++j; } fi(i, L, R) if(qr[i].type == 1) tmp[qr[i].x] = W[qr[i].x]; fi(i, L, id) if(qr[i].type == 1) W[qr[i].x] = qr[i].w; fi(i, L, R) if(qr[i].type == 1 && W[qr[i].x] >= w) Add(i); ans[id] = - dfs(Dsu.get_rt(x), DEL); fi(i, L, R) if(qr[i].type == 1 && W[qr[i].x] >= w) clear(i); fi(i, L, R) if(qr[i].type == 1) W[qr[i].x] = tmp[qr[i].x]; } fi(i, L, R) if(qr[i].type == 1) W[qr[i].x] = qr[i].w; sort(E + 1, E + 1 + m, [](int x, int y) { return W[x] > W[y]; }); P.clear(); } fi(i, 1, q) if(qr[i].type == 2) cout << ans[i] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(Neco".inp", "r")) { freopen(Neco".inp", "r", stdin); freopen(Neco".out", "w", stdout); } int nTest = 1; // cin >> nTest; while(nTest--) { solve(); } return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...