Gym - 101630 C - Connections 两次dfs强连通图

codeforces的链接:Gym - 101630 C - Connections
牛客的链接:https://ac.nowcoder.com/acm/problem/54385

题目描述

Hard times are coming to Byteland. Quantum computing is becoming mainstream and Qubitland isgoing to occupy Byteland. The main problem is that Byteland does not have enough money for this war,so the King of Byteland Byteman 0x0B had decided to reform its road system to reduce expenses.Byteland has n cities that are connected by m one-way roads and it is possible to get from any city toany other city using these roads. No two roads intersect outside of the cities and no other roads exist. Bythe way, roads are one-way because every road has a halfway barrier that may be passed in one directiononly. These barriers are intended to force enemies to waste their time if they choose the wrong way.The idea of the upcoming road reform is to abandon some roads so that exactly 2n roads remain. Advisersof the King think that it should be enough to keep the ability to get from any city to any other city.(Maybe even less is enough? They do not know for sure.) The problem is how to choose roads to abandon.Everyone in Byteland knows that you are the only one who can solve this problem.

输入描述

Input consists of several test cases. The first line of the input contains the number of tests cases.The first line of each test case contains n and m — the number of cities and the number of roadscorrespondingly (n ≥ 4, m > 2n). Each of the next m lines contains two numbers xi and yi denoting aroad from city xi to city yi (1 ≤ xi, yi ≤ n, xi 6= yi). It is guaranteed that it is possible to get from anycity to any other city using existing roads only. For each pair (x, y) of cities there is at most one roadgoing from city x to city y and at most one road going from city y to city x. The solution is guaranteedto exist. The sum of m over all test cases in a single input does not exceed 100 000.

输出描述

For each test case output m − 2n lines. Each line describes a road that should be abandoned. Print theroad in the same format as in the input: the number of the source city and the number of the destinationcity. The order of roads in the output does not matter, but each road from the input may appear in theoutput at most once and each road in the output must have been in the input. It still must be possibleto get from any city to any other city using the remaining roads.

样例

在这里插入图片描述

题意

一个有向图,有n个点,有m条边,现在需要你删除m-2n条边,使得剩下的2n条边依旧可以组成强连通图,且把删除的边输出

题解

正反向建图且对各个边进行相同的下标标记,默认1为起始位置,分别在正反图进行dfs遍历,并判断是否经历过该点和该边,这就可以保证有一个强连通图了,故可以直接for遍历选出前n-2*m个没有被遍历过的边了。

#pragma GCC optimize(2)
#include<bits/stdc++.h> 
using namespace std;
#define ll long long
#define endl "\n"
const int MAX=1e6+7;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
struct node{
	int to,id;
};
bool vis[MAX],check[MAX];
vector<pair<int,int>>edge; 
vector<node>g[MAX],g2[MAX];
void dfs1(int u){
	vis[u]=1;
	for(auto it: g[u]){
		int v=it.to, id=it.id;
		if(!vis[v]){
			check[id]=1;
			dfs1(v);
		}
	}
}
void dfs2(int u){
	vis[u]=1;
	for(auto it:g2[u]){
		int v=it.to,id=it.id;
		if(!vis[v]){
			check[id]=1;
			dfs2(v);
		} 
	}
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--){
		int n,m;cin>>n>>m;
		edge.clear();
        for(int i=0;i<=m;i++)vis[i]=check[i]=0;
		for(int i=0;i<=n;i++)g[i].clear(),g2[i].clear();
		for(int i=0;i<m;i++){
			int u,v;cin>>u>>v;
			g[u].push_back({v,i}); 
			g2[v].push_back({u,i});
			edge.push_back({u,v});
		}
		dfs1(1);
        for(int i=0;i<=m;i++)vis[i]=0;  //反向边遍历需清0
		dfs2(1);
		for(int i=0,j=0;i<m&&j<m-2*n;i++){
			if(!check[i]){
				j++;
				cout<<edge[i].first<<" "<<edge[i].second<<endl;
			}
		}
	}
   return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值