【Redundant Paths】【无向图】【双连通分量】【缩点】
2015-09-02 09:17
513 查看
Redundant Paths
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit
Status
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 forced to take a particular path and want to build some new paths so that they will always have a choice of at least
two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.
Given a description of the current set of R(F−1≤R≤10,000) paths
that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate
if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Line 1:Two space-separated integers: F and R
Lines 2⋯R+1:
Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Line 1:A single integer that is the number of new paths that must be built.
Sample input and output
Sample Input | Sample Output |
---|---|
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7 | 2 |
Hint
Explanation of the sample:
One visualization of the paths is:
Building new paths from 1 to 6 and
from 4 to 7 satisfies
the conditions.
Check some of the routes:
1–2:1–>2 and 1–>6–>5–>2
1–4:1–>2–>3–>4 and 1–>6–>5–>4
3–7:3–>4–>7 and 3–>2–>5–>7 Every
pair of fields is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7).
Adding two paths, however, is the minimum
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define mp push_back const int MAXN = 50010; const int MAXM = 100010; int n,m; struct Edge { int to,next,id; }edge[MAXM]; int vis[MAXN]; int low[MAXN]; int myst[MAXN]; int inst[MAXN]; int myblock[MAXN]; int in_de[MAXN]; int out_de[MAXN]; int H[MAXN]; int top; int len; int times; int sccnum; void addedge(int from,int to,int id) { edge[len].id = id; edge[len].to = to; edge[len].next = H[from]; H[from] = len ++; } void tarjan(int u,int x) { myst[top++] = u; low[u] = vis[u] = ++times; inst[u] = true; for(int i=H[u];~i;i=edge[i].next) { int v = edge[i].to; if(x == edge[i].id) continue; if(vis[v] == -1) { tarjan(v,edge[i].id); low[u] = min(low[u],low[v]); } else { low[u] = min(low[u],vis[v]); } } int t; if(low[u] == vis[u]) { sccnum ++; do { t = myst[--top]; inst[t] = false; myblock[t] = sccnum; } while(t != u); } } void solve(int n) { for (int i = 1; i <= n; ++i) { if(vis[i] == -1) tarjan(i,-1); } for (int u = 1; u <= n; ++u) { for (int j = H[u]; ~j; j = edge[j].next) { int v = edge[j].to; if(myblock[u] != myblock[v]) { out_de[myblock[v]] ++; out_de[myblock[u]] ++; } } } int cnt = 0; for(int i=1;i<=sccnum;i++) { if(out_de[i]/2 == 1) { ++cnt; } } printf("%d\n", (cnt + 1) >> 1); } int main() { freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m) != EOF) { len = 0; times = 0; top = 0; sccnum = 0; memset(vis,-1,sizeof(vis)); memset(H,-1,sizeof(H)); memset(inst,0,sizeof(inst)); memset(myst,0,sizeof(myst)); memset(myblock,0,sizeof(myblock)); memset(in_de,0,sizeof(in_de)); memset(out_de,0,sizeof(out_de)); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v,i); addedge(v,u,i); } solve(n); } }
相关文章推荐
- ASCII码表
- 人脸性别识别文献阅读笔记(3)
- Android 自定义控件玩转字体变色 打造炫酷ViewPager指示器
- app在小米4上的图标是机器人
- php mysql函数库总结(一)
- 苹果开发 笔记(69)SVProgressHUD
- 精华] 跟我一起写 Makefile
- GDI+学习笔记3-坐标系和坐标变换
- 单例模式
- 我眼中的编程语言 Rust 和 GO
- Code Craft(编程匠艺)之代码表面
- CALLBACK--_stdcall(PASCAL调用方式),VS默认是C语言调用方式即__cdecl
- POST方式跨域上传文件
- Android百度地图定位
- 盘古分词
- caffe配置cuDNN
- 11971 - Polygon(连续概率)
- 简单的内存池实现
- seajs学习一天后的总结归纳
- 自定义刻度jQuery进度条及插件