Redundant Paths(POJ-3177)
Problem 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 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
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7Sample Output
2
題意:給出一個 n 個點 m 條邊的無向圖,現在要向圖中加邊,求最少加幾條邊,能使圖變為邊雙連通圖
思路:與 Road Construction(POJ-3352)相似,不同的是,本題有重邊,要先進行去重
Source Program
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 20001
#define MOD 16007
#define E 1e-6
#define LL long long
using namespace std;
vector<int> G[N];
int n,m;
int dfn[N],low[N];
int degree[N];
int block_cnt;
bool repeat[N][N];//去重用陣列
int Tarjan(int x,int father){
int lowx=dfn[x]=++block_cnt;
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(y==father)
continue;
if(dfn[y]==0){
int lowy=Tarjan(y,x);
lowx=min(lowx,lowy);
}
else if(dfn[y]<dfn[x]){
lowx=min(lowx,dfn[y]);
}
}
return low[x]=lowx;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
block_cnt=0;
memset(dfn,0,sizeof(dfn));
memset(degree,0,sizeof(degree));
for(int i=0;i<n;i++)
G[i].clear();
//去重儲存
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
repeat[x][y]=true;
repeat[y][x]=true;
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(repeat[i][j]){
G[i].push_back(j);
G[j].push_back(i);
}
}
}
Tarjan(1,-1);//求所有點的low值
for(int x=1;x<=n;x++){//遍歷每條邊
for(int i=0;i<G[x].size();i++){
int y=G[x][i];
if(low[x]!=low[y])//每個不同的low值代表一個邊雙連通分量
degree[low[y]]++;
}
}
int cnt=0;
for(int i=1;i<=n;i++)
if(degree[i]==1)
cnt++;
printf("%d\n",(cnt+1)/2);//加邊條數
}
return 0;
}
相關文章
- 684-Redundant Connection
- Avoided redundant navigation to current location: "/users"IDENavigation
- Paths和Files
- Vue, Avoided redundant navigation to current location: "/login".VueIDENavigation
- Leetcode 685. Redundant Connection II JavascriptLeetCodeJavaScript
- Leetcode Binary Tree PathsLeetCode
- find: paths must precede expression:Express
- 257. Binary Tree Paths
- swagger ui remove springboot pathsSwaggerUIREMSpring Boot
- 257-Binary Tree Paths
- Java NIO 的 Files Path 和 PathsJava
- LeetCode Unique Paths(062)解法總結LeetCode
- java 檔案的操作(Path、Paths、Files)Java
- LeetCode之All Paths From Source to Target(Kotlin)LeetCodeKotlin
- Though Our Paths May Diverge(JSOI 2024 遊記)JS
- [ABC211D] Number of Shortest paths 題解
- Setup had an error Error: At least one of these paths should existErrorAST
- 【論文筆記】Shortest Paths and Centrality in Uncertain Networks筆記AI
- Angular tsconfig.json 檔案裡的 paths 用途AngularJSON
- Mac下的paths.d目錄神奇用法Mac
- 題解:AT_abc378_d [ABC378D] Count Simple Paths
- [論文閱讀筆記] Are Meta-Paths Necessary, Revisiting Heterogeneous Graph Embeddings筆記
- 在node中使用ts的compilerOptions.paths的簡單姿勢Compile
- Angular tsconfig.json 檔案裡的 paths 用法和 scoped module 定義AngularJSON
- 所有結點對的最短路徑問題(All-Paris Shortest Paths)
- 關於 SAP UI5 應用 ui5.yaml 裡的 paths 對映問題UIYAML
- iOS資料結構與演算法實戰 二叉樹的演算法實戰 Binary Tree PathsiOS資料結構演算法二叉樹
- CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 樹上啟發式合併(DSU ON TREE)
- Zero-shot Learning零樣本學習 論文閱讀(四)——Zero-Shot Recognition using Dual Visual-Semantic Mapping PathsAPP