# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
335045 | TheEpicCowOfLife | Bridges (APIO19_bridges) | C++14 | 134 ms | 19812 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
int n,m;
struct e{
int a,b,w;
};
vector<e> ev;
int ua[100005];
int ncnt;
int height[100005];
int sz[100005];
int f(int x){
if (ua[x] == x) return x;
return ua[x] = f(ua[x]);
}
int cur_node[100005]; // cur_node[i] is the node that represents the group led by node i
int par[100005][20];
int main(){
scanf("%d %d", &n, &m);
height[0] = -1;
ncnt = n;
for (int i = 1; i <= n; i++){
ua[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= m; i++){
int a,b,w;
scanf("%d %d %d", &a, &b, &w);
ev.push_back({a,b,w});
}
sort(ev.begin(),ev.end(),[](e l, e r){
return l.w < r.w;
});
for (e cur : ev){
int a = cur.a;
int b = cur.b;
if (f(a) == f(b)) continue;
int w = cur.w;
int fa = cur_node[f(a)];
int fb = cur_node[f(b)];
int cn = ++ncnt; // cn is the new node we're making
height[cn] = w;
par[fa][0] = cn;
par[fb][0] = cn;
sz[cn] = sz[fa] + sz[fb];
ua[f(a)] = f(b);
cur_node[f(b)] = cn;
}
for (int i = 1; i <= 18; i++){
for (int j = 1; j <= ncnt; j++){
par[j][i] = par[par[j][i-1]][i-1];
}
}
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++){
int t,x,w;
scanf("%d %d %d", &t, &x, &w);
assert(t == 2);
for (int i = 18; i >= 0; i--){
if (height[par[x][i]] >= w){
x = par[x][i];
}
}
printf("%d\n", sz[x]);
}
}
Compilation message (stderr)
# | 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... |