Submission #367086

#TimeUsernameProblemLanguageResultExecution timeMemory
367086Jarif_RahmanBridges (APIO19_bridges)C++17
0 / 100
100 ms15004 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; struct dsu{ int n; vector<int> id; vector<vector<int>> comp; dsu(int nn){ n = nn; for(int i = 0; i < n; i++) id.pb(i), comp.pb({i}); } void unite(int a, int b){ a = id[a], b = id[b]; if(a == b) return; if(comp[a].size() > comp[b].size()) swap(a, b); while(!comp[a].empty()){ int x = comp[a].back(); comp[a].pop_back(); id[x] = b; comp[b].pb(x); } } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; dsu ds(n); vector<tuple<ll, int, int>> edge; for(int i = 0; i < m; i++){ int a, b; ll w; cin >> a >> b >> w; a--, b--; edge.pb({w, a, b}); } int q; cin >> q; sort(edge.begin(), edge.end()); vector<tuple<ll, int, int>> query; for(int i = 0; i < q; i++){ int tt; cin >> tt; if(tt == 2){ ll w; int s; cin >> w >> s; s--; query.pb({w, s, i}); } } vector<int> ans(q); sort(query.rbegin(), query.rend()); for(auto [w, s, in]: query){ while(!edge.empty() && get<0>(edge.back()) >= w){ auto [ww, a, b] = edge.back(); edge.pop_back(); ds.unite(a, b); } ans[in] = ds.comp[ds.id[s]].size(); } for(int x: ans) cout << x << "\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...