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. keepalive+nginx 热备跟负载均衡

    结构图 keepalived配置 master跟backup除了state跟优先级,其它一样,优先级master需大于backup ! Configuration File for keepalive ...

  2. Objects源码解析

    Objects类解析 ​ JDK7新增Objects类介绍(以下程序以1.8来说明) 简介: ​ JDK7里面新增的Objects类,本人学习HashMap源码偶遇此类,所以研究一下,本类将对象常用的 ...

  3. u-boot移植(二)---修改前工作:代码流程分析1

    一.代码执行总体流程图 1.1 代码路径 U-boot.lds (arch\arm\cpu) vectors.S (arch\arm\lib) start.S (arch\arm\cpu\arm920 ...

  4. (转)MFC界面风格

    以前在XP写的程序,现在系统换成了WIN7,现在对话框在编辑和预览的时候显示都如图一所示,可实际编译生成之后的显示却如图二所示,是什么问题?如何设置两者的显示风格使其保持一致? ----------- ...

  5. android 不同Activity之间数据传递

    1. 传值Activity package mydemo.mycom.demo2; import android.content.Intent; import android.support.v7.a ...

  6. js 执行上下文理解

    前端基础进阶(三):变量对象详解http://www.jianshu.com/p/330b1505e41d 1.创建阶段 a.生成变量对象    1.创建arguments对象   2.functio ...

  7. Java中在特定区间产生随机数

    生成指定范围内的随机数 这个是最常用的技术之一.程序员希望通过随机数的方式来处理众多的业务逻辑,测试过程中也希望通过随机数的方式生成包含大量数字的测试用例.问题往往类似于: 如何随机生成 1~100 ...

  8. SpringBoot整合SpringDataElasticSearch操作ES

    (1).添加starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  9. aiohttp分流处理

    # -*- coding: utf-8 -*- # @Time : 2018/12/26 9:55 PM # @Author : cxa # @Software: PyCharm import asy ...

  10. motor的使用

    # -*- coding: utf-8 -*- # @Time : 2018/11/18 10:41 PM # @Author : cxa # @File : motordb.py # @Softwa ...