您的位置:首页 > 其它

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

2017-12-09 21:02 537 查看
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

中石油博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gym 2017 acm dfs 连通图