ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeForces - 1176E - Cover it!

2020-02-21 12:55:27  阅读:351  来源: 互联网

标签:ch const int vertices Cover CodeForces 1176E include define


题目链接:https://vjudge.net/problem/CodeForces-1176E

题目描述:

You are given an undirected unweighted connected graph consisting of nn vertices and mm edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.

Your task is to choose at most ⌊n2⌋⌊n2⌋ vertices in this graph so each unchosen vertex is adjacent (in other words, connected by an edge) to at least one of chosen vertices.

It is guaranteed that the answer exists. If there are multiple answers, you can print any.

You will be given multiple independent queries to answer.

Input

The first line contains a single integer tt (1≤t≤2⋅1051≤t≤2⋅105) — the number of queries.

Then tt queries follow.

The first line of each query contains two integers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤min(2⋅105,n(n−1)2)n−1≤m≤min(2⋅105,n(n−1)2)) — the number of vertices and the number of edges, respectively.

The following mm lines denote edges: edge ii is represented by a pair of integers vivi, uiui (1≤vi,ui≤n1≤vi,ui≤n, ui≠viui≠vi), which are the indices of vertices connected by the edge.

There are no self-loops or multiple edges in the given graph, i. e. for each pair (vi,uivi,ui) there are no other pairs (vi,uivi,ui) or (ui,viui,vi) in the list of edges, and for each pair (vi,uivi,ui) the condition vi≠uivi≠ui is satisfied. It is guaranteed that the given graph is connected.

It is guaranteed that ∑m≤2⋅105∑m≤2⋅105 over all queries.

Output

For each query print two lines.

In the first line print kk (1≤⌊n2⌋1≤⌊n2⌋) — the number of chosen vertices.

In the second line print kk distinct integers c1,c2,…,ckc1,c2,…,ck in any order, where cici is the index of the ii-th chosen vertex.

It is guaranteed that the answer exists. If there are multiple answers, you can print any.

Example

Input
2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
6 8
2 5
5 4
4 3
4 1
1 3
2 3
2 6
5 6
Output
2
1 3
3
4 3 6

Note

In the first query any vertex or any pair of vertices will suffice.

Note that you don't have to minimize the number of chosen vertices. In the second query two vertices can be enough (vertices 22 and 44) but three is also ok.

题目大意就是让你最多选n/2个点(向下取整)使得每个点旁边最少有一个被选中的点,因为最多可以选n/2个点,
所以我们把这些点分成两种交替出现的状态,肯定必有一种状态满足题意。
1.通过判断子节点与父节点来选取数量较少的一边:
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, int> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 2e5+10;
int vis[maxn];
vector<int> eage[maxn], odd, even;
void dfs(int u, int flag) {
    if (flag && !vis[u])
        odd.push_back(u);
    else
        even.push_back(u);
    vis[u] = true;
    for (auto node : eage[u])
        if (!vis[node])
            dfs(node, !flag);
}
int main(void) {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 0, u, v; i<m; ++i) {
            scanf("%d%d", &u, &v);
            eage[u].push_back(v);
            eage[v].push_back(u);
        
        dfs(1, 1);
        int size1 = odd.size(), size2 = even.size();
        printf("%d\n", min(size1, size2));
        if (size1 < size2)
            for (int i = 0; i<size1; ++i)
                printf(i == size1-1 ? "%d\n" : "%d ", odd[i]);
        else
            for (int i = 0; i<size2; ++i)
                printf(i == size2-1 ? "%d\n" : "%d ", even[i]);
        for (int i = 0; i<=n; ++i)
            eage[i].clear(), vis[i] = 0;
        odd.clear(), even.clear();
    }
    return 0;
2.通过判断与某个结点距离的奇偶性
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 2e5+10;
int d[maxn];
vector<int> odd, even, eage[maxn];
void bfs() {
    queue<int> qe;
    d[1] = 0;
    qe.push(1);
    even.push_back(1);
    while(!qe.empty()) {
        int t = qe.front();
        qe.pop();
        for (auto to : eage[t]) 
            if (d[to] == INF) {
                d[to] = d[t]+1;
                qe.push(to);
                if (d[to]&1)
                    odd.push_back(to);
                else 
                    even.push_back(to);
            }
    }
}
int main(void) {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 0; i<n+10; ++i)
            d[i] = INF;
        for (int i = 0, u, v; i<m; ++i) {
            scanf("%d%d", &u, &v);
            eage[u].push_back(v);
            eage[v].push_back(u);
        }
        bfs();
        int size1 = odd.size(), size2 = even.size();
        printf("%d\n", min(size1, size2));
        if (size1 < size2)
            for (int i = 0; i<size1; ++i)
                printf(i == size1-1 ? "%d\n" : "%d ", odd[i]);
        else
            for (int i = 0; i<size2; ++i)
                printf(i == size2-1 ? "%d\n" : "%d ", even[i]);
        for (int i = 0; i<=n; ++i)
            eage[i].clear();
        odd.clear(), even.clear();
    }
    return 0;
}

 

标签:ch,const,int,vertices,Cover,CodeForces,1176E,include,define
来源: https://www.cnblogs.com/shuitiangong/p/12340955.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有