dfs 深入学习(1) Connections Gym - 101630C

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 100 000.
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.
Example
input
1
4 9
1 2
1 3
2 3
2 4
3 2
3 4
4 1
4 2
4 3
output:
1 3
题目大意:给定一个连通图,现删去m-n*2条边还能使图联通,求出这些边并输出出来,(特判,多解输出其一即可)
思路:正向存边,反向存边,然后对点1正向dfs,反向dfs,把过程中遍历的边都标记,然后输出其他任意的(m-n*2)条边即可
code1:
采用了一种特殊的建边方法:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int del[200000];
int u[200000];
int v[200000];
int use[200000];
vector<int>o[200000];
int n,m;
void dfs(int x,int f,int *s)
{
    int len;
    len = o[x].size();
    use[x] = 1;
    for(int i=0;i<len;i++)
    {
        int yy = o[x][i];
        int y = s[yy];
        if(y==f||y==x)continue;
        if(!use[y])
        {
            del[yy] = false;
            dfs(y,x,s);
        }
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
           cin>>n>>m;
        for(int i=0;i<=n;i++)
            o[i].clear();
        memset(del,true,sizeof(del));

         int uu = m-n*2;
          for(int i=1;i<=m;i++)
          {
                scanf("%d%d",&u[i],&v[i]);
                o[u[i]].push_back(i);
               o[v[i]].push_back(i);
          }
//          for(int i=1;i<=n;i++)
//          {
//               for(int j=0;j<o[i].size();j++)
//               {
//                   printf("%d ",o[i][j]);
//               }
//               printf("\n");
//          }
        memset(use,0,sizeof(use));
        dfs(1,1,u);
        memset(use,0,sizeof(use));
        dfs(1,1,v);
        for(int i=1;i<=m;i++)
        {
            if(uu&&del[i])
            {
                uu--;
                printf("%d %d\n",u[i],v[i]);
            }
        }

    }

    return 0;
}

code2:
by QAQ杨兴志学长
这里写图片描述
参考博客:
炉石1
炉石2
炉石3
校赛1
校赛2
dfs校赛3
中石油博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值