POJ 1236 Network of Schools
来源:互联网 发布:apache ab源码下载 编辑:程序博客网 时间:2024/06/10 09:47
Description
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.
Input
Output
Sample Input
52 4 3 04 5 0001 0
Sample Output
12
Source
//Tarjan算法
题意:有一些网络学校,他们各自有一份网络维护清单, 上面写着维护学校的编号
问题:1.至少需要copy多少份软件给那些学校,使得每个学校都可以得到软件
2.至少需要增加多少个学校的清单项目,才能使用一个软件 可以让每一个学校都得到
分析:应用Tarjan,将原图缩为无环有向图,对于问题1,其实就是求无环有向图的所有点中入度为0的点的个数,记为ans1
对于问题2,要求所有点中出度为0的点的个数,记为ans2,然后ans1和ans2大的就是答案
注意:当原图是一个强连通分支时,那么就ans1,此时原图将缩成一个点,那么可以看出答案就是1和0
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define MAXV 102
#define min(x, y) (x)<(y)? (x):(y)
int stack[MAXV], instack[MAXV], top;
int dfn[MAXV], low[MAXV], cnt;
int num[MAXV], scc;
vector <int> edge[MAXV];
vector <int> comp[MAXV];
int g[MAXV][MAXV];
void Tarjan(int i)
{
int j;
dfn[i] = low[i] = cnt++;
stack[++top] = i;
instack[i] = 1;
for(int k = 0; k < edge[i].size(); k++)
{
j = edge[i][k];
if(dfn[j] == -1)
{
Tarjan(j);
low[i] = min(low[i], low[j]);
}
else if(instack[j])
{
low[i] = min(low[i], dfn[j]);
}
}
if(dfn[i] == low[i])
{
scc++;
do
{
j = stack[top--];
instack[j] = 0;
comp[scc].push_back(j);
num[j] = scc;
}while(i != j);
}
}
void solve(int N)
{
int i, j;
memset(instack, 0, sizeof(instack));
memset(dfn, -1, sizeof(dfn));
top = scc = 0; cnt = 1;
for(i = 1; i <= N; i++)
{
if(dfn[i] == -1)
Tarjan(i);
}
}
int main()
{
int i, j, k, x, n;
while(scanf("%d", &n) != EOF)
{
for(i = 0; i <= n; i++)
{
edge[i].clear();
comp[i].clear();
}
for(i = 1; i <= n; i++)
{
scanf("%d", &x);
while(x)
{
edge[i].push_back(x);
scanf("%d", &x);
}
}
solve(n);
if(scc == 1)
{
printf("1\n");
printf("0\n");
continue;
}
memset(g, 0, sizeof(g));
// for(i = 1; i <= 5; i++)
// cout<<'*'<<num[i]<<endl;
for(i = 1; i <= n; i++)
{
for(j = 0; j < edge[i].size(); j++)
{
int y = edge[i][j];
if(num[i] != num[y])
{
g[num[i]][num[y]] = 1;
}
}
}
int ans1 = 0, ans2 = 0;
for(i = 1; i <= scc; i++)
{
int flag = 1;
for(j = 1; j <= scc; j++)
{
if(g[j][i]) {
flag = 0;
break;
}
}
if(flag) ans1++;
flag = 1;
for(j = 1; j <= scc; j++)
{
if(g[i][j])
{
flag = 0;
break;
}
}
if(flag) ans2++;
}
printf("%d\n", ans1);
printf("%d\n", ans1 > ans2? ans1:ans2);
}
return 0;
}
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- poj 1236 Network of Schools
- POJ 1236 Network of Schools
- POJ-1236-Network of Schools
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- poj 1236 Network of Schools
- POJ-1236-Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- Network of Schools (POJ 1236)
- POJ 1236:Network of Schools
- How to recover after rebooting the LFS procession
- 休みの日本語二課
- 剖析Shapefile
- MyEclipse8.6生成注册码
- #pragma pack()与__declspec(align())
- POJ 1236 Network of Schools
- winform datagridview增加合计行
- 创业团队 不要均分股份
- ACM培养的是能力
- vc++6源码在VC2010中编译提示iostream.h库找不到问题的解决办法
- C++虚函数表解析
- 光学显微镜油镜的使用
- HDOJ1031投票问题
- javascript比较两个时间大小