[强连通分量] POJ 1236 Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16803 | Accepted: 6641 |
Description
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
Output
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
原题大意:给学校的数目n,接下来n行,第i行有若干个数,以0结束,代表了第i-1个学校可以传递消息的学校编号(单向),问,最少需要多少份拷贝资料,如果所有学校两两间可以传达到资料,那么需要新建多少个列表? 解题思路: 同样缩点,很容易想出入度为0的点即为最少拷贝数。
入度为0的点和出度为0的点的最大值便是所需新建数。
#include<stdio.h>
#include<string.h>
struct list
{
int v;
list *next;
};
list *head[10010],*rear[10010];
int dfn[10010],low[10010],stack[10010],s[10010],times,cnt,top;
bool instack[10010];
void tarjian(int v)
{
dfn[v]=low[v]=++times;
stack[top++]=v;
instack[v]=true;
for(list *p=head[v];p!=NULL;p=p->next)
if(!dfn[p->v])
{
tarjian(p->v);
if(low[p->v]<low[v]) low[v]=low[p->v];
}else if(instack[p->v]&&low[p->v]<low[v]) low[v]=low[p->v];
if(dfn[v]==low[v])
{
++cnt;
do
{
v=stack[--top];
instack[v]=false;
s[v]=cnt;
}while(dfn[v]!=low[v]);
}
return;
}
int main()
{
int n,k,j,i,rud[10010],chd[10010],ans1,ans2;
scanf("%d",&n);
memset(head,0,sizeof(head));
memset(rear,0,sizeof(rear));
for(j=1;j<=n;++j)
{
head[j]=rear[j]=new list;
rear[j]->v=j;
while(scanf("%d",&k),k)
{
rear[j]->next=new list;
rear[j]=rear[j]->next;
rear[j]->v=k;
}
rear[j]->next=NULL;
}
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(s,0,sizeof(s));
memset(instack,false,sizeof(instack));
times=cnt=top=0;
for(i=1;i<=n;++i) if(!dfn[i]) tarjian(i);
if(cnt==1)
{
printf("1\n0\n");
return 0;
}
memset(rud,0,sizeof(rud));memset(chd,0,sizeof(chd));
ans1=ans2=0;
for(i=1;i<=n;++i)
for(list *p=head[i];p!=NULL;p=p->next)
if(s[p->v]!=s[i])
{
++rud[s[p->v]];
++chd[s[i]];
}
for(i=1;i<=cnt;++i) if(!rud[i]) ++ans1;
for(i=1;i<=cnt;++i) if(!chd[i]) ++ans2;
printf("%d\n",ans1);
printf("%d\n",ans1>ans2?ans1:ans2);
return 0;
}
[强连通分量] POJ 1236 Network of Schools的更多相关文章
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- 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 ...
- POJ 1236 Network of Schools(强连通分量/Tarjan缩点)
传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...
- POJ 1236 Network of Schools (有向图的强连通分量)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9073 Accepted: 359 ...
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
随机推荐
- jsp中用EL读取了数据库里面的时间,怎么设置格式显示的格式
首先导入标签 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> < ...
- C#使用二叉树算法设计一个无限分级的树表
效果图: 数据库: 操作树的示意图: 控制器代码: using Dw.Business; using Dw.Entity; using Dw.Utilities; using System; usin ...
- -XX:PermSize -XX:MaxPermSize 永久区参数设置
-XX:PermSize -XX:MaxPermSize –设置永久区的初始空间和最大空间 -XX:PermSize 设置持久代(perm gen)初始值,物理内存的1/64 -XX:MaxPe ...
- 数位DP 求K进制下0~N的每个数每位上出现的数的总和
好久没写博客了,因为感觉时间比较紧,另一方面没有心思,做的题目比较浅也是另一方面. 热身赛第二场被血虐了好不好,于是决定看看数位DP吧. 进入正题: 如题是一道经(简)典(单)的数位dp. 第一步,对 ...
- phpcms V9 添加模块
为phpcms创建一个模块的开发流程 [1]创建模块目录 通过前面的学习,我们已经知道phpcms V9框架中的模块位于phcms/modules目录中,每一个目录称之为一个模块. 如果要创建一个模块 ...
- android 蓝牙串口通讯使用简介
需要的权限 <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-perm ...
- css美化checkbox radio样式
/*check,radio*/ input.check_txt[type='checkbox']{ display: none; } input.check_txt[type='checkbox'] ...
- I18N
App.config <?xml version="1.0" encoding="utf-8" ?> <configuration> & ...
- JAVA基础知识之NIO.2——Path,Paths,Files
NIO.2 JDK7对NIO进行了重大改进,主要包含以下两方面 新增Path接口,Paths工具类,Files工具类. 这些接口和工具类对NIO中的功能进行了高度封装,大大简化了文件系统的IO编程. ...
- SourceTree 免登录跳过初始设置
SourceTree 安装之后需要使用账号登陆以授权,以前是可以不登陆的,但是现在是强制登陆. 虽然是免费授权,但是碰上不可抗力因素,登陆不是很方便,这里记录一下跳过这个初始化的步骤. 安装之后,转到 ...