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数相等的个数. ...
随机推荐
- Oracle PL/SQL 高级编程
1. 复合数据类型--记录类型 Ø 语法格式 type 类型名 is record ( 字段1 字段1类型 [not null]:=表达式1; 字段2 字段2类型 [not n ...
- 【转】【Python + selenium】linux和mac环境,驱动器chromedriver和测试报告HTMLTestRunner放置的位置
感谢: 作者:gz_tester,文章:<linux和mac环境,chromedriver和HTMLTestRunner放置的位置> 使用场景 配置python selenium 环境 使 ...
- hihoCoder #1388 : Periodic Signal
NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...
- Redis加锁与解锁
Redis加锁 customerM = BaseMemCached.setMLock(customerId); /** * 个人账户表加锁 **/ public static CustomerM se ...
- phpcms控制器变量分配到模板
跟TP.CI框架不同,phpcmsv9分配变量的方式是: 控制器中声明了变量$a='zrp'或$data=array('aa','bb'); 在模板中就可以直接输出: 字符串:{$a} 数组:遍历 { ...
- 【BZOJ4010】[HNOI2015]菜肴制作 拓扑排序
[BZOJ4010][HNOI2015]菜肴制作 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高 ...
- dubbo启动报错多个资源争缓存问题
Dubbo Failed to save registry store file, cause: Can not lock the registry cache file 目录(?)[+] 启动的Du ...
- rate limiter - system design
1 问题 Whenever you expose a web service / api endpoint, you need to implement a rate limiter to preve ...
- why factory pattern and when to use factory pattern
1 factory pattern本质上就是对对象创建进行抽象 抽象的好处是显然的,可以方便用户去获取对象. 2 使用factory pattern的时机 第一,当一个对象的创建依赖于其它很多对象的时 ...
- Django 之 admin组件使用&源码解析
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED ...