tarjan——校园网(缩点,再构图)
2024-09-05 05:37:22
P2746 [USACO5.3]校园网Network of Schools
任务一:求缩完点后入度为0的点的个数(有向边)
任务二:求缩完点后入度为0和出度为0的最大值(要把图构造成强连通分量)
注意,任务二需要特判整张图是一个强联通
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=;
int pre[maxn],other[maxn],last[maxn],l;
int n;
void add(int x,int y)
{
l++;
pre[l]=last[x];
last[x]=l;
other[l]=y;
}
int dfn[maxn],low[maxn];
int cnt;
int belong[maxn],qw;
int ru[maxn];
stack<int> s;
void dfs(int x)
{
low[x]=dfn[x]=++cnt;
s.push(x);
//ru[x]=1;
for(int p=last[x];p;p=pre[p])
{
int v=other[p];
// if(v==fa) continue;
if(!dfn[v])
{
dfs(v);
low[x]=min(low[x],low[v]);
}
else if(!belong[v])
{
low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x])
{
belong[x]=++qw;
while()
{
int y=s.top();
s.pop();
//ru[y]=0;
belong[y]=qw;
if(y==x) break;
}
}
}
int ans1,ans2;
int chu[maxn];
int main()
{
scanf("%d",&n);
for(int i=,x;i<=n;i++) while(scanf("%d",&x)&&x){add(i,x);}
for(int i=;i<=n;i++)
{
if(!dfn[i])
{
dfs(i);
}
}
for(int i=;i<=n;i++)
{
for(int p=last[i];p;p=pre[p])
{
int v=other[p];
if(belong[i]!=belong[v])
{
chu[belong[i]]++;
ru[belong[v]]++;
}
}
}
for(int i=;i<=qw;i++)
{
if(!ru[i]) ans1++;
if(!chu[i]) ans2++;
}
if(qw==)
{
printf("1\n0");
return ;
}
printf("%d\n",ans1);
printf("%d",max(ans1,ans2));
return ;
}
最新文章
- select初始化添加option,通过标签给出回显值,由于回显值和初始化值option中有一个值重复,去重等问题!
- hive数据操作
- java cef3 禁止右键菜单项
- PHP mongodb AR
- Microsoft Visual Studio Ultimate 2013 with Update 3 CN+EN
- 项目乱码 GBK转UTF-8工具
- 服务器压力测试 ab
- AcmeAir安装AI探针--SaaS版
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_all_from_list(self, locator)
- Linux命令(1)-scp
- 【转】十二个移动App云测试服务盘点
- Unity 5.4大赞:HTC Vive经典The lab渲染器开源
- hdu 4662
- Ensures there will be no &#39;console is undefined&#39; errors
- java_tomcat_Server at localhost was unable to start within 45 seconds 小喵咪死活启动报错-二
- [bzoj3673] 可持久化并查集 by zky
- Core Animation 文档翻译(第三篇)
- Web前端2019面试总结
- [CF1137E]Train Car Selection[维护凸壳]
- .net core定时任务
热门文章
- Http 与 Https区别
- C语言数组不知道输入几个整数以及输入一直到为0
- ml
- Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (16944839 >; 16777216). You can change this value on the server by setting the max_allowed_packet&#39; variable.
- (详细)JAVA使用JDBC连接MySQL数据库(2)- MySQL Connectors
- Reeds-Shepp曲线和Dubins曲线
- Ubuntu的apt-get代理设置
- 【Day4】5.Request对象之Http Post请求案例分析
- python-----批量操作xml文件(新建、增、删、改、查)
- 各位大佬Python的第一部分道基础题已经整理好了,希望大家面试的时候能用的上。