【题目链接】

点击打开链接

【算法】

对于第一问,将这个图缩点,输出出度为零的点的个数

对于第二问,同样将这个图缩点,输出入度为零、出度为零的点的个数的最大值

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 10010 struct Edge
{
int to,nxt;
} e[MAXN]; int i,x,n,tot,timer,top,cnt;
int head[MAXN],dfn[MAXN],low[MAXN],stk[MAXN],belong[MAXN];
bool instack[MAXN]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
}
inline void add(int u,int v)
{
tot++;
e[tot] = (Edge){v,head[u]};
head[u] = tot;
}
inline void tarjan(int u)
{
int i,v;
dfn[u] = low[u] = ++timer;
instack[u] = true;
stk[++top] = u;
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u],low[v]);
} else
{
if (instack[v])
low[u] = min(low[u],dfn[v]);
}
}
if (dfn[u] == low[u])
{
cnt++;
while (stk[top+] != u)
{
instack[stk[top]] = false;
belong[stk[top]] = cnt;
top--;
}
}
}
inline void solve()
{
int i,j,s1 = ,s2 = ;
static int in[MAXN],out[MAXN];
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
cnt = timer = ;
for (i = ; i <= n; i++)
{
if (!dfn[i])
tarjan(i);
}
for (i = ; i <= n; i++)
{
for (j = head[i]; j; j = e[j].nxt)
{
if (belong[i] != belong[e[j].to])
{
in[belong[e[j].to]]++;
out[belong[i]]++;
}
}
}
for (i = ; i <= cnt; i++)
{
if (!in[i]) s1++;
if (!out[i]) s2++;
}
if (cnt == ) printf("%d\n%d\n",,);
else printf("%d\n%d\n",s1,max(s1,s2));
} int main() { while (scanf("%d",&n) != EOF)
{
tot = ;
for (i = ; i <= n; i++) head[i] = ;
for (i = ; i <= n; i++)
{
while (scanf("%d",&x) && x) add(i,x);
}
solve();
} return ; }

【IOI 1996】 Network of Schools的更多相关文章

  1. 【POJ 3694】 Network(割边&lt;桥&gt;+LCA)

    [POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7971 ...

  2. 【图论】Network of Schools

    [POJ1236]Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18969   Acc ...

  3. 【IOI 1998】 Picture

    [题目链接] 点击打开链接 [算法] 线段树扫描线求周长并 [代码] #include <algorithm> #include <bitset> #include <c ...

  4. 【POJ 1144】 Network

    [题目链接] 点击打开链接 [算法] Tarjan算法求割点 [代码] #include <algorithm> #include <bitset> #include < ...

  5. 【BZOJ 3732】 Network

    [题目链接] 点击打开链接 [算法] 求出这个图的最小生成树,对于每次询问,用倍增法求出最近公共祖先,查询最小生成树上两点路径上的最大值 算法的正确性?            假设x和y在最小生成树中 ...

  6. 【IOI 1994】 The Buses

    [题目链接] http://poj.org/problem?id=1167 [算法] 深度优先搜索 + 迭代加深 [代码] #include <algorithm> #include &l ...

  7. 【IOI 2011】Race

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2599 [算法] 点分治 [代码] #include<bits/stdc++.h ...

  8. 【poj1236】 Network of Schools

    http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...

  9. 【洛谷P2746】Network of Schools

    题目大意:给定一个 N 个点,M 条边的有向图,第一问求至少从多少个点出发才能遍历整个有向图,第二问求至少在这个有向图的基础上加多少条边才能使得该无向图强连通. 题解:先进行 Tarjan 缩点,得到 ...

随机推荐

  1. hexo干货系列:(七)hexo安装统计插件

    前言 前面介绍了如何让百度和谷歌收录我们的博客,那如何查看自己的博客每天被多少人访问呢~ 这里我介绍下hexo中如何使用统计插件,每天看到自己的博客访问量越来越高也是一种享受. 正文 开启统计功能 我 ...

  2. POJ 2115 C Looooops【数论】

    很容易看出来一个同余式,说到底是解一个线性同余方程,计算机解通常有拓展欧几里得和欧拉定理两种算法,参照去年的NOIP水题,问题是这题数据范围是2^32所以要int64 TAT #include< ...

  3. bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树

    Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1951  Solved: 746[Submi ...

  4. 【BZOJ2006】超级钢琴(RMQ,priority_queue)

    题意: 思路: 用三元组(i, l, r)表示右端点为i,左端点在[l, r]之间和最大的区间([l, r]保证是对于i可行右端点区间的一个子区间),我们用堆维护一些这样的三元组. 堆中初始的元素为每 ...

  5. msp430项目编程00

    msp430中项目---LED数码管显示 1.数码管介绍 2.代码(直接使用引脚驱动) 3.代码(使用译码器驱动) 4.项目总结 msp430项目编程 msp430入门学习

  6. HDU——2444 The Accomodation of Students

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. HDU 1024 【DP】

    题意: 给n个数将其分成连续的m组,使得这些组的数加和最大,组与组之间可以空数. /* dp[i][j]表示将前j个数分成i个组最大值 状态转移方程是: dp[i][j]=max(dp[i-1][0. ...

  8. MySQL事务及Spring事务管理

    事务,是在数据库中用于保证数据正确性的一种机制,涉及到很多概念以及不同的情况,这里做一个总结 相关概念 事务四特性(ACID) 原子性(Atomicity,或称不可分割性):要么全部完成或者全部不完成 ...

  9. eclipse中maven插件上传项目jar包到私服

    我们知道,每一个公司都会有自己的工具包或公共包.这样的包就能够上传到公司的maven私服,就不用每一个人都去同步开发包了. 那么,怎么把本地项目打包并公布到私服呢?依照例如以下步骤就能够轻松完毕. 1 ...

  10. Capture and report JavaScript errors with window.onerror

    原文:https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html onerror is a sp ...