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条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...
随机推荐
- mysql 事务,锁,与四大隔离级别
概念 事务 原子性:事务必须是一个自动工作的单元,要么全部执行,要么全部不执行. 一致性:事务结束的时候,所有的内部数据都是正确的. 隔离性:并发多个事务时,各个事务不干涉内部数据,处理的都是另外一个 ...
- 图论:最短路-SPFA
该算法由Bellman-Ford算法演变过来,首先介绍一下Bellman-Ford算法 最短路最多经过n-1个点,可以用n-1轮松弛操作来得到 ;i<n;i++) d[i]=INF; d[]=; ...
- CentOS查看系统版本号
命令:cat /etc/redhat-release [elsearch@localhost data]$ cat /etc/redhat-release Red Hat Enterprise Lin ...
- MyBatis框架的使用及源码分析(七) MapperProxy,MapperProxyFactory
从上文<MyBatis框架中Mapper映射配置的使用及原理解析(六) MapperRegistry> 中我们知道DefaultSqlSession的getMapper方法,最后是通过Ma ...
- mvc Dapper_Report_Down_ExcelFile
一.基于Aspose.Cells.Dapper导出Excel Dapper的Query返回要不是对象的IEnumerable,要不是Dynamic的IEnumerable,都不适合不用反射就能够动态获 ...
- windows下用时间戳创建文件名
英文环境下: echo Archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.zip 中文: ...
- 【1】记一次破解wifi
当然,使用的依旧是aircrack套件,这次依旧是跑字典,今天,捉到了另一个实验室icephone的wpa握手包,我猜测实验室的wifi一般都跟自己的名字有关,icephone刚好是8位字母,于是我就 ...
- 去掉每行的特定字符py脚本
百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...
- android CVE 漏洞汇总
arm exploits 技术教程: Learning Pentesting for Android Devices CVE-2015-1530 ,CVE-2015-1474 两个android整数溢 ...
- openboot的项目
http://docs.oracle.com/cd/E19201-01/821-0901-10/OK_OBP.html https://www.openfirmware.info/OpenBIOS h ...