Codeforces732F Tourist Reform
求出无向图的所有边双联通分量,然后缩点就成了一颗树。
然后我们选取最大的那个边双联通分量作为根,这样我们就可以确定所有割边的方向了。
对于边双联通分量里面的边,我们随便dfs一下就可以把它变成强连通分量,方向也就确定了。
#include <bits/stdc++.h>
using namespace std; vector<int> G[];
vector<pair<int, int>> G2[];
set<pair<int, int>> bridge;
set<pair<int, int>> ansset;
pair<int, int> old[];
int vis[];
int dfn[];
int low[];
int tag[]; void get_bridge(int cur, int father, int dep)
{
vis[cur] = ;
dfn[cur] = low[cur] = dep;
int children = ;
for (auto to : G[cur])
{
if (to != father && vis[to] == )
{
if (dfn[to] < low[cur])
low[cur] = dfn[to];
}
if (vis[to] == )
{
get_bridge(to, cur, dep + );
children++;
if (low[to] < low[cur])
low[cur] = low[to];
if (low[to] > dfn[cur])
bridge.insert({cur, to}), bridge.insert({to, cur});
}
}
vis[cur] = ;
} int dfs(int u, int tot)
{
int cnt = ;
vis[u] = true;
tag[u] = tot;
for (auto to : G[u])
{
if (ansset.find({u, to}) == ansset.end() && ansset.find({to, u}) == ansset.end() && bridge.find({u, to}) == bridge.end())
{
ansset.insert({u, to});
if (!vis[to])
cnt += dfs(to, tot);
}
}
return cnt;
} void dfs2(int cur, int fa)
{
for (auto e : G2[cur])
{
int nxt = tag[e.second];
if (nxt != fa)
{
ansset.insert({e.second, e.first});
dfs2(nxt, cur);
}
}
} int main()
{
int n, m;
scanf("%d%d", &n, &m);
int u, v;
for (int i = ; i < m; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
old[i] = {u, v};
}
get_bridge(, -, );
memset(vis, , sizeof(vis));
int ans = ;
int tot = ;
int idx = ;
for (int i = ; i <= n; i++)
{
if (!vis[i])
{
int siz = dfs(i, ++tot);
if (siz > ans)
ans = siz, idx = tot;
}
}
for (auto e : bridge)
G2[tag[e.first]].push_back(e);
dfs2(idx, -);
printf("%d\n", ans);
for (int i = ; i < m; i++)
{
if (ansset.find(old[i]) == ansset.end())
printf("%d %d\n", old[i].second, old[i].first);
else
printf("%d %d\n", old[i].first, old[i].second);
}
return ;
}
Codeforces732F Tourist Reform的更多相关文章
- 732F Tourist Reform
// CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...
- CodeForces 732F Tourist Reform
边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...
- CF 732F Tourist Reform——v-SCC+dfs
题目:http://codeforces.com/contest/732/problem/F 给无向图定向使得从每个点出发能去的点数最小值最大. SCC.点内部dfs定向.点间以siz最大的为起点反向 ...
- Codeforces Round #377 (Div. 2) F - Tourist Reform
前言:关于如何求双连通分量,我们可以在tarjan搜索时标记下所有桥的位置(双连通分量(可以认为是没有桥的无向图图)即可通过删去所有桥得到),那么怎么找桥呢,对于每一条搜索到的边u->x,如果l ...
- Codeforces 732F. Tourist Reform (Tarjan缩点)
题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...
- CF732 F Tourist Reform——边双连通分量
题目:http://codeforces.com/contest/732/problem/F 首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位: ...
- 【codeforces 732F】Tourist Reform
[题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...
- CF732F Tourist Reform(边双联通)
题意 在一张有向图中,设 ri 为从点 i 出发能够到达的点的数量. 定义有向图的“改良值”为 ri 的最小值. 现给出一张无向图,要求给每条边定一个方向,使产生的有向图“改良值”最大. 输出 最大改 ...
- CF732F Tourist Reform[边双缩点]
题意:给无向图每一条边定向,使得每个点可达点数$R_i$最小值尽可能大,求方案. 条件反射想到二分答案,然后看怎么检验,发现要让所有点$R_i$大于等于某一个值,首先我们关注某些特殊的子图:如果有环的 ...
随机推荐
- Spring Boot 使用Java代码创建Bean并注冊到Spring中
从 Spring3.0 開始,添加了一种新的途经来配置Bean Definition,这就是通过 Java Code 配置 Bean Definition. 与Xml和Annotation两种配置方式 ...
- SpringBoot学习之文件结构和配置文件
Springboot文件结构和配置文件 转载:http://www.zslin.com/web/article/detail/11 项目文件结构 新建的Springboot项目的文件结构如下: |-c ...
- jQuery源代码框架思路
開始计划时间读源代码,第一节jQuery框架阅读思路整理 (function(){ jQuery = function(){}; jQuery一些变量和函数和给jQuery对象加入一些方法和属性 ex ...
- oracle user locke
1:管理员登录 sqlplus sys/pwd as sysdba sql->alter user jd account unlock; commit; SQL> password new ...
- 新拿到的app跑的时候出现问题
连接器链接失败,是因为对应类build后的中间产物.o文件没有生成,对应架构下,用模拟器跑的,很可能是因为无法编译产出x86,或i386架构的中间产物,所以linker链接转换机器码时候找不到对应的中 ...
- firefox 45 版本
在做项目的时候,发现45版本的firefox浏览器.声明函数要放在调用者的上方.而firefox的47,48版本则没有这种情况发生.
- HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法
CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中,背景图片的定位方法有3种: 1)关键字:background-position: top left; 2)像素:bac ...
- 转:Oracle客户端NLS_LANG参数的设置详解
原文:http://database.51cto.com/art/201107/279361.htm 我们知道,Oracle客户端语言支持可以通过NLS_LANG参数的设置来完成,不同的系统平台上NL ...
- 666 专题五 AC自动机
Problem A.Keywords Search d.n个关键字,1段描述,求描述中出现了多少关键字 s. c. /* ac自动机模板 n个关键字,1段描述,求描述中出现了多少关键字 */ #inc ...
- I.MX6 u-boot 2009 lvds hdmi lcd 补丁
/************************************************************************* * I.MX6 u-boot 2009 lvds ...