Submission #167552

#TimeUsernameProblemLanguageResultExecution timeMemory
167552qkxwsmBridges (APIO19_bridges)C++14
14 / 100
141 ms14628 KiB
#include <bits/stdc++.h> using namespace std; template<class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template<class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } #define MP make_pair #define PB push_back #define LB lower_bound #define UB upper_bound #define fi first #define se second #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) #define SZ(x) ((int) (x).size()) #define ALL(x) (x).begin(), (x).end() #define MAXN 100013 typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpi; typedef vector<pll> vpl; int N, M, Q; pair<pii, int> edge[MAXN], quer[MAXN]; vi edges[MAXN]; int ans[MAXN]; int dsu[MAXN], siz[MAXN]; vector<array<int, 3> > events; int get(int u) { return (u == dsu[u] ? u : dsu[u] = get(dsu[u])); } void merge(int u, int v) { u = get(u); v = get(v); if (u == v) return; if (siz[v] > siz[u]) swap(u, v); dsu[v] = u; siz[u] += siz[v]; siz[v] = 0; return; } //add an edge? resolve a query? int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); cerr << fixed << setprecision(4); cin >> N >> M; FOR(i, 0, N) { dsu[i] = i; siz[i] = 1; } FOR(i, 0, M) { int u, v, d; cin >> u >> v >> d; if (u > v) swap(u, v); u--; v--; edge[i] = {{u, v}, d}; edges[u].PB(i); edges[v].PB(i); events.PB({d, 1, i}); } cin >> Q; FOR(i, 0, Q) { cin >> quer[i].se >> quer[i].fi.fi >> quer[i].fi.se; quer[i].fi.fi--; events.PB({quer[i].fi.se, -1, i}); } sort(ALL(events), greater<array<int, 3> >()); for (auto e : events) { int idx = e[2]; if (e[1] == 1) { int u = edge[idx].fi.fi, v = edge[idx].fi.se; merge(u, v); } else { int u = quer[idx].fi.fi; u = get(u); ans[idx] = siz[u]; } } FOR(i, 0, Q) { cout << ans[i] << '\n'; } return 0; }
#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...