题意:一个有向图。第一问:最少给几个点信息能让所有点都收到信息。第二问:最少加几个边能实现在任意点放信息就能传遍所有点

思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度

tarjan算法:问问神奇海螺啥是tarjan

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<queue>
  6. #include<cmath>
  7. #include<string>
  8. #include<map>
  9. #include<stack>
  10. #include<set>
  11. #include<vector>
  12. #include<iostream>
  13. #include<algorithm>
  14. #define INF 0x3f3f3f3f
  15. const int N=110;
  16. const int MOD=1000;
  17. using namespace std;
  18. int n,index,scc_cnt; //scc_cnt记录SCC
  19. int dfn[N],low[N],sccno[N],in[N],out[N];
  20. vector<int> g[N];
  21. stack<int> s;
  22. void tarjan(int x){
  23. int i;
  24. dfn[x]=low[x]=++index;
  25. s.push(x);
  26. for(i=0;i<g[x].size();i++){
  27. int v=g[x][i];
  28. if(!dfn[v]){ //未走过
  29. tarjan(v);
  30. low[x]=min(low[x],low[v]);
  31. }
  32. else if(!sccno[v]){ //走过且在栈中
  33. low[x]=min(low[x],dfn[v]);
  34. }
  35. }
  36. if(dfn[x]==low[x]){
  37. scc_cnt++; //增加一个大点
  38. int a;
  39. while(1){ //x及以后全部出栈
  40. a=s.top();
  41. s.pop();
  42. sccno[a]=scc_cnt;
  43. if(a==x) break;
  44. }
  45. }
  46. }
  47. void done(){
  48. index=scc_cnt=0;
  49. memset(dfn,0,sizeof(dfn));
  50. memset(sccno,0,sizeof(sccno));
  51. for(int i=0;i<n;i++){
  52. if(!dfn[i]) tarjan(i);
  53. }
  54. }
  55. int main(){
  56. int t;
  57. while(~scanf("%d",&n) && n){
  58. for(int i=0;i<n;i++) g[i].clear();
  59. for(int u=0;u<n;u++){
  60. while(~scanf("%d",&t) && t){
  61. t--;
  62. g[u].push_back(t);
  63. }
  64. }
  65. done();
  66. int ans1,ans2;
  67. for(int i=1;i<=scc_cnt;i++){
  68. in[i]=out[i]=0; //入度出度初始化为无
  69. }
  70. for(int i=0;i<n;i++){
  71. for(int j=0;j<g[i].size();j++){
  72. int v=g[i][j];
  73. if(sccno[i]!=sccno[v]){
  74. in[sccno[v]]=out[sccno[i]]=1; //一个有入度一个有出度
  75. }
  76. }
  77. }
  78. ans1=ans2=0;
  79. for(int i=1;i<=scc_cnt;i++){
  80. if(in[i]==0) ans1++;
  81. if(out[i]==0) ans2++;
  82. }
  83. if(scc_cnt==1) printf("1\n0\n");
  84. else printf("%d\n%d\n",ans1,max(ans1,ans2));
  85. }
  86. return 0;
  87. }

POJ 1236 Network of Schools(tarjan)题解的更多相关文章

  1. Poj 1236 Network of Schools (Tarjan)

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

  2. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  3. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

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

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

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

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

  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(又是强连通分量+缩点)

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

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

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

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

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

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

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

随机推荐

  1. Python 标准输出 sys.stdout 重定向

    本文环境:Python 2.7 使用 print obj 而非 print(obj) 一些背景 sys.stdout 与 print 当我们在 Python 中打印对象调用 print obj 时候, ...

  2. 启动yarn

    $cd /app/hadoop/hadoop-2.2.0/sbin $./start-yarn.sh

  3. navicat的安装

    1.首先在官网下载navicat,具体安装步骤比较简单,下一步下一步即可. 2.安装之后,按照下面的网址做法激活 http://www.jianshu.com/p/b1f9194e1e31 3.教程: ...

  4. LINUX中的ACL

    一. 为什么要使用ACL先让我们来简单地复习一下Linux的文件权限. 在 linux下,对一个文件(或者资源)可以进行操作的对象被分为三类: file owner(文件 的拥有者),group(组, ...

  5. mac shell终端编辑命令行快捷键

    Ctrl + d        删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof) Ctrl + h        退格删除一个字符,相当 ...

  6. 复习总结《一》MFC消息映射

    长时间人容易遗忘,从新捡起!特做下记录 MFC消息映射 1.在MFC中消息映射主要牵扯到三个宏分别为: DECLARE_MESSAGE_MAP() BEGIN_MESSAGE_MAP(theClass ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON ConvolImage

    zw版[转发·台湾nvp系列Delphi例程]HALCON ConvolImage procedure TForm1.Button1Click(Sender: TObject);begin img.D ...

  8. python 字符串的I/O 操作

    想使用操作类文件对象的程序来操作文本或二进制字符串 使用io.StringIO() 和io.BytesIO() 类来创建类文件对象操作字符串数据 >>> s = io.StringI ...

  9. linux常用命令:ps 命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  10. 20145316许心远《网络对抗》EXP7网络欺诈技术防范

    20145316许心远<网络对抗>EXP7网络欺诈技术防范 实验后回答问题 通常在什么场景下容易受到DNS spoof攻击 公共共享网络里,同一网段可以ping通的网络非常容易被攻击 在日 ...