poj1236 Network of Schools--Kosaraju算法 & 缩点 & 强连通分量
2016-09-10 10:01
393 查看
原题链接:http://poj.org/problem?id=1236
题意:n个学校,给定n行,第i行代表i学校到其他学校是可传输的,单向。问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
分析:强连通缩点,找到缩点后DAG图的各个点的出度入度情况即可,不过需要判断一个特殊情况就是原图本身就是一个强连通图。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int n;
int cnt;
int g[105][105];
int rg[105][105];
int num[105];
int index[105];
bool mark[105];
int in[105];//缩点后的DAG各点的出度入度
int out[105];
int inNum, outNum;
vector<int> s;
void dfs1(int x)
{
mark[x] = 1;
for (int i = 1; i <= n; i++)
if (g[x][i] && !mark[i])
dfs1(i);
s.push_back(x);
}
void dfs2(int x)
{
index[x] = cnt;
num[cnt]++;
mark[x] = 1;
for (int i = 1; i <= n; i++)
if (rg[x][i] && !mark[i])
dfs2(i);
}
void Kosaraju()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (g[i][j] && index[i] != index[j])
{
in[index[j]]++;
out[index[i]]++;
}
}
}
for (int i = 1; i <= cnt; i++)
{
if (in[i] == 0)
inNum++;
if (out[i] == 0)
outNum++;
}
printf("%d\n%d\n", inNum, max(inNum, outNum));
}
int main()
{
scanf("%d", &n);
int x;
for (int i = 1; i <= n; i++)
{
while (scanf("%d", &x) && x)
{
g[i][x] = 1;
rg[x][i] = 1;
}
}
for (int i = 1; i <= n; i++)
if (!mark[i])
dfs1(i);
memset(mark, 0, sizeof(mark));
cnt = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (!mark[s[i]])
{
cnt++;
dfs2(s[i]);
}
}
if (cnt == 1)//注意这点。原图就是一个强连通图
printf("1\n0\n");
else
Kosaraju();
return 0;
}
题意:n个学校,给定n行,第i行代表i学校到其他学校是可传输的,单向。问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
分析:强连通缩点,找到缩点后DAG图的各个点的出度入度情况即可,不过需要判断一个特殊情况就是原图本身就是一个强连通图。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int n;
int cnt;
int g[105][105];
int rg[105][105];
int num[105];
int index[105];
bool mark[105];
int in[105];//缩点后的DAG各点的出度入度
int out[105];
int inNum, outNum;
vector<int> s;
void dfs1(int x)
{
mark[x] = 1;
for (int i = 1; i <= n; i++)
if (g[x][i] && !mark[i])
dfs1(i);
s.push_back(x);
}
void dfs2(int x)
{
index[x] = cnt;
num[cnt]++;
mark[x] = 1;
for (int i = 1; i <= n; i++)
if (rg[x][i] && !mark[i])
dfs2(i);
}
void Kosaraju()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (g[i][j] && index[i] != index[j])
{
in[index[j]]++;
out[index[i]]++;
}
}
}
for (int i = 1; i <= cnt; i++)
{
if (in[i] == 0)
inNum++;
if (out[i] == 0)
outNum++;
}
printf("%d\n%d\n", inNum, max(inNum, outNum));
}
int main()
{
scanf("%d", &n);
int x;
for (int i = 1; i <= n; i++)
{
while (scanf("%d", &x) && x)
{
g[i][x] = 1;
rg[x][i] = 1;
}
}
for (int i = 1; i <= n; i++)
if (!mark[i])
dfs1(i);
memset(mark, 0, sizeof(mark));
cnt = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (!mark[s[i]])
{
cnt++;
dfs2(s[i]);
}
}
if (cnt == 1)//注意这点。原图就是一个强连通图
printf("1\n0\n");
else
Kosaraju();
return 0;
}
相关文章推荐
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- poj1236 Network of Schools【强连通分量(tarjan)缩点】
- 【强连通分量】Tarjan(缩点)POJ1236-Network of Schools
- poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点
- POJ1236---Network of Schools (强连通分量,缩点,Tarjan算法)
- [POJ1236]Network of Schools(Tarjan缩点+强连通分量)
- poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点
- 【tarjan+缩点】POJ1236[IOI1996]-Network of Schools
- 【强连通分量模板题 && 加几条边变强连通】POJ - 1236 Network of Schools
- POJ1236 Network of Schools 【强连通分量】
- POJ1236_Network of Schools_强连通分量_Korasaju算法
- POJ 1236 Network of Schools(强连通分量,缩点)
- poj1236 Network of Schools(强连通-缩点)
- POJ1236-Network of Schools(Tarjan + 缩点)
- poj1236 Network of Schools(tarjan缩点)
- 【POJ1236】Network of Schools 强联通分量缩点(本文kosaraju)
- [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
- poj1236 Network of Schools(强连通分量,tarjan)
- POJ-1236-Network of Schools【强连通分量】【缩点】
- POJ1236-Network of Schools(Tarjan + 缩点)