zoukankan      html  css  js  c++  java
  • Portal 并查集(去重)

    Problem Description
    ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
     
    Input
    There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
     
    Output
    Output the answer to each query on a separate line.
     
    Sample Input
    10 10 10
    7 2 1
    6 8 3
    4 5 8
    5 8 2
    2 8 9
    6 4 5
    2 1 5
    8 10 5
    7 3 7
    7 8 8
    10
    6
    1
    5
    9
    1
    8
    2
    7
    6
     
    Sample Output
    36
    13
    1
    13
    36
    1
    36
    2
    16
    13
    ***************************************************************************************************************************
    并查集&&离线算法
    ***************************************************************************************************************************
     1 /*
     2 求最大边的最小值,用贪心排序
     3 */
     4 #include<iostream>
     5 #include<string>
     6 #include<cstring>
     7 #include<cmath>
     8 #include<cstdio>
     9 #include<algorithm>
    10 using namespace std;
    11 int fa[50001],num[50001];
    12 int ans[50001];
    13 int n,m,q;
    14 int i,j,k;
    15 void init()
    16 {
    17     int it;
    18     for(int it=1;it<=n;it++)
    19     {
    20         fa[it]=it;
    21         num[it]=1;
    22     }
    23 }
    24 struct node
    25 {
    26     int st,en,len;
    27 }e[50001];
    28 struct out
    29 {
    30     int id;
    31     int value;
    32 }A[50001];
    33 bool cmp1(node a,node b)
    34 {
    35     return a.len<b.len;
    36 }
    37 bool cmp2(out a,out b)
    38 {
    39     return a.value<b.value;
    40 }
    41 int find(int x)
    42 {
    43     int temp=x;
    44     int root;
    45     while(x!=fa[x])
    46      x=fa[x];
    47     root=x;
    48     x=temp;
    49     while(x!=fa[x])
    50     {
    51         temp=x;
    52         fa[x]=root;
    53         x=fa[temp];
    54     }
    55     return root;
    56 }
    57 int Unon(int a,int b)
    58 {
    59     int x=find(a);
    60     int y=find(b);
    61     if(x==y)return  0;
    62     int temp1=num[x]*num[y];
    63     num[x]+=num[y];
    64     num[y]=0;//此处很重要,主要为了去重
    65     fa[y]=x;
    66     return temp1;
    67 }
    68 int main()
    69 {
    70    while(scanf("%d %d %d",&n,&m,&q)!=EOF)
    71    {
    72        init();
    73        for(i=0;i<m;i++)
    74         scanf("%d %d %d",&e[i].st,&e[i].en,&e[i].len);
    75        sort(e,e+m,cmp1);
    76        for(i=0;i<q;i++)
    77        {
    78            scanf("%d",&A[i].value);
    79            A[i].id=i;
    80 
    81        }
    82        sort(A,A+q,cmp2);
    83        int temp=0;
    84        k=0;
    85        for(i=0;i<q;i++)
    86        {
    87            while(k<m&&e[k].len<=A[i].value)
    88            {
    89                temp+=Unon(e[k].st,e[k].en);
    90                k++;
    91            }
    92            ans[A[i].id]=temp;
    93        }
    94        for(i=0;i<q;i++)
    95        {
    96            printf("%d
    ",ans[i]);
    97        }
    98    }
    99 }
    View Code
  • 相关阅读:
    SD卡的控制方法(指令集和控制时序)
    MDK的优化应用(转)
    SD卡的SPI模式的初始化顺序(转)
    SD卡读写扇区注意事项(转)
    MDK下调试时提示AXF文件无法导入的解决方法(转)
    把一个 int 数字 n 格式化成16进制的字符串(前面补零成0位)
    DB9 公头母头引脚定义及连接、封装
    RS232 DB9 公头 母头 串口引脚定义
    Codeforces 91C Ski Base 加边求欧拉回路数量
    Cocos Code IDE + Lua初次使用FastTiledMap的坑
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3401460.html
Copyright © 2011-2022 走看看