【DFS】【枚举】Gym - 101246G - Revolutionary Roads
给你一张有向图,问你将任意一条边变成双向后,所能得到的最大强连通分量的大小。
缩点之后,预处理can(i,j)表示i能到j。
之后枚举每一条边(u,v),再枚举其他所有点t,如果can(u,t) && can(t,v),则t能和u、v共在一个强连通分量,尝试更新答案。
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
bool can[1010][1010];
struct Edge{
int id,v;
};
vector<Edge>G[1010],G2[1010];
vector<int>vs,rG[1010];
int n,m,K;
bool used[1010];
void dfs(int U){
used[U]=1;
for(int i=0;i<G[U].size();++i){
if(!used[G[U][i].v]){
dfs(G[U][i].v);
}
}
vs.push_back(U);
}
int cmp[1010];
void rdfs(int U){
used[U]=1;
cmp[U]=K;
for(int i=0;i<rG[U].size();++i){
if(!used[rG[U][i]]){
rdfs(rG[U][i]);
}
}
}
int a[1010],ans;
void dfs3(int rt,int U){
can[rt][U]=1;
for(int i=0;i<G2[U].size();++i){
if(!can[rt][G2[U][i].v]){
dfs3(rt,G2[U][i].v);
}
}
}
int p,anss[20010];
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
G[x].push_back((Edge){i,y});
rG[y].push_back(x);
}
for(int i=1;i<=n;++i){
if(!used[i]){
dfs(i);
}
}
memset(used,0,sizeof(used));
for(int i=vs.size()-1;i>=0;--i){
if(!used[vs[i]]){
++K;
rdfs(vs[i]);
}
}
for(int i=1;i<=n;++i){
++a[cmp[i]];
}
for(int i=1;i<=K;++i){
ans=max(ans,a[i]);
}
for(int i=1;i<=m;++i){
anss[i]=i;
}
p=m;
for(int i=1;i<=n;++i){
for(int j=0;j<G[i].size();++j){
if(cmp[i]!=cmp[G[i][j].v]){
G2[cmp[i]].push_back((Edge){G[i][j].id,cmp[G[i][j].v]});
}
}
}
for(int i=1;i<=K;++i){
dfs3(i,i);
}
for(int i=1;i<=K;++i){
for(int j=0;j<G2[i].size();++j){
int tmp=a[i]+a[G2[i][j].v];
for(int l=1;l<=K;++l){
if(l!=i && l!=G2[i][j].v && can[i][l] && can[l][G2[i][j].v]){
tmp+=a[l];
}
}
if(tmp>ans){
p=1;
anss[p]=G2[i][j].id;
ans=tmp;
}
else if(tmp==ans){
anss[++p]=G2[i][j].id;
}
}
}
sort(anss+1,anss+p+1);
printf("%d\n%d\n",ans,p);
for(int i=1;i<p;++i){
printf("%d ",anss[i]);
}
if(p){
printf("%d\n",anss[p]);
}
return 0;
}
【DFS】【枚举】Gym - 101246G - Revolutionary Roads的更多相关文章
- Gym 101246G Revolutionary Roads
http://codeforces.com/gym/101246/problem/G 题意: 给出一个有向图,现在可以把图中的任意一条边改为无向边,问强连通分量最多可以有多少个点,在此情况下输出所有能 ...
- POJ1288 Sly Number(高斯消元 dfs枚举)
由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
随机推荐
- 4.0docker部署
设置容器的端口映射 -P :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...
- 【shell】shell编程(六)-shell函数的应用
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...
- Python学习笔记 - day8 - 异常
异常 在程序运行过程中,总会遇到各种各样的错误.有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,有的错误是用户输入造成的,比如让用户输入email地址,结果得到一个空字符串,这种 ...
- 使用makecontext实现用户线程【转】
转自:http://blog.csdn.net/cyberlabs/article/details/6920138 使用makecontext实现用户线程 现代Unix系统都在ucontext.h中提 ...
- 在64位linux下编译32位程序
在64位linux下编译32位程序 http://blog.csdn.net/xsckernel/article/details/38045783
- MVC自定义路由实现URL重写,SEO优化
//App_Start-RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollect ...
- HDU 5129 Yong Zheng's Death
题目链接:HDU-5129 题目大意为给一堆字符串,问由任意两个字符串的前缀子串(注意断句)能组成多少种不同的字符串. 思路是先用总方案数减去重复的方案数. 考虑对于一个字符串S,如图,假设S1,S2 ...
- 阿里云ECS的使用
一.阿里云ECS的使用 1.Linux CentOS Ubuntu Readhat 2.远程登录 xshell 远程登录 winScp 远程文件操作 3.Linux命令 cd 目录名 ls . ls ...
- mui 怎样监听scroll事件的滚动距离
var scroll = mui('.mui-scroll-wrapper').scroll(); document.querySelector('.mui-scroll-wrapper' ).add ...
- socket编程之select(),poll(),epoll()
socket编程,通信 client端 socket() ----->connect() ------->recv() -----> close(); server端 socket ...