ACM ICPC 2017–2018, NEERC – Northern Eurasia Finals --C

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;
}

 

转载于:https://www.cnblogs.com/SparkPhoneix/p/8833106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值