HDU 3938 Portal (Offline +kruskal+ and look-up collection)

Source: Internet
Author: User
Tags bool sort

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=3938

Topic:

Problem Description

Zlgg found a magic theory that bigger banana the bigger banana. This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals would cost min{t} energies. T in a path between point V and Point U's the length of the longest edge in the path. There May is lots of paths between two points. Now ZLGG owned L energies and him want to know how many kind of the path he could.

Input

There are multiple test cases. The "a" 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 was 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 th E 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

Ten
7 2 1 6 8 3 4 5 8 5 8 2 2 8 9 6 4 5 2 1 5 8 5 7 3 7 7 + 8
10
  6
1
5
9
1
8
2 7 6

Sample Output

1
1
2
13

Analysis and Summary:

The concept of "off-line algorithm" has been learned in this topic. The so-called "off-line" means that all the data are entered and then calculated, "online" is the edge input edge calculation.

In this question, because of the "query part" in the input, Q asks how many different ways each l can have. Since the large L is bound to contain the small l, it is possible to reduce the number of computations by inputting all the problems, then sorting them from large to small.

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

This problem also needs to be used and the focus of the "weight", expressed in rank array, that is, a tree K has rank "K" node. The points between the same tree are connected, and any point can lead to any other point, and when two trees are merged into a tree, the rank[a]*rank[b] path is added.

Code:

#include <cstdio> #include <algorithm> using namespace std;  
    
#define N 10005 int f[n], rank[n], ans[n], N, M, Q;  
    struct edge{int u, V, Val;  
    friend BOOL operator < (const edge&a,const edge&b) {return a.val < B.val;  
    
}}arr[n*5];  
    struct query{int id, L;  
    friend bool operator< (const Query&a,const query&b) {return a.l<b.l;  
    
}}q[n];  
void Init () {for (int i=0; i<=n; ++i) f[i]=i, rank[i]=1;  
    int find (int x) {int i, j=x;  
    while (J!=f[j]) j=f[j];  
    while (X!=J) {i=f[x]; f[x]=j x=i;}  
Return J;  
    int Union (int x,int y) {int a=find (x), B=find (y);  
    if (a==b) return 0;  
    int t=rank[a]*rank[b];  
    Rank[a] + = rank[b];  
    F[B] = A;  
return t;   
            int main () {while (~scanf ("%d%d%d", &n,&m,&q)) {for (int i=0; i<m; ++i) scanf ("%d%d%d", &arr[i).U,&arr[i].v,&arr[i].val); for (int i=0; i<q; ++i) scanf ("%d", &q[i).  
            
        L), q[i].id=i;  
        Sort (arr,arr+m);  
            
        Sort (q,q+q);  
        int cnt=0, j=0;  
        Init (); for (int i=0; i<q; ++i) {while (J<m && arr[j].val<=q[i].  
                L) {cnt + = Union (arr[j].u, ARR[J].V);  
            ++j;  
        } Ans[q[i].id] = cnt;  
    for (int i=0; i<q; ++i) printf ("%d\n", Ans[i]);  
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.