Problem C. Connections
Time limit: 3 seconds
Hard times are coming to Byteland. Quantum computing is becoming mainstream and Qubitland is going 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 to any other city using these roads. No two roads intersect outside of the cities and no other roads exist. By the way, roads are one-way because every road has a halfway barrier that may be passed in one direction only. 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. Advisers of 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
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 roads correspondingly (n ≥ 4, m > 2n). Each of the next m lines contains two numbers xi and yi denoting a road from city xi to city yi (1 ≤ xi,yi ≤ n, xi 6= yi). It is guaranteed that it is possible to get from any city to any other city using existing roads only. For each pair (x,y) of cities there is at most one road going from city x to city y and at most one road going from city y to city x. The solution is guaranteed to exist. The sum of m over all test cases in a single input does not exceed 100000.
Output
For each test case output m − 2n lines. Each line describes a road that should be abandoned. Print the road in the same format as in the input: the number of the source city and the number of the destination city. The order of roads in the output does not matter, but each road from the input may appear in the output at most once and each road in the output must have been in the input. It still must be possible to get from any city to any other city using the remaining roads.
题意:给你一个图,问你删去哪些边(直到剩下2*n条)可以构成强连通图。
思路:我一开始写的是BFS正反搜两遍,但是不知道什么原因过不了,然后就看了大佬的思路换成了DFS,构建两个图,一个正一个反。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<stack> #include<map> #include<vector> #include<queue> #include<set> #include<iomanip> #include<cctype> using namespace std; int vis[100005],vis1[1000005],sum1,sum; int head[100005],head1[100005]; int n,m; struct node { int u,v,next; }bian[100005],bian1[100005]; void intit() { sum=0;sum1=0; memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); memset(head1,-1,sizeof(head1)); memset(vis1,0,sizeof(vis1)); } void add(int u,int v) { bian[sum].u=u; bian[sum].v=v; bian[sum].next=head[u]; head[u]=sum++; bian1[sum1].v=u; bian1[sum1].next=head1[v]; head1[v]=sum1++; } void dfs(int t) { for(int i=head[t];i!=-1;i=bian[i].next) { if(vis[bian[i].v]==0) { vis[bian[i].v]=1; vis1[i]=1; dfs(bian[i].v); } } } void dfs1(int t) { for(int i=head1[t];i!=-1;i=bian1[i].next) { if(vis[bian1[i].v]==0) { vis[bian1[i].v]=1; vis1[i]=1; dfs1(bian1[i].v); } } } int main() { int i,j,N; cin>>N; while(N--) { intit(); int n,m,a,b; cin>>n>>m; for(i=0;i<m;i++) { cin>>a>>b; add(a-1,b-1); } dfs(0); memset(vis,0,sizeof(vis)); dfs1(0); int summ=m; for(i=0;i<m&&summ!=2*n;i++) { if(vis1[i]==0) { summ--; cout<<bian[i].u+1<<' '<<bian[i].v+1<<endl; } } } return 0; }