题目:http://codeforces.com/contest/732/problem/F

首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位;

然后对于缩点之后的图,找到 siz 最大的点作为根 dfs,再连反边,那么只有 siz 最大的那个点只能走到自己内部,就可以使答案最大;

结构体要开得精细一点,防止爆空间?还是什么奇奇怪怪的错误之类的...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=4e5+;
int n,m,hd[maxn],ct=,cnt=,dfn[maxn],low[maxn],tim,col[maxn],cr,siz[maxn];
int sta[maxn],top,head[maxn];
bool vis[maxn];
//struct N{
// int to,nxt,bh,u,v,f;
// N(int t=0,int n=0,int b=0):to(t),nxt(n),bh(b) {}
//}ed[maxn<<1],e[maxn],edge[maxn<<1];//这样写会爆空间?!
struct N{
int to,nxt,bh;
N(int t=,int n=,int b=):to(t),nxt(n),bh(b) {}
}ed[maxn<<],edge[maxn<<];
struct E{int u,v,f;}e[maxn];
void add(int x,int y,int b){ed[++ct]=N(y,hd[x],b); hd[x]=ct;}
void add2(int x,int y,int b){edge[++cnt]=N(y,head[x],b); head[x]=cnt;}
void tarjan(int x,int f)
{
dfn[x]=low[x]=++tim; vis[x]=; sta[++top]=x;
for(int i=hd[x];i;i=ed[i].nxt)
{
int u=ed[i].to;
if(u==f)continue;
if(!dfn[u]) tarjan(u,x),low[x]=min(low[x],low[u]);
else if(vis[u]) low[x]=min(low[x],dfn[u]);
}
if(dfn[x]==low[x])
{
cr++;
while(sta[top]!=x)
{
int y=sta[top]; top--;
vis[y]=; col[y]=cr; siz[cr]++;
}
top--; vis[x]=; col[x]=cr; siz[cr]++;
}
}
void dfs(int x)
{
vis[x]=;
for(int i=head[x],u,eg;i;i=edge[i].nxt)
{
if(vis[u=edge[i].to])continue;
if(x==col[e[eg=edge[i].bh].u]) e[eg].f=;
else e[eg].f=;
dfs(u);
}
}
void dfs2(int x)
{
vis[x]=;
for(int i=hd[x],u,eg;i;i=ed[i].nxt)
{
u=ed[i].to;
if(col[u]!=col[x])continue;
if(x==e[eg=ed[i].bh].u) e[eg].f=;
else e[eg].f=;
if(!vis[u])dfs2(u);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&e[i].u,&e[i].v);
add(e[i].u,e[i].v,i); add(e[i].v,e[i].u,i);
}
for(int i=;i<=n;i++) if(!dfn[i])tarjan(i,);
memset(vis,,sizeof vis);
for(int i=;i<=n;i++) if(!vis[i])dfs2(i);
for(int i=,u,v;i<=ct;i++)
if((u=col[ed[i].to])!=(v=col[ed[i^].to]))add2(u,v,ed[i].bh),add2(v,u,ed[i].bh);
int rt=,mx=;
for(int i=;i<=cr;i++)
if(siz[i]>mx)mx=siz[i],rt=i;
printf("%d\n",mx);
memset(vis,,sizeof vis);
dfs(rt);
for(int i=;i<=m;i++)
{
if(e[i].f==)printf("%d %d\n",e[i].u,e[i].v);
else printf("%d %d\n",e[i].v,e[i].u);
}
return ;
}

CF732 F Tourist Reform——边双连通分量的更多相关文章

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

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

  2. CF732F Tourist Reform[边双缩点]

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

  3. CF732F Tourist Reform(边双联通)

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

  4. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  5. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  6. 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分

    E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...

  7. 【BZOJ-2730】矿场搭建 Tarjan 双连通分量

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 751[Submit][Statu ...

  8. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  9. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

随机推荐

  1. JSON字符串的生成

    public class Corporation { public string remark { get; set; } public string version { get; set; } pu ...

  2. (独孤九剑)--MySQL入门

    :[一]概论 (1)什么是 MySQL? 一种关系型开源数据库,定义了存储信息的结构. 在数据库中,存在着一些表.类似 HTML 表格,数据库表含有行.列以及单元. 在分类存储信息时,数据库非常有用. ...

  3. win10 javac无效

    win10配置环境变量时,要写绝对路径,不再需要写JAVA_HOME和classpaht,直接在pass上添加全路径就可以了.

  4. HDU - 4803 - Poor Warehouse Keeper (思维)

    题意: 给出x,y两个值分别代表x个物品,总价为y 有两种变化: 1.使总价+1,数量不变 2.数量+1,总价跟着变化 (y = y + y / x) 思路: 给出目标x,y,计算最少变化次使数量变化 ...

  5. 动态 SQL(2)

    前面我们学习了使用动态 SQL 的 if.where.trim元素来处理一些简单查询操作,但对于一些 SQL 语句中含有 in 条件,需要迭代条件集合来生成的情况,我们就需要使用 foreach 标签 ...

  6. 08.C语言:特殊函数

    C语言:特殊函数 1.递归函数: 与普通函数比较,执行过程不同,该函数内部调用它自己,它的执行必须要经过两个阶段:递推阶段,回归阶段: 当不满足回归条件,不再递推: #include <stdi ...

  7. 洛谷 2777 [AHOI2016初中组]自行车比赛

    [题解] 为了让某个选手能够获得总分第一,就让他最后一天的得分是n,并且让别的选手的得分的最大值尽量小.于是我们先把目前积分排序,并且让他们最后一天的排名刚好与积分排名相反.即某个积分排名为X的人最后 ...

  8. BZOJ 2806 Luogu P4022 [CTSC2012]Cheat (广义后缀自动机、DP、二分、单调队列)

    题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2806 (luogu) https://www.luogu.org/pro ...

  9. vue 刷新当前页面的时候重新调用新的cookie

    data() { return{ AdminToken: this.getCookie('token'), } }, updated() { //刷新当前页面的时候重新调用新的cookie this. ...

  10. 【Codeforces 1118D1】Coffee and Coursework (Easy version)

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 从小到大枚举天数. 然后贪心地,从大到小分配a[i]到各个天当中. a[n]分配到第1天,a[n-1]分配到第2天,...然后a[n-x]又分 ...