http://poj.org/problem?id=1236

N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

首先求出该图所有的强连通分量,将其缩为一点,因为对于强连通分量,任意两点都能互相可达,只要给DAG(强连通分量)中的任意一点都能互相到达。

缩点后然后统计每个点的入度与出度,入度为0的点肯定是A的答案,而对于答案B则是max(入度为0的点的个数,出度为0的点的个数)。B:入度为0我们只有给他一条路线入度+1能获得软件,出度同理。

View Code

#include <iostream>#include <cstdio>#include <cstring>#define maxn 107using namespace std;struct node{int v;int next;}g[maxn*maxn];int low[maxn],dfn[maxn],head[maxn],stack[maxn],belong[maxn];int in[maxn],out[maxn];bool instack[maxn];int bcnt,index,t,top,n;void init(){    memset(g,0,sizeof(g));for (int i = 0; i < maxn; ++i)    {        head[i] = low[i] = dfn[i] =stack[i] = in[i] = out[i] = 0;        belong[i] = 0;        instack[i]  = false;    }    t = 1;    index = bcnt = top = 0;}void add(int u,int v){    g[t].v = v;    g[t].next = head[u];    head[u] = t++;}void tarjan(int i){int j,k;    low[i] = dfn[i] = ++index;    instack[i] = true;    stack[++top] = i;for (k = head[i]; k; k = g[k].next)    {        j =g[k].v;if (!dfn[j])        {            tarjan(j);            low[i] = min(low[i],low[j]);        }else if(instack[j])        {            low[i] = min(low[i],dfn[j]);        }    }if (dfn[i] == low[i])    {        bcnt++;do        {            j = stack[top--];            instack[j] = false;            belong[j] = bcnt;        }while (j != i);    }}void solve(){for (int i = 1; i <= n; ++i)    {if (!dfn[i])         tarjan(i);    }for (int i = 1; i <= n; ++i)    {//printf("~!!!%d\n",belong[i]);        for (int j = head[i]; j; j = g[j].next)        {int k = g[j].v;if (belong[k] != belong[i])            {out[belong[i]]++;in[belong[k]]++;            }        }    }int ct1 = 0;int ct2 = 0;for (int i = 1; i <= bcnt; ++i)//这里要小于bcnt因为这是缩点后的    {if (!in[i]) ct1++;if (!out[i]) ct2++;    }    printf("%d\n",ct1);if (bcnt == 1) printf("0\n");else    printf("%d\n",max(ct1,ct2));}int main(){//freopen("d.txt","r",stdin);    int i,x;while (cin>>n)    {         init();for (i = 1; i <= n; ++i)      {while (cin>>x)          {if (!x) break;              add(i,x);          }      }      solve();    }return 0;}

pku 1236 Network of Schools (tarjan缩点)相关推荐

  1. poj 1236 Network of Schools (强连通分支缩点)

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  2. POJ - 1236 Network of Schools(强连通缩点)

    题目链接:点击查看 题目大意:一个学校连接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(学校A支援学校B,并不表示学校B一定支援学校A).当某校获得一个新软件时,无论是 ...

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

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

  4. [tarjan] poj 1236 Network of Schools

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

  5. POJ 1236 Network of Schools(tarjan)

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

  6. poj1236/luogu2746 Network of Schools (tarjan)

    tarjan缩点后,第一问答案显然是入度为零的点得个数 第二问:考虑到 没有入度或出度为0的点 的图强连通, 所以答案就是max{入度为零的个数,出度为零的个数} (把出度为零的连到入度为零的点,然后 ...

  7. POJ 1236 Network of Schools(强连通分量缩点求根节点和叶子节点的个数)

    Description: A number of schools are connected to a computer network. Agreements have been developed ...

  8. POJ - 1236 Network of Schools

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  9. poj 1236 Network of Schools

    题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...

最新文章

  1. Feature Selection Based on Mutual Information:Criteria of Max-Dependency, Max-Relevance,and Min-Redu
  2. BorderContainer的圆角问题
  3. C#中,什么时候用yield return
  4. android 一个很漂亮的控件ObservableScrollView(含片段代码和源码)
  5. UCMA(OCS) 开发系列之一
  6. 全国计算机二级考试c语言指针,全国计算机等级考试二级C语言指针检测试题.doc...
  7. 白色情人节为你身边的程序猿献上一份礼物!
  8. WDK安装调试以及注意事项
  9. 检查字符串是否为合法的日期格式
  10. Error:Execution failed for task ':app:compileDebugNdk'. Error: NDK integrat
  11. html图片标签 imag
  12. iReport导出Excel文件一个字段要占用多列问题的解决
  13. EasyExcel动态导出-动态头
  14. 在厦门,“隐藏”了一个超高逼格的智慧园区
  15. Xmanager 5 远程连接linux图形界面
  16. 计算机/微机组成及相关概念
  17. 将AAB(Android App Bundle)转换为APK
  18. ZigBee室内定位设备的天线与射频接口电路设计
  19. 翻译软件哪个准确度高【免费】
  20. 计算机网络 TCP 滑动窗口协议 详解

热门文章

  1. OpenCV相交凸intersectConvex的实例(附完整代码)
  2. C语言实现图形ADT(Graph ADT)接口COMP2521(附完整源码)
  3. C语言实现AVL树(附完整源码)
  4. freetds mysql_apache+mysql+php+gd+freetds
  5. springboot整合mysql5.7_每天五分钟写K8(四):SpringBoot与mysql整合
  6. python以运行效率高著称吗_几个提升Python运行效率的方法之间的对比
  7. php 需要已安装且正在运行的邮件系统_php如何发送邮件?一个函数轻松搞定
  8. Spring2.5整合JPA
  9. Java调用.Net的web service的几种方式
  10. 通过已有SQL语句,生成数据库模型PDM