0
点赞
收藏
分享

微信扫一扫

poj1236 - Network of Schools(强连通分量模板+缩点模板)

松鼠树屋 2023-02-07 阅读 102


Network of Schools 
Time Limit: 1000MS Memory Limit: 10000K 
Total Submissions: 15674 Accepted: 6213 
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


2 4 3 0 
4 5 0 


1 0 
Sample Output



Source 
IOI 1996

题意

给出各个学校电脑的连接情况,注意给出是单向边,求两个问题:

1、最少向网络中的几台电脑投放文件,则整个网络中的所有电脑能立刻获得该文件;

2、最少要向网络中添加几条单向连接可以使得这个网络中只要投放一份文件到任意一台电脑,则所有电脑都能获得该文件。

分析:

对于第一问,我们求出所有的强连通分量,缩点,得到有向无环图。第一问的答案就是这些0入度点。

对于第二问,转化一下,就是将任意的有向图转化为强连图。设缩点后的有向连通图有q个0入度点,p个0出度点,答案为max(p,q),特别的是本身就是一个强连图图,输出为0

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 106;
int n, dfn[N], low[N], num = 0;
int st[N], top = 0, tot = 0, c[N], ru[N], chu[N];
bool v[N];
vector<int> e[N], scc[N], sc[N];

void tarjan(int x) {
dfn[x] = low[x] = ++num;
st[++top] = x;
v[x] = 1;
for (unsigned int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if (!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if (v[y]) low[x] = min(low[x], dfn[y]);
}
if (dfn[x] == low[x]) {
v[x] = 0;
scc[++tot].push_back(x);
c[x] = tot;
int y;
while (x != (y = st[top--])) {
scc[tot].push_back(y);
v[y] = 0;
c[y] = tot;
}
}
}

int main()
{

cin >> n;
for (int i = 1; i <= n; i++) {
int x;
while (scanf("%d", &x) && x) e[i].push_back(x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int x = 1; x <= n; x++)
for (unsigned int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if (c[x] == c[y]) continue;
sc[c[x]].push_back(c[y]);
ru[c[y]]++;
chu[c[x]]++;
}
int ansa = 0, ansb = 0;
for (int i = 1; i <= tot; i++) {
if (!ru[i]) ansa++;
if (!chu[i]) ansb++;
}
cout << ansa << endl;
int ans = max(ansa, ansb);
if (tot == 1) puts("0");
else
cout << ans << endl;
return 0;
}

 

邻接矩阵模板

// Tarjan算法求有向图强连通分量并缩点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N = 100010, M = 1000010;
int ver[M], Next[M], head[N], dfn[N], low[N];
int stack[N], ins[N], c[N];
int vc[M], nc[M], hc[N], tc;
vector<int> scc[N];
int n, m, tot, num, top, cnt;

void add(int x, int y) {
ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}

void add_c(int x, int y) {
vc[++tc] = y, nc[tc] = hc[x], hc[x] = tc;
}

void tarjan(int x) {
dfn[x] = low[x] = ++num;
stack[++top] = x, ins[x] = 1;
for (int i = head[x]; i; i = Next[i])
if (!dfn[ver[i]]) {
tarjan(ver[i]);
low[x] = min(low[x], low[ver[i]]);
}
else if (ins[ver[i]])
low[x] = min(low[x], dfn[ver[i]]);
if (dfn[x] == low[x]) {
cnt++; int y;
do {
y = stack[top--], ins[y] = 0;
c[y] = cnt, scc[cnt].push_back(y);
} while (x != y);
}
}

int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int x = 1; x <= n; x++)
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (c[x] == c[y]) continue;
add_c(c[x], c[y]);
}
}

 

举报

相关推荐

0 条评论