이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |