Codeforces 160D Edges in MST tarjan找桥
在用克鲁斯卡尔求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找桥的更多相关文章
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Tarjan找桥和割点与点连通分量与边连通分量【未成形】
之前只学了个强连通Tarjan算法,然后又摸了缩点操作: 然后今天在lightoj摸了一道模板题,是求所有桥的题: 然后发现,要把:割点,割点集合,双连通,最小割边集合(桥),点连通分量,边连通分量都 ...
- CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3
http://codeforces.com/problemset/problem/160/D 这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中 明显桥边 ...
- Codeforces 160 D. Edges in MST
\(>Codeforces \space 160 D. Edges in MST<\) 题目大意 : 给出一张带权无向图,求对于这张图上的每一条边,其是必然存在于每一种最小生成树中,还是至 ...
- hdu 4738 Caocao's Bridges (tarjan求桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...
- [CF160D]Edges in MST
[CF160D]Edges in MST 题目大意: 一个\(n(n\le10^5)\)个点,\(m(m\le10^5)\)条边的连通图.对于图中的每条边,判断它与该图最小生成树的关系: 在该图所有的 ...
- Tarjan 求桥,割,强连通
最近遇到了这种模板题,记录一下 tarjan求桥,求割 #include <bits/stdc++.h> using namespace std; #define MOD 99824435 ...
- Tarjan求桥
传送门(poj3177) 这道题是Tarjan求桥的模板题.大意是要求在原图上加上数量最少的边,使得整张图成为一个边双联通分量. 具体的做法是,先在图中求出所有的桥,之后把边双联通分量缩成点,这样的话 ...
- [CF160D]Edges in MST (最小生成树+LCA+差分)
待填坑 Code //CF160D Edges in MST //Apr,4th,2018 //树上差分+LCA+MST #include<cstdio> #include<iost ...
随机推荐
- EasyUI动态修改easyui-textbox验证信息
<tr> <td>编码:</td> <td><input type="text" id="code" na ...
- C++ error LNK2001
1.没有函数的实现部分 2.函数的头文件和实现部分不一致 3.缺库LIB.DLL 4.库H文件函数和库体不一致
- Linux - 操作系统信号
linux操作系统提供的信号 kill -l # 查看linux提供的信号 trap # shell使用 trap 捕捉退出信号 # 发送信号一般有两种原因: # (被动式) 内核检测到一个系统事件. ...
- 停靠窗口QDockWidget
停靠窗口QDockWidget要和QMainWindow一起搭配使用的 样式: import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets ...
- luogu P2779 [AHOI2016初中组]黑白序列
传送门 注:本题解中下标从1开始 这题可以想出一个\(O(n^2)\)的dp,只要考虑每个偶数位置可以从前面的哪个位置加上一个"B...W..."转移过来 然而数据范围有5e5,, ...
- ACM-ICPC 2018 徐州赛区网络预赛 G题
题目链接: https://nanti.jisuanke.com/t/31459 具体思路: 先顺序输入,然后回溯,假设已经加入了n个点,那么在加入的同时,首先看一下原先x轴上已经有过的点,找到第一个 ...
- mysql 案例 ~ pt-io工具的使用
一 简介:如何使用pt-iopfile调查io具体信息二 目的:利用pt-iopfile分析mysql内部IO操作密集的文件,用以发现问题三 使用: pt-iopfile -p mysql_pid ...
- JS实现随机背景图片与图片大小变换的效果
经常在网上见一些网站访问一次背景图片改变一次,而且图片的大小不停变换,于是想着自己研究一下. 背景图片可以通过JS的随机数来改变图片的src来实现随机图片,图片的大小变换可以用JS的setInterv ...
- SpringBoot整合MyBatis(XML)
(1).添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...
- Bootstrap3.0学习第一轮(入门)
详情请查看 http://aehyok.com/Blog/Detail/7.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...