Codeforces Round #377 (Div. 2) F - Tourist Reform
前言:关于如何求双连通分量,我们可以在tarjan搜索时标记下所有桥的位置(双连通分量(可以认为是没有桥的无向图图)即可通过删去所有桥得到),那么怎么找桥呢,对于每一条搜索到的边u->x,如果low【u】>dfn【x】则说明u不能通过子图到达比x更早的节点,那么就说明该边是桥
题意:把一个无向图变成有向图,对于这个有向图来说,每个点的价值是它所有能到达的点的数量,要求使得所有点中最小的价值最大
题解:现学的边-双联通分量,先求一遍边-双联通分量,然后在每个双连通分量里dfs一边把双向边变成单向边,最后从点数最大的那个双连通分量开始dfs,删去反向边,易证最大的点数就是答案,然后直接输出单向边即可
#include<bits/stdc++.h>
#include<ext/rope>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; map<int,int>ma[N];
map<int,int>ans[N];
vector<int>v[N];
int dfn[N],low[N];
int a[N],b[N];
int index,sz;
void tarjan(int u,int f)
{
dfn[u]=low[u]=++index;
for(int i=;i<v[u].size();i++)
{
int x=v[u][i];
if(x==f)continue;
if(!dfn[x])
{
tarjan(x,u);
low[u]=min(low[u],low[x]);
if(low[x]>dfn[u])ma[u][x]=ma[x][u]=;
}
else low[u]=min(low[u],dfn[x]);
}
}
void init(int n)
{
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
index=;
for(int i=;i<=n;i++)
{
ma[i].clear();
v[i].clear();
ans[i].clear();
}
}
void dfs(int u,int f)
{
dfn[u]=;
sz++;
for(int i=;i<v[u].size();i++)
{
int x=v[u][i];
if(!ma[x][u]&&x!=f)
{
// cout<<u<<" "<<x<<endl;
if(ans[x][u]&&ans[u][x])ans[x][u]=;
if(!dfn[x])dfs(x,u);
}
}
}
void dfs2(int u,int f)
{
dfn[u]=;
for(int i=;i<v[u].size();i++)
{
int x=v[u][i];
if(!dfn[x])
{
if(ans[x][u]&&ans[u][x])ans[u][x]=;
dfs2(x,u);
}
}
}
int main()
{
int n,m;
cin>>n>>m;
init(n);
for(int i=;i<m;i++)
{
cin>>a[i]>>b[i];
ans[a[i]][b[i]]=ans[b[i]][a[i]]=;
v[a[i]].pb(b[i]);
v[b[i]].pb(a[i]);
}
tarjan(,-);
memset(dfn,,sizeof dfn);
int maxx=,id;
for(int i=;i<=n;i++)
{
if(!dfn[i])
{
sz=;
dfs(i,-);
if(maxx<sz)
{
maxx=sz;
id=i;
}
}
}
memset(dfn,,sizeof dfn);
dfs2(id,-);
cout<<maxx<<endl;
for(int i=;i<m;i++)
{
if(ans[a[i]][b[i]])cout<<a[i]<<" "<<b[i]<<endl;
else cout<<b[i]<<" "<<a[i]<<endl;
}
return ;
}
/************
7 8
1 2
2 3
1 3
3 4
4 5
5 6
6 7
7 4
************/
Codeforces Round #377 (Div. 2) F - Tourist Reform的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #377 (Div. 2) D. Exams
Codeforces Round #377 (Div. 2) D. Exams 题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
随机推荐
- x264源代码学习1:概述与架构分析
函数背景色 函数在图中以方框的形式表现出来.不同的背景色标志了该函数不同的作用: 白色背景的函数:不加区分的普通内部函数. 浅红背景的函数:libx264类库的接口函数(API). 粉红色背景函数:滤 ...
- IEnumerable, IEnumerator接口
IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...
- mysql 考勤表异常 【待修改】
有考勤刷卡记录表,表名为attendance ,有如下字段: 姓名 卡号 刷卡时间 刷卡类型 name id time type 张三 59775623 2010-04-01 07:23:37 ...
- tomcat6指定的服务为安装
tomcat6w.exe 运行 提示 指定的服务未安装 unable to open the service 'tomcat6' 新下载的Tomcat6.0.29 解压版,解压缩完成后,双击tomca ...
- [转]为 windows cmd 设置代理
为 windows cmd 设置代理 转自:http://blog.csdn.net/lovelyelfpop/article/details/69586366 通过cmd命令行执行某些命令,如果这些 ...
- 九度OJ 1195:最长&最短文本 (搜索)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3144 解决:1156 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输 ...
- hibernate Session的CRUD操作
使用Session里面的方法进行CRUD操作 (1) 增加 save 方法 (2) 查找 get 方法(根据id查) (3) 修改 update 方法 (4) 删除 delete 方法 1.增加 /* ...
- HDU 5355 Cake (WA后AC代码,具体解析,构造题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...
- linux c编程:信号(二) alarm和pause函数
使用alarm函数可以设置一个定时器,在将来的某个时刻该定时器超时.当定时器超时后,产生SIGALRM信号.如果忽略或不捕捉此信号,则其默认动作是终止调用该alarm函数的进程 #include< ...
- 如何使用Django实现用户登录验证
最初开始搞用户登录验证的时候感觉没什么难的,不就是增删改查中的查询数据库么,但是还是遇到许多小问题,而且感觉在查询数据库的时候,要把前端的数据一条一条的进行比对,会导致我的代码很丑,而且方式很不智,所 ...