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

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

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

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=110;
const int MOD=1000;
using namespace std;
int n,index,scc_cnt; //scc_cnt记录SCC
int dfn[N],low[N],sccno[N],in[N],out[N];
vector<int> g[N];
stack<int> s;
void tarjan(int x){
int i;
dfn[x]=low[x]=++index;
s.push(x);
for(i=0;i<g[x].size();i++){
int v=g[x][i];
if(!dfn[v]){ //未走过
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(!sccno[v]){ //走过且在栈中
low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x]){
scc_cnt++; //增加一个大点
int a;
while(1){ //x及以后全部出栈
a=s.top();
s.pop();
sccno[a]=scc_cnt;
if(a==x) break;
}
}
}
void done(){
index=scc_cnt=0;
memset(dfn,0,sizeof(dfn));
memset(sccno,0,sizeof(sccno));
for(int i=0;i<n;i++){
if(!dfn[i]) tarjan(i);
}
}
int main(){
int t;
while(~scanf("%d",&n) && n){
for(int i=0;i<n;i++) g[i].clear();
for(int u=0;u<n;u++){
while(~scanf("%d",&t) && t){
t--;
g[u].push_back(t);
}
}
done();
int ans1,ans2;
for(int i=1;i<=scc_cnt;i++){
in[i]=out[i]=0; //入度出度初始化为无
}
for(int i=0;i<n;i++){
for(int j=0;j<g[i].size();j++){
int v=g[i][j];
if(sccno[i]!=sccno[v]){
in[sccno[v]]=out[sccno[i]]=1; //一个有入度一个有出度
}
}
}
ans1=ans2=0;
for(int i=1;i<=scc_cnt;i++){
if(in[i]==0) ans1++;
if(out[i]==0) ans2++;
}
if(scc_cnt==1) printf("1\n0\n");
else printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return 0;
}

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. Constructing Roads----poj2421(最小生成树Kruskal)

    题目链接: http://poj.org/problem?id=2421 想把n个村庄连接在一起:求最小生成树,不同的是已经有了m条线段链接在一起了,求剩下的: 感觉用Kruskal会简单一点 #in ...

  2. openssl配置

    1.update openssl yum update openssl 2.修改openssl配置 vim /etc/ssh/sshd_config RSAAuthentication yesPubk ...

  3. python count()

    count() 描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: str.count(sub, st ...

  4. Android应用之——百度地图最新SDK3.0应用,实现最经常使用的标注覆盖物以及弹出窗覆盖物

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/yanglfree/article/details/33333413 一.概述 最新版的百度地图SDK ...

  5. listview点击控件显示EditText,键盘弹出消失的解决方法:

    1.软键盘弹出后消失解决方法 AndoridManifet 在activity中添加: android:windowSoftInputMode="adjustPan" 2.使用方式 ...

  6. python中文件变化监控-watchdog

    在python中文件监控主要有两个库,一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一个是watchdog(http://pytho ...

  7. 性能测试之nmon对linux服务器的监控

    大家都知道在做性能测试的时候,需要监控服务器的资源情况,而大多数服务器是Linux系统,网上资料嘿多,这里汇总介绍下Nmon监控工具: -------------------------------- ...

  8. 去掉python的警告

    1.常规警告 import warnings warnings.filterwarnings("ignore") 2.安装gensim,在python中导入的时候出现一个警告: w ...

  9. Windows2008 IIS配置FTP站点

    视频教程:http://vodcdn.video.taobao.com/player/ugc/tb_ugc_pieces_core_player_loader.swf?version=1.0.2015 ...

  10. Qt5

    最简单的分割窗体 #include <QApplication> #include <QLabel> #include <QSplitter> int main(i ...