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条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...
随机推荐
- Java中主线程如何捕获子线程抛出的异常
首先明确线程代码的边界.其实很简单,Runnable接口的run方法所界定的边界就可以看作是线程代码的边界.Runnable接口中run方法原型如下: public void run(); 而所有的具 ...
- java主线程捕获子线程中的异常
本文主要参考:<think in java> 好,下面上货. 正常情况下,如果不做特殊的处理,在主线程中是不能够捕获到子线程中的异常的. 例如下面的情况. package com.xuey ...
- 【hdu1828/poj1177】线段树求矩形周长并
题意如图 题解:这题非常类似与矩形面积并,也是维护一个被覆盖了一次以上的线段总长. 但是周长要算新出现的,所以每次都要和上一次做差求绝对值. x轴做一遍,y轴做一遍. 但是有个问题:矩形边界重合的时候 ...
- 【BZOJ】1709: [Usaco2007 Oct]Super Paintball超级弹珠
[算法]模拟 [题解]O(n^2)预处理横线(y),纵线(x),主对角线(y-x+n),副对角线(x+y). 然后n^2枚举每个点.
- python学习笔记(二)之python简单实践
1 安装python开发环境 Linux环境下自动安装好了python,可以通过以下命令更新到python最新版本. #echo "alias python=/usr/bin/python3 ...
- linux 下用 c 实现 ls -l 命令
#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/sta ...
- perl6中字符串字母编历
use v6; my $input = prompt '输入字符串:'; for $input.words -> $word { say $word; } for $input.comb -&g ...
- LCD 每隔10分钟 自动熄灭 --打开Framebuffer console的时候【转】
转自:http://blog.csdn.net/liujia2100/article/details/9009063 版权声明:本文为博主原创文章,未经博主允许不得转载. 之前移植LCD的时候,一切正 ...
- [device tree] interrupt
Specifying interrupt information for devices ============================================ 1) Interru ...
- hardseed
hardseed https://github.com/yangyangwithgnu/hardseed