Submission #335045

#TimeUsernameProblemLanguageResultExecution timeMemory
335045TheEpicCowOfLifeBridges (APIO19_bridges)C++14
0 / 100
134 ms19812 KiB
#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)

bridges.cpp: In function 'int main()':
bridges.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   27 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
bridges.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   36 |         scanf("%d %d %d", &a, &b, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
bridges.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |         scanf("%d %d %d", &t, &x, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...