Connections (gym101630)搜索

原文地址 C题

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 x i and y i denoting a
road from city x i to city y i (1 ≤ x i , y i ≤ n, x i 6 = y i ). 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

题意:一个连通图有n个顶点,有m条单向边。要保证在删除(m-2*n)条边的情况下,即,使边数变为2*n条,这个图仍然是连通的(连通表示,从图上任意一点 i 出发都能到达图上任意一点 j )。询问的是可以删掉哪些边,只需要输出一种符合条件的情况就行。

思路:数据量n是2e5,开不了2维数组,所以用vector存储。题目中保证的图的连通性,我们分别建立正向图和反向图,分别遍历从1到达的所有边,两次筛选能够保证一张连通图的建立,而其余的边则是可以舍去的,然后遍历一遍边表,找出没有实际logg的边删掉就可以。

AC代码:

/***
 * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
 * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
 * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
 * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
 * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
 * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
 * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
 * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
 * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
 * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
 * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
 * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
 * │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
 * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
 */

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define PI acos(-1)
const int mod=1e9+7;
const int M=2e5 + 10;
struct A
{
    int x;
    int y;
    int id;
} a[M];
int vis[M];
int logg[M];
vector<A> v1[M],v2[M];
void DFS1(int s)
{
    for(int i=0; i<v1[s].size(); i++)
    {
        int now=v1[s][i].y;
        if(vis[now]==0)
        {
            logg[v1[s][i].id]=1;
            vis[now]=1;
            DFS1(now);
        }
    }
}
void DFS2(int s)
{
    for(int i=0; i<v2[s].size(); i++)
    {
        int now=v2[s][i].y;
        if(vis[now]==0)
        {
            logg[v2[s][i].id]=1;
            vis[now]=1;
            DFS2(now);
        }
    }
}
int main()
{
    //freopen("//home//acm//桌面//in","r",stdin);
    int t,n,m;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0; i<=n; i++)
            {
                v1[i].clear();
                v2[i].clear();
            }
            memset(vis,0,sizeof(vis));
            memset(logg,0,sizeof(logg));
            memset(a,0,sizeof(a));
            for(int i=1; i<=m; i++)
            {
                scanf("%d%d",&a[i].x,&a[i].y);
                a[i].id=i;
                A t;
                t.y=a[i].y;
                t.id=i;
                v1[a[i].x].push_back(t);
                t.y=a[i].x;
                v2[a[i].y].push_back(t);
            }
            vis[1]=1;
            DFS1(1);
            memset(vis,0,sizeof(vis));
            vis[1]=1;
            DFS2(1);
            int sum=m-2*n,e=0;
            for(int i=1; i<=m; i++)
            {
                if(e>=sum)
                    break;
                if(logg[i]==0)
                    printf("%d %d\n",a[i].x,a[i].y),e++;
            }
        }
    }
    return 0;
}
发布了350 篇原创文章 · 获赞 715 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ZCY19990813/article/details/99938789