D. Edges in MST 图论
http://codeforces.com/contest/160/problem/D
base on 克鲁斯卡尔,
首先每次都是对权值相同的边进行统一处理,假如加入了当前这条边出现了回路,那就能确定这条边是none的。
否则,让它加入进图,(先不合并),然后找到这个图的桥,那些就是any的,其他都是at least one的。
唯一要注意的就是不能像克鲁斯卡尔这样,没加入一条边,都合并。这里是把权值相同的边加入来后,再统一合并。
4 4
1 2 1
2 3 1
3 4 2
1 3 2
any
any
any
none
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + ;
int first[maxn], num;
struct Edge {
int u, v, w, id, tonext;
}e[maxn * ];
void addEdge(int u, int v, int w, int id) {
e[num].u = u, e[num].v = v, e[num].w = w, e[num].id = id, e[num].tonext = first[u];
first[u] = num++;
}
struct Node {
int u, v, w, id;
}data[maxn];
bool cmp1(struct Node a, struct Node b) {
return a.w < b.w;
}
int fa[maxn];
int tofind(int x) {
if (fa[x] == x) return x;
else return fa[x] = tofind(fa[x]);
}
char ans[][] = {"none", "any", "at least one"};
int res[maxn], in[maxn];
int DFN[maxn], low[maxn], when, vis[maxn];
void dfs(int cur, int fromID) {
DFN[cur] = low[cur] = ++when;
for (int i = first[cur]; ~i; i = e[i].tonext) {
int v = e[i].v;
if (e[i].id == fromID) continue;
vis[e[i].id] = true;
if (!DFN[v]) {
dfs(v, e[i].id);
low[cur] = min(low[cur], low[v]);
if (low[v] > DFN[cur]) {
res[e[i].id] = ;
}
} else low[cur] = min(low[cur], DFN[v]);
}
}
void work() {
num = ;
memset(first, -, sizeof first);
for (int i = ; i <= maxn - ; ++i) fa[i] = i;
int n, m;
cin >> n >> m;
for (int i = ; i <= m; ++i) {
cin >> data[i].u >> data[i].v >> data[i].w;
data[i].id = i;
}
sort(data + , data + + m, cmp1);
for (int i = ; i <= m;) {
int en;
for (en = i + ; en <= m && data[en].w == data[i].w; ++en);
for (int j = i; j < en; ++j) {
int x = tofind(data[j].u), y = tofind(data[j].v);
if (x == y) continue;
addEdge(x, y, data[j].w, data[j].id);
addEdge(y, x, data[j].w, data[j].id);
res[data[j].id] = ;
in[data[j].id] = true;
}
for (int j = i; j < en; ++j) {
if (vis[data[j].id] || !in[data[j].id]) continue;
vis[data[j].id] = true;
dfs(tofind(data[j].u), -);
}
for (int j = i; j < en; ++j) {
int x = tofind(data[j].u), y = tofind(data[j].v);
if (x != y) {
fa[y] = x;
first[x] = first[y] = -;
DFN[x] = DFN[y] = low[x] = low[y] = false;
when = ;
}
}
i = en;
}
for (int i = ; i <= m; ++i) {
cout << ans[res[i]] << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}
D. Edges in MST 图论的更多相关文章
- Codeforces 160D Edges in MST tarjan找桥
Edges in MST 在用克鲁斯卡尔求MST的时候, 每个权值的边分为一类, 然后将每类的图建出来, 那些桥就是必须有的, 不是桥就不是必须有. #include<bits/stdc++.h ...
- Codeforces 160 D. Edges in MST
\(>Codeforces \space 160 D. Edges in MST<\) 题目大意 : 给出一张带权无向图,求对于这张图上的每一条边,其是必然存在于每一种最小生成树中,还是至 ...
- [CF160D]Edges in MST
[CF160D]Edges in MST 题目大意: 一个\(n(n\le10^5)\)个点,\(m(m\le10^5)\)条边的连通图.对于图中的每条边,判断它与该图最小生成树的关系: 在该图所有的 ...
- [CF160D]Edges in MST (最小生成树+LCA+差分)
待填坑 Code //CF160D Edges in MST //Apr,4th,2018 //树上差分+LCA+MST #include<cstdio> #include<iost ...
- Codeforces Round #599 (Div. 1) B. 0-1 MST 图论
D. 0-1 MST Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math ...
- CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3
http://codeforces.com/problemset/problem/160/D 这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中 明显桥边 ...
- 清北学堂(2019 5 2) part 5
今天讲图论,顺便搞一搞之前没弄完的前向星dij 1.图的基本概念(课件原话): G (图)= (V(点); E(边)) 一般来说,图的存储难度主要在记录边的信息 无向图的存储中,只需要将一条无向边拆成 ...
- Codeforces Round #111 (Div. 2)
Codeforces Round #111 (Div. 2) C. Find Pair 题意 给\(N(N \le 10^5)\)个数,在所有\(N^2\)对数中求第\(K(K \le N^2)\)对 ...
- 算法8-4:Kruskal算法
Kruskal算法用于计算一个图的最小生成树.这个算法的过程例如以下: 依照边的权重从小到达进行排序 依次将每条边添加到最小生成树中,除非这条边会造成回路 实现思路 第一个步骤须要对边进行排序,排序方 ...
随机推荐
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
- JavaScript的中类型转换
JavaScript的类型转换 By 大志若愚 (一)转换为字符串 X + '' toString() String() 函数转换为字符串一般是将函数体输出,不过可以重写其toString方法 ( ...
- puppet初始化安装和配置(puppet自动化系列1)
一.服务器规划 以下均直接yum安装最新版. 服务器操作系统为centos6.2 Puppetmaster1 10.168.32.116 puppstmaster1.jq.com Puppetmast ...
- jquery中attr() & prop() 的区别与其实现方法
$(function(){ $('#check').attr('checked'); // undefind ???一头雾水 }) 在jquery中 attr 本来就是用来设置或者获取属性的,可是上面 ...
- 用fpm模式在虚拟主机上安装phpmyadmin
实验环境:CentOS7 1.配置虚拟主机 [root@conf.d localhost]#vi /etc/httpd/conf.d/vhost.conf #配置在/etc/httpd/conf.d下 ...
- WPF RichTextBox 插入回车
richtextbox插入回车,开始是这样写的,在win7下运行时没有问题: MyMessageBox.CaretPosition.InsertLineBreak(); MyMessageBox.Ca ...
- Java之匿名类讲解
参考https://blog.csdn.net/jiaotuwoaini/article/details/51542059 匿名类,正如名字一样在java中没有名字标识的类,当然了编译后还是会安排一个 ...
- 利用html5看雪花飘落的效果
html5飘落的雪花堆积动画特效 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-T ...
- css hack汇总
注意点: 网上很多资料中常常把!important也作为一个hack手段,其实这是一个误区.!important常常被我们用来更改样式,而不是兼容hack.造成这个误区的原因是IE6在某些情况下不主动 ...
- 模拟一则ORA-600 [4194][][]故障并处理
环境:OEL 5.7 + Oracle 11.2.0.3 1.模拟ORA-600 [4194][][]故障 2.使用bbed处理 3.尝试启动数据库 1.模拟ORA-600 [4194][][]故障 ...