参考博客:有向图——最小点基和最小权点基
/*
最小点基和最小权点基:
最小点基=从每个入度为0的SCC中任意选出一个点的集合
最小权点基=从每个入度为0的SCC中选出最小权值的点的集合
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x7fffffff;
const int MAX = 1005;
int n,m;
int node[MAX];
struct Edge
{
int from;
int to;
int next;
};
Edge e[2005];
int head[MAX],edgeNum;
int dfn[MAX],low[MAX],seq;
int stack[MAX],top;
bool inStack[MAX];
int belong[MAX],cnt;
int ind[MAX];
int cost[MAX];
void addEdge(int from, int to)
{
e[edgeNum].from = from;
e[edgeNum].to = to;
e[edgeNum].next = head[from];
head[from] = edgeNum++;
}
int min(int a, int b)
{
return a<b?a:b;
}
void Tarjan(int u)
{
dfn[u] = low[u] = seq++;
stack[top++] = u;
inStack[u] = true;
for(int i = head[u]; i != -1; i = e[i].next)
{
int w = e[i].to;
if(dfn[w] < 0)
{
Tarjan(w);
low[u] = min(low[u],low[w]);
}
else if(inStack[w])
low[u] = min(low[u],dfn[w]);
}
if(dfn[u] == low[u])
{
int v;
cnt++;
do
{
top--;
v =stack[top];
inStack[v] = false;
belong[v] = cnt; //v点所属的强连通分量的编号
}while(u != v);
}
}
void solve()
{
int i;
for(i = 1; i <= n; i++)
if(dfn[i] < 0)
Tarjan(i); //对所有连通块打一遍tarjan
for(i = 0; i < edgeNum; i++) //对边表进行遍历
{
if(belong[e[i].from] != belong[e[i].to]) //发现了从一个分量指向另一个分量
ind[belong[e[i].to]]++; //被指向的分量入度++
}
for(i = 1; i <= n; i++) //对顶点遍历一遍
{
if(cost[belong[i]] > node[i]) //每个强连通分量的最小权
cost[belong[i]] = node[i];
}
int result1=0,result2=0;
for(i = 1; i <= cnt; i++) //对强连通分量进行遍历
{
if(ind[i]==0) //如果这个强连通分量的入度为0
{
result1++; //最小点基的数目
result2 += cost[i]; // 最小权点基的数目
}
}
printf("%d %d\n",result1,result2);
}
int main()
{
int i;
int from,to;
while(scanf("%d %d",&n,&m)!=EOF)
{
edgeNum = seq = cnt = top = 0;
memset(head,-1,sizeof(head));
memset(dfn,-1,sizeof(dfn));
memset(inStack,0,sizeof(inStack));
memset(ind,0,sizeof(ind));
for(i = 1; i <= n; i++)
cost[i] = INF;
for(i = 1; i <= n; i++)
scanf("%d",&node[i]);
for(i = 0; i < m; i++)
{
scanf("%d %d",&from,&to);
addEdge(from,to);
}
solve();
}
return 0;
}