题目链接: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[连通分量]的更多相关文章

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

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

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

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  3. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  4. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

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

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  7. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

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

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  9. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

随机推荐

  1. js原生bind()用法[注意不是jquery里面的bind()]

    <div id="a"> <div></div> <div></div> <div></div> ...

  2. 基于JQuery实现相同内容合并单元格[转]

    <script type="text/javascript"> jQuery.fn.rowspan = function(colIdx) { //封装的一个JQuery ...

  3. 2D几何

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

  4. OpenWebFlow0.9用户手册与设计说明

    1.    OpenWebFlow概述 OpenWebFlow是基于Activiti扩展的工作流引擎.Activiti (官方网站http://activiti.org/,代码托管在https://g ...

  5. C# 反射 设置字段值无效的解决办法

    FieldInfo.SetValue的原型是:void SetValue(object obj, object value)当你传递一个值类型(结构是值类型)的时候,它要转化成object,也就是要装 ...

  6. Android 6.0doze和standby 的一点理解

    之前写的压力测试程序,在开发版和5.0上面测试好好的,即使熄灭屏幕也会跑.我的程序主要是在Activity里面开启了一个thread,其中不断在界面上显示任务信息.换到6.0的 机器上面,发现一个现象 ...

  7. 让aspx页面也可以通过url路由进行访问

    参考文章:http://blog.csdn.net/zhanglong_longlong/article/details/8841030 这两天,在工作中需要将aspx的页面虚拟成url路径访问.比如 ...

  8. System.Data.SqlClient.SqlError: 对文件……的目录查找失败[转]

    System.Data.SqlClient.SqlError: 对文件……的目录查找失败,出现操作系统错误 3 的处理办法 在还原SQL SERVER数据库时出现了查找目录失败的原因,困扰了我一个多小 ...

  9. Linq 中的TakeWhile 和 SkipWhile

    这两个概念容易搞混 理解了一番后 在这里写下便于记忆 SkipWhile 可以理解为如果条件满足  就一直跳过   知道不满足后 就取剩下的所有元素(后面的不会再判断) TakeWhile 可以理解为 ...

  10. Vijos P1325桐桐的糖果计划

    > P1325桐桐的糖果计划 标签:**图结构 强连通分量** 描述 桐桐很喜欢吃棒棒糖.他家处在一大堆糖果店的附近. 但是,他们家的区域经常出现塞车.塞人等情况,这导致他不得不等到塞的车或人走 ...