边双连通分量。

这题有一点构造的味道。一个有向图,经过强连通缩点之后会形成一个有向无环图。

如果将最大的强连通分量放在顶端,其余的强连通分量都直接或间接指向他,那么这样就构造出了符合要求的图。

接下来就是要去寻找强连通分量。对于一个无向图来说,每一个边-双联通分量都可以将每条边定向之后构造成一个强连通分量,$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的更多相关文章

  1. Codeforces 732F. Tourist Reform (Tarjan缩点)

    题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...

  2. 732F Tourist Reform

    // CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...

  3. CF 732F Tourist Reform——v-SCC+dfs

    题目:http://codeforces.com/contest/732/problem/F 给无向图定向使得从每个点出发能去的点数最小值最大. SCC.点内部dfs定向.点间以siz最大的为起点反向 ...

  4. 【codeforces 732F】Tourist Reform

    [题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...

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

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

  6. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  7. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  8. CodeForces 659E New Reform

    题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...

  9. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

随机推荐

  1. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

  2. Codeforces Round #380 (Div. 2)/729E Subordinates 贪心

    There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a ...

  3. mysql 创建视图

    1.单表创建视图 例如:创建一个选择语句,选出学生的编号,姓名和考号 //创建一个视图名字为stu_view1选择 来自数据表student中的id,name 和kn 中的数据 create view ...

  4. 51nod 1766 树上的最远点对——线段树

    n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c<=j& ...

  5. bzoj 4034: [HAOI2015]树上操作——树链剖分

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...

  6. 【BZOJ】3502 PA2012 Tanie linie

    [算法]贪心,一般DP [题解] --- 胡策k≤10的环状DP做法: 1.钦定法:先确定第一位(可能和第n位)的状态,然后后面正常做DP,显然正确答案是一定会被记录的,因为从整体上看不会有影响. 2 ...

  7. SQL Server Delete Duplicate Rows

    There can be two types of duplication of rows in a table 1. Entire row getting duplicated because th ...

  8. 【HNOI】 攻城略池 tree-dp

    [题目大意] 给定一棵树,边有边权,每个节点有一些兵,现在叶子节点在0时刻被占领,并且任意节点在x被占领,那么从x+1开始,每单位时间产生一个兵,兵会顺着父亲节点一直走到根(1),其中每经过一个节点, ...

  9. Coursera课程《Machine Learning》吴恩达课堂笔记

    强烈安利吴恩达老师的<Machine Learning>课程,讲得非常好懂,基本上算是无基础就可以学习的课程. 课程地址 强烈建议在线学习,而不是把视频下载下来看.视频中间可能会有一些问题 ...

  10. ogre 3d游戏开发框架指南

    ogre 3d游戏开发框架指南pdf 附光盘代码 http://www.ddooo.com/softdown/74228.htm OGRE3D游戏开发框架指南.pdf http://vdisk.wei ...