【杭电OJ3938】【离线+并查集】

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

Portal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1921    Accepted Submission(s): 955

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

题目大意:给一个图,然后会有Q次询问,询问路径上最大权值的边的权值小于 L的路径数。

题目分析:

【1】由于Q的范围是10^4,而如果一个一个查找的话,会TLE。而观察由于每次询问并未确定对路径的起点或者终点,所以每次询问的小L值可以累加得到询问时的大L的值,也就是每次询问的查找是有重叠的,所以就可以将Q次询问的L值进行从小到大的排序,小L慢慢累加得到大L,避免了不必要的重复查询

【解题的第一步-询问存起来等待离线】

【2】由于所连接的边的权值必须不大于L,所以也要将所有边进行排序

【解题第二步-->对边进行排序,等待查询是否能够连接】

【3】而由于是求路径数,两个图【一个顶点数为A,一个为B】连成一个图之后所能增加的路径数是A*B,所以可以利用并查集判断需要连接的两个图是否已经连接,如果没有则进行连接,并且路径数增加A*B,且根节点记录的图的顶点数增加A(或者B)

【解题最关键的一步,从小到大连接边,并更新L[i]的路径数,其中利用了两个连通图连接之后路径数增加A*B的原理】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 struct edge{
 7     int to1;
 8     int to2;
 9     int lrn;
10 }EDGE[50005];
11 struct qury{
12     int id;
13     int x;
14 }qwq[10005];
15 int n,m,q;
16 int pre[10005],num[10005];;
17 int find(int x)
18 {
19
20     int xx=x;
21     while(x!=pre[x])
22     {
23         x=pre[x];
24     }
25     while(pre[xx]!=x)
26     {
27         int t=pre[xx];
28         pre[xx]=x;
29         xx=t;
30     }
31     return x;
32 }
33 bool cmp1(struct edge orz1,struct edge orz2)
34 {
35     return orz1.lrn<orz2.lrn;
36 }
37 bool cmp2(struct qury orz3,struct qury orz4)
38 {
39     return orz3.x<orz4.x;
40 }
41 int main()
42 {
43     while(scanf("%d%d%d",&n,&m,&q)==3)
44     {
45     for(int i = 1 ; i <= n ; i++)
46     {
47         pre[i]=i;
48         num[i]=1;
49     }
50     for(int i = 0 ; i < m ; i++)
51     {
52         scanf("%d%d%d",&EDGE[i].to1,&EDGE[i].to2,&EDGE[i].lrn);
53     }
54     sort(EDGE,EDGE+m,cmp1);
55     for(int i = 0; i < q ; i++)
56     {
57         qwq[i].id=i;
58         scanf("%d",&qwq[i].x);
59     }
60     sort(qwq,qwq+q,cmp2);
61     int j=0;
62     long long sum=0;
63     long long ans[10005];
64     memset(ans,0,sizeof(ans));
65     for(int i = 0 ; i < q ; i++)
66     {
67         for(;j<m;j++)
68         {
69             if(EDGE[j].lrn>qwq[i].x)break;
70             int wqw1=find(EDGE[j].to1);
71             int wqw2=find(EDGE[j].to2);
72             if(wqw1!=wqw2)
73             {
74                 pre[wqw1]=wqw2;
75                 sum+=num[wqw1]*num[wqw2];
76                 num[wqw2]+=num[wqw1];
77             }
78         }
79         ans[qwq[i].id]=sum;
80
81     }
82     for(int i = 0 ;i < q ; i++)
83     {
84         printf("%lld\n",ans[i]);
85     }
86 }
87     return 0;
88  } 

原文地址:https://www.cnblogs.com/MekakuCityActor/p/9015623.html

时间: 2024-10-10 14:54:12

【杭电OJ3938】【离线+并查集】的相关文章

杭电4496--D-City(***并查集***)

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2649    Accepted Submission(s): 922 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-c

Dragon Balls HDU杭电3635 【并查集,递归的方法找根节点】

Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. His country has N cities and there are exactly N dragon bal

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar

ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs &amp;&amp; 离线 &amp;&amp; 并查集)

http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时间看这一题.不过打过之前一场BestCoder的应该都会有点思路,虽然BC那题是二维,这题是三维的,但是思路应该是一样的,没错,就是离线加并查集. 正过来考虑的时候,发现第一个要求相邻块是好处理的,但是第二个要求能否到达(1000, 1000, 1000)这个条件似乎比较难判断,当时BC上的题根据题

HDU-5441 Travel 离线-并查集

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4680    Accepted Submission(s): 1532 Problem Description Jack likes to travel around the world, but he doesn't like to wait. Now, he is t

题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

Description In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimen

2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v)满足两点之间每一条边都不超过x,问有多少对 思路:首先我想到的是dfs求出每个查询小于等于他的一个连通块,然后num*(num-1)就是答案,但是时间只有一秒,这个复杂度是5*1e8,超时了(亲身体验了) 然后我们想这个是离线的,我们可不可以由小到大来推,这样前面的贡献到后面就依然能用了,但是我们

集合问题 离线+并查集 HDU 3938

题目大意:给你n个点,m条边,q个询问,每条边有一个val,每次询问也询问一个val,定义:这样条件的两个点(u,v),使得u->v的的价值就是所有的通路中的的最长的边最短.问满足这样的点对有几个. 思路:我们先将询问和边全部都按照val排序,然后我们知道,并查集是可以用来划分集合的,所以我们就用并查集来维护每一个集合就行了. //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespac

NOJ 1044 连通 OR 不连通 (离线并查集 + hash)

连通 OR 不连通 时间限制(普通/Java) :1000 MS/ 3000 MS          运行内存限制 : 65536 KByte 总提交 : 333            测试通过 : 65 题目描述 给定一个无向图,一共n个点,请编写一个程序实现两种操作: D x y 从原图中删除连接x,y节点的边. Q x y 询问x,y节点是否连通 输入 第一行两个数n,m(5<=n<=40000,1<=m<=100000) 接下来m行,每行一对整数 x y (x,y<=