Submission #1299657

#TimeUsernameProblemLanguageResultExecution timeMemory
1299657adscodingBridges (APIO19_bridges)C++20
100 / 100
1629 ms10448 KiB
#include <bits/stdc++.h> #define time() cerr << " \n " << "Time : " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms." #define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i) #define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i) #define all(x) x.begin(), x.end() #define uni(x) x.erase(unique(all(x)), x.end()) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define dbg(...) cerr << "[ " << #__VA_ARGS__ << " ] = ", debug(__VA_ARGS__) template<typename T> void debug(T x) { cerr << x << "\n\n"; } template<typename T, typename... Args> void debug(T x, Args... args) { cerr << x << " , ", debug(args...); } // -------------------------------------------------------------------------------------------------------------------- const int maxn = 1e5 + 3; const int B = 1000; int n, m, q, a[maxn], U[maxn], V[maxn], W[maxn], ans[maxn]; bool mark[maxn]; vector<int> to_do[B + 5]; struct Qr { int op, x, y; }; Qr qrs[maxn]; // -------------------------------------------------------------------------------------------------------------------- bool cmpE(const int &x, const int &y) { return W[x] > W[y]; } bool cmpQ(const int &x, const int &y) { return qrs[x].y > qrs[y].y; } struct DSU { vector<int> e; vector<pair<int &, int>> history; void init() {e.clear(); history.clear(); e.assign(n + 3, -1);} int get(int u) {return e[u] < 0 ? u : get(e[u]);} bool unite(int u, int v) { u = get(u); v = get(v); if (u == v) return false; if (e[u] > e[v]) swap(u, v); history.push_back({e[u], e[u]}); history.push_back({e[v], e[v]}); e[u] += e[v]; e[v] = u; return true; } int versions() {return (int)history.size();} void rollback(int until) { while (history.size() > until) { history.back().first = history.back().second; history.pop_back(); } } }; void solve() { cin >> n >> m; FOR(i, 1, m) { cin >> U[i] >> V[i] >> W[i]; if (U[i] > V[i]) swap(U[i], V[i]); } cin >> q; FOR(iq, 1, q) cin >> qrs[iq].op >> qrs[iq].x >> qrs[iq].y; DSU dsu; for (int sta = 1; sta <= q; sta += B) { dsu.init(); int ed = min(sta + B - 1, q); vector<int> Ask, Upd, unchange, change; FOR(i, 1, m) mark[i] = true; FOR(iq, sta, ed) { if (qrs[iq].op != 1) continue; mark[qrs[iq].x] = false; } FOR(i, 1, m) if (mark[i]) unchange.push_back(i); else change.push_back(i); FOR(iq, sta, ed) { if (qrs[iq].op == 1) { W[qrs[iq].x] = qrs[iq].y; Upd.push_back(iq); } else { to_do[iq - sta].clear(); for (int x : change) { if (W[x] >= qrs[iq].y) to_do[iq - sta].push_back(x); } Ask.push_back(iq); } } sort(all(unchange), cmpE); sort(all(Ask), cmpQ); int ptr = 0; for (int iq : Ask) { while (ptr < unchange.size() && W[unchange[ptr]] >= qrs[iq].y) { dsu.unite(U[unchange[ptr]], V[unchange[ptr]]); ++ptr; } int timer = dsu.versions(); for (int id : to_do[iq - sta]) { dsu.unite(U[id], V[id]); } ans[iq] = -dsu.e[dsu.get(qrs[iq].x)]; dsu.rollback(timer); } } FOR(i, 1, q) if (ans[i]) cout << ans[i] << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define TASK "test" if (fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); freopen(TASK".OUT", "w", stdout); } solve(); time(); return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |         freopen(TASK".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...