POJ 1236 Network of Schools[连通分量]
题目链接:http://poj.org/problem?id=1236
题目大意:给出N台电脑,电脑间单向连通传送文件
问题1.网络中最少放几个文件保证所有电脑都能接受到文件
问题2.最少向网络中加几条线保证任意放1个文件所有电脑都能接受到
解题思路:
1.求出强连通分量
2.缩点 ,进行重新构图
3.如果入度为0,说明没有文件传输到该网络。解决问题1,统计入度为0的个数即可
4.一个强连通图的入度和出度都不为0。入度为0相当于根root,出度为0相当于叶子leave.max(root, leave)即为答案。
就是需要加的线数,保证整个网络强连通。
若连通分量个数为0,那么答案为0
代码如下:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 105
#define M 10005
struct Edge
{
int v, next;
}edge[M]; int node[N], stack[N], instack[N], dfn[N], out[N], in[N];
int low[N], belong[N], index, cnt_edge, n, m, cnt_tar, top;
int ee[M][]; void add_Edge(int u, int v)
{
edge[cnt_edge].next=node[u];
edge[cnt_edge].v=v;
node[u]=cnt_edge++;
}
void tarjan(int u)
{
int i, j, v;
dfn[u]=low[u]=++index;
stack[++top]=u;
instack[u]=;
for(i=node[u]; i!=-; i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u], low[v]);
}
else if(instack[v])
low[u]=min(low[u], dfn[v]);
}
if(dfn[u]==low[u])
{
cnt_tar++;
do
{
j=stack[top--];
instack[j]=;
belong[j]=cnt_tar;
}while(j!=u);
} }
void solve()
{
int i;
top=, index=, cnt_tar=;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
for(i=; i<=n; i++)
if(!dfn[i])
tarjan(i);
}
int main()
{
int i, u, v;
while(scanf("%d", &n)!=EOF)
{
cnt_edge=, m=;
memset(node, -, sizeof(node));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
for(i=; i<=n; i++)
{
while(scanf("%d", &v))
{
if(v==) break;
ee[m][]=i, ee[m++][]=v;
add_Edge(i, v);
}
}
solve();
for(i=; i<=m; i++)
{
int xx=belong[ee[i][]], yy=belong[ee[i][]];
if(xx!=yy)
{
in[yy]++;
out[xx]++;
}
}
int innum=, outnum=;
for(i=; i<=cnt_tar; i++)
{
if(in[i]==)
innum++;
if(out[i]==)
outnum++;
}
if(cnt_tar==)
printf("1\n0\n");
else
printf("%d\n%d\n", innum, max(innum, outnum));
}
return ;
}
POJ 1236 Network of Schools[连通分量]的更多相关文章
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
随机推荐
- MATLAB的基本元素
MALTAB程序的基本数据单元是数组,MATLAB 的变量名必须以字母开头,后面可以跟字母,数字和下划线(_).只有前31个字符是有效的:如果超过了31 个字符,基余的字符将被忽略.如果声明两个变量, ...
- linux commond
1 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2 ifconfig 3 ping 172.22.14.59 4 ping 1 ...
- highcharts 去掉右下角链接
去掉右下角的highcharts.com链接需要加入以下代码: credits: { enabled:false }, 如果不设置,那么默认为显示.
- [MSDN]关键字查询语言 (KQL) 语法参考
MSDN:http://msdn.microsoft.com/library/ee558911(v=office.15) 了解如何构造 SharePoint 2013 中的搜索功能 的 KQL 查询. ...
- Android进阶——声波振幅显示
最近博主想做一个app,中间有一个是录音的功能.于是博主想把UI做的好看一些,想仿照微信或者QQ语音输入时,能够随着声音的大小显示声波振幅.于是查找了一些资料,现在把这个功能的Demo分享给大家,以后 ...
- vs: 编译: 计算机中丢失mspdb100.dll
在vs 2010中, 将Microsoft Visual Studio 10.0\VC\bin目录配置在path后(甚至添加lib以及include)后, 报如题错误. 是由于Microsoft Vi ...
- iOS开发——极光推送
1.到极光官网 https://www.jpush.cn/ 下载极光推送SDK. 具体如何集成最好参考官网的文档,以及一些失败的原因.文档非常详细,我也是参考集成的. 2.到极光推送官网注册自己的应用 ...
- (转)解读Flash矩阵
转自: http://hi.baidu.com/cabtw/item/d2dbd212d4ae3e9398ce337f 图片看不到请去原网站看 Matrix: scale(a,d); 比例变换就是将平 ...
- [翻译]AOP编程
翻译文章链接http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit ...
- 10_HTTP协议_入门知识
[什么是HTTP协议] 对 浏览器客户端 和 服务器端之间的数据传输的格式规范. 客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵循一定的通讯格式,HTTP协议用于定义客户端与 ...