首页 技术 正文
技术 2022年11月19日
0 收藏 998 点赞 2,960 浏览 1814 个字

题目大意:给定一个 N 个点,M 条边组成的无向图,求至少在图中加入几条边才能使得整个图没有割边。

题解:求出该无向图的所有边双联通分量,每个边双联通分量可以理解成无向图的一个极大环,对该无向图进行缩点,形成一棵树。至少加入的边数和树中入度为 1 的节点个数有关,找找规律即可求得结果。

代码如下

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define cls(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=5010;
const int maxm=1e4+10;
const double eps=1e-6;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll fpow(ll a,ll b,ll c){ll ret=1%c;for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;return ret;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
/*------------------------------------------------------------*/struct edge{int x,y;}e[maxm];
vector<P> G[maxn];int tot=1;
int dfs_clk,dfn[maxn],low[maxn],dcc,cor[maxn],indeg[maxn];
int n,m;
bool bridge[maxm<<1];void read_and_parse(){
n=read(),m=read();
for(int i=1,x,y;i<=m;i++){
x=read(),y=read();
e[i].x=x,e[i].y=y;
G[x].pb(mp(y,++tot)),G[y].pb(mp(x,++tot));
}
}void tarjan(int u,int fe){
dfn[u]=low[u]=++dfs_clk;
for(auto p:G[u]){
int v=p.fi,idx=p.se;
if(!dfn[v]){
tarjan(v,idx);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])bridge[idx]=bridge[idx^1]=1;
}else if(idx!=(fe^1)){
low[u]=min(dfn[v],low[u]);
}
}
}
void dfs(int u){
cor[u]=dcc;
for(auto p:G[u]){
int v=p.fi,idx=p.se;
if(cor[v]||bridge[idx])continue;
dfs(v);
}
}void solve(){
tarjan(1,0);
for(int i=1;i<=n;i++)if(!cor[i])++dcc,dfs(i);
if(dcc==1)return (void)puts("0");
for(int i=1;i<=m;i++){
int x=e[i].x,y=e[i].y;
if(cor[x]==cor[y])continue;
++indeg[cor[x]],++indeg[cor[y]];
}
int cnt=0;
for(int i=1;i<=dcc;i++)if(indeg[i]==1)++cnt;
if(cnt&1)++cnt;
printf("%d\n",cnt/2);
}
int main(){
read_and_parse();
solve();
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903