n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少。

最小瓶颈路显然在kruskal求得的MST上。而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了。

拿到这题第一反映是用次小生成树的prim算法在求MST的同时求出每对点对的瓶颈路。几乎就是一个模板题,无奈却MLE。。。

于是换算法,用kruskal求MST,然后对于MST,离线LCA求出所有点对的瓶颈路。同UVA 11354 Bond(MST + LCA)然后剩下的就是读入&二分查找输出了。。无奈还是MLE!!!

最后。。。反思了一下。。。在kruskal的过程,当前加入的边必定是新图中最大的边!也就是说,每次加入一条边,求出当前图中经过该边的点对数就行了。。。求一个图中经过该边的点对数,将该边割开,分别从两个端点dfs,左边能遍历到x个点,右边能遍历到y个点,那么点对数就是x*y了。

原图不连通的情况也是存在的吧,这个几乎对算法不影响,只需在进入MST的点数==n的时候终止函数就行了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define LL long long
#define PB push_back
#define eps 1e-10
#define debug puts("**debug**")
using namespace std; const int maxn = 10010;
const int maxm = 555555;
const int INF = 1e9;
int n, m, dfs_clock, q, f, cnt, fa[maxn];
LL sum[maxn*2];
bool seen[maxn];
vector<int> edge; struct E
{
int u, v, w;
E(){}
E(int u, int v, int w) : u(u), v(v), w(w){}
bool operator < (const E& rhs) const
{
return w < rhs.w;
}
}e[maxm]; //kruskal的边 vector<int> G[maxn]; //dfs用
inline void add(int a, int b)
{
G[a].PB(b);
G[b].PB(a);
} int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); } void dfs(int u, int fa)
{
dfs_clock++;
REP(i, G[u].size())
{
int v = G[u][i];
if(v != fa) dfs(v, u);
}
} void MST()
{
int ret = 0;
cnt = 1;
sum[0] = 0;
CLR(seen, 0);
sort(e, e+m); REP(i, m)
{
int x = findset(e[i].u), y = findset(e[i].v);
if(x != y)
{
//统计进入森林的点数
if(!seen[e[i].u]) ret++;
if(!seen[e[i].v]) ret++;
seen[e[i].u] = 1;
seen[e[i].v] = 1; fa[x] = y;
add(e[i].u, e[i].v); //将边切割双向统计两边点数
dfs_clock = 0;
dfs(e[i].u, e[i].v);
int a = dfs_clock; dfs_clock = 0;
dfs(e[i].v, e[i].u);
int b = dfs_clock; //edge保存所有MST中边 sum[i]为前i条边和
edge.PB(e[i].w);
sum[cnt] = sum[cnt-1] + a*b;
cnt++;
}
if(ret == n) return ; //终止MST
}
return ;
} void solve()
{
scanf("%d", &q);
while(q--)
{
scanf("%d", &f);
int t = lower_bound(edge.begin(), edge.end(), f) - edge.begin();
//找到f的lower_bound 答案便是总和减去小于f的点对和 注意乘以2
printf("%lld\n", (sum[cnt-1]-sum[t])*2);
}
} int main()
{
while(~scanf("%d%d", &n, &m))
{
REP(i, n) G[i].clear(), fa[i] = i;
edge.clear();
REP(i, m)
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); MST(); solve(); }
return 0;
}

hdu 4750 Count The Pairs (2013南京网络赛)的更多相关文章

  1. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  3. HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  4. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  7. HDU 4734 F(x) (2013成都网络赛,数位DP)

    F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. 2013南京网赛1003 hdu 4750 Count The Pairs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...

随机推荐

  1. VBA清除Excelpassword保护,2003/2007/2010均适用

    Sub Macro1() ' ' Breaks worksheet and workbook structure passwords. Jason S ' probably originator of ...

  2. XCode中在提示窗体中对已弃用的API接口画上红线

    当我们在XCode中写程序时会不断的出现相关API提示窗体,那敲起来是一个爽啊. 有时候会看到一些API已经弃用了被画上红色的横线.说明该接口已经被弃用,仍保留,但不建议使用,对弃用API实现画横线事 ...

  3. ubuntu下30天自制操作系统还在继续学习中

    操作系统还在学习中,进度不是非常确定,近期学习到了第13天的中部,由于把ucgui移植上去花了一点时间. 同一时候为了方便代码的兴许管理和分享,也为了学习github的代码管理使用思想, 所以建立了一 ...

  4. Java / Android H基于ttp多线程下载的实现

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/26994463 有个朋友须要个多线程如今的样例,就帮忙实现了.在此分享下~ 先说下 ...

  5. CPU性能测试

    用计算圆周率的办法来测试cpu性能 4*a(1) 是 bc 主動提供的一個計算 pi 的函數,至於 scale 就是要 bc 計算幾個小數點下位數的意思.當 scale 的數值越大, 代表 pi 要被 ...

  6. IE浏览器中hasLayout的介绍

    haslayout是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么对自身的内容进行计算大小和组织,要么依赖于父元素来计 ...

  7. html进阶css(4)

    盒子模型-边框 首先请看下图 <!doctype html> <html> <head> <meta charset="utf-8"> ...

  8. ASP.NET MVC 部分视图(转)

    [部分视图] ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control.我们的页面往往会有许多重用的地方,可以进行封装重用.使用 部分视图 :  1. 可以简写代 ...

  9. iOS 开发者证书总结

    iOS 证书分两种类型. 第一种为$99美元的,这种账号有个人和公司的区别,公司账号能创建多个子账号,但个人的不能.这种账号可以用来上传app store 第二种为¥299美元的,这种账号只能用于企业 ...

  10. Web 上一页下一页 用超链接 用按钮

              方法一超链接 Default.aspx.cs html代码************************************************************* ...