Gym101630C Connections
题目大意:
给出一个\(n\)个点\(m\)条边的有向图,无自环无重边。要求把这个图进行删边,直到只剩下\(2n\)条边,使得图中每个点都可以相互连通。
知识点: DFS
解题思路:
从点\(1\)出发,进行一次\(DFS\),把所有的点都访问一次,标记经过的边,这些边保证了点\(1\)能到所有的点。再额外建一个图(图中所有的边都是原图的边的反向),再进行一次与上面类似的DFS操作:从点\(1\)出发,把所有的点都访问一次,标记经过的边,这些边保证了所有的点都能到点\(1\)。两次\(DFS\)最多标记\(2n-2\)条边,并保证了所有点都能到点\(1\),点\(1\)能到所有点,如此所有点都能到所有点,每个点都可以相互连通。被标记的边就是必需的,删除其他边直到只剩下\(2n\)条边即可。
AC代码:
#include <bits/stdc++.h> using namespace std;
typedef pair<int,int> P;
const int maxn=+;
vector<int>G[maxn],tG[maxn];
P edge[maxn];
map<P,int> used;
int n;
bool vis[maxn];
void init(){
for(int i=;i<=n;i++) vis[i]=false;
}
void dfs1(int rt){
for(int i=;i<G[rt].size();i++){
if(!vis[G[rt][i]]){
vis[G[rt][i]]=true;
dfs1(G[rt][i]);
used[make_pair(rt,G[rt][i])]++;
}
}
}
void dfs2(int rt){
for(int i=;i<tG[rt].size();i++){
if(!vis[tG[rt][i]]){
vis[tG[rt][i]]=true;
dfs2(tG[rt][i]);
used[make_pair(tG[rt][i],rt)]++;
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int m,u,v;
scanf("%d%d",&n,&m);
used.clear();
for(int i=;i<=n;i++){
G[i].clear();
tG[i].clear();
}
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v),tG[v].push_back(u);
edge[i]=make_pair(u,v);
}
init();
vis[]=true;
dfs1();
init();
vis[]=true;
dfs2();
int has=m;
for(int i=;i<=m&&has>*n;i++){
if(used[edge[i]]==){
printf("%d %d\n",edge[i].first,edge[i].second);
has--;
}
}
}
return ;
}
Gym101630C Connections的更多相关文章
- Gym-101630C:Connections(生成树&构造)
题意:给定N点,M条有向边,满足任意点可以到达任意点.现在叫你保留2*N边,任然满足任意点可以到达任意点,输出删除的边. 思路:从1出发,DFS,得到一颗生成树,有N-1条边.反向建题.还是从1出发, ...
- 解决mysql too many connections的问题
由于公司服务器上的创建的项目太多,随之创建的数据库不断地增加,在用navicat链接某一个项目的数据库时会出现too many connections ,从而导致项目连接数据库异常,致使启动失败. 为 ...
- Data source rejected establishment of connection, message from server: "Too many connections"解决办法
异常名称 //数据源拒绝从服务器建立连接.消息:"连接太多" com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnection ...
- Configure Security Settings for Remote Desktop(RDP) Services Connections
catalogue . Configure Server Authentication and Encryption Levels . Configure Network Level Authenti ...
- 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...
- Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem
1.问题描述 在启动使用mysql数据库的项目时,遇到一个报错,如下: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConn ...
- Configure the max limit for concurrent TCP connections(转)
To keep the TCP/IP stack from taking all resources on the computer, there are different parameters t ...
- 打开mysql时,提示 1040,Too many connections
打开mysql时,提示 1040,Too many connections,这样就无法打开数据库,看不了表里边的内容了. 出现这个问题的原因是,同时对数据库的连接数过大,mysql默认的最大连接数是1 ...
- mysql连接数设置操作(Too many connections)
mysql在使用过程中,发现连接数超了~~~~ [root@linux-node1 ~]# mysql -u glance -h 192.168.1.17 -pEnter password: ERRO ...
随机推荐
- 二进制安装MySQL及破解密码
二进制安装MySQL及破解密码 1.确保系统中有依赖的libaio 软件,如果没有: yum -y install libaio 2.解压二进制MySQL软件包 tar xf mysql-5.7.24 ...
- Spring PropertyPlaceholderConfigurer类载入外部配置
2019独角兽企业重金招聘Python工程师标准>>> 通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.co ...
- Docker学习之搭建MySql容器服务
描述 MySQL 5.6 SQL数据库服务器Docker镜像,此容器映像包含用于OpenShift的MySQL 5.6 SQL数据库服务器和一般用法.用户可以选择RHEL和基于CentOS的图像.然后 ...
- 图论--2-SAT--POJ Ikki's Story IV - Panda's Trick
Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping ...
- HBase Filter 过滤器之RowFilter详解
前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...
- 在linux上搭建nacos集群(步骤详细,linux小白也搞得定)
(1)nacos官网:https://github.com/alibaba/nacos/releases/tag/1.2.1下载nacos安装包到window本地(后缀为tar.zip) (2)在li ...
- OSG程序设计之Hello World 2.0
现在为Hello World添加一些键盘响应事件. //需要多添加两个库:osgGAd.lib.osgd.lib 代码如下: #include <osgDB/ReadFile> #incl ...
- Java种sleep和wait的区别
1,sleep方法是Thread类的静态方法,wait()是Object超类的成员方法 2,sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时 ...
- VM虚拟机手动配置IP地址
1.查看虚拟机的网关 编辑-->虚拟网络编辑器 VMnet8 NAT模式-->NAT设置-->网关IP 2.设置IP地址 系统-->首选项-->网络连接 system e ...
- 记一次sqoop安装后测试的问题
运行命令: sqoop import --connect "jdbc:mysql://x.x.x.x:3306/intelligent_qa_bms?useUnicode=true& ...