There is No Alternative

题目连接:

http://codeforces.com/gym/100803/attachments

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 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 N. M 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 Si

, Di 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 6= j and Si = Sj , then Di 6= 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

Sample Output

1 3

Hint

题意

给你一个图,然后问你有哪些边一定在所有的最小生成树上面

题解:

点只有500,所以我们直接O(nm)暴力就好了

我们只用ban掉一开始在最小生成树上的边,然后判断就好了

代码

#include<bits/stdc++.h>
using namespace std; const int maxn = 100050; int u[maxn],v[maxn],w[maxn],r[maxn];
int pa[maxn],n,m;
int vis[maxn];
int ans1=0,ans2=0;
bool cmp(int i,int j)
{
return w[i]<w[j];
}
int fi(int x)
{
return pa[x]==x?x:pa[x]=fi(pa[x]);
}
int kruskal(int c)
{
int ans=0;
int num=0;
for(int i=0;i<=n;i++)
pa[i]=i;
for(int i=0;i<m;i++)
{
if(i==c)continue;
int e=r[i];
int x=fi(u[e]);
int y=fi(v[e]);
if(x!=y)
{
num++;
vis[i]=1;
ans+=w[e],pa[x]=y;
}
if(num==n-1)break;
}
if(num!=n-1)return -1;
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
u[i]=x,v[i]=y,w[i]=z;
}
for(int i=0;i<m;i++)
r[i]=i;
sort(r,r+m,cmp);
int temp = kruskal(m);
for(int i=0;i<m;i++)
{
if(!vis[i])continue;
if(temp!=kruskal(i))
{
ans1++;
ans2+=w[r[i]];
}
}
cout<<ans1<<" "<<ans2<<endl;
}

Codeforces Gym 100803F There is No Alternative 暴力Kruskal的更多相关文章

  1. Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力

    原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...

  2. Codeforces Gym 100342C Problem C. Painting Cottages 暴力

    Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1 ...

  3. Codeforces Gym 100513I I. Sale in GameStore 暴力

    I. Sale in GameStore Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/p ...

  4. Codeforces Gym 100203G G - Good elements 标记暴力

    G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  5. Codeforces Gym 100650D Queens, Knights and Pawns 暴力

    Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...

  6. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  7. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  8. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  9. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

随机推荐

  1. Java核心 --- 泛型

    CoreJava 泛型 java泛型的出现避免了强制类型转换,便于代码更好的被阅读 本文的写作参照了张孝祥的泛型介绍:http://www.itcast.cn/news/dbfd20f1/f4b1/4 ...

  2. Visual Studio 2010+Oracle 10g +NHibernate配置

    南京酷都面试,考官问:你知道NHibernate吗?瞬间我就急了:只听说过Hibernate,NHibernate是什么?还有其他问题也是不知道,所以后果就悲剧了. 自己做一个小系统,总是想如果数据量 ...

  3. WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5

    以下是我的程序(取自headfirst Java): import javax.sound.midi.*; public class MiniMiniMusicApp { public static ...

  4. IOS平台设计规范

    一.UI的控件概述: 1.框架UI的元素分为4类: A:栏:状态栏目和导航栏的结合体; B:内容视图:应用显示的内容信息,包括相关的交互行为,例如滚屏.插入.删除等操作进行排序; C:控制元素:产品行 ...

  5. effective c++:virtual函数的替代方案

    绝不重新定义继承来的缺省值 首先明确下,虚函数是动态绑定,缺省参数值是静态绑定 // a class for geometric shapes class Shape { public: enum S ...

  6. 关于面试mysql优化的几点纪要

    1.减少查询次数 ,如何减少 ?  2. 表结构优化,如何优化 ? 3. 列选取原则  ? 4.建索引原则   ? 5.mysql语句优化 ?   6.增加mysql处理性能 ? 通过这几点, 再来说 ...

  7. MapReduce运行时出现java.lang.NoClassDefFoundError

    最近在编写MapReduce程序时遇到了一个错误,错误提示信息为: Error: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.H ...

  8. JavaScript的function对象

    我必须先说Java与JavaScript没有关系,不是我以前想的那个样子的(JavaScript是Java的一种超进化) 在JavaScript中,函数(function)就是对象. JavaScri ...

  9. 《java数据结构与算法》笔记-CH4-8栈结构实现后缀表达式计算结果

    /** * 中缀表达式转换成后缀表达式: 从输入(中缀表达式)中读取的字符,规则: 操作数: 写至输出 左括号: 推其入栈 右括号: 栈非空时重复以下步骤--> * 若项不为(,则写至输出: 若 ...

  10. [cocos2d-js]按钮整合成大图后打APK后不显示

    网页版本都能正常显示碎图和整合成大图的 手机版本不正常 var btnKick = cc.MenuItemImage.create( "#btn_kick.png", " ...