原题

这是一道强连通分量板子题。

显然subtask1 是要输出入度为0的点的个数

而subtask2,我们考虑一下最优一定是把一个出度为零的点连到入度为零的点上,这样我们要输出的就是max(出度为零的个数,入度为零的个数)

另外,如果只有一个强连通分量,那么subtask2答案是0

#include<cstdio>
#include<algorithm>
#include<stack>
#define N 10010
#define M 50010
using namespace std;
int n,m,head[N],dfn[N],low[N],cnt=1,t,sum,bel[N],out[N],num[N],in[N],ans,as;
bool instk[N];
stack <int> stk;
struct hhh
{
int to,next;
}edge[M]; int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
} void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Tarjan(int x)
{
dfn[x]=low[x]=++t;
stk.push(x);
instk[x]=1;
int v;
for (int i=head[x];i;i=edge[i].next)
{
v=edge[i].to;
if (!dfn[v])
{
Tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (instk[v]) low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x])
{
sum++;
do
{
v=stk.top();
stk.pop();
instk[v]=0;
bel[v]=sum;
num[sum]++;
}while(v!=x);
}
} int main()
{
n=read();
for (int i=1,a;i<=n;i++)
{
a=read();
while(a!=0)
{
add(i,a);
a=read();
}
}
for (int i=1;i<=n;i++)
if (!dfn[i]) Tarjan(i);
for (int i=1;i<=n;i++)
for (int j=head[i],v;j;j=edge[j].next)
{
v=edge[j].to;
if (bel[i]!=bel[v]) in[bel[v]]++,out[bel[i]]++;
}
for (int i=1;i<=sum;i++)
{
if (!in[i]) ans++;
if (!out[i]) as++;
}
printf("%d\n",ans);
if (sum==1) printf("0\n");
else printf("%d",max(ans,as));
return 0;
}

[poj] 1236 networks of schools的更多相关文章

  1. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  2. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  3. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  4. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  5. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  7. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  8. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  9. POJ 1236 Network of Schools - 缩点

    POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...

随机推荐

  1. 利用bootstrap制作滚动监听

    滚动监听(Scrollspy)插件,即自动更新导航插件,会根据滚动条的位置自动更新对应的导航目标. 在这里,需要引入三个文件:bootstrap.min.css      jquery-3.3.1.j ...

  2. Cluster - HA -keepalived

    学习须知 VRRP:https://www.cnblogs.com/aftree/p/9376427.html 需求 集群中,对后端RealServer的状态做检测,实现自动化问题检测和问题自动处理机 ...

  3. Target runtime Apache Tomcat v8.5 is not defined.

    Target runtime Apache Tomcat v8.5(或者其它版本) is not defined. 这个错误通常是在从文件夹中导入别人的项目的时候发生,因为 在 .setting 中有 ...

  4. nuxt generate静态化后回退问题

    之前线上的项目是nuxt build后的项目发布在服务器上,pm2来管理node的进程,nuxt还是运行在node的环境里. 这个方案用了半年左右,访问速度什么的确实很快,pm2管理下的node在wi ...

  5. vue 点击当前元素添加class 去掉兄弟的class

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. CF961E Tufurama 树状数组

    E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...

  7. POJ 2891 中国剩余定理(不互素)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 17877 ...

  8. B1081 检查密码 (15分)

    B1081 检查密码 (15分) 本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母 ...

  9. poj 1759 二分搜索

    题意:N个等差数列,初项X_i,末项Y_i,公差Z_i,求出现奇数次的数? 思路: 因为只有一个数出现的次数为奇数个 假设 第二个数字的个数为 奇数个,其余全部都是偶数个 ,累计出现的次数 a1偶数 ...

  10. mysql学习第二天函数

    -- 1.绝对值 select abs(-1)from dual -- 2.求平方根select sqrt(6)from dual -- 3.圆周率select pi()from dual -- 4. ...