poj 1236 Network of Schools 【Tarjan】
题目链接:http://poj.org/problem?id=1236
题意:
本题为有向图。
需解决两个问题:
1 须要给多少个点,才干传遍全部点。
2 加多少条边,使得整个图变得强连通。
使用Tarjan进行缩点,得到一个SCC图、
这个图有多少个入度为0的,多少个出度为0的。
如果有n个入度为0,m个出度为0
那么第一个答案就是n,第二个答案是max(n,m)
代码:
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <time.h>
using namespace std;
const int MAXN = 20010;
const int MAXM = 50010;
struct Edge
{
int to, next;
}edge[MAXM];
int head[MAXM], tot;
int Low[MAXN], Dfn[MAXN], Stack[MAXN], Belong[MAXN];//Belong的值为 1 ~ scc
int Index, top;
int scc;//强连通个数
bool Instack[MAXN];
int num[MAXN];//各个强连通包括的点的个数
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
Low[u] = Dfn[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if (!Dfn[v])
{
Tarjan(v);
if (Low[u] > Low[v])
Low[u] = Low[v];
}
else if (Instack[v] && Low[u] > Dfn[v])
Low[u] = Dfn[v];
}
if (Low[u] == Dfn[u])
{
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
} while (v != u);
}
}
int in[MAXN], out[MAXN];
void solve(int N)
{
memset(Dfn,0,sizeof(Dfn));
memset(Instack,false,sizeof(Instack));
memset(num,0,sizeof(num));
Index = scc = top = 0;
for (int i = 1; i <= N; i++)
{
if (!Dfn[i])
Tarjan(i);
}
if (scc == 1)
{
printf("1\n0\n");
return;
}
for (int i = 1; i <= scc; i++)
in[i] = out[i] = 0;
for (int u = 1; u <= N; u++)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (Belong[u] != Belong[v])
{
in[Belong[v]]++;
out[Belong[u]]++;
}
}
}
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= scc; i++)
{
if (in[i] == 0) ans1++;
if (out[i] == 0) ans2++;
}
//printf("%d\n",scc);
printf("%d\n%d\n",ans1,max(ans1,ans2));
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
int main()
{
int n;
int u, v;
while (~scanf("%d", &n))
{
init();
for (int i = 1; i <= n; i++)
{
while (~scanf("%d", &u) && u)
{
addedge(i,u);
}
}
solve(n);
}
return 0;
}
poj 1236 Network of Schools 【Tarjan】的更多相关文章
- POJ 1236 Network Of Schools 【Targan】+【缩点】
<题目链接> 题目大意: 有N个学校,每个学校之间单向可以发送软件,现在给你一些学校之间的收发关系.问你下面两个问题:至少要给多少个学校发送软件才能使得最终所有学校都收到软件:至少要多加多 ...
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13800 Accepted: 55 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- POJ 1236 Network of Schools (Tarjan)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22745 Accepted: 89 ...
- POJ 1236 Network of Schools(tarjan)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- POJ 1236 Network of Schools(tarjan算法 + LCA)
这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...
- POJ 1236 Network of Schools(tarjan求强连通分量+思维)
题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...
随机推荐
- Get buck-boost performance from a boost regulator
The SEPIC (single-ended, primary-inductance-converter) topology is generally a good choice for volta ...
- windows样式(style)参考
[WTL] 窗体Style的含义比较有用的设置:(1)WS_POPUP:弹出式窗体,没有标题栏!WS_BORDER:细的黑边框WS_OVERLAPPED:一般的有标题栏的窗体,见多了.WS_CHILD ...
- How to create a Maven web app and deploy to Tomcat - fast
原文地址: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.html Procedure Prerequisites and Assu ...
- Python任务调度模块 – APScheduler。动态修改调度时间间隔
APScheduler可以把调度任务放到内存里,也可以把任务放到数据库里,那么如何交互式修改定时任务的执行时间间隔或者下次执行时间呢? 方案一:把定时任务放到数据库里,修改数据库里任务的调度时间 方案 ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- [Android开发常见问题-16] FragmentActivity cannot be resolve to a type
只需要将android-support-v4.jar放到自己的libs目录下即可. 如果ADT版本比较低,则放到libs之后还需要按照下图导入这个jar包.
- #pragma详细解释(一)
#pragma详细解释 #pragma详细解释(一) 2010-04-18 14:21:00| 分类: 默认分类 | 标签: |字号大中小订阅 在#Pragma是预处理指令它的作用是设定编 ...
- java设计模式3--单例模式(Singleton)
本文地址:http://www.cnblogs.com/archimedes/p/java-singleton-pattern.html,转载请注明源地址. 单例模式 保证一个类仅有一个实例,并提供一 ...
- C# 动态装载 DLL
C# 动态装载 DLL LoadDllTool.cs 如下: using System; using System.Collections.Generic; using System.Text; us ...
- matlab中的size(),length(),ndims()函数的使用方法
1.size()使用方法: size(a)表示矩阵每一个维度的长度 比方size([1 2 3;4 5 6]) 等于[2 3]: 表示他有2行3列. size([1 2 3]) 等于[1 3]: 表示 ...