UVa 11747 - Heavy Cycle Edges
题目大意:计算最小生成树有两种算法:一种是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的更多相关文章
- leetcode-685-冗余连接②
题目描述: 参考后提交:并查集: class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) - ...
- leetcode-并查集
- 题目:130 并查集: class Solution: def solve(self, board: List[List[str]]) -> None: """ ...
- UVA 11090 - Going in Cycle!!(Bellman-Ford)
UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- UVA 11090 Going in Cycle!!
要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...
- uva 1561 - Cycle Game(推理)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style=""& ...
- UVa 11090 Going in Cycle!! (Bellman_Ford)
题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...
- UVA 11090 Going in Cycle!! SPFA判断负环+二分
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- 快速部署Python应用:Nginx+uWSGI配置详解
在PHP里,最方便的就是deployment了,只要把php文件丢到支持PHP的路径里面,然后访问那个路径就能使用了:无论给主机添加多少PHP应用,只要把目录改好就没你的事了,完全不用关心php-cg ...
- 虚拟ip
网卡上增加一个IP: ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0 删除网卡的第二个IP地址: ip addr del 192.168.0.1 d ...
- vm选项大全
http://hllvm.group.iteye.com/group/topic/27945 java -XX:后边的总记不住 vm选项大全 http://www.oracle.com/technet ...
- Java socket通信
首先抛开语言层面,简单介绍一下socket通信过程: 1.服务器端开启监听端口,阻塞进程 等待客户端连接 2.客户端连接,这时就产生了一个socket socket就相当于一个传递消息的通道,一般都 ...
- POJ 3254 Corn Fields(状态压缩)
一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...
- LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods
我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...
- mac ox 配置java和maven
参考http://www.cnblogs.com/iOS-mt/p/5726380.html 以及http://blog.csdn.net/done58/article/details/5113805 ...
- CSS实现三角形方法二--border+content
方法说明: 1.将一个div块的内容设置为空(content=" "), 2.设置它的边框(上下左右)颜色为透明(transparent), 3.设置它的左侧边框颜色为pink. ...
- PHP 对MySQLI预处理的包装
mysql 类 <?php class Mysql { private static $instance; private $link; private $query; private $stm ...
- C#WinForm中复制、粘贴文本到剪贴板
//复制: private void button1_Click(object sender, System.EventArgs e) { if(textBox1.SelectedText != ...