您的位置:首页 > 其它

poj 1236 Network of Schools (最小点基)

2018-02-21 15:17 375 查看
Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20658 Accepted: 8149
DescriptionA number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
InputThe first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.OutputYour program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.Sample Input5
2 4 3 0
4 5 0
0
0
1 0
Sample Output1
2
Source

题意:有n座学校,学校与学校之间可以传输文件,但传输是单向的,问你现在要有一份文件传给所有的学校,问你最少需要多少给几个学校备份,就可以将文件传给全部的学校并且为了优化传输,若想使备份给任意一个学校(即只用把备份给一个学校),全部学校都能接收到文件,求至少需要增加几条传输线路
解析:经典的最小点基问题和有向无环图变成强连通图问题求图G的强连通分量,入度为0的强连通分量个数即为最小点基,从每个入度为0的强连通分量中取出权值最小的点,构成的集合即最小权点基。
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int MAXN = 1e4+10;

typedef struct node
{
int u;
int v;
int next;
}node;

node edge[MAXN];
int head[MAXN],cnt;
int DFN[MAXN],LOW[MAXN],vis[MAXN];
int n,index;
int comnum;
int com[MAXN];
stack<int> ms;

void addedge(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}

void tarjan(int x)
{
DFN[x]=LOW[x]=++index;
ms.push(x);
vis[x]=1;
for(int i=head[x];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!DFN[v])
{
tarjan(v);
LOW[x]=min(LOW[x],LOW[v]);
}
else if(vis[v])
{
LOW[x]=min(LOW[x],DFN[v]);
}
}

if(DFN[x]==LOW[x])
{
int u;
comnum++;
do
{
u=ms.top();
ms.pop();
com[u]=comnum;
vis[u]=0;
}while(u!=x);
}
}

void Tarjansolve()
{
memset(DFN,0,sizeof(DFN));
memset(LOW,0,sizeof(LOW));
memset(vis,0,sizeof(vis));
index=comnum=0;
for(int i=1;i<=n;i++)
{
if(!DFN[i]) tarjan(i);
}
}

int indeg[MAXN];
int outdeg[MAXN];
int main()
{
scanf("%d",&n);
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
int tmp;
while(scanf("%d",&tmp),tmp)
{
addedge(i,tmp);
}
}

Tarjansolve();

memset(indeg,0,sizeof(indeg));
memset(outdeg,0,sizeof(outdeg));
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=edge[j].next)
{
int tt=com[edge[j].v];
if(tt!=com[i])
{
indeg[tt]++;
outdeg[com[i]]++;
}
}
}
int in,out;
in=out=0;
for(int i=1;i<=comnum;i++)
{
if(!indeg[i]) in++;
if(!outdeg[i]) out++;
}

printf("%d\n",in);
if(comnum==1) printf("0\n");
else printf("%d\n",max(in,out));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 tarjan acm acm模板 poj