POJ 1236 Network of Schools (tarjan算法+缩点)
思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数。
一个取值需要讨论,当这个图就是强连通图的时候,答案输出1和0.
个人经历:作为初学者这个题错了很多遍,学姐给我们讲的在某个点已经被访问过的时候low值是否更新的问题,使用的是id的判断方法,只有当id为零的时候才能更新low值,这个现在我是理解的,但当时因为错误太多看别人的代码时,看到了是否在栈中的记录方式,当时我和然然大神就方了,以为是这个题需要特别判定,但后来经过一番讨论,发现这种记录方式跟id的效果是一样的,这个证明我就不给出了,自己画一个有两个强连通分量且这两个分量有一条边相连的图就可以了,个人感觉id的效果更好,它节省了空间,但是我这里还是给出栈判别方式的代码,毕竟还是要知道这种办法是怎么回事的嘛。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
#define maxn 111
stack<int> s;
int dfn[maxn],low[maxn],id[maxn],out[maxn],in[maxn];
int tot,sum,maps[maxn][maxn],n;
void tarjan(int u)
{
dfn[u] = low[u] = ++tot;
s.push(u);
for(int i = ; i <= n; i++)
{
if(!maps[u][i]) continue;
int vv = i;
if(!dfn[vv])
{
tarjan(vv);
low[u] = min(low[vv],low[u]);
}
else if(id[vv] == ) low[u] = min(low[u],dfn[vv]);
}
if(low[u] == dfn[u])
{
sum++;
while(!s.empty())
{
int num = s.top();
s.pop();
id[num] = sum;
if(num == u)
break;
}
}
return;
}
void init()
{
tot = ,sum = ;
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(id,,sizeof(id));
while(!s.empty()) s.pop();
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(maps,,sizeof(maps));
}
int main()
{
int y;
while(cin >> n)
{
init();
for(int i = ; i <= n; i++)
{
while(cin >> y)
{
if(!y) break;
maps[i][y] = ;
}
}
for(int i = ; i <= n; i++)
{
if(!dfn[i]) tarjan(i);
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(!maps[i][j]) continue;
if(id[i] != id[j])
{
out[id[i]]++;
in[id[j]]++;
}
}
}
int ans1 = ;
for(int i = ; i <= sum; i++)
{
if(!in[i]) ans1++;
}
if(sum == ) ans1 = ;
int num1 = ,num2 = ;
for(int i = ; i <= sum; i++)
{
if(!in[i]) num1++;
if(!out[i]) num2++;
}
int ans2 = max(num1,num2);
if(sum == ) ans2 = ;
cout<<ans1<<endl;
cout<<ans2<<endl;
}
return ;
}
POJ 1236 Network of Schools (tarjan算法+缩点)的更多相关文章
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
- POJ 1236 Network of Schools (强连通分量缩点求度数)
题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
随机推荐
- [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力
Couple Cover Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Descri ...
- Announcement
本来是习惯把每天的内容写在一个txt里. 似乎不符合要求.无论格式还是内容.于是转战blog. 事实上.有专业课学习加上马上考四级以及下学期可能的专业调整.此学期时间紧张. 能完成日常作业并掌握周课内 ...
- css3字体
1.定义个性化字体 @font-face{ font-family:字体名字; src:字体地址,可以多写几个用逗号隔开兼容浏览器 } div{ font-family:字体名字/*使用字体*/ } ...
- 一个神奇SQL引发的故障【转】
前几天一个客户数据库主实例告警,诊断过程中发现是由一个慢SQL导致的数据库故障,而在排查逐步深入之后却发现这个现象的不可思议. 问题描述 2016年12日09日,大概9点26分左右,一个客户的生产库主 ...
- List泛型集合常用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...
- sort 排序详解
http://www.qdfuns.com/notes/17398/c56832f62b3271e79671f77dcdd2c058.html. http://www.qdfuns.com/notes ...
- .net core 系列
1..net core 验证码 2..net core 导出excel 3..net core 上传文件 4..net core 时间戳转换 5..net core 读取配置文件 6..net cor ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- MQL5 获取最后一单 利润
///x 最后几单 double getLastProfit(int x) { HistorySelect(,TimeCurrent()); double profit ; long ticket ; ...
- 购物车(Shopping cart) —— B2C网站核心产品设计 (二)
购物车是做什么的? 我们先来看一下现实超市中的购物车,一个带四个轱辘的铁筐子,客人推来推去,看到什么东西喜欢,就扔进去,觉得东西差不多了,就推到收银台. 那B2C网站中的购物车又是一个什么东西呢? 从 ...