Edges in MST

在用克鲁斯卡尔求MST的时候, 每个权值的边分为一类, 然后将每类的图建出来, 那些桥就是必须有的, 不是桥就不是必须有。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); struct Edge {
int a, b, id, tmp1, tmp2;
}; int n, m, fa[N], ans[N];
bool brige[N];
int dfn[N], low[N], idx;
vector<Edge> vc[N];
vector<PII> G[N]; int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} void tarjan(int u, int id) {
dfn[u] = low[u] = ++idx;
for(auto& e : G[u]) {
if(e.se == id) continue;
if(!dfn[e.fi]) {
tarjan(e.fi, e.se);
low[u] = min(low[u], low[e.fi]);
if(dfn[u] < low[e.fi]) brige[e.se] = true;
} else low[u] = min(low[u], dfn[e.fi]);
}
}
int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i <= m; i++) {
int a, b, w;
scanf("%d%d%d", &a, &b, &w);
vc[w].push_back(Edge{a, b, i, , });
}
for(int i = ; i <= ; i++) {
if(!SZ(vc[i])) continue;
idx = ;
for(auto& e : vc[i]) {
e.tmp1 = getRoot(e.a);
e.tmp2 = getRoot(e.b);
if(e.tmp1 > e.tmp2) swap(e.tmp1, e.tmp2);
}
for(auto& e : vc[i]) {
if(e.tmp1 == e.tmp2) {
ans[e.id] = ;
} else {
G[e.tmp1].clear();
G[e.tmp2].clear();
dfn[e.tmp1] = dfn[e.tmp2] = ;
}
}
for(auto& e : vc[i]) {
if(e.tmp1 != e.tmp2) {
G[e.tmp1].push_back(mk(e.tmp2, e.id));
G[e.tmp2].push_back(mk(e.tmp1, e.id));
}
}
for(auto& e : vc[i]) {
if(e.tmp1 != e.tmp2) {
if(!dfn[e.tmp1]) tarjan(e.tmp1, );
if(!dfn[e.tmp2]) tarjan(e.tmp2, );
}
}
for(auto& e : vc[i]) {
if(e.tmp1 != e.tmp2) {
if(brige[e.id]) ans[e.id] = ;
else ans[e.id] = ;
}
}
for(auto& e : vc[i]) {
int x = getRoot(e.a);
int y = getRoot(e.b);
if(x != y) fa[x] = y;
}
}
for(int i = ; i <= m; i++) {
if(ans[i] == ) puts("any");
else if(ans[i] == ) puts("at least one");
else puts("none");
}
return ;
} /*
*/

Codeforces 160D Edges in MST tarjan找桥的更多相关文章

  1. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  2. Tarjan找桥和割点与点连通分量与边连通分量【未成形】

    之前只学了个强连通Tarjan算法,然后又摸了缩点操作: 然后今天在lightoj摸了一道模板题,是求所有桥的题: 然后发现,要把:割点,割点集合,双连通,最小割边集合(桥),点连通分量,边连通分量都 ...

  3. CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3

    http://codeforces.com/problemset/problem/160/D 这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中 明显桥边 ...

  4. Codeforces 160 D. Edges in MST

    \(>Codeforces \space 160 D. Edges in MST<\) 题目大意 : 给出一张带权无向图,求对于这张图上的每一条边,其是必然存在于每一种最小生成树中,还是至 ...

  5. hdu 4738 Caocao's Bridges (tarjan求桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...

  6. [CF160D]Edges in MST

    [CF160D]Edges in MST 题目大意: 一个\(n(n\le10^5)\)个点,\(m(m\le10^5)\)条边的连通图.对于图中的每条边,判断它与该图最小生成树的关系: 在该图所有的 ...

  7. Tarjan 求桥,割,强连通

    最近遇到了这种模板题,记录一下 tarjan求桥,求割 #include <bits/stdc++.h> using namespace std; #define MOD 99824435 ...

  8. Tarjan求桥

    传送门(poj3177) 这道题是Tarjan求桥的模板题.大意是要求在原图上加上数量最少的边,使得整张图成为一个边双联通分量. 具体的做法是,先在图中求出所有的桥,之后把边双联通分量缩成点,这样的话 ...

  9. [CF160D]Edges in MST (最小生成树+LCA+差分)

    待填坑 Code //CF160D Edges in MST //Apr,4th,2018 //树上差分+LCA+MST #include<cstdio> #include<iost ...

随机推荐

  1. 22. SpringBoot 集成 Mybatis

    1. 引入Mybatis的maven 依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> < ...

  2. Java EE之Hibernate异常总结【5】java.lang.StackOverflowError[栈溢出]

    Exception in thread "main" java.lang.StackOverflowError at java.lang.reflect.InvocationTar ...

  3. mysql 架构~多写模式MGR

    一  简介:今天咱们来聊聊MGR的单主切换和新节点加入二 单主模式下变成多主:  1 3306  STOP group_replication;  set global group_replicati ...

  4. mysql 开源~canal的深度解读1

    一 简介:经过一段时间的研究,对canal有了一些见解 二 配置文件: 1 canal.properties (系统根配置文件)     主要参数列表   canal.properties (系统根配 ...

  5. 实用技能之Python打包制作成EXE可执行程序

    制作环境:Andconda3,python3.6 一.安装pyInstaller 方式一): 在命令行输入:pip install pyinstaller 方式二): ①   下载pyInstalle ...

  6. LOJ 2567: 洛谷 P3643: bzoj 4584: 「APIO2016」划艇

    题目传送门:LOJ #2249. 题意简述: 有 \(n\) 个位置,第 \(i\) 个位置可以填在 \([a_i,b_i]\) (\(1\le a_i\le b_i\le 10^9\))之间的整数, ...

  7. 安装VNC

    一.安装相应桌面环境与vnc服务端和客户端: # yum groupinstall "GNOME Desktop Environment"(CentOS 5.x安装GNOME桌面环 ...

  8. OpenWrt启动过程分析+添加自启动脚本【转】

    一.OpenWrt启动过程分析 转自: http://www.eehello.com/?post=107 总结一下OpenWrt的启动流程:1.CFE->2.linux->3./etc/p ...

  9. 一个有趣的小例子,带你入门协程模块-asyncio

    一个有趣的小例子,带你入门协程模块-asyncio 上篇文章写了关于yield from的用法,简单的了解异步模式,[https://www.cnblogs.com/c-x-a/p/10106031. ...

  10. OCM_第七天课程:Section3 —》数据库可用性

    注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...