题目描述
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 ar
e expressed by bold edges in figure F.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.
输入
The input consists of a single test case.
N M
S1 D1 C1
.
.
.
SM DM CM
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 ≠ 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 two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.
样例输入
4 4 1 2 3 1 3 3 2 3 3 2 4 3
样例输出
1 3
大意为寻找所有的不可去掉最小生成树的边,并统计其权重
题解代码
#include <iostream>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int mod = 998244353;
const int N = 5e6 + 10;
int n,m,s,d,w,p[N],sum,ss;
struct Edge{
int a,b,w,f;
bool operator <(const Edge &W) const
{
return w<W.w;
}
}edges[N];
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&edges[i].a,&edges[i].b,&edges[i].w);
}
sort(edges+1,edges+m+1);
for(int i=1;i<=n;i++) p[i]=i;
int res=0;
for(int i=1;i<=m;i++){
int a=edges[i].a,b=edges[i].b,w=edges[i].w;
a=find(a),b=find(b);
if(a==b) edges[i].f=0;
if(a!=b){
edges[i].f=1;
p[a]=b;
res+=w;
}
}
for(int i=1;i<=m;i++){
int o=0;
if(edges[i].f==0) continue;
for(int q=1;q<=n;q++) p[q]=q;
for(int q=1;q<=m;q++){
if(edges[i].f==1&&i==q) continue;
int a=edges[q].a,b=edges[q].b,w=edges[q].w;
a=find(a),b=find(b);
if(a!=b){
p[a]=b;
o+=w;
}
}
if(o!=res&&edges[i].f==1){
sum++;
ss+=edges[i].w;
}
}
printf("%d %d",sum,ss);
return 0;
}