POJ 1236.Network of Schools 解题报告

首先要强连通缩点,统计新的图的各点的出度和入度。

第一问直接输出入度为0的点的个数

第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个

注意特判,输入已经是一个极大强连通图的情况,输出 1 0

code

/*
       无向图强连通的Garbow算法,思路与Tarjan算法相同,实现更直接,效率更好
       时间复杂度同样为O(n+m)
       思路:
       dfn记录访问顺序,st为访问栈,tem为辅助栈
       每次找到环时,将环中除顺序最靠前的点其他的点全出栈st
       tem中当前点之上(包括当前点)的所有点为一个强连通分量
*/
#include <iostream>
#include <cstring>
using namespace std;
const int INF = 109;
struct node {
	int u, v, ne;
} E[INF*INF];
int head[INF], cnt;
int dfn[INF], num[INF], sta[INF], Tops, tem[INF], Topt, scc;
int n;
void addedge (int u, int v) {
	E[++cnt].u = u, E[cnt].v = v;
	E[cnt].ne = head[u];
	head[u] = cnt;
}
void dfs (int k, int t) {
	sta[++Tops] = tem[++Topt] = k;
	dfn[k] = ++t;
	for (int i = head[k]; i != 0; i = E[i].ne) {
		int v = E[i].v;
		if (!dfn[v]) dfs (v, t);
		else if (num[v] == 0)
			while (dfn[sta[Tops]] > dfn[v]) Tops--;
	}
	if (sta[Tops] == k) {
		Tops--, scc++;
		do
			num[tem[Topt]] = scc;
		while (tem[Topt--] != k);
	}
}
void Garbow (int n) {
	memset (dfn, 0, sizeof dfn);
	memset (num, 0, sizeof num);
	Tops = Topt = scc = 0;
	for (int i = 1; i <= n; i++)
		if (!num[i]) dfs (i, 0);
}
void make() {
	Garbow (n);
	int degi[INF], dego[INF];
	memset (degi, 0, sizeof degi);
	memset (dego, 0, sizeof dego);
	if (scc == 1) {
		cout << 1 << endl << 0 << endl;
		return ;
	}
	for (int i = 1; i <= cnt; i++) {
		int u = E[i].u, v = E[i].v;
		if (num[u] != num[v]) {
			degi[num[v]] = 1;
			dego[num[u]] = 1;
		}
	}
	int ans1=0, ans2=0;
	for (int i = 1; i <= scc; i++) {
		if (degi[i] == 0) ans1++;
		if (dego[i] == 0) ans2++;
	}
	cout << ans1 <<endl<< max (ans1, ans2);
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		while (cin >> x && x)
			addedge (i, x);
	}
	make();
	return 0;
}

  

时间: 2024-10-13 05:40:15

POJ 1236.Network of Schools 解题报告的相关文章

poj 1236 Network of Schools 【强连通图】

题目:poj 1236 Network of Schools 类似题目hdoj 2767 3836 /*******以下kuang大神的解释,写的很好就不解释了*************************/ 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任

Poj 1236 Network of Schools (Tarjan)

题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个网络才能够让所有学校被网络覆盖?2:至少要加几条线路就能做到在任意一个学校安装网络都可以覆盖全部学校? 解题思路: 先用Tarjan对强连通分量进行缩点,然后对缩点以后的图进行处理,统计图中节点出度为零的有多少,入度为零的有多少个? 因为入度为零的点不能由其他的点到达,在每个入度为零的节点安装网络可

POJ 1236 Network of Schools(强连通分量)

POJ 1236 Network of Schools 链接:http://poj.org/problem?id=1236 题意:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B 在学校A 的列表中,则A 不一定在B 的列表中. 任务A:计算为使得每个学校都能通过网络收到软件,你至少需要准备多少份软件拷贝. 任务B:考虑一个更长远的任务,想确保给任意一个学校发放一个新的软件拷贝,该软件拷贝能发布到网络中的每个学

POJ 1236 Network of Schools(强连通 Tarjan+缩点)

POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入?几条边使得有向图成为一个强连通图. 分析: 跟HDU 2767 Proving Equivalences(题解)一样的题目,只是多了个问题,事实上转化成DAG后就不难考虑了,事实上仅仅要选择入度为0的点即可了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> *

[tarjan] poj 1236 Network of Schools

题目链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11433   Accepted: 4551 Description A number of schools are connected to a computer network. Agreements have been developed among thos

poj 1236 Network of Schools(连通图入度,出度为0)

http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13046   Accepted: 5215 Description A number of schools are connected to a computer network. Agreements have been developed among those scho

POJ 1236——Network of Schools——————【加边形成强连通图】

Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1236 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: e

[ACM] poj 1236 Network of Schools (有向强连通分量)

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11407   Accepted: 4539 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

poj 1236 Network of Schools(tarjan+缩点)

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