hdu 1512 并查集

又是基于连通性的问题,可以用并查集来解决,每个集合都维护一个优先队列,合并的时候按照优先队列的大小启发式合并即可。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 using namespace std;
 6
 7 const int N = 100001;
 8 int f[N];
 9 priority_queue<int> q[N];
10 int n, m;
11
12 void init()
13 {
14     for ( int i = 1; i <= n; i++ )
15     {
16         f[i] = i;
17         while ( !q[i].empty() ) q[i].pop();
18     }
19 }
20
21 int findf( int x )
22 {
23     if ( f[x] != x ) f[x] = findf( f[x] );
24     return f[x];
25 }
26
27 int union_set( int x, int y )
28 {
29     x = findf(x), y = findf(y);
30     if ( x == y ) return -1;
31     int sx = q[x].top();
32     q[x].pop();
33     sx >>= 1;
34     int sy = q[y].top();
35     q[y].pop();
36     sy >>= 1;
37     if ( q[x].size() > q[y].size() ) swap( x, y );
38     f[x] = y;
39     while ( !q[x].empty() )
40     {
41         int nn = q[x].top();
42         q[x].pop();
43         q[y].push(nn);
44     }
45     q[y].push(sx);
46     q[y].push(sy);
47     return q[y].top();
48 }
49
50 int main ()
51 {
52     while ( scanf("%d", &n) != EOF )
53     {
54         init();
55         for ( int i = 1; i <= n; i++ )
56         {
57             int tmp;
58             scanf("%d", &tmp);
59             q[i].push(tmp);
60         }
61         scanf("%d", &m);
62         while ( m-- )
63         {
64             int u, v;
65             scanf("%d%d", &u, &v);
66             printf("%d\n", union_set( u, v ));
67         }
68     }
69     return 0;
70 }
时间: 2024-10-26 02:40:57

hdu 1512 并查集的相关文章

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

HDU 3938 并查集

求小于L的路径点对数(路上的最大值),按权值排序,从小到大并查集建图,有点kruskal的意思. /** @Date : 2017-09-22 17:30:11 * @FileName: HDU 3938 并查集 离线.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h>

HDU 1829 并查集

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8224    Accepted Submission(s): 2631 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp