POJ 1236 Network of Schools Tarjan缩点
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22729 | Accepted: 8926 |
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
first line contains an integer N: the number of schools in the network
(2 <= N <= 100). The schools are identified by the first N
positive integers. Each of the next N lines describes a list of
receivers. The line i+1 contains the identifiers of the receivers of
school i. Each list ends with a 0. An empty list contains a 0 alone in
the line.
Output
program should write two lines to the standard output. The first line
should contain one positive integer: the solution of subtask A. The
second line should contain the solution of subtask B.
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
哦豁,这道水题。我wa了一天,从11点WA到晚上九点。。最后发现少写一句话orz...
忘记给u标记orzorzorzorz然后发现自己上午敲的板子就是错的 wori....
以后我只用bin神的模板!!!
这个题很简单,还是tarjan缩点,分别找一下入度和出度为0的点。
最少通知几个,易得,考虑入度为0的强连通分量~
最少加几条边,sum1和sum2的最大值?为什么呢,因为要让任意选一个点都满足条件,画一下图,得让每个强连通分量有入有出。所以要取最大值。
贴代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 1000000000
#define maxn 105
#define maxm 100005
struct node
{
int to,next;
} edge[maxm];
int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn];
stack <int> s;
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(cdu,,sizeof(cdu));
memset(rdu,,sizeof(rdu));
while(!s.empty()) s.pop();
cnt=;
timi=;
top=;
cut=;
}
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
void tarjan(int u)
{
dfn[u]=timi;
low[u]=timi;
timi++;
s.push(u);
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
{
if(vis[v])
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
cut++;
int x=s.top();
while(x!=u)
{
vis[x]=;
num[x]=cut;
s.pop();
x=s.top();
}
num[x]=cut;
vis[x]=;
s.pop();
}
}
int mp[maxn][maxn];
int main()
{
int a;
while(cin>>n)
{
init();
for(int i=; i<=n; ++i)
{
while(~scanf("%d",&a)&&a) {
addedge(i,a);
mp[i][a]=;
}
}
for(int i=; i<=n; ++i)
{
if(!dfn[i]) tarjan(i);
}
if(cut==)
{
printf("1\n0\n");
continue;
}
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j)
{
if(mp[i][j]==&&num[i]!=num[j])
{cdu[num[i]]++;rdu[num[j]]++;}
}
int sum1=,sum2=;
for(int i=; i<=cut; ++i)
{
if(!cdu[i]) sum1++;
if(!rdu[i]) sum2++;
}
//cout<<sum1<<" "<<sum2<<endl;
printf("%d\n%d\n",sum2,max(sum1,sum2));
}
return ;
}
POJ 1236 Network of Schools Tarjan缩点的更多相关文章
- 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)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools 连通图缩点
题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...
- POJ 1236 Network of Schools —— (缩点的应用)
题目大意:有N个学校和一些有向边将它们连结,求: 1.最少需要向几个学校发放软件,使得他们中的每一个学校最终都能够获得软件. 2.最少需要增加几条有向边使得可以从任意一个学校发放软件,使得每一个学校最 ...
- 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 ...
随机推荐
- Uva 长城守卫——1335 - Beijing Guards
二分查找+一定的技巧 #include<iostream> using namespace std; +; int n,r[maxn],Left[maxn],Right[maxn];//因 ...
- 洛谷 3567/BZOJ 3524 Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2895 Solved: 1189[Submit][S ...
- Linux时区修改
Linux修改时区的正确方法 CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 [root@centos7 ~]# ...
- tcl之string操作
- 力扣题目汇总(丑数,重复N的元素,求众数)
丑数 1.题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 ...
- Logistic回归python实现小样例
假设现在有一些点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归.利用Logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,依次进行分类.Lo ...
- C 语言 习题 1-9
练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替. #include <stdio.h> int main(int argc, char const *ar ...
- Android Spinner组件的使用方法
Spinner是什么呢,其实就是我们常见的下拉框,比如: 首先,我们要创建一个Spinner,才能在Spinner中添加我们想要的元素,在xml文件中: <Spinner android:id= ...
- Python 模块搜索路径
Python 会在什么地方寻找文件来导入模块? 使用命名为 path 变量的存储在标准 sys 模块 下的一系列目录名和 ZIP 压缩文件. 你可以读取和修改这个列表.下面是在我的 Mac 上 Pyt ...
- Map的常用方法keySet()、entrySet()
Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...