/**
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. [LeetCode]22. Generate Parentheses括号生成

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. 位运算(5)——Power of Two

    判断一个整数是不是2的幂. 关键是弄明白2的幂的二进制形式只有一个1. public class Solution { public boolean isPowerOfTwo(int n) { int ...

  3. 同源策略和Jsonp、CORS

    一.同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之 ...

  4. BZOJ4010: [HNOI2015]菜肴制作(拓扑排序 贪心)

    题意 题目链接 Sol 震惊,HNOI竟出NOI原题 直接在反图上贪心一下. // luogu-judger-enable-o2 // luogu-judger-enable-o2 #include& ...

  5. Linux中怎么从root用户切换到普通用户

    su是在用户间切换,可以是从普通用户切换到root用户, test@ubuntu:~$ su Password:  root@ubuntu:/home/test# 也可以是从root用户切换到普通用户 ...

  6. Qtl和JS、HTML通信/交互

    http://www.cnblogs.com/sigma0/p/7346727.html Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就 ...

  7. python进程与线程介绍

    很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行多个任务.打个比方,你 ...

  8. svn图标显示不正常,文件夹显示但文件不显示svn图标

    svn图标显示不正常,文件夹显示但文件不显示svn图标   这个问题的引发是自己造成的,使用myEclipse时progress会卡在 refresh svn status cache (0%)这里, ...

  9. 面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序

    第一:private, public, protected访问标号的访问范围. private:只能由          1.该类中的函数          2.其友元函数访问 不能被任何其他访问,该 ...

  10. Python对数组的基本操作

    # coding=utf-8创建并打印数组'''arr = ["aex", "bfe", "mpilgrim", "zddd&qu ...