题意:在一个无向图中选择尽量少的点涂黑,使得任意删除一个点后,每个连通分量至少有一个黑点,问你最少选几个点和其方案数。
分析:求出所有点双连通分量后,构造block forest data structure,很容易发现我们只需染”叶子虚点“下的点就可以了,注意当全图为一个连通分量时答案为2,方案为n*(n-1)/2。
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 100010
using namespace std;
int n,m,num,dfs_clock,bcc_cnt,son[MAXN],pre[MAXN],low[MAXN],bccno[MAXN],rd[MAXN];
bool iscut[MAXN];
long long ans1,ans2;
vector <int> a[MAXN],b[MAXN],bcc[MAXN];
struct Edge
{
int u,v;
Edge(int x,int y)
{
u = x;
v = y;
}
};
stack <Edge> S;
int dfs(int u,int fa)
{
low[u] = pre[u] = ++dfs_clock;
int child = 0;
for(int v : a[u])
{
Edge e = Edge(u,v);
if(!pre[v])
{
S.push(e);
child++;
low[u] = min(low[u],dfs(v,u));
if(low[v] >= pre[u])
{
iscut[u] = true;
bcc[++bcc_cnt].clear();
for(;;)
{
Edge x = S.top();
S.pop();
if(bccno[x.u] != bcc_cnt)
{
bcc[bcc_cnt].push_back(x.u);
bccno[x.u] = bcc_cnt;
}
if(bccno[x.v] != bcc_cnt)
{
bcc[bcc_cnt].push_back(x.v);
bccno[x.v] = bcc_cnt;
}
if(x.u == u && x.v == v) break;
}
}
}
else
if(pre[v] < pre[u] && v != fa)
{
S.push(e);
low[u] = min(low[u],pre[v]);
}
}
if(fa < 0 && child == 1) iscut[u] = 0;
return low[u];
}
int main()
{
while(~scanf("%d",&m) && m)
{
dfs_clock = bcc_cnt = n = 0;
ans1 = 0ll,ans2 = 1ll;
memset(pre,0,sizeof(pre));
memset(son,0,sizeof(son));
memset(iscut,0,sizeof(iscut));
memset(bccno,0,sizeof(bccno));
memset(rd,0,sizeof(rd));
for(int i = 1;i <= 2*m;i++)
{
a[i].clear();
b[i].clear();
}
for(int i = 1;i <= m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
n = max(n,max(x,y));
}
dfs(1,-1);
bool flag = false;
for(int i = 1;i <= n;i++)
if(iscut[i])
{
flag = true;
break;
}
if(!flag)
{
cout<<"Case "<<++num<<": ";
cout<<2<<" "<<1ll*n*(n-1)/2<<endl;
continue;
}
for(int i = 1;i <= bcc_cnt;i++)
for(int u : bcc[i])
{
son[i+n]++;
if(iscut[u]) rd[i+n]++;
}
for(int i = n+1;i <= 2*n;i++)
if(rd[i] == 1)
{
ans1++;
ans2*=1ll*(son[i]-1ll);
}
cout<<"Case "<<++num<<": ";
cout<<ans1<<" "<<ans2<<endl;
}
}