POJ1236 tarjan
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19613 | Accepted: 7725 |
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
Source
思路:求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。
缩点以后,显然入度为0的点的个数就是第一问的答案。
然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。
代码:
#include"bits/stdc++.h" #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e4 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int low[N], dfn[N],head[N],beg[N];
bool ins[N];
int in[N],out[N];
int n;
int cnt, id, num;
stack<int> s; struct P {
int to, nxt;
} e[ * N]; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++id; ins[u]=;
s.push(u);//入栈
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
int v;
do{
v=s.top();s.pop();
beg[v]=num;//缩点
ins[v]=;
}while(u!=v);
num++;
}
} void init() {
memset(ins, , sizeof(ins));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(beg, , sizeof(beg));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
while(!s.empty()) s.pop();
cnt = id = num = ;
}
int main() {
while (scanf("%d",&n) != EOF) {
init();
for(int i=;i<=n;i++){
int x;
while(scanf("%d",&x)&&x) add(i,x);
}
for (int i = ; i <= n; i++) if (!dfn[i]) tarjan(i);
for (int i = ; i <= n; i++) {
for (int j = head[i]; j != -; j = e[j].nxt) {
int v=e[j].to;
if(beg[i]!=beg[v]) out[beg[i]]++,in[beg[v]]++;
}
}
int I=,O=;
for(int i=;i<num;i++){
if(!in[i]) I++;
if(!out[i]) O++;
}
if(num==) printf("1\n0");
else pi(I),pi(max(I,O));
}
return ;
}
POJ1236 tarjan的更多相关文章
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- poj1236 Tarjan算法模板 详解
思想: 做一遍DFS,用dfn[i]表示编号为i的节点在DFS过程中的访问序号(也可以叫做开始时间)用low[i]表示i节点DFS过程中i的下方节点所能到达的开始时间最早的节点的开始时间.初始时dfn ...
- 最近切的两题SCC的tarjan POJ1236 POJ2186
两题都是水题,1236第一问求缩点后入度为0的点数,第二问即至少添加多少条边使全图强连通,属于经典做法,具体可以看白书 POJ2186即求缩点后出度为0的那个唯一的点所包含的点数(即SCC里有多少点) ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- poj1236 Network of Schools【强连通分量(tarjan)缩点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html ---by 墨染之樱花 [题目链接]http://poj.org/pr ...
- POJ1236【Tarjan+缩点】
题目大意:有向关系体现在电脑可以通过网络单向的传输文件,并规定一旦有电脑存在该文件,那么所有它能传输的电脑就能在第一时间得到这个文件,题目有两个问题,第一个是最少向网络中的几台电脑投放文件,能使得整个 ...
- poj1236/luogu2746 Network of Schools (tarjan)
tarjan缩点后,第一问答案显然是入度为零的点得个数第二问:考虑到 没有入度或出度为0的点 的图强连通, 所以答案就是max{入度为零的个数,出度为零的个数} (把出度为零的连到入度为零的点,然后剩 ...
- POJ1236:Network of Schools(tarjan+缩点)?
题目: http://poj.org/problem?id=1236 [题意] N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1 ...
- POJ1236学校网络——tarjan
题目:http://poj.org/problem?id=1236 Tarjan+缩点.温习一下Tarjan的写法. 1.在缩点后的TAG中,有几个联通块等价于有几个入度为0的点! 2.把它们都联通相 ...
随机推荐
- ul标签在FF中默认只有padding值(即:padding-left:40px)
- mysql导入大量数据时报MySQL server has gone away错误的解决办法
https://blog.csdn.net/eric520zenobia/article/details/77619469
- Python新式类 单例模式与作用域(四)
1 新式类与旧式类 新式类拥有经典类的全部特性之外,还有一些新的特性,比如 __init__发生变化,新增了静态方法__new__,python3目前都采用新式类,新式类是广度优先,旧式类是深度优先 ...
- 文件读取方法(FileHelpers) z
using System; using System.Collections.Generic; using System.Linq; using System.Text; using FileHelp ...
- elenium2学习(十六)-- 富文本(自动发帖)
前言 富文本编辑框是做web自动化最常见的场景,有很多小伙伴遇到了不知道无从下手,本篇以博客园的编辑器为例,解决如何定位富文本,输入文本内容 一.加载配置 1.打开博客园写随笔,首先需要登录,这里为了 ...
- when create a table,then show error ora-00952 tablespace tsb_1 not exist
QUESTION:When create a table,then show error ora-00952 tablespace tsb_1 not exist. STEP: 1.select us ...
- 细说new与malloc的10点区别(转载)
原地址https://www.cnblogs.com/QG-whz/p/5140930.html#_label1_0 new与malloc的10点区别 1. 申请的内存所在位置 new操作符从自由存储 ...
- 使用Sleep方法延迟时间
实现效果: 关键知识:(线程的定义) 实现代码: private void Form1_Load(object sender, EventArgs e) { Thread show = new Thr ...
- springMVC+thymeleaf form表单提交前后台数据传递
后端: @RequestMapping(value = "/add", method=RequestMethod.POST) public String save(@ModelAt ...
- 手机验证码免费10条\java、C#、html....
使用互亿无线短信接口网址:http://www.ihuyi.com/. 首先第一步,进行注册 第二步:注册成功后进来的页面 第三步:实名认证 第四步:个人信息 等待认证成功后才能继续操作 第五步:进行 ...