给一个无向图,选择一些点,使得任意去掉一个点后,任一点都可以连接到至少一个选择的点
缩点后,每个块:
1. 割点数量为0, 要建立2个站点, 方案+= len(len-1)
2. 数量为1,建立1个站点 方案+=len-1
#include <bits/stdc++.h> using namespace std ; const int N=502; vector<int> g[N],bk[N]; stack<int> st; int n,m,low[N],dfn[N],cut[N],pool,tot; void add(int x,int y){ g[x].push_back(y); } void init(){ int i,x,y; for(i=0;i<N;i++) g[i].clear(),cut[i]=dfn[i]=low[i]=0; pool=tot= 0; while(!st.empty()) st.pop(); for(i=1;i<=m;i++){ cin>>x>>y; n=max(n,max(x,y)); add(x,y),add(y,x); } } void tar(int x,int fa){ dfn[x]=low[x]=++pool; st.push(x); int i,y,t,c=0; for(i=0;i<g[x].size();i++){ y=g[x][i]; if(!dfn[y]){ c++; tar(y,x),low[x]=min(low[x],low[y]); if(!fa&&c>1||fa&&dfn[x]<=low[y]) cut[x]=1; if(dfn[x]<=low[y]){ tot++; bk[tot].clear(); do{ t=st.top(),st.pop(); bk[tot].push_back(t); }while(t!=y); bk[tot].push_back(x); } } else low[x]=min(low[x],dfn[y]); } } typedef long long ll; void sov(){ int i,j,x,len; ll c1=0,c2=1,t; for(i=1;i<=tot;i++){ t=0,len=bk[i].size(); for(j=0;j<len;j++){ x=bk[i][j]; if(cut[x]) t++; } if(t==0){ c1+=2; c2=c2*len*(len-1)/2; } if(t==1) c1++,c2=c2*(len-1); } cout<<c1<<' '<<c2<<'\n'; } int main(){ int i,j,x,y,cas=0; while(cin>>m,m){ init(); cout<<"Case "<<++cas<<": "; for(i=1;i<=n;i++) if(!dfn[i]) tar(i,0); sov(); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!