[USACO06JAN]Redundant Paths

OJ题号:
洛谷2860、POJ3177

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

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

 1 #include<stack>
 2 #include<cstdio>
 3 #include<cctype>
 4 #include<vector>
 5 inline int getint() {
 6     char ch;
 7     while(!isdigit(ch=getchar()));
 8     int x=ch^‘0‘;
 9     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^‘0‘);
10     return x;
11 }
12 const int V=5001;
13 struct Edge {
14     int to,id;
15 };
16 std::vector<Edge> e[V];
17 inline void add_edge(const int u,const int v,const int id) {
18     e[u].push_back((Edge){v,id});
19 }
20 int dfn[V]={0},low[V]={0},scc[V]={0},cnt=0,id=0;
21 bool ins[V]={0};
22 std::stack<int> s;
23 void Tarjan(const int x,const int eid) {
24     dfn[x]=low[x]=++cnt;
25     s.push(x);
26     ins[x]=true;
27     for(unsigned i=0;i<e[x].size();i++) {
28         if(e[x][i].id==eid) continue;
29         int &y=e[x][i].to;
30         if(!dfn[y]) {
31             Tarjan(y,e[x][i].id);
32             low[x]=std::min(low[x],low[y]);
33         }
34         else if(ins[y]) {
35             low[x]=std::min(low[x],dfn[y]);
36         }
37     }
38     if(low[x]==dfn[x]) {
39         int y;
40         id++;
41         do {
42             y=s.top();
43             s.pop();
44             ins[y]=false;
45             scc[y]=id;
46         } while(y!=x);
47     }
48 }
49 int deg[V]={0};
50 int main() {
51     int n=getint();
52     for(int m=getint();m;m--) {
53         int u=getint(),v=getint();
54         add_edge(u,v,m);
55         add_edge(v,u,m);
56     }
57     for(int i=1;i<=n;i++) {
58         if(!dfn[i]) Tarjan(i,0);
59     }
60     for(int x=1;x<=n;x++) {
61         for(unsigned i=0;i<e[x].size();i++) {
62             int &y=e[x][i].to;
63             if(scc[x]!=scc[y]) deg[scc[x]]++,deg[scc[y]]++;
64         }
65     }
66     int cnt=0;
67     for(int i=1;i<=id;i++) {
68         if(deg[i]==2) cnt++;
69     }
70     printf("%d\n",(cnt+1)>>1);
71     return 0;
72 } 
时间: 2024-10-11 17:30:06

[USACO06JAN]Redundant Paths的相关文章

Luogu2860 [USACO06JAN]冗余路径Redundant Paths

\(\verb|Luogu2860 [USACO06JAN]冗余路径Redundant Paths|\) 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\leq m\leq10^4\) tarjan 边双无疑不用考虑,于是就可以边双缩点成一棵树 令现在要连的边为 \((u,\ v)\) ,点集 \(\{u->lca(u,\ v),\ v->lca(u,\ v)\}\) 将会变为一个新的点双,可以将他们看为一个新的点 可以贪心地连

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

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

poj 3177 Redundant Paths

Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc

POJ3177 Redundant Paths 双连通分量

Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using

poj3177 Redundant Paths 边双连通分量

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define eps 1e-

BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 5009; struct edge { int to; b

【连通图|边双连通+缩点】POJ-3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K       Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the T

[双连通分量] POJ 3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the