Description

ICPC (Isles of Coral Park City) consist of several beautiful islands.

The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.

The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.

However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.

Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.

Write a program that advises the mayor which bridges are no alternative bridges for the given input.

Input

The input consists of several tests case.

Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

N MS1 D1 C1⋮SM DM CMN MS1 D1 C1⋮SM DM CM

For each test, the first line contains two positive integers N and M . N represents the number of islands and each island is identified by an integer 1 through NM represents the number of the pairs of islands between which a bridge may be built.

Each line of the next M lines contains three integers SiDi and Ci (1 ≤ i ≤ M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3 ≤ N ≤ 500, N − 1 ≤ M ≤ min(50000, N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges connect the same pair of two islands, that is, if i ≠ j and Si = Sj , then Di ≠ Dj. If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.

Output

Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.

Sample Input

4 4
1 2 3
1 3 3
2 3 3
2 4 3 4 4
1 2 3
1 3 5
2 3 3
2 4 3 4 4
1 2 3
1 3 1
2 3 3
2 4 3 3 3
1 2 1
2 3 1
1 3 1

Sample Output

1 3
3 9
2 4
0 0 可以组成多种最小生成树,求他们的公共边,和权值和;
这个n ,可以直接暴力枚举;
暴力出奇迹
暴力枚举一下就好了;
先求出一个最小生成树,记录边;
依次删边,看新的最小生成树的权值是否相等
不相等则证明,必须有的边,
 #include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
using namespace std;
const int maxn = 5e4 + ;
const int INF = 1e9 + ;
int fa[], vis[maxn];
struct node {
int u, v, w;
} qu[maxn];
int cmp(node a, node b) {
return a.w < b.w;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int combine(int x, int y) {
int nx = find(x);
int ny = find(y);
if(nx != ny) {
fa[nx] = ny;
return ;
}
return ;
}
int kruskal(int num, int flag, int x) {
int sum = , k = ;
for(int i = ; i < num; i++) {
if(x == i) continue;
if(combine(qu[i].u, qu[i].v)) {
sum += qu[i].w;
if(flag) vis[k++] = i;
}
}
return sum;
}
int main() {
// freopen("DATA.txt", "r", stdin);
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
for (int i = ; i < m ; i++) {
scanf("%d%d%d", &qu[i].v, &qu[i].u, &qu[i].w);
}
sort(qu, qu + m, cmp);
int temp = kruskal(m, , -);
int ans1 = , ans2 = ;
for (int i = ; i <= n ; i++) fa[i] = i;
for (int i = ; i < n - ; i++ ) {
for (int j = ; j <= n ; j++) fa[j] = j;
int sum = kruskal(m, , vis[i]);
if (sum != temp) {
ans1++;
ans2 += qu[vis[i]].w;
}
}
printf("%d %d\n", ans1, ans2 );
}
return ;
}

There is No Alternative~最小生成树变形的更多相关文章

  1. bzoj 2753 最小生成树变形

    我们根据高度建图,将无向边转化为有向边 首先对于第一问,直接一个bfs搞定,得到ans1 然后第二问,我们就相当于要求找到一颗最小生成树, 满足相对来说深度小的高度大,也就是要以高度为优先级 假设现在 ...

  2. hdu 4081 最小生成树变形

    /*关于最小生成树的等效边,就是讲两个相同的集合连接在一起 先建立一个任意最小生成树,这条边分开的两个子树的节点最大的一个和为A,sum为最小生成树的权值和,B为sum-当前边的权值 不断枚举最小生成 ...

  3. POJ1789&amp;ZOJ2158--Truck History【最小生成树变形】

    链接:http://poj.org/problem?id=1789 题意:卡车公司有悠久的历史,它的每一种卡车都有一个唯一的字符串来表示,长度为7,它的全部卡车(除了第一辆)都是由曾经的卡车派生出来的 ...

  4. poj 2253 Frogger【最小生成树变形】【kruskal】

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30427   Accepted: 9806 Descript ...

  5. UVa 1395 - Slim Span(最小生成树变形)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. HDU 4786 最小生成树变形 kruscal(13成都区域赛F)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. poj2377Bad Cowtractors (最小生成树变形之——最大生成树)

    题目链接:http://poj.org/problem?id=2377 Description Bessie has been hired to build a cheap internet netw ...

  8. UESTC 918 WHITE ALBUM --生成树变形

    最小生成树变形. 题目已经说得很清楚,要求到达每个房间,只需求一个最小生成树,这时边权和一定是最小的,并且那k个房间一定与所有点都有通路,即一定都可以逃脱. 但是有可能当所有点都有了该去的安全房间以后 ...

  9. pta7-20 畅通工程之局部最小花费问题(Kruskal算法)

    题目链接:https://pintia.cn/problem-sets/15/problems/897 题意:给出n个城镇,然后给出n×(n-1)/2条边,即每两个城镇之间的边,包含起始点,终点,修建 ...

随机推荐

  1. 《java入门第一季》之泛型方法和泛型接口

    一.泛型方法. /* * 泛型方法:把泛型定义在方法上.格式:public <泛型类型> 返回类型 方法名(泛型类型 t) public <T> void show(T t){ ...

  2. (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)

    上篇说道要想将手臂固定在机器人身体上,而且手臂还能转动,简单的办法是使用物理关节.但这不是只有这种办法.用关节固定物体有时候不能满足需要,这时必须自己动手写代码处理,后面会介绍另一种固定的方法. 在S ...

  3. 《java入门第一季》之正则表达式小案例

    案例一: 判断手机号码是否满足要求 import java.util.Scanner; /* * * 需求: * 判断手机号码是否满足要求? * * 分析: * 13436975980 * 13688 ...

  4. shell sed过滤器详解

    1. Sed简介sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中 ...

  5. ISLR系列:(4.2)模型选择 Ridge Regression & the Lasso

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  6. jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能

     Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文 ...

  7. Ext.Net_1.X_WINDOW遮罩层被GridPanel挡住

    通过调试HTML代码,发现其实是DIV. chrome 中修改DIV Z:INDEX 就不被遮住了?但是又晓得如何修改window的Z:INDEX.那就修改"背景"GP的吧.

  8. 【Qt编程】Qt 小时钟

    Hello World! 学习编程语言的最简单最经典的小程序,当然Qt也不例外.在学习画图时,我觉得写个时钟小程序也是个比较好的开始.在之前的<Matlab及Java小时>一文中,我也从写 ...

  9. 调用bios喇叭发声

    话不多说,上代码: #include <windows.h> #include <iostream> #include <map> using namespace ...

  10. 《java第一季之入门篇》的想法

    学习java也有一段时间了,但是考虑到自己现在上课.复习.考试等耗费很多时间,感觉没有静下心来的时间去写一个长期的博客.计划今年7月1号开始写一套关于java的入门篇博客文章,入门篇计划这样--涵盖j ...