/**
problem: http://poj.org/problem?id=1236 缩点后入度为0的点的总数为需要发放软件的学校个数
缩点后出度为0的点的总数和入度为0的点的总数的最大值为需要增加的传输线路的条数(头尾相接)
特别的,当图为强连通图时,发放软件的学校个数为1, 增加线路为0
**/
#include<stdio.h>
#include<stack>
#include<algorithm>
using namespace std; class Graphics{
const static int MAXN = ;
const static int MAXM = ;
private:
struct Edge{
int to, next;
bool bridge;
}edge[MAXM];
struct Point{
int dfn, low, color;
}point[MAXN];
int sign, dfnNum, colorNum, sumOfPoint, first[MAXN];
bool vis[MAXN];
stack<int> stk;
void tarjan(int u){
point[u].dfn = ++dfnNum;
point[u].low = dfnNum;
vis[u] = true;
stk.push(u);
for(int i = first[u]; i != -; i = edge[i].next){
int to = edge[i].to;
if(!point[to].dfn){
tarjan(to);
point[u].low = min(point[to].low, point[u].low);
if(point[to].low > point[u].dfn){
edge[i].bridge = true;
}
}else if(vis[to]){
point[u].low = min(point[to].dfn, point[u].low);
}
}
if(point[u].dfn == point[u].low){
vis[u] = false;
point[u].color = ++colorNum;
while(stk.top() != u){
vis[stk.top()] = false;
point[stk.top()].color = colorNum;
stk.pop();
}
stk.pop();
}
}
public:
void clear(int n){
sign = dfnNum = colorNum = ;
for(int i = ; i <= n; i ++){
first[i] = -;
vis[i] = ;
}
sumOfPoint = n;
while(!stk.empty()) stk.pop();
}
void addEdgeOneWay(int u, int v){
edge[sign].to = v;
edge[sign].bridge = false;
edge[sign].next = first[u];
first[u] = sign ++;
}
void addEdgeTwoWay(int u, int v){
addEdgeOneWay(u, v);
addEdgeOneWay(v, u);
}
void tarjanAllPoint(){
for(int i = ; i <= sumOfPoint; i ++){
if(!point[i].dfn)
tarjan(i);
}
}
pair<int, int> getAns(){
int ans = , ans2 = ;
int *indegree = new int[sumOfPoint+];
int *outdegree = new int[sumOfPoint+];
for(int i = ; i <= sumOfPoint; i ++){
indegree[i] = ;
outdegree[i] = ;
}
tarjanAllPoint();
for(int i = ; i <= sumOfPoint; i ++){
for(int j = first[i]; j != -; j = edge[j].next){
int to = edge[j].to;
if(point[to].color != point[i].color){
outdegree[point[i].color] ++;
indegree[point[to].color] ++;
}
}
}
for(int i = ; i <= colorNum; i ++){
if(!indegree[i]){
ans ++;
}
if(!outdegree[i]){
ans2 ++;
}
}
delete []indegree; delete []outdegree;
if(colorNum == ){
return make_pair(, );
}else{
return make_pair(ans, max(ans, ans2));
}
}
}graph; int main(){
int n;
scanf("%d", &n);
graph.clear(n);
for(int i = ; i <= n; i ++){
int a;
while(scanf("%d", &a) && a){
graph.addEdgeOneWay(i, a);
}
}
pair<int, int> ans = graph.getAns();
printf("%d\n%d\n", ans.first, ans.second);
return ;
}

ps:

这两句话是不一样的,在有向图中存在非环边还是非桥的情况

poj 1236 Network of Schools : 求需要添加多少条边成为强连通图 tarjan O(E)的更多相关文章

  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【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  8. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  9. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

随机推荐

  1. git的commit规范及强制校验

      1.背景 在多人协作项目中,如果代码风格统一.代码提交信息的说明准确,那么在后期协作以及Bug处理时会更加方便. 先来介绍本人公司采用的commit规范 Commit message格式 < ...

  2. canvas-菜鸟版画布时钟

    这是以前自己练习写的一个画布时钟 <!DOCTYPE html><html lang="en"> <head> <meta charset ...

  3. access与excel

    我们承认,Excel是伟大的,但却又不得不承认,Excel不是万能的,它至少在"多数据表关联"."数据处理自动化"."大量数据的处理"等方面 ...

  4. LotusScript_文档查询循环方法整理

    1.  视图(View)查询 ... Set view = db.GetView("ViewName") Set doc = view.GetFirstDocument While ...

  5. SCI收录的期刊查询

    SCI   收录的期刊查询(动态) 1.Science Citation Index (SCI)收录期刊查询地址:http://www.isinet.com/cgi-bin/jrnlst/jlopti ...

  6. CodeMirror教程,CodeMirrorAPI中文信息

    <html> <head>     <link rel="stylesheet" href="codemirror.css"> ...

  7. matlab练习程序(Hilbert图像置乱)

    正好刚写了Hibert生成曲线,不如再加一篇应用的程序. 关于Hilbert图像置乱,我在网上搜的应用领域主要集中在数字水印和图像加密上,而这两个领域我都没怎么接触过. 大部分的图像置乱都是如下图的置 ...

  8. Android进阶笔记12:ListView篇之图片优化

    1.图片异步加载: (1)处理图片的方式: 如果ListView中自定义的Item中有涉及到大量图片的,一定要对图片进行细心的处理,因为图片占的内存是 ListView 项中最头疼的,处理图片的方法大 ...

  9. 【BZOJ2510】弱题

    题目大意 有\(M\)个球,一开始每个球均有一个初始标号,标号范围为\(1-N\)且为整数,标号为i的球有\(a_i\)个,并保证\(\sum a_i=M\). 每次操作等概率取出一个球(即取出每个球 ...

  10. matlab各类数据l图像之间的转化

    matlab各类数据图像之间的转化 rgb类型转化为二值的步骤例如以下: 1.採用命令im2double将rgb类型转化三维的double >> str='E:\programing\Ei ...