【IOI 1996】 Network of Schools
【题目链接】
【算法】
对于第一问,将这个图缩点,输出出度为零的点的个数
对于第二问,同样将这个图缩点,输出入度为零、出度为零的点的个数的最大值
【代码】
#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的更多相关文章
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- 【图论】Network of Schools
[POJ1236]Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18969 Acc ...
- 【IOI 1998】 Picture
[题目链接] 点击打开链接 [算法] 线段树扫描线求周长并 [代码] #include <algorithm> #include <bitset> #include <c ...
- 【POJ 1144】 Network
[题目链接] 点击打开链接 [算法] Tarjan算法求割点 [代码] #include <algorithm> #include <bitset> #include < ...
- 【BZOJ 3732】 Network
[题目链接] 点击打开链接 [算法] 求出这个图的最小生成树,对于每次询问,用倍增法求出最近公共祖先,查询最小生成树上两点路径上的最大值 算法的正确性? 假设x和y在最小生成树中 ...
- 【IOI 1994】 The Buses
[题目链接] http://poj.org/problem?id=1167 [算法] 深度优先搜索 + 迭代加深 [代码] #include <algorithm> #include &l ...
- 【IOI 2011】Race
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2599 [算法] 点分治 [代码] #include<bits/stdc++.h ...
- 【poj1236】 Network of Schools
http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...
- 【洛谷P2746】Network of Schools
题目大意:给定一个 N 个点,M 条边的有向图,第一问求至少从多少个点出发才能遍历整个有向图,第二问求至少在这个有向图的基础上加多少条边才能使得该无向图强连通. 题解:先进行 Tarjan 缩点,得到 ...
随机推荐
- DatePickerDialog
package com.pingyijinren.helloworld.activity; import android.app.DatePickerDialog; import android.su ...
- Python中排序的灵活使用
Python中列表按指定标准排序实例 概述 本题需要先输入机器的数目和任务的数目. 在接下来的n行中每行分别包含机器的最大执行时间和机器所能执行任务的最大强度. 在接下来的n行中每行分别包含任务执行时 ...
- [Bzoj1022][SHOI2008]小约翰的游戏John(博弈论)
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2976 Solved: 1894[Submit] ...
- codeforces 938F(dp+高维前缀和)
题意: 给一个长度为n的字符串,定义$k=\floor{log_2 n}$ 一共k轮操作,第i次操作要删除当前字符串恰好长度为$2^{i-1}$的子串 问最后剩余的字符串字典序最小是多少? 分析: 首 ...
- 检测socket链接是否断开
[解决方案] 1. 发送重试,由业务完成. 因为club_l5的send接口不会保留用户发送的内容,在recv失败的情况下,用户发送的数据已经丢失,所以只能由业务进行重试. 结论: ...
- how to read openstack code: service plugin
We have learned core plugin, service plugin and extension in last post. Now let`s review: Core Plugi ...
- B/S(WEB)系统中使用Activex插件调用扫描仪实现连续扫描并上传图像(IE文件扫描并自动上传)
IE浏览器下使用Activex插件调用客户端扫描仪扫描文件并山传,可以将纸质档案(如合同.文件.资料等)扫描并将扫描图像保存到服务器,可以用于合同管理.档案管理等. 通过插件方式调用扫描仪扫描并获取图 ...
- mysql资料整理
###SQL的语言分类 1.DQL(Data Query Language):数据查询语言 select 2.DML(Data Manipulate Language):数据操作语言 insert . ...
- the attribute buffer size is too small 解决方法
在进行查询的时候引发The attribute buffer size is too small错误解决 http://bbs.esrichina-bj.cn/esri/viewthread.php? ...
- 【转】AOP
原文:http://blog.csdn.net/zhoudaxia/article/details/38502347 .---------------------------------------- ...