【POJ 3177】Redundant Paths
http://poj.org/problem?id=3177
又写了一遍手动栈。。。
把边双都缩点,缩成一棵树,答案就是树中度数为1的点的个数除2上取整。
为什么呢?因为1个度数为1的点的树需要多连0条边,2个度数为1的点的树需要多连1条边,3个度数为1的点的树需要多连2条边。
然后对于n(n>3)个度数为1的点的树,连一次边后再缩点能变成有n-2个度数为1的点的树。
无向图边双跟有向图强连通分量的求法很像喔,重点在于特判后继节点是否是自己的father连过来的边。
#include<cstdio>
#include<bitset>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 5003;
const int M = 10003;
bitset <N> inst;
struct node {int nxt, to;} E[M << 1];
int cnt = 1, tot = 0, bel[N], point[N], cur[N], st[N], sta[N], top, statop, fa[N], low[N], dfn[N], n, m, fae[N];
void ins(int u, int v) {E[++cnt] = (node) {point[u], v}; point[u] = cnt;}
void tarjan(int x) {
st[top = 1] = x;
while (top) {
int u = st[top];
if (!dfn[u]) {
inst[sta[++statop] = u] = 1;
dfn[u] = low[u] = ++cnt;
}
if (cur[u]) {
if (cur[u] == fae[u]) {cur[u] = E[cur[u]].nxt; continue;}
int v = E[cur[u]].to;
if (inst[v]) low[u] = min(low[u], dfn[v]);
else if (!dfn[v]) fa[st[++top] = v] = u, fae[v] = cur[u] ^ 1;
cur[u] = E[cur[u]].nxt;
} else {
low[fa[u]] = min(low[fa[u]], low[u]);
if (dfn[u] == low[u]) {
++tot;
while (sta[statop] != u) {
bel[sta[statop]] = tot;
inst[sta[statop]] = 0;
--statop;
}
bel[u] = tot;
inst[u] = 0;
--statop;
}
--top;
}
}
}
int du[N];
int main() {
scanf("%d%d", &n, &m);
int u, v;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
ins(u, v); ins(v, u);
}
for (int i = 1; i <= n; ++i) cur[i] = point[i];
cnt = 0;
for (int i = 1; i <= n; ++i)
if (!dfn[i])
tarjan(i);
for (int i = 1; i <= n; ++i)
for (int j = point[i]; j; j = E[j].nxt)
if (bel[i] != bel[E[j].to] && E[j].to > i)
++du[bel[i]], ++du[bel[E[j].to]];
int ret = 0;
for (int i = 1; i <= tot; ++i)
if (du[i] == 1) ++ret;
printf("%d\n", (ret + 1) >> 1);
return 0;
}
【POJ 3177】Redundant Paths的更多相关文章
- 【POJ 3177】Redundant Paths(边双连通分量)
求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...
- (poj 3177) Redundant Paths
题目链接 :http://poj.org/problem?id=3177 Description In order to <= F <= ,) grazing fields (which ...
- POJ 3177 (Redundant Paths) —— (有重边,边双联通,无向图缩点)
做到这里以后,总算是觉得tarjan算法已经有点入门了. 这题的题意是,给出若干个点和若干条边连接他们,在这个无向图中,问至少增加多少条边可以使得这个图变成边双联通图(即任意两点间都有至少两条没有重复 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
随机推荐
- SQLserver 字符串分割函数
CREATE function Get_StrArrayStrOfIndex ( @str varchar(), --要分割的字符串 @split varchar(), --分隔符号 @index i ...
- 二叉查找树、平衡二叉树、红黑树、B-/B+树性能对比
转载:https://blog.csdn.net/z702143700/article/details/49079107 前言:BST.AVL.RBT.B-tree都是动态结构,查找时间基本都在O(l ...
- WAMP Apache 2.5 配置虚拟主机
1.在 Apache 的安装目录下 conf/httpd.conf 文件中搜索 hosts,去掉 Include 前面的 “#” 号后,即可启用虚拟主机. # Virtual hosts #Inclu ...
- python基础===两个list之间移动元素
首先我们先了解一下list的几个常用函数: a = [123,456,"tony","jack"] #list中增加元素a.append("www&q ...
- Linux内核基础--事件通知链(notifier chain)good【转】
转自:http://www.cnblogs.com/pengdonglin137/p/4075148.html 阅读目录(Content) 1.1. 概述 1.2.数据结构 1.3. 运行机理 1. ...
- linux和ubuntu防火墙相关命令
1.永久有效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2.即刻生效 开启: service iptables start 关闭: se ...
- Canvas开发库封装
一.Canvas第三方类库 1.常见的第三方类库 konva.js <style> body{ margin:0; } </style> </head> <b ...
- clearcase command (linux 常用命令)
http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m0/index.jsp?topic=/com.ibm.rational.clearcase.h ...
- jQuery为多个元素绑定同一个事件
$('.toals,input[type=datetime]').on('focus',function(){ $('.footer-focus-none').css('display','none' ...
- c语言中数组,指针数组,数组指针,二维数组指针
1.数组和指针 ] = {,,,,};// 定义数组 // 1. 指针和数组的关系 int * pa = array; pa = array; // p[0] == *(p+0) == array[0 ...