OJ题号:
洛谷2860、POJ3177

题目大意:
给定一个无向图,试添加最少的边使得原图中没有桥。

思路:
Tarjan缩点,然后统计度为$1$的连通分量的个数(找出原图中所有的桥)。
考虑给它们每两个连通分量连一条边,这样一次性可以解决两个。
如果最后还有多的,就专门给它随便连一条边。
设度为$1$的连通分量的个数为$c$,则答案为$\lfloor{\frac{c+1}{2}}\rfloor$。
因为是无向图,所以用一般的Tarjan会来回走同一条边,这样就会把桥的两岸缩在同一个点中,不合题意。
考虑Tarjan中记录当前结点的父亲结点,往下递归时判断是否与其相等。这样看起来是正确的,但是交到洛谷上会WA一个点。
因为原图中不一定保证相同的两个点之间只有一条边,因此如果当某两点间同时存在两条边时,不能算桥。但是按照上面的算法不会将这两点缩在一起。
考虑记录每条边的编号,每次递归判断枚举到的出边是否与入边编号相等即可。

 #include<stack>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int V=;
struct Edge {
int to,id;
};
std::vector<Edge> e[V];
inline void add_edge(const int u,const int v,const int id) {
e[u].push_back((Edge){v,id});
}
int dfn[V]={},low[V]={},scc[V]={},cnt=,id=;
bool ins[V]={};
std::stack<int> s;
void Tarjan(const int x,const int eid) {
dfn[x]=low[x]=++cnt;
s.push(x);
ins[x]=true;
for(unsigned i=;i<e[x].size();i++) {
if(e[x][i].id==eid) continue;
int &y=e[x][i].to;
if(!dfn[y]) {
Tarjan(y,e[x][i].id);
low[x]=std::min(low[x],low[y]);
}
else if(ins[y]) {
low[x]=std::min(low[x],dfn[y]);
}
}
if(low[x]==dfn[x]) {
int y;
id++;
do {
y=s.top();
s.pop();
ins[y]=false;
scc[y]=id;
} while(y!=x);
}
}
int deg[V]={};
int main() {
int n=getint();
for(int m=getint();m;m--) {
int u=getint(),v=getint();
add_edge(u,v,m);
add_edge(v,u,m);
}
for(int i=;i<=n;i++) {
if(!dfn[i]) Tarjan(i,);
}
for(int x=;x<=n;x++) {
for(unsigned i=;i<e[x].size();i++) {
int &y=e[x][i].to;
if(scc[x]!=scc[y]) deg[scc[x]]++,deg[scc[y]]++;
}
}
int cnt=;
for(int i=;i<=id;i++) {
if(deg[i]==) cnt++;
}
printf("%d\n",(cnt+)>>);
return ;
}

[USACO06JAN]Redundant Paths的更多相关文章

  1. 洛谷P2860 [USACO06JAN]Redundant Paths G (tarjan,边双缩点)

    本题的大意就是加最少的边使得图成为边双. 多举例子,画图分析可得:最终答案就是叶子节点(度数为1的点)的个数加1在除以2. 那么我们的目的就转化为找叶子节点: 首先通过tarjan找到割边,再dfs将 ...

  2. Luogu2860 [USACO06JAN]冗余路径Redundant Paths

    Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...

  3. 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告

    P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...

  4. 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths

    P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...

  5. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  6. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

  7. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  8. [POJ3177]Redundant Paths(双联通)

    在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  9. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

随机推荐

  1. Spark记录-实例和运行在Yarn

    #运行实例 #./bin/run-example SparkPi 10   #./bin/spark-shell --master local[2] #./bin/pyspark --master l ...

  2. HDU 5528 反演

    $f(m)=\sum\limits_{i=1}^{m-1}\sum\limits_{j=1}^{m-1}[(ij,m) \ne m]$,$g(n)=\sum\limits_{m|n}f(m)$,$1 ...

  3. Java SSM框架之MyBatis3(七)MyBatis之参数取值

    在mybatis中,参数取值方式有两种:#{ } 和 ${ } 一.#{ } select * from student where name=#{name} 编译后执行的sql语句: select ...

  4. html5 canvas结构基础

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. JavaScript执行优先顺序

    js在html中的加载执行顺序 1.加载顺序:引入标记<script />的出现顺序, 页面上的Javascript代码是HTML文档的一部分,所以Javascript在页面装载时执行的顺 ...

  6. [整理]解析Json需要设置Mime

    IIS6.0 1.打开IIS添加Mime项 关联扩展名:*.json内容类型(MIME):application/x-javascript 2.添加映射: 位置在IIS对应站点右键属性:”主目录”-” ...

  7. [python]python三元表达式另类实现方式

    () variable = a if exper else b ()variable = (exper and [b] or [c])[] () variable = exper and b or c

  8. C++的Enum hack

    从一个例子开始吧 class Game { private: static const int GameTurn = 10; int scores[GameTurn]; }; 对于支持类内初始化的C+ ...

  9. Anaconda+django写出第一个web app(七)

    今天来实现如何在页面弹出一些信息,比如注册成功后弹出注册成功的信息.这一点可以通过materialize里的Toasts来实现. django自带的messages可以告诉我们是否注册成功,以及注册失 ...

  10. bash脚本:集群资源争夺战crazy-killer

    背景 公司的集群很多人一起用,有时候就难免资源紧张,某次需要用的时候没资源等了半天还是没资源,再等半天还是没资源,于是就写了个脚本泄愤,建议看到的人拷走放在自己公司集群上长期运行 :) 实现 此脚本运 ...