CF732 F Tourist Reform——边双连通分量
题目: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——边双连通分量的更多相关文章
- Codeforces Round #377 (Div. 2) F - Tourist Reform
前言:关于如何求双连通分量,我们可以在tarjan搜索时标记下所有桥的位置(双连通分量(可以认为是没有桥的无向图图)即可通过删去所有桥得到),那么怎么找桥呢,对于每一条搜索到的边u->x,如果l ...
- CF732F Tourist Reform[边双缩点]
题意:给无向图每一条边定向,使得每个点可达点数$R_i$最小值尽可能大,求方案. 条件反射想到二分答案,然后看怎么检验,发现要让所有点$R_i$大于等于某一个值,首先我们关注某些特殊的子图:如果有环的 ...
- CF732F Tourist Reform(边双联通)
题意 在一张有向图中,设 ri 为从点 i 出发能够到达的点的数量. 定义有向图的“改良值”为 ri 的最小值. 现给出一张无向图,要求给每条边定一个方向,使产生的有向图“改良值”最大. 输出 最大改 ...
- 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 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分
E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...
- 【BZOJ-2730】矿场搭建 Tarjan 双连通分量
2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1602 Solved: 751[Submit][Statu ...
- 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 ...
- Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】
一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...
随机推荐
- ViewData丶ViewBag和TempData
案例: public ActionResult Index() { ViewData[; ViewData.Add(); ViewBag.myNum = ; TempData[; Student st ...
- java8 foreach不能使用break、countinue
在学习1.8新特性的过程中,发现foreach中不可以使用break和countinue,然后我使用了return,结果如下图,对循环并没有影响. 百度一下,发现了一个不一样的回答 然后我就看了下源码 ...
- libevent reference Mannual I
FYI:http://www.wangafu.net/~nickm/libevent-book/ This lib is a integral of asynchronous IO. we shoul ...
- <SpringMvc>入门四 响应结果
1.响应String类型 根据试图解析器,去找相对应的jsp Model将对象存在request中 2.响应void类型 可以看出,此时void方法执行了,系统默认会去找testVoid.jsp 意思 ...
- TestNG超时测试
用@Test(timeOut = XXX) 指定超时时间,单位是毫秒 package com.janson; import org.testng.annotations.Test; public cl ...
- CentOS \Linux文件权限详解
文件和目录权限概述 在linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问这些文件和目录. 通过设定权限可以从以下三种访问方式限制访问权限:只允许用户自己访问:允许一个 ...
- Python supprocess模块
当我们需要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出, ...
- 【模板】51nod 1006 最长公共子序列Lcs
[题解] dp转移的时候记录一下,然后倒着推出答案即可. #include<cstdio> #include<cstring> #include<algorithm> ...
- 零基础到架构师 不花钱学JavaEE(基础篇)- 概述
Java简单来说是一门语言,Java能干什么? 网站:开发大,中,小型网站. 服务器端程序:企业级程序开发. APP:Android的APP基本使用Java开发. 云:Hadoop就是使用Java语言 ...
- [luoguP3047] [USACO12FEB]附近的牛Nearby Cows(DP)
传送门 dp[i][j][0] 表示点 i 在以 i 为根的子树中范围为 j 的解 dp[i][j][1] 表示点 i 在除去 以 i 为根的子树中范围为 j 的解 状态转移就很好写了 ——代码 #i ...