题目大意:

  给出一个\(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的更多相关文章

  1. Gym-101630C:Connections(生成树&构造)

    题意:给定N点,M条有向边,满足任意点可以到达任意点.现在叫你保留2*N边,任然满足任意点可以到达任意点,输出删除的边. 思路:从1出发,DFS,得到一颗生成树,有N-1条边.反向建题.还是从1出发, ...

  2. 解决mysql too many connections的问题

    由于公司服务器上的创建的项目太多,随之创建的数据库不断地增加,在用navicat链接某一个项目的数据库时会出现too many connections ,从而导致项目连接数据库异常,致使启动失败. 为 ...

  3. Data source rejected establishment of connection, message from server: "Too many connections"解决办法

    异常名称 //数据源拒绝从服务器建立连接.消息:"连接太多" com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnection ...

  4. Configure Security Settings for Remote Desktop(RDP) Services Connections

    catalogue . Configure Server Authentication and Encryption Levels . Configure Network Level Authenti ...

  5. 问题解决: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 ...

  6. Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem

    1.问题描述 在启动使用mysql数据库的项目时,遇到一个报错,如下: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConn ...

  7. 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 ...

  8. 打开mysql时,提示 1040,Too many connections

    打开mysql时,提示 1040,Too many connections,这样就无法打开数据库,看不了表里边的内容了. 出现这个问题的原因是,同时对数据库的连接数过大,mysql默认的最大连接数是1 ...

  9. mysql连接数设置操作(Too many connections)

    mysql在使用过程中,发现连接数超了~~~~ [root@linux-node1 ~]# mysql -u glance -h 192.168.1.17 -pEnter password: ERRO ...

随机推荐

  1. Spring5参考指南:事件Event

    文章目录 基于继承的Event 基于注解的Event 异步侦听器 Spring提供了很方便的事件的处理机制,包括事件类ApplicationEvent和事件监听类ApplicationListener ...

  2. xml文件错误

    2019独角兽企业重金招聘Python工程师标准>>> xml文件错误The processing instruction target matching "[xX][mM ...

  3. Qt之QListWidget:项目的多选与单选设置

    2019独角兽企业重金招聘Python工程师标准>>> #include "widget.h" #include <QApplication> #in ...

  4. python 多进程处理 multiprocessing模块

    前提: 有时候一个用一个进程处理一个列表中的每个元素(每个元素要传递到一个函数中进行处理),这个时候就要用多进程处理 1 现场案例: 我有一个[ip1,ip2,ip3,.......]这样的列表,我要 ...

  5. Entity framework 加载多层相关实体数据

    Entity framework有3种加载数据的方式:懒汉式(Lazy loading),饿汉式(Eager loading),显示加载(Explicit loading).3种加载方式有各自的优缺点 ...

  6. java socket实现服务端,客户端简单网络通信。Chat

    之前写的实现简单网络通信的代码,有一些严重bug.后面详细写. 根据上次的代码,主要增加了用户注册,登录页面,以及实现了实时显示当前在登录状态的人数.并解决一些上次未发现的bug.(主要功能代码参见之 ...

  7. Jmeter简单压测之服务器监控

    此篇为最近工作需要到内容,故现在做一个总结. 最近家里电脑坏了,等待会公司空闲在编写. 文章构思中,敬请期待.......

  8. B - Housewife Wind POJ - 2763 树剖+边权转化成点权

    B - Housewife Wind POJ - 2763 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...

  9. Struts Scan工具的使用

    前言 最近看了关于Struts2漏洞,参考文章 https://www.freebuf.com/vuls/168609.html,这篇文章里对Struts2的漏洞及原理进行了详细的讲解.自己也从网上找 ...

  10. Spring Cloud 学习 之 Spring Cloud Eureka(架构)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 Eureka服务治理基础架构的三个核心要素: 服务治理机制: 服务提供者: ...