概述
原题链接:http://codeforces.com/problemset/problem/1176/E
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
题意:给你一个图要你选边(边数最多为n/2向下取整)但有个要求就是如果选了a边,那么必须要有一条没有选的b边。
思路:这是一个经典的染色问题,我们把选的边用1标记,没选的边用0标记即可。作一次图的遍历相邻的边用不同的颜色染色。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#define LL long long
using namespace std;
const int maxn=2e5+100;
struct node
{
int to,Next;
};
node Edge[2*maxn];
int Head[maxn];
int tot[maxn];
int cnt;
int gg0,gg1;
int vis[maxn];
int n,m;
void add(int u,int v)
{
Edge[++cnt].to=v;
Edge[cnt].Next=Head[u];
Head[u]=cnt;
return ;
}
void dfs(int x,int jb)
{
vis[x]=jb;
for(int i=Head[x];i!=-1;i=Edge[i].Next)
{
int v=Edge[i].to;
if(vis[v]==-1)
dfs(v,jb^1);
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
{
Head[i]=-1;
}
for(int i=1;i<=n;i++)
{
vis[i]=-1;
}
cnt=0;
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
gg1=0;
gg0=0;
dfs(1,0);
for(int i=1;i<=n;i++)
{
if(vis[i])
{
gg1++;
}
else
{
gg0++;
}
}
if(gg0>gg1)
{
printf("%dn",gg1);
for(int i=1;i<=n;i++)
{
if(vis[i]==1)
{
printf("%d ",i);
}
}
printf("n");
}
else
{
printf("%dn",gg0);
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
printf("%d ",i);
}
}
printf("n");
}
}
return 0;
}
最后:这个题也可以BFS分层遍历和染色的想法类似。
最后
以上就是热心西装为你收集整理的A - Cover it!的全部内容,希望文章能够帮你解决A - Cover it!所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复