CodeForces 732F Tourist Reform
边双连通分量。
这题有一点构造的味道。一个有向图,经过强连通缩点之后会形成一个有向无环图。
如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图。
接下来就是要去寻找强连通分量。对于一个无向图来说,每一个边-双联通分量都可以将每条边定向之后构造成一个强连通分量,$dfs$一遍即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} const int maxn = + ;
int n,m; int pre[maxn], dfs_clock, bcc_cnt;
vector<int>G[maxn], bcc[maxn]; int belong[maxn],U[maxn],V[maxn],sz;
int flag[maxn],ff[maxn],gg[maxn],ge[maxn],dd[maxn]; int Tarjan(int u,int fa)
{
int lowu=pre[u]=++dfs_clock; for(int i=;i<G[u].size();i++)
{
int v=V[G[u][i]];
if(!pre[v])
{
int lowv=Tarjan(v,u);
lowu=min(lowu,lowv); if(lowv > pre[u]) ge[(G[u][i]+)/]=;
}
else if(v!=fa) lowu=min(lowu,pre[v]);
} return lowu;
} void add(int a,int b)
{
sz++; U[sz]=a; V[sz]=b;
G[a].push_back(sz);
} void DFS(int x,int y)
{
flag[y]=;
for(int i=;i<G[y].size();i++)
{
int v=V[G[y][i]];
if(v==x) continue;
if(ff[(G[y][i]+)/]==) continue;
ff[(G[y][i]+)/]=;
gg[G[y][i]]=;
if(flag[v]==) DFS(y,v);
}
} void dfs(int x)
{
belong[x]=bcc_cnt;
for(int i=;i<G[x].size();i++)
{
if(ge[(G[x][i]+)/]==) continue;
int v=V[G[x][i]];
if(belong[v]!=) continue;
dfs(v);
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int a,b; scanf("%d%d",&a,&b);
add(a,b); add(b,a);
} for (int i = ; i <= n; i++) if (!pre[i]) Tarjan(i, -); for(int i=;i<=n;i++)
{
if(belong[i]!=) continue;
bcc_cnt++; dfs(i);
} for(int i=;i<=n;i++) dd[belong[i]]++; int mx=,idx=;
for(int i=;i<=bcc_cnt;i++) if(dd[i]>mx) mx=dd[i],idx=i; for(int i=;i<=n;i++) if(belong[i]==idx) { DFS(-,i); break; } printf("%d\n",mx);
for(int i=;i<=sz;i++)
if(gg[i]) printf("%d %d\n",V[i],U[i]); return ;
}
CodeForces 732F Tourist Reform的更多相关文章
- Codeforces 732F. Tourist Reform (Tarjan缩点)
题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...
- 732F Tourist Reform
// CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...
- CF 732F Tourist Reform——v-SCC+dfs
题目:http://codeforces.com/contest/732/problem/F 给无向图定向使得从每个点出发能去的点数最小值最大. SCC.点内部dfs定向.点间以siz最大的为起点反向 ...
- 【codeforces 732F】Tourist Reform
[题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...
- Codeforces Round #377 (Div. 2) F - Tourist Reform
前言:关于如何求双连通分量,我们可以在tarjan搜索时标记下所有桥的位置(双连通分量(可以认为是没有桥的无向图图)即可通过删去所有桥得到),那么怎么找桥呢,对于每一条搜索到的边u->x,如果l ...
- codeforces 340C Tourist Problem
link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...
- codeforces 340C Tourist Problem(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Tourist Problem Iahub is a big fan of tou ...
- CodeForces 659E New Reform
题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...
- Codeforces 659E New Reform【DFS】
题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...
随机推荐
- [LeetCode] 6. ZigZag Conversion ☆☆☆
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- iOS tag的使用
一.添加标记 (标记不能为0) UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(,,,)]; backBtn.backgr ...
- oracle scott用户不存在
scott用户拥有一些基础的数据表,可以供我们练习sql.先执行 alter user scott account unlock; 查看scott用户是否存在 当scott用户不存在,我们就需要在$O ...
- java springmvc4 图片或文件上传
1.文件配置 配置文件解析 上传文件处理的核心方法 // uploadOneFile.jsp, uploadMultiFile.jsp submit to. @RequestMapping(value ...
- Ribbon自带负载均衡策略比较
Ribbon自带负载均衡策略比较 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailableRule extends ClientC ...
- bootstrap-table设置某列序号自增
col = [{ field: 'SerialNumber', title: '序号', formatter: function (value, row, index) { return index+ ...
- Python阶段复习 - part 4 - 用户登录程序
简易版: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__:Dahlhin import sys userinfo = r'userinf ...
- Django 1.10中文文档-第一个应用Part4-表单和通用视图
本教程接Part3开始.继续网页投票应用程序,并将重点介绍简单的表单处理和精简代码. 一个简单表单 更新一下在上一个教程中编写的投票详细页面的模板polls/detail.html,让它包含一个HTM ...
- wait函数
当一个进程正常或异常终止时,内核就向其父进程发送SIGCHLD信号.子进程终止是一个异步事件(其能够在父进程运行的任何时候发生). 对于wait(),其调用者,即父进程可以有如下状态: 如果其所有子进 ...
- C++之指针,引用与数组
引用只是对象的另一个名字,通过在变量名前面添加"&”符号来定义,而指针保存的是另一个对象的地址,它们两都提供了间接访问所服务变量的途径. 但是它们的差别还是挺大的: 先从它们的值说起 ...