[ACM] poj 1236 Network of Schools (有向强连通分量)
2014-09-25 17:30
387 查看
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
Sample Output
Source
IOI 1996
题意以及解题思路来自北大暑期培训课件:
![](https://img-blog.csdn.net/20140925171248787?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20140925171256883?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20140925171305962?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20140925171316305?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20140925171325759?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
本题例子解释:
![](https://img-blog.csdn.net/20140925173110741?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int maxn=102;
vector<int>g[maxn],gre[maxn];//存储正向图和逆图
int ord[maxn];//正向搜索,顶点的编号
bool vis[maxn];
int out[maxn];//转化为DAG以后的每个缩点的出度
int in[maxn];
int belong[maxn];//当前顶点属于哪个集合,相当于染色,当前顶点被染成了什么颜色
int ans[maxn];//每种颜色包括多少顶点,也就是强联通分量的个数
int color;//代表不同的颜色
int no;//正向搜索排序的编号
int n;//顶点数
void dfs1(int u)//从当前u顶点开始DFS
{
vis[u]=1;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
dfs1(v);
}
ord[no++]=u;//为每个顶点编号
}
void dfs2(int u)
{
vis[u]=1;
belong[u]=color;//当前顶点u被染成了color
for(int i=0;i<gre[u].size();i++)
{
int v=gre[u][i];
if(!vis[v])
{
dfs2(v);
}
}
}
void kosaraju()
{
color=1,no=1;
memset(in,0,sizeof(vis));
memset(out,0,sizeof(out));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
if(!vis[i])
dfs1(i);
memset(vis,0,sizeof(vis));
for(int i=no-1;i>=1;i--)//编好号以后,从排号最大的开始搜索
{
int v=ord[i];
if(!vis[v])
{
dfs2(v);
color++;
}
}
//构造DAG
for(int i=1;i<=n;i++)
{
for(int j=0;j<g[i].size();j++)
{
if(belong[i]==belong[g[i][j]])
continue;
out[belong[i]]++;
in[belong[g[i][j]]]++;
}
}
int inzero=0,outzero=0;
for(int i=1;i<color;i++)
{
if(!in[i])
inzero++;
if(!out[i])
outzero++;
}
if(color==2)
printf("1\n0\n");
else
printf("%d\n%d\n",inzero,max(inzero,outzero));
}
int main()
{
scanf("%d",&n);
int to;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&to)&&to)
{
g[i].push_back(to);
gre[to].push_back(i);
}
}
kosaraju();
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11407 | Accepted: 4539 |
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
Source
IOI 1996
题意以及解题思路来自北大暑期培训课件:
本题例子解释:
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int maxn=102;
vector<int>g[maxn],gre[maxn];//存储正向图和逆图
int ord[maxn];//正向搜索,顶点的编号
bool vis[maxn];
int out[maxn];//转化为DAG以后的每个缩点的出度
int in[maxn];
int belong[maxn];//当前顶点属于哪个集合,相当于染色,当前顶点被染成了什么颜色
int ans[maxn];//每种颜色包括多少顶点,也就是强联通分量的个数
int color;//代表不同的颜色
int no;//正向搜索排序的编号
int n;//顶点数
void dfs1(int u)//从当前u顶点开始DFS
{
vis[u]=1;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
dfs1(v);
}
ord[no++]=u;//为每个顶点编号
}
void dfs2(int u)
{
vis[u]=1;
belong[u]=color;//当前顶点u被染成了color
for(int i=0;i<gre[u].size();i++)
{
int v=gre[u][i];
if(!vis[v])
{
dfs2(v);
}
}
}
void kosaraju()
{
color=1,no=1;
memset(in,0,sizeof(vis));
memset(out,0,sizeof(out));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
if(!vis[i])
dfs1(i);
memset(vis,0,sizeof(vis));
for(int i=no-1;i>=1;i--)//编好号以后,从排号最大的开始搜索
{
int v=ord[i];
if(!vis[v])
{
dfs2(v);
color++;
}
}
//构造DAG
for(int i=1;i<=n;i++)
{
for(int j=0;j<g[i].size();j++)
{
if(belong[i]==belong[g[i][j]])
continue;
out[belong[i]]++;
in[belong[g[i][j]]]++;
}
}
int inzero=0,outzero=0;
for(int i=1;i<color;i++)
{
if(!in[i])
inzero++;
if(!out[i])
outzero++;
}
if(color==2)
printf("1\n0\n");
else
printf("%d\n%d\n",inzero,max(inzero,outzero));
}
int main()
{
scanf("%d",&n);
int to;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&to)&&to)
{
g[i].push_back(to);
gre[to].push_back(i);
}
}
kosaraju();
return 0;
}
相关文章推荐
- 【强连通分量缩点】poj 1236 Network of Schools
- POJ 1236 Network of Schools 有向图强连通分量
- POJ 1236 Network of Schools[连通分量]
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- POJ 1236 Network Of Schools ( tarjan求强连通分量 + 缩点成DAG图 )
- Network of Schools POJ - 1236 tarjan强连通分量缩点
- Network of Schools hoj&poj 强连通分量的应用 经典题!
- POJ 1236 Network of Schools 强连通
- 【强连通】 POJ 1236 Network of Schools
- POJ 1236 Network of Schools【强连通缩点】
- 【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
- POJ 1236 Network of Schools(korasaju Algorithm求强连通子集,缩点后DAG上出或入度为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(连通图入度,出度为0)
- 【POJ】1236 Network of Schools 强连通
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
- POJ 1236 Network of Schools(强联通分量)