HDU 5624 KK's Reconstruction
这题目测是数据水了。我这种暴力写法显然是可以卡超时的。
假设有2000个点,15000条边,前面10000条不能构成树,后面5000条可以,这种数据显然可以卡超时。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std; const int maxn = + ;
const int maxm = + ;
const int inf = 0x7fffffff;
int T;
int n, m;
struct Edge
{
int u;
int v;
int id;
int val;
}e[maxm];
int fa[maxn]; bool cmp(const Edge&a, const Edge&b)
{
return a.val<b.val;
} void read()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++)
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].val);
sort(e + , e + + m, cmp);
} int Find(int x)
{
if (x != fa[x]) fa[x] = Find(fa[x]);
return fa[x];
} void work()
{
int ans = inf;
int i, j;
int cnt;
for ( i = ; i <= m; i++)
{
for ( j = ; j <= n; j++) fa[j] = j;
cnt = n;
for ( j = i; j <= m; j++)
{
int fu = Find(e[j].u);
int fv = Find(e[j].v);
if (fu == fv) continue;
fa[fu] = fv; cnt--;
if (cnt == )
{
ans = min(ans, e[j].val - e[i].val);
break;
}
}
if (cnt != ) break;
} if (ans == inf) ans = -;
printf("%d\n", ans);
} int main()
{
scanf("%d", &T);
while (T--)
{
read();
work();
}
return ;
}
HDU 5624 KK's Reconstruction的更多相关文章
- HDU 5624 KK's Reconstruction 最小生成树
题意:这是bc round 71 div 1 的 1004 直接去看中文题意 分析: 首先,一种合法方案对应了原图的一棵生成树. 我们知道,最小生成树有一个性质是最大边最小. 因此,我们可以枚举生成树 ...
- HDU 5623 KK's Number (博弈DP)
KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...
- HDU 5620 KK's Steel (斐波那契序列)
KK's Steel 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/J Description Our lovely KK ha ...
- hdu 5623 KK's Number(dp)
问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗104)个数,每次KK都会先拿数.每 ...
- hdu 5620 KK's Steel(推理)
Problem Description Our lovely KK has a difficult mathematical problem:he has a N(1≤N≤1018) meters s ...
- HDU 5622 KK's Chemical DP
题意:bc round 71(中文题面) 分析(官方题解): 根据药品之间的相互关系,我们可以构建一张图,我们对相互会发生反应的药品连边 这个图的特征,是一个环加上一些“树”(可能有多个联通块) 一个 ...
- hdu 5621 KK's Point(数学,推理题)
题解: 在圆上点三个点时,除圆上三个交点外,圆内没有交点:在圆上点四个点时,除圆上四个交点外,圆内出现了一个交点,因此,在N个点中每四个点便可以在圆内产生一个交点,因此N个点在圆内形成的点的个数为CN ...
- HDU 5621 KK's Point
N个点中任意选取四个点,就能产生一个圆内的交点,所以圆内总共有C(N,4)个交点,圆上有N个,相加就可以了. 注意:组合数运算的时候会爆longlong,中间先除一下就可以了. #include &l ...
- HDU 5620 KK's Steel
想了一下发现是斐波那契数列.....水题 #include <stdio.h> #include <algorithm> #include <string.h> # ...
随机推荐
- centos7如何关闭防火墙
1.centos7自带了firewall,而不是iptables: 关闭firewall: service firewalld stop 或者: systemctl stop firewalld 禁止 ...
- 由浅到深理解java反射
1.基础概念 class类: 1.1java是面向对象的,但是在java中存在两种东西不是面向对象的 一种是普通的数据类型,这也是封装数据类存在的原因. 二种是静态静态成员. 1.2所以我们首先要理解 ...
- FileSystemXmlApplicationContext方法的绝对路径问题
public AgentServer(Socket c,String confDir) { this.client = c; ApplicationContext ac = new FileSyste ...
- POJ 1182 食物链 经典并查集+关系向量简单介绍
题目: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有 ...
- Photos FrameWork 续
1. Model PHAsset .PHAssetCollection.PHCollectionList 是Photos框架中的模型类,PHAsset类模型是图片或者视频文件数据:PHAssetCol ...
- java 实例变量和类变量的区别
Example4_10.java public class Example4_10 { public static void main(String args[]) { Lader.下底=100; / ...
- ZOJ 2866 Overstaffed Company
树状数组 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- R语言——绘图函数深入学习
利用R自带数据集 通过data()函数可以查看R自带数据集. > data() 返回以下结果,每一条记录都是一个数据,键入相应的数据名称可以查看具体信息. Data sets in packag ...
- dev GridControl直接打印 纵向合并单元格
GridControl纵向合并单元格 只需设置 gridView->OptionView->AllowCellMerge=true; 效果 提示: 精确到列 前提是gridview1已经允 ...
- listview设置条目点击的时候不变色(让状态选择器不起作用)
未设置前的效果如下图: 很明显,“酷狗音乐”那个条目被点击的时候,条目背景变为蓝色,怎么去掉这个颜色呢? java代码可以这么写: listView.setSelector(new ColorDraw ...