求出无向图的所有边双联通分量,然后缩点就成了一颗树。

然后我们选取最大的那个边双联通分量作为根,这样我们就可以确定所有割边的方向了。

对于边双联通分量里面的边,我们随便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的更多相关文章

  1. 732F Tourist Reform

    // CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...

  2. CodeForces 732F Tourist Reform

    边双连通分量. 这题有一点构造的味道.一个有向图,经过强连通缩点之后会形成一个有向无环图. 如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图. 接下来 ...

  3. CF 732F Tourist Reform——v-SCC+dfs

    题目:http://codeforces.com/contest/732/problem/F 给无向图定向使得从每个点出发能去的点数最小值最大. SCC.点内部dfs定向.点间以siz最大的为起点反向 ...

  4. Codeforces Round #377 (Div. 2) F - Tourist Reform

    前言:关于如何求双连通分量,我们可以在tarjan搜索时标记下所有桥的位置(双连通分量(可以认为是没有桥的无向图图)即可通过删去所有桥得到),那么怎么找桥呢,对于每一条搜索到的边u->x,如果l ...

  5. Codeforces 732F. Tourist Reform (Tarjan缩点)

    题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...

  6. CF732 F Tourist Reform——边双连通分量

    题目:http://codeforces.com/contest/732/problem/F 首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位: ...

  7. 【codeforces 732F】Tourist Reform

    [题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...

  8. CF732F Tourist Reform(边双联通)

    题意 在一张有向图中,设 ri 为从点 i 出发能够到达的点的数量. 定义有向图的“改良值”为 ri 的最小值. 现给出一张无向图,要求给每条边定一个方向,使产生的有向图“改良值”最大. 输出 最大改 ...

  9. CF732F Tourist Reform[边双缩点]

    题意:给无向图每一条边定向,使得每个点可达点数$R_i$最小值尽可能大,求方案. 条件反射想到二分答案,然后看怎么检验,发现要让所有点$R_i$大于等于某一个值,首先我们关注某些特殊的子图:如果有环的 ...

随机推荐

  1. 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...

  2. 全国省市区三级联动js

    function Dsy(){ this.Items = {}; } Dsy.prototype.add = function(id,iArray){ this.Items[id] = iArray; ...

  3. CF 535c Tavas and Karafs

    Tavas and Karafs Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  4. node.js内存泄露问题记录

    先说一下.事情的来龙去脉. 公司开发一款游戏棋牌游戏,服务端的开发是IO密集型,开发的时候,考虑过使用python,java,node.js. 终于选择了node.js(node.js宣传的杀手功能. ...

  5. ObjectARX学习笔记(三十二)----怎样设置AcDbMText对齐方式

    //_T("\\pxql;") 居左 //_T("\\pxqr;") 居右 //_T("\\pxqc;") 居中 //_T("\\ ...

  6. (28)java web的hibernate使用

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  7. Tomcat启动报:invalid LOC header (bad signature)的问题

    原因:这种一般是因为项目依赖的某个jar包损坏引起的, 解决办法: 1.右键项目,选择maven,更新(update maven project) 2.通过右击项目名 ->  Run as -& ...

  8. js中数组遍历的几种方法及其区别

    参考网站: http://www.cnblogs.com/lvmh/p/6104397.html 第一种最常用的:for循环 for(j = 0; j < arr.length; j++) { ...

  9. express 中文文档

    express() 创建一个express应用程序 var express = require('express'); var app = express(); app.get('/', functi ...

  10. jquery清空div里所有input输入框的值

    $("#divId input").val("");