http://codeforces.com/problemset/problem/160/D

这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中

明显桥边一定存在于所有最小生成树中,然而怎么处理存在某个最小生成树的边呢?

借助kruskal算法的性质,由小到大,每次处理同一权值的边,如果边连接的点已经联通就不要管,否则那些处理的边一定存在于某最小生成树上

批量处理的思想很巧妙

#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn=2e5+5;
int first[maxn];
struct edge{
int t,ind,nxt;
}e[maxn];
int from[maxn],to[maxn],cost[maxn],index[maxn];
int sta[maxn];
int n,m,len; void addedge(int f,int t,int ind,int i){
e[i].nxt=first[f];
first[f]=i;
e[i].t=t;
e[i].ind=ind;
}
int par[maxn],num[maxn];
int fnd(int x){return par[x]==x?x:par[x]=fnd(par[x]);}
void unit(int a,int b){
if(fnd(a)==fnd(b))return;
num[fnd(b)]+=num[fnd(a)];
num[fnd(a)]=0;
par[fnd(a)]=fnd(b);
}
bool cmp(int a,int b) {return cost[a]<cost[b];} int dfn[maxn],low[maxn],cnt;
void dfs(int s,int f){
dfn[s]=low[s]=++cnt;
for(int p=first[s];p!=0;p=e[p].nxt){
int t=e[p].t;
if(p==(((f-1)^1)+1))continue;
if(dfn[t]==0){
dfs(t,p);
if(low[t]>dfn[s]){
sta[e[p].ind]=2;
}
else{
low[s]=min(low[s],low[t]);
}
}
else {
low[s]=min(low[s],dfn[t]);
}
}
} int st[maxn],tail;
void kruskal2(){
sort(index,index+m,cmp);
for(int i=0;i<m&&num[fnd(1)]<n;){
int j=i;
while(cost[index[i]]==cost[index[j]]&&j<m){j++;}
for(int k=i;k<j;k++){
int indk=index[k];
if(fnd(from[indk])!=fnd(to[indk])){
st[tail++]=indk;
sta[indk]=1;
}
}
for(int p=0;p<tail;p++){
int tp=st[p];
int f=fnd(from[tp]),t=fnd(to[tp]);
addedge(f,t,tp,++len);
addedge(t,f,tp,++len);
}
for(int p=0;p<tail;p++){
int tp=st[p];
int f=fnd(from[tp]),t=fnd(to[tp]);
dfs(f,0);
unit(f,t);
}
cnt=0;
len=0;
for(int p=0;p<tail;p++){
int tp=st[p];
int f=fnd(from[tp]),t=fnd(to[tp]);
first[f]=first[t]=0;
dfn[f]=dfn[t]=0;
} i=j;
tail=0;
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){par[i]=i;num[i]=1;} for(int i=0;i<m;i++){
scanf("%d%d%d",from+i,to+i,cost+i);
index[i]=i;
}
kruskal2(); for(int i=0;i<m;i++){
if(sta[i]==2)puts("any");
else if(sta[i]==1)puts("at least one");
else puts("none");
}
return 0;
}

  

CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3的更多相关文章

  1. Codeforces 160D Edges in MST tarjan找桥

    Edges in MST 在用克鲁斯卡尔求MST的时候, 每个权值的边分为一类, 然后将每类的图建出来, 那些桥就是必须有的, 不是桥就不是必须有. #include<bits/stdc++.h ...

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

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

  3. Codeforces 160 D. Edges in MST

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

  4. [CF160D]Edges in MST

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

  5. MST最小生成树

    首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...

  6. [BZOJ1937][SHOI2004]Mst最小生成树(KM算法,最大费用流)

    1937: [Shoi2004]Mst 最小生成树 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 802  Solved: 344[Submit][Sta ...

  7. 【BZOJ1937】[Shoi2004]Mst 最小生成树 KM算法(线性规划)

    [BZOJ1937][Shoi2004]Mst 最小生成树 Description Input 第一行为N.M,其中 表示顶点的数目, 表示边的数目.顶点的编号为1.2.3.…….N-1.N.接下来的 ...

  8. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  9. 【KM】BZOJ1937 [Shoi2004]Mst 最小生成树

    这道题拖了好久因为懒,结果1A了,惊讶∑( 口 || [题目大意] 给定一张n个顶点m条边的有权无向图.现要修改各边边权,使得给出n-1条边是这张图的最小生成树,代价为变化量的绝对值.求最小代价之和. ...

随机推荐

  1. 个人理解---KMP与Next数组详解

    Kmp就是求子串在母串中的位置等相关问题:当然KMP最重要的是Next数组,也称失败数组,Next[i]代表的意思是子串 sub 从sub[0] 到 sub[i-1]的前缀和后缀的最大匹配.模拟KMP ...

  2. linux 下创建虚拟环境 python

    virtualenv是一个可以在同一计算机中隔离多个python版本的工具.有时,两个不同的项目可能需要不同版本的python,如 python2.7 / python3.6 ,但是如果都装到一起,经 ...

  3. Day01 html详解

      day01 html详解   1.html的简介     1.1 什么是html?         - HyperText Markup Language:超文本标记语言,网页语言         ...

  4. 【抓包】火狐浏览器F12

    页面请求服务器的get.post两种请求,还有其他种,但是其他中基本不用,所以只记住get和post两种请求方法即可. 1.get(当前页面向服务器传值--即请求服务器)---弊端--传值长度有限 F ...

  5. PAT 1083 List Grades[简单]

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  6. Web安全大揭秘

    web安全大揭秘,通常会有那些web安全问题呢? 1,xss 2,sql注入 3,ddos攻击

  7. Swagger生成的接口需要权限验证的处理方法

    通常开发API的时候需要对接口进行权限验证,而我们在使用Swagger生成接口文档界面的时候,直接调用需要权限验证的接口会提示"当前用户没有登陆" 为了解决此问题,我们需要更改一下 ...

  8. redhat 5的中文包安装

    中文包文件名.在iso文件的/server/文件夹下fonts-chinese-3.02-9.6.el5.noarch.rpmfonts-ISO8859-2-75dpi-1.0-17.1.noarch ...

  9. 生成对抗网络(Generative Adversarial Network)阅读笔记

    笔记持续更新中,请大家耐心等待 首先需要大概了解什么是生成对抗网络,参考维基百科给出的定义(https://zh.wikipedia.org/wiki/生成对抗网络): 生成对抗网络(英语:Gener ...

  10. Python numpy 安装以及处理报错 is not a supported wheel on this platform

    1.    安装 1)去这里搜索https://pypi.org/ 2)搜索框输入numpy 3)一般第一个就是搜索到的 4)点进去 5) Download files 点进去,找自己的版本 6)nu ...