Submission #1070481

#TimeUsernameProblemLanguageResultExecution timeMemory
1070481ThanhsBridges (APIO19_bridges)C++14
100 / 100
2906 ms10464 KiB
#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, int save) { u = find(u), v = find(v); if (save) c.emplace_back(-1, -1); if (u == v) return; if (save) 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, mp[NM]; bool o[NM]; void giai() { dsu.init(n); vector<int> id; vector<Query> QQ, E; vector<Edge> T; for (auto t : Q) { if (t.a == 1) { if (!o[t.b]) id.push_back(t.b); 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, 0); pt++; } int cnt = 0; for (auto ee : E) { if (ee.c < t.c) mp[ee.b] = ee.a; if (!mp[ee.b]) mp[ee.b] = e[ee.b].w; } for (auto i : id) if (mp[i] >= t.a) { dsu.join(e[i].u, e[i].v, 1); cnt++; } for (auto i : id) mp[i] = 0; 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 member function 'void DSU::join(int, int, int)':
bridges.cpp:50:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   50 |         if (save)
      |         ^~
bridges.cpp:52:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   52 |             c.emplace_back(v, p[v]);
      |             ^
bridges.cpp: In function 'void giai()':
bridges.cpp:99:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         while (pt < T.size() && T[pt].w >= t.a)
      |                ~~~^~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:165:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |         freopen("out.txt", "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...