E - Reachability from the Capital
思路:首先使用tarjan算法进行染色,缩点。到最后判断 缩减后 入度为零的不含有城市中心的强连通子图的个数就可以了!!!
原因,染完色之后,如果有入度为0的强连通子图,那么这个点就可能符合,然后吧满足这些条件的记录一下。注意,在累加 的时候,不能将入度为0的含有城市中心的强连通子图计算在内,因为这个图中,有城市中心的强连通图中,这个图中的其他所有的点都能由城市中心到达,所以不用累加。
代码如下:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
# define maxn
vector<int >wakaka[maxn];
map<int,int>p;
stack<int>q;
int dfn[maxn],low[maxn],vis[maxn];
int out[maxn],cnt[maxn],color[maxn],in[maxn];
int num,ans;
void tarjan(int u)
{
vis[u]=;
q.push(u);
low[u]=dfn[u]=++num;
int len=wakaka[u].size();
for(int i=; i<len; i++)
{
int v=wakaka[u][i];
if(vis[v]==)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
if(vis[v]==)
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
ans++;
int top;
do
{
top=q.top();
q.pop();
vis[top]=-;
color[top]=ans;
}
while(top!=u);
}
}
int main()
{
int n,m,t;
while(cin>>n>>m>>t)
{
num=ans=;
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt));
memset(out,,sizeof(out));
memset(in,,sizeof(in));
memset(color,,sizeof(color));
while(!q.empty())q.pop();
for(int i=; i<=n; i++)
{
wakaka[i].clear();
}
p.clear();
for(int i=; i<=m; i++)
{
int u,v;
cin>>u>>v;
p[u]++;
p[v]++;
wakaka[u].push_back(v);
}
for(int i=; i<=n; i++)
{
if(vis[i]==)
tarjan(i);
}
//cout<<color[1]<<endl<<color[2]<<endl;
for(int i=; i<=n; i++)
{
int len=wakaka[i].size();
for(int j=; j<len; j++)
{
if(color[i]!=color[wakaka[i][j]])
{
in[color[i]]++;
out[color[wakaka[i][j]]]++;
}
}
cnt[color[i]]++;
}
int t1=,t2=;
for(int i=; i<=ans; i++)
{
if(in[i]==)
t1++;
if(out[i]==&&i!=color[t])
{
t2++;
}
}
cout<<t2<<endl;
//else {
//cout<<max(t1,t2)<<endl;
//}
//}
}
return ;
}
E - Reachability from the Capital的更多相关文章
- E. Reachability from the Capital dfs暴力
E. Reachability from the Capital 这个题目就是给你一个有向图,给你起点,问增加多少条边让这个图变成一个连通图. 这个因为n只有5000m只有5000 所以可以暴力枚举这 ...
- Reachability from the Capital CodeForces - 999E (强连通)
There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in Berla ...
- Reachability from the Capital
题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...
- CF999E Reachability from the Capital来自首都的可达性
题目大意: 有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达 这题一看就是一个缩点啊 其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量( ...
- Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)
题意: 问至少加几条边 能使点s可以到达所有的点 解析: 无向图的连通分量意义就是 在这个连通分量里 没两个点之间至少有一条可以相互到达的路径 所以 我们符合这种关系的点放在一起, 由s向这些点的任 ...
- Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)
题目链接:http://codeforces.com/contest/999/problem/E 题目: 题意:给你n个城市,m条单向边,问你需要加多少条边才能使得从首都s出发能到达任意一个城市. 思 ...
- [CF999E]Reachability from the Capital
题目大意:有一个$n$个点$m$条边的有向图,起点$S$,要求你添加最少的边使得$S$可以到达所有点 题解:缩点,答案就是没有入边的强连通分量个数,注意,如果起点$S$所在的强连通块没有入边则不计入答 ...
- E. Reachability from the Capital(tarjan+dfs)
求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...
- codeforces#999 E. Reachability from the Capital(图论加边)
题目链接: https://codeforces.com/contest/999/problem/E 题意: 在有向图中加边,让$S$点可以到达所有点 数据范围: $ 1 \leq n \leq 50 ...
随机推荐
- Prometheus-自定义Node_Exporter
标量(Scalar):一个浮点型的数字值 标量只有一个数字,没有时序. 需要注意的是,当使用表达式count(http_requests_total),返回的数据类型,依然是瞬时向量.用户可以通过内置 ...
- influxDB和grafana
influxdb启动服务 sudo service influxdb start 登录数据库 influx 在influxDB中,measurement相当于sql中的table, 插入measure ...
- 2017-12-15python全栈9期第二天第五节之while else的用法二当不被break打断时else内容的结果会被打印
#!/user/bin/python# -*- coding:utf-8 -*-count = 0while count <=5 : count += 1 if count == 3 : pas ...
- ubuntu14.04安装tun/tap网络设备
14.04的系统默认是没有tun设备的,所以需要通过在内核中编译时勾选此设备.接下来分步来介绍如何安装tun设备. 一.更新ubuntu桌面版源: sudo gedit /etc/apt/source ...
- OS + Linux nmon / nmon analyser / nmon_analyser_v52_1.zip
s nmon_analyser_v52_1.zip https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Pow ...
- Nginx简单手册
Nginx 变量 变量名 注解 $arg_name 请求中的的参数名,即“?”后面的arg_name=arg_value形式的arg_name $args 请求中的参数值 $binary_remot ...
- python Bootstarp框架和inconfont、font-awesome使用
http://www.bootcss.com/ http://www.runoob.com/bootstrap/bootstrap-panels.html 查找基本的没问题 https://www. ...
- Silver 操作Cookie
public class CookiesUtils { public static void SetCookie(String key, String value) { SetCookie(key, ...
- Kafka技术内幕 读书笔记之(二) 生产者——服务端网络连接
KafkaServer是Kafka服务端的主类, KafkaServer中和网络层有关的服务组件包括 SocketServer.KafkaApis 和 KafkaRequestHandlerPool后 ...
- C#设计模式(6)——原型模式
1.原型模式介绍 在软件系统开发中,有时候会遇到这样的情况:我们需要用到多个相同实例,最简单直接的方法是通过多次调用new方法来创建相同的实例.如下: Person person=}; Person ...