题目大意:计算最小生成树有两种算法:一种是kruskal算法,另一种是与之相反的:如果图中存在环,去掉权重最大的边,直到不存在环。输出去掉的那些边。

  可以用kruskal算法解决,在判断一条边时如果加入该边能形成环,保存该边即可。

 #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 1100
typedef pair<int, int> ii; int p[MAXN]; int find(int x)
{
return p[x] == x ? x : p[x]=find(p[x]);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n, m;
while (scanf("%d%d", &n, &m) && (n || m))
{
int u, v, w;
vector<pair<int, ii> > EdgeList;
for (int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
EdgeList.push_back(make_pair(w, make_pair(u, v)));
}
sort(EdgeList.begin(), EdgeList.end());
for (int i = ; i < n; i++)
p[i] = i;
vector<int> ans;
for (int i = ; i < EdgeList.size(); i++)
{
w = EdgeList[i].first;
u = EdgeList[i].second.first;
v = EdgeList[i].second.second;
int pu = find(u);
int pv = find(v);
if (pu != pv) p[pv] = pu;
else ans.push_back(w);
}
if (ans.empty()) printf("forest\n");
else
{
for (int i = ; i < ans.size(); i++)
printf("%s%d", i == ? "" : " ", ans[i]);
printf("\n");
}
}
return ;
}

UVa 11747 - Heavy Cycle Edges的更多相关文章

  1. leetcode-685-冗余连接②

    题目描述: 参考后提交:并查集: class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) - ...

  2. leetcode-并查集

    - 题目:130 并查集: class Solution: def solve(self, board: List[List[str]]) -> None: """ ...

  3. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  4. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

  5. UVa 11090 Going in Cycle!!【Bellman_Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

  6. UVA 11090 Going in Cycle!!

    要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...

  7. uva 1561 - Cycle Game(推理)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style=""& ...

  8. UVa 11090 Going in Cycle!! (Bellman_Ford)

    题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...

  9. UVA 11090 Going in Cycle!! SPFA判断负环+二分

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

随机推荐

  1. POJ 2289 Jamie's Contact Groups

    二分答案+网络最大流 #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...

  2. springMVC拦截器简单配置

    <!-- 拦截器 -->    <mvc:interceptors>        <mvc:interceptor>            <!-- 拦截所 ...

  3. hdu_2457_DNA repair(AC自动机+DP)

    题目连接:hdu_2457_DNA repair 题意: 给你N个字符串,最后再给你一个要匹配的串,问你最少修改多少次,使得这个串不出现之前给的N的字符串 题解: 刚学AC自动机,切这题还真不知道怎么 ...

  4. codeforces 369 div2 C dp

    http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...

  5. C#入门经典第四章-流程控制-1

    布尔类型:

  6. Spring + iBATIS完整示例

    最近研究了一下Spring + iBATIS.发现看别人的例子是一回事,自己写一个完整的应用又是另外一回事.自己受够了网上贴的一知半解的代码. iBATIS是一个持久化框架,封面了sql过程,虽然sq ...

  7. FusionCharts生成报表应用

    1.需要组装要展示的数据,至于如何怎样去设计数据模型,看你要展示的图形和需要的数据就行了.来个简单的. 实体类,只有两个属性,也可以使用Bean里面的实体类,无所谓了. package com.gol ...

  8. WeakSelf和StrongSelf

    转载自:http://sherlockyao.com/blog/2015/08/08/weakself-and-strongself-in-blocks/ 现在我们用 Objective-C 写代码时 ...

  9. Mac 下面,添加android adb命令(一般环境变量的添加方法)

    1. 在 用户目录下   ~/.bash_profile vim  ~/.bash_profile 2.加入我们需要添加的环境变量,这里是 添加 android   platform-tools 和 ...

  10. PowerDesigner中几个使用技巧

    一.主键自增 二.设置列的约束 三.修改Name和Code一起改变的选项 Tools = > Generator Options=>Dialog -> Name to Code mi ...