2 条题解
-
0
20241225代码:
#include<bits/stdc++.h> using namespace std; const int N=1e3+10; vector<pair<int,int>>G[N];int cut[N],root;vector<int> vdcc[N]; int tsp,cnt,dfn[N],low[N]; stack<int>stk;bool instk[N]; void tarjan(int x,int in_id) { dfn[x]=low[x]=++tsp; if(G[x].size()==0){vdcc[++cnt].push_back(x);return ;} stk.push(x);instk[x]=1; int child=0; for(auto i:G[x])if(i.second!=in_id) { int y=i.first,id=i.second; if(dfn[y]==0) { tarjan(y,id); low[x]=min(low[x],low[y]); if(dfn[x]<=low[y]) { child++; if(x!=root || child>1)cut[x]=1; cnt++; for(int z=-1;z!=y;) { z=stk.top();stk.pop();instk[z]=0; vdcc[cnt].push_back(z); }; vdcc[cnt].push_back(x); } } else if(instk[y]==1)low[x]=min(low[x],dfn[y]); } } int main() { int n,m,T=0; while(scanf("%d",&m)!=EOF && m) { n=0; memset(G,0,sizeof(G)); for(int i=1,x,y;i<=m;i++) { scanf("%d%d",&x,&y); G[x].push_back({y,i}); G[y].push_back({x,i}); n=max({n,x,y}); } tsp=cnt=0;memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(cut,0,sizeof(cut)); memset(instk,0,sizeof(instk));memset(vdcc,0,sizeof(vdcc)); for(int i=1;i<=n;i++)if(dfn[i]==0)root=i,tarjan(i,0); int sum=0; unsigned long long ans=1; for(int i=1;i<=cnt;i++) { int t=0,cn=vdcc[i].size(); for(int j:vdcc[i])if(cut[j])t++; if(t==0) { if(cn>1)sum+=2,ans=ans*cn*(cn-1)/2; else sum++; } else if(t==1)sum++,ans=ans*(cn-1); } printf("Case %d: %d %llu\n",++T,sum,ans); } return 0; }
- 1
信息
- ID
- 4395
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- 递交数
- 31
- 已通过
- 11
- 上传者
