POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点)
ACM
题目地址:POJ 1236
题意:
给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得有向图成为一个强连通图。
分析:
跟HDU 2767 Proving Equivalences(题解)一样的题目,只是多了个问题,事实上转化成DAG后就不难考虑了,事实上仅仅要选择入度为0的点即可了。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: 1236.cpp
* Create Date: 2014-07-30 15:13:12
* Descripton: Tarjan
*/ #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include <stack>
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll; const int N = 105; vector<int> G[N];
stack<int> S;
int low[N], dfn[N], sccno[N], tclock, scccnt;
int id[N], od[N];
int n, rd; void tarjan(int u) {
low[u] = dfn[u] = ++tclock;
S.push(u); int sz = G[u].size();
repf (i, 0, sz - 1) {
int v = G[u][i];
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (!sccno[v]) {
low[u] = min(low[u], dfn[v]);
}
} if (low[u] == dfn[u]) {
scccnt++;
int v = -1;
while (v != u) {
v = S.top();
S.pop();
sccno[v] = scccnt;
}
}
} void read() {
repf (i, 1, n) {
G[i].clear();
while (scanf("%d", &rd) && rd) {
G[i].push_back(rd);
}
}
} void find_scc() {
tclock = scccnt = 0;
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(sccno, 0, sizeof(sccno)); repf (i, 1, n) {
if (!dfn[i]) {
tarjan(i);
}
}
} void solve() {
if (scccnt == 1) {
printf("1\n0\n");
return;
} memset(id, 0, sizeof(id));
memset(od, 0, sizeof(od)); repf (u, 1, n) {
int sz = G[u].size();
repf (i, 0, sz - 1) {
int v = G[u][i];
if (sccno[u] != sccno[v]) {
id[sccno[v]]++;
od[sccno[u]]++;
}
}
} int idnum = 0, odnum = 0;
repf (i, 1, scccnt) {
idnum += (id[i] == 0);
odnum += (od[i] == 0);
} printf("%d\n%d\n", idnum, max(idnum, odnum));
} int main() {
while (~scanf("%d", &n)) {
read();
find_scc();
solve();
}
return 0;
}
POJ 1236 Network of Schools(强连通 Tarjan+缩点)的更多相关文章
- 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 (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
- POJ 1236 Network of Schools (强连通分量缩点求度数)
题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...
- 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)题解
题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...
- POJ 1236 Network of Schools(tarjan求强连通分量+思维)
题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...
- POJ 1236 Network of Schools (tarjan算法+缩点)
思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...
- poj 1236 Network of Schools 【Tarjan】
题目链接:http://poj.org/problem?id=1236 题意: 本题为有向图. 需解决两个问题: 1 须要给多少个点,才干传遍全部点. 2 加多少条边,使得整个图变得强连通. 使用Ta ...
随机推荐
- php fpm start.sh
#! /bin/bash #Source function library. . /etc/init.d/functions #Check that networking is up. . /etc/ ...
- Mad Lib程序
单选框 复选框 按钮 标签 文本框的应用 #coding=utf-8 __author__ = 'minmin' from Tkinter import * class Application ...
- python 发送安全邮件
用python 写了一个发送邮件的脚本,配上host 和端口,发现一直报错: smtplib.SMTPException: No suitable authentication method foun ...
- ThinkPHP中的CURD操作
<?php //查询多条记录,返回二维数组 $result = M("admin")->select(); $result = M("admin") ...
- mysql-connector-c 安装
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local make make install
- 关于Android Launcher图标上面动态改变数字的实现
由于项目需要使用到类似小米应用商店的图标数字提示功能,谷歌百度了许多文章都没看到有真正意义上的实现(没有在国外网站上搜索),有实现在APP内部的一个ImageView上面更新数字的,当然这种太简单无非 ...
- Windows Server 2012 R2 Standard序列号
备用一个吧,免得用起来的时候找不到. NB4WH-BBBYV-3MPPC-9RCMV-46XCB
- CodeForces 235C Cyclical Quest(后缀自动机)
[题目链接] http://codeforces.com/contest/235/problem/C [题目大意] 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,圆环匹配的意思是 ...
- BZOJ 1816: [Cqoi2010]扑克牌( 二分答案 )
二分答案.. 一开始二分的初始右边界太小了然后WA,最后一气之下把它改成了INF... -------------------------------------------------------- ...
- 经典union的使用
一个用户下广告位 某一天有收入和支出 有支出不一定有收入 有收入不一定有支出 下例为按用户查询 sanhao 下的信息 支出如下: 收入如下: 按天进行查询,例如查询: 得到结果如下: 使用一 ...