ACM: 图论题 poj 1236 强连通
2016-05-19 23:26
363 查看
Network of
Schools
Description
A 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.
Input
The 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.
Output
Your 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 Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
题意: n个学校要进行软件维护, 软件进行拷贝, 并且每个学校有自己的维护表(拷贝软件给其它学校),
至少需要复制多少次软件, 使毎个学校都能够得到该软件, 要在所有的学校的清单里面至少一共
增加几项才能 使得软件给任意一个学校,所有的学校都能收得到.
解题思路:
1. 题意很明确, 强连通图问题. tarjan求出强连通分量, 然后进行每一个连通分量进行缩点处理
最后求出缩点后入度为0的个数, 和 max(入度为0的个数, 出度为0的个数)即可.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 105
int n;
int g[MAX][MAX];
int dfn[MAX], low[MAX];
int stack[MAX], bcc[MAX], count, top;
bool vis[MAX];
int in[MAX], out[MAX];
inline int min(int a, int b)
{
return a < b ? a : b;
}
void readGraph()
{
memset(g, 0, sizeof(g));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(stack, 0, sizeof(stack));
memset(bcc, 0, sizeof(bcc));
memset(vis, false, sizeof(vis));
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
count = top = 0;
int v;
for(int i = 1; i <= n; ++i)
{
while(true)
{
scanf("%d",
&v);
if(v == 0)
break;
else g[i][v]
= 1;
}
}
}
void tarjan(int u, int deep)
{
dfn[u] = low[u] = deep;
vis[u] = true;
stack[top++] = u;
for(int v = 1; v <= n; ++v)
{
if( g[u][v] )
{
if( !dfn[v]
)
{
tarjan(v,
deep+1);
low[u]
= min(low[u], low[v]);
}
else if(
vis[v] )
low[u]
= min(low[u], dfn[v]);
}
}
if( dfn[u] == low[u] )
{
count++;
int v;
do
{
v =
stack[top-1];
top--;
vis[v] =
false;
bcc[v] =
count;
}while(v != u);
}
}
void solve()
{
int i, j;
int t_in = 0, t_out = 0;
for(i = 1; i <= n; ++i)
{
for(j = 1; j <=
n; ++j)
{
if(g[i][j]
&& bcc[i] != bcc[j])
{
in[
bcc[j] ]++;
out[
bcc[i] ]++;
}
}
}
for(i = 1; i <= count;
++i)
{
if( in[i] == 0 ) t_in++;
if( out[i] == 0 )
t_out++;
}
if(count == 1)
printf("1\n0\n");
else
printf("%d\n%d\n", t_in, t_in
> t_out ? t_in : t_out);
}
int main()
{
// freopen("input.txt", "r", stdin);
while(scanf("%d", &n) !=
EOF)
{
readGraph();
for(int i = 1; i
<= n; ++i)
if( !low[i]
)
tarjan(i,
1);
solve();
}
return 0;
}
Schools
Description
A 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.
Input
The 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.
Output
Your 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 Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
题意: n个学校要进行软件维护, 软件进行拷贝, 并且每个学校有自己的维护表(拷贝软件给其它学校),
至少需要复制多少次软件, 使毎个学校都能够得到该软件, 要在所有的学校的清单里面至少一共
增加几项才能 使得软件给任意一个学校,所有的学校都能收得到.
解题思路:
1. 题意很明确, 强连通图问题. tarjan求出强连通分量, 然后进行每一个连通分量进行缩点处理
最后求出缩点后入度为0的个数, 和 max(入度为0的个数, 出度为0的个数)即可.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 105
int n;
int g[MAX][MAX];
int dfn[MAX], low[MAX];
int stack[MAX], bcc[MAX], count, top;
bool vis[MAX];
int in[MAX], out[MAX];
inline int min(int a, int b)
{
return a < b ? a : b;
}
void readGraph()
{
memset(g, 0, sizeof(g));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(stack, 0, sizeof(stack));
memset(bcc, 0, sizeof(bcc));
memset(vis, false, sizeof(vis));
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
count = top = 0;
int v;
for(int i = 1; i <= n; ++i)
{
while(true)
{
scanf("%d",
&v);
if(v == 0)
break;
else g[i][v]
= 1;
}
}
}
void tarjan(int u, int deep)
{
dfn[u] = low[u] = deep;
vis[u] = true;
stack[top++] = u;
for(int v = 1; v <= n; ++v)
{
if( g[u][v] )
{
if( !dfn[v]
)
{
tarjan(v,
deep+1);
low[u]
= min(low[u], low[v]);
}
else if(
vis[v] )
low[u]
= min(low[u], dfn[v]);
}
}
if( dfn[u] == low[u] )
{
count++;
int v;
do
{
v =
stack[top-1];
top--;
vis[v] =
false;
bcc[v] =
count;
}while(v != u);
}
}
void solve()
{
int i, j;
int t_in = 0, t_out = 0;
for(i = 1; i <= n; ++i)
{
for(j = 1; j <=
n; ++j)
{
if(g[i][j]
&& bcc[i] != bcc[j])
{
in[
bcc[j] ]++;
out[
bcc[i] ]++;
}
}
}
for(i = 1; i <= count;
++i)
{
if( in[i] == 0 ) t_in++;
if( out[i] == 0 )
t_out++;
}
if(count == 1)
printf("1\n0\n");
else
printf("%d\n%d\n", t_in, t_in
> t_out ? t_in : t_out);
}
int main()
{
// freopen("input.txt", "r", stdin);
while(scanf("%d", &n) !=
EOF)
{
readGraph();
for(int i = 1; i
<= n; ++i)
if( !low[i]
)
tarjan(i,
1);
solve();
}
return 0;
}
相关文章推荐
- ACM: 图论题 poj 1149 网络流问题
- ACM: 图论题 poj 1201 差分约束
- ACM: 搜索题 poj 1020
- ACM: 图论题 poj 3308 最大流问题
- Android的自定义控件起步
- ACM: 图论题 poj 3177 同 3352
- dos批处理中%~dp0%的说明
- ACM: 图论题 poj 2186 强连通分量
- ACM: 博弈题 poj 1143 状态压缩
- 本周学习的代码(数构 深度 广度 优先遍历)
- [原创]关于设置linux中vim 显示行号
- ACM: 动态规划题 poj&nb…
- ACM: 图论题 poj 3…
- 兴趣学习: opencv 实现画图程序
- 兴趣学习: opencv -- 视频读写, 鼠…
- ACM: 动态规划题 poj 2923
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 poj 3093 0-1背包
- ACM: 动态规划题 poj&nb…
- ipvsadm-2 DR模式及自动检测后端服务器状态