题意:一张有向图,一问至少给几个点发送软件,才能让所有点都能收到软件;二问是至少添加几条边才能让整个图是一个连通分量;

分析:一般求连通分量都会求缩点,在这里缩点之后,生成一张新的图,在新的图中求每一个点的出度,入度。答案就是sum(入度=0),max(sum(出度 == 0),sum(入度 == 0));

注意:如果整张图本来就是一个强连通分量,需要特判。因为它出度,入度都等于0,即max(1,1) = 1,但是实际上不用再补充边了,应该是0,按照上面的分析答案就错了。

 ///POJ1236
///时间复杂度也是O(N+M)
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <iostream>
#define repu(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#define N 105 /// 题目中可能的最大点数
stack<int>sta; /// 存储已遍历的结点
vector<int>gra[N]; /// 邻接表表示图
int dfn[N]; /// 深度优先搜索访问次序
int low[N]; /// 能追溯到的最早的次序
int InStack[N];
/// 检查是否在栈中(2:在栈中,1:已访问,且不在栈中,0:不在)
vector<int> Component[N]; /// 获得强连通分量结果
int InComponent[N]; /// 记录每个点在第几号强连通分量里
int Index,ComponentNumber;/// 索引号,强连通分量个数
int n, m; /// 点数,边数
int d[N][N],chu[N],ru[N]; void init()///清空容器,数组
{
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(chu, , sizeof(chu));
memset(ru, , sizeof(ru));
memset(InStack, , sizeof(InStack));
Index = ComponentNumber = ;
for (int i = ; i <= n; ++ i)
{
gra[i].clear();
Component[i].clear();
}
repu(i,,n+)
repu(j,,n+)
d[i][j] = ;
while(!sta.empty())
sta.pop();
}
void tarjan(int u)
{
InStack[u] = ;
low[u] = dfn[u] = ++ Index;
sta.push(u);///寻找u所在的强连通分量
for (int i = ; i < gra[u].size(); ++ i)
{
int t = gra[u][i];
if (dfn[t] == )///不在的继续递归
{
tarjan(t);///递归到头了就
low[u] = min(low[u], low[t]);
}
else if (InStack[t] == )///在栈里
{
low[u] = min(low[u], dfn[t]);
}
}
if(low[u] == dfn[u])///sta出栈就是一个强连通分量的
{
++ComponentNumber;///强连通分量个数
while (!sta.empty())
{
int j = sta.top();
sta.pop();
InStack[j] = ;///已访问但不在栈中
Component[ComponentNumber].push_back(j);
///用vector存储第ComponentNumber个强连通分量
InComponent[j]=ComponentNumber;
///记录每个点在第几号强连通分量里
if (j == u)
break;
}
}
}
void input()
{
repu(i,,n+)
{
while(scanf("%d",&m) &&m)
d[i][m] = ,gra[i].push_back(m);///有向图才有强连通分量
}
} void solve(void)
{
for(int i=; i<=n; i++)
if(!dfn[i])
tarjan(i);
if(ComponentNumber == )
{
printf("1\n0\n");
return;
}
///缩点
for(int i=; i<=ComponentNumber; i++)
{
for(int j = ; j < Component[i].size(); j++)
{
for(int k = ; k<=n; k++)
{
if(d[k][Component[i][j]] && k != Component[i][j])
{
int s = InComponent[k];
int t = InComponent[Component[i][j]];
if(s!=t)
{
chu[s]++;
ru[t]++;
}
}
}
}
}
int sum = ,num = ;
for(int i=; i<=ComponentNumber; i++)
{
if(!chu[i])
sum++;
if(!ru[i])
num++;
}
printf("%d\n%d\n",num,max(sum,num));
} int main()
{
while(~scanf("%d",&n))
{
init();
input();
solve();
/*每一个强连通分量的具体数字
for(int i = 1; i <= ComponentNumber; i++)
{
for(int j = 0; j < Component[i].size(); j++)
if(!j)
cout << Component[i][j];
else
cout <<"-->"<< Component[i][j];
cout<<endl;
}
*/
}
return ;
}

POJ 1236 SCC+缩点的更多相关文章

  1. poj 1236 scc强连通分量

    分析部分摘自:http://www.cnblogs.com/kuangbin/archive/2011/08/07/2130277.html 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大 ...

  2. poj 1236强连通图缩点

    题目链接:http://poj.org/problem?id=1236 #include <cstdio> #include <cmath> #include <algo ...

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

    题目大意 有N个学校,这些学校之间用一些单向边连接,若学校A连接到学校B(B不一定连接到A),那么给学校A发一套软件,则学校B也可以获得.现给出学校之间的连接关系,求出至少给几个学校分发软件,才能使得 ...

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

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

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

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

  6. POJ 1236 Network of Schools - 缩点

    POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...

  7. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  8. 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767

    poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图  任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...

  9. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. 20160411002 经典SQL语句大全

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  2. 【iOS】我的Objective-C学习笔记

    1.代码中增加标记 #pragma mark - #pragma mark 2.点语法 Person *p = [Person new]; // 点语法的本质还是方法调用 p.age = 10; // ...

  3. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  4. SSH服务器拒绝密码检测

    这两天在配置Ubuntu 14.04的环境时,碰到一个典型的问题:在用xshell 连接Ubuntu时,显示"SSH服务器拒绝密码检测"的问题,在经过一系列配置修改后,最终怀疑是否 ...

  5. Postman Postman测试接口之POST提交本地文件数据

    举例: 文件同步接口 接口地址:http://183.xxx.xxx.xxx:23333/ditui/fileupload HTTP请求方式:POST 针对上述这种POST本地文件的接口,接口数据咋提 ...

  6. cygwin配置git

    对于windows用户来说,使用git bash经常会出现乱码情况,那么一款优质高尚的软件,值得推荐一下了,那就是cygwin 下载cygwin后,在安装过程中,安装git,安装vim编辑器 然后会在 ...

  7. [Mysql] 一些记录

    1> 修改表的字段 alter table trade_market change reqype reqtype int(10) unsigned not null;alter table tr ...

  8. hdu2457DNA repair(ac自动机+dp)

    链接 从开始节点往下走,不能走到病毒节点,如果当前状态与原始串不一样就+1,取一个最小值. #include <iostream> #include<cstdio> #incl ...

  9. win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)

    原文:http://www.cnblogs.com/naniannayue/archive/2012/11/19/2776948.html 要想成功架设SSL安全站点关键要具备以下几个条件. 1.需要 ...

  10. centos6.5安装sublime text 2

    今天在看ueillemmx的博客的时候,看到一神级编辑器,随即安装试了试,我了个去,果然好用,自动补全,自动对齐,样样精通啊! 下面是根据ueillemmx的步骤在CentOS上安装Sublime的过 ...