HDU 3938 Portal【併査集】


Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2604 Accepted Submission(s): 1246
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
Source 2011 Multi-University Training Contest 10 - Host by HRBEU
問題リンク:HDU 3938 Portal問題概要:(略)問題分析:    重みを持って問題を調べ,kruskalアルゴリズムを変形した.    クエリー結果を一度に算出して格納し、結果を一度に出力する必要があります.これにより、速度が速くなります.プログラム摘要:(略)参照リンク:(略)題記:(略)
ACのC++言語プログラムは以下の通りである.
/* HDU3938 Portal */

#include 

using namespace std;

typedef long long LL;

const int N = 10000;
int f[N + 1];
LL cnt[N + 1], ans[N + 1];
void UFInit(int n)
{
     
    for(int i = 0; i <= n; i++)
        f[i] = i, cnt[i] = 1;
}
int Find(int a) {
     return a == f[a] ? a : f[a] = Find(f[a]);}
int Union(int a, int b)
{
     
    if ((a = Find(a)) != (b = Find(b))) {
     
        int tmp = cnt[a] * cnt[b];
        f[a] = b;
        cnt[b] += cnt[a];
        cnt[a] = 0;
        return tmp;
    } else
        return 0;
}

int n, m, q;

int main()
{
     
    while(~scanf("%d%d%d", &n, &m, &q)) {
     
        memset(ans, 0, sizeof(ans));
        vector<pair<int, pair<int, int> > > g(m);
        vector<pair<int, int > > qq(q);

        for(int i = 0; i < m; i++)
            scanf("%d%d%d", &g[i].second.first, &g[i].second.second, &g[i].first);
        for(int i = 0; i < q; i++)
            scanf("%d", &qq[i].first), qq[i].second = i;

        sort(g.begin(), g.end());
        sort(qq.begin(), qq.end());

        UFInit(n);
        LL sum = 0;
        int j = 0;
        for(int i = 0; i < q; i++) {
     
            while(j < m && g[j].first  <= qq[i].first)
                sum += Union(g[j].second.first, g[j].second.second), j++;
            ans[qq[i].second] = sum;
        }

        for(int i = 0; i < q; i++)
            printf("%lld
"
, ans[i]); } return 0; }

ACのC++言語プログラムは以下の通りである.
/* HDU3938 Portal */

#include 

using namespace std;

typedef long long LL;

const int N = 10000;
int f[N + 1];
LL cnt[N + 1], ans[N + 1];
void UFInit(int n)
{
     
    for(int i = 0; i <= n; i++)
        f[i] = i, cnt[i] = 1;
}
int Find(int a) {
     return a == f[a] ? a : f[a] = Find(f[a]);}
int Union(int a, int b)
{
     
    if ((a = Find(a)) != (b = Find(b))) {
     
        int tmp = cnt[a] * cnt[b];
        f[a] = b;
        cnt[b] += cnt[a];
        cnt[a] = 0;
        return tmp;
    } else
        return 0;
}

int n, m, q;

struct Node {
     
    int u, v, w;
} e[N*5];
struct Q {
     
    int l, id;
} qq[N];

int cmp1(Node a, Node b)
{
     
    return a.w < b.w;
}

int cmp2(Q a, Q b)
{
     
    return a.l < b.l;
}

int main()
{
     

    while(~scanf("%d%d%d", &n, &m, &q)) {
     
        memset(ans, 0, sizeof(ans));

        for(int i = 0; i < m; i++)
            scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
        for(int i = 0; i < q; i++)
            scanf("%d", &qq[i].l), qq[i].m = i;

        sort(e, e + m, cmp1);
        sort(qq, qq + q, cmp2);

        UFInit(n);
        LL sum = 0;
        int j = 0;
        for(int i = 0; i < q; i++) {
     
            while(j < m && e[j].w <= qq[i].l)
                sum += Union(e[j].u, e[j].v), j++;
            ans[qq[i].id] = sum;
        }

        for(int i = 0; i < q; i++)
            printf("%lld
"
, ans[i]); } return 0; }