题目大意:计算最小生成树有两种算法:一种是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. Redis(2)用jedis实现在java中使用redis

    昨天已经在windows环境下安装使用了redis. 下面准备在java项目中测试使用redis. redis官网推荐使用jedis来访问redis.所以首先准备了jedis的jar包,以及需要依赖的 ...

  2. struts2 里escape="false"的问题?

    <s:property value="html" escape="false"/> 没有name 不知道你是怎么取的值 <s:hidden n ...

  3. 实例:SSH结合Easyui实现Datagrid的新增功能和Validatebox的验证功能

    在我前面一篇分页的基础上,新增了添加功能和添加过程中的Ajax与Validate的验证功能.其他的功能在后面的博客写来,如果对您有帮助,敬请关注. 先看一下实现的效果: (1)点击添加学生信息按键后跳 ...

  4. cell选中与取消选中调用的方法

    //选中与取消选中都会调用哦,注意!!- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ [super setSelected:se ...

  5. CentOS 在同一窗口打开文件夹

    1.打开一个文件夹 2.编辑 - 首选项 - 行为,勾选“总是在浏览器窗口打开”,点击关闭.

  6. FTP 1.0

    自己写的可以实现文件的下载(必须自己知道文件名),还有很多要优化. 譬如:不能看可以下载的文件,输入错误无法处理,不能处理多个用户,每次只能下载一个结束,服务器没有完成守护进程:没有用函数封装,简化m ...

  7. 在IE6里面当元素浮动后再设置margin那么就会产生双倍边距

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. How to create a zip file in NetSuite SuiteScript 2.0 如何在现有SuiteScript中创建和下载ZIP压缩文档

    Background We all knows that: NetSuite filecabinet provided a feature to download a folder to a zip ...

  9. 继续几道经典的js题(局部和全局变量,对象等)

    1. //现有代码如下:var foo = 1;function main(){alert(foo);var foo = 2;alert(this.foo)this.foo = 3;}//1.请给出以 ...

  10. BZOJ 2276 Temperature

    two-pointer型单调队列.... #include<iostream> #include<cstdio> #include<cstring> #includ ...