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 ...
随机推荐
- 基础篇(2):c++顺序结构程序设计
一个程序最基本的结构莫过于3种:顺序,选择,循环.这篇讲讲顺序结构. c++语言的运算符与表达式数量之多,在高级语言中是少见的,也使得它的语言功能十分完善. c++的运算符有单目与双目之分(作用于一个 ...
- hive的常用HQL语句
1.过滤条件 where .limit. distinct. between and . null. is not nullselect * from emp where sal > 3000; ...
- java+Mysql大数据的一些优化技巧
众所周知,java在处理数据量比较大的时候,加载到内存必然会导致内存溢出,而在一些数据处理中我们不得不去处理海量数据,在做数据处理中,我们常见的手段是分解,压缩,并行,临时文件等方法; 例如,我们要将 ...
- stark组件前戏(2)之单例模式
单,一个. 例,实例.对象. 通过利用Python模块导入的特性:在Python中,如果已经导入过的文件再被重新导入时候,python不会重新解释一遍,而是选择从内容中直接将原来导入的值拿来用. ...
- poj 1258 建光迁问题 最小生成树
题意:给全村建光纤,求花费最小 思路:最小生成树,树相对于图来说就是没有环 m用来存图 v判断是否访问 low用来存两点间的最短距离 给low赋值 for(i=1;i<=n;i++){if(i ...
- (HTML)写导航感悟
代码要规范,路径要写全 如: .div1 ul li a:link { text-decoration: none; color: white; } .div1 ul li a:visited { t ...
- 重写BaseAdapter实现ListView
public class BaseAdapterActivity extends BaseActivity { private ListView base_adapter_listView; priv ...
- DiyCode开源项目 AboutActivity分析
1.首先看一下效果 这是手机上显示的效果: 1.1首先是一个标题栏,左侧一个左箭头,然后一个图标. 1.2然后下方是一个可以滑动的页面. 1.3分成了7个部分. 1.4DiyCode的图标. 1.5然 ...
- linux下编译运行TIGL Viewer步骤
linux下编译运行TIGL Viewer步骤(仅为了正确编译安装的话直接跳到步骤3) 1. linux发行版选择:由于linux发行版众多,不同版本包含的库版本可能存在差别,因此需要选择正确的版本. ...
- 使用android-junit-report.jar导出单元测试报告
Android在使用脚本编译和测试时,使用默认的testrunner不会输出文件类型的单元测试报告,每次只能分析logcat的无法直观的看到单元测试结果和报告,这给编写自动化脚本带来了不少麻烦,虽然可 ...