POJ - 1236 Network of Schools (强连通分量)
2018-01-26 15:01
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
Sample Output
解题思路:很多博客都有了。用于保存 tarjan模板。之前一直以为缩点很牛逼,其实也就这样嘛……
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <memory.h>
#include <bitset>
#include <map>
#include <deque>
#include <math.h>
#include <stdio.h>
using namespace std;
using namespace std;
typedef long long int ll;
const int MAXN = 300005;
vector<int> G[105];//图
int N;
int color[105];//每个点所属的颜色(缩点)
int dfn[105];//被访问的时间
int low[105];//最后被访问到的最小的dfn
int vis[105];//是否被访问过
int instack[105];//是否在栈中
int sta[105];//栈
int cnt = 0;//栈顶
int vistime = 1;//访问时间
int col = 1;//缩点后的点数
void tarjan(int v1)
{
dfn[v1] = low[v1] = vistime;
vistime++;
vis[v1] = 1;
sta[cnt++] = v1;
instack[v1] = 1;
for (int i = 0; i < G[v1].size(); i++)
{
if (!vis[G[v1][i]])
{
tarjan(G[v1][i]);
low[v1] = min(low[v1], low[G[v1][i]]);
}
else if (instack[G[v1][i]])
low[v1] = min(low[v1], dfn[G[v1][i]]);
}
if (dfn[v1] == low[v1])
{
int temp;
do
{
temp = sta[--cnt];
color[temp] = col;//缩点
instack[temp] = 0;
} while (temp != v1);
col++;
}
}
int indegree[105];
int outdegree[105];
//缩点后求入度和出度
void SD()
{
for (int i = 1; i <= N; i++)
{
for (int j = 0; j < G[i].size(); j++)
{
if (color[i] != color[G[i][j]])
{
outdegree[color[i]]++;
indegree[color[G[i][j]]]++;
}
}
}
}
int main()
{
int temp;
scanf("%d", &N);
for (int i = 1; i <= N; i++)
{
while (1)
{
scanf("%d", &temp);
if (temp == 0)
break;
G[i].push_back(temp);
}
}
for (int i = 1; i <= N; i++)
if (!vis[i])
tarjan(i);
// for (int i = 1; i <= N; i++)
// cout << color[i] << " ";
SD();
int t1 = 0, t2 = 0;
for (int i = 1; i < col; i++)
{
if (indegree[i] == 0)
t1++;
if (outdegree[i] == 0)
t2++;
}
if (col == 2)
printf("1\n0\n");
else
printf("%d\n%d\n", t1, max(t1, t2));
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20402 | Accepted: 8047 |
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
解题思路:很多博客都有了。用于保存 tarjan模板。之前一直以为缩点很牛逼,其实也就这样嘛……
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <memory.h>
#include <bitset>
#include <map>
#include <deque>
#include <math.h>
#include <stdio.h>
using namespace std;
using namespace std;
typedef long long int ll;
const int MAXN = 300005;
vector<int> G[105];//图
int N;
int color[105];//每个点所属的颜色(缩点)
int dfn[105];//被访问的时间
int low[105];//最后被访问到的最小的dfn
int vis[105];//是否被访问过
int instack[105];//是否在栈中
int sta[105];//栈
int cnt = 0;//栈顶
int vistime = 1;//访问时间
int col = 1;//缩点后的点数
void tarjan(int v1)
{
dfn[v1] = low[v1] = vistime;
vistime++;
vis[v1] = 1;
sta[cnt++] = v1;
instack[v1] = 1;
for (int i = 0; i < G[v1].size(); i++)
{
if (!vis[G[v1][i]])
{
tarjan(G[v1][i]);
low[v1] = min(low[v1], low[G[v1][i]]);
}
else if (instack[G[v1][i]])
low[v1] = min(low[v1], dfn[G[v1][i]]);
}
if (dfn[v1] == low[v1])
{
int temp;
do
{
temp = sta[--cnt];
color[temp] = col;//缩点
instack[temp] = 0;
} while (temp != v1);
col++;
}
}
int indegree[105];
int outdegree[105];
//缩点后求入度和出度
void SD()
{
for (int i = 1; i <= N; i++)
{
for (int j = 0; j < G[i].size(); j++)
{
if (color[i] != color[G[i][j]])
{
outdegree[color[i]]++;
indegree[color[G[i][j]]]++;
}
}
}
}
int main()
{
int temp;
scanf("%d", &N);
for (int i = 1; i <= N; i++)
{
while (1)
{
scanf("%d", &temp);
if (temp == 0)
break;
G[i].push_back(temp);
}
}
for (int i = 1; i <= N; i++)
if (!vis[i])
tarjan(i);
// for (int i = 1; i <= N; i++)
// cout << color[i] << " ";
SD();
int t1 = 0, t2 = 0;
for (int i = 1; i < col; i++)
{
if (indegree[i] == 0)
t1++;
if (outdegree[i] == 0)
t2++;
}
if (col == 2)
printf("1\n0\n");
else
printf("%d\n%d\n", t1, max(t1, t2));
return 0;
}
相关文章推荐
- Network of Schools - POJ 1236 - 强连通分量
- poj 1236 Network of Schools 强连通分量
- POJ 1236 Network of Schools(强连通分量)#by zh
- POJ 1236 Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量 Tarjan算法)
- 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 [强连通分量]