Codeforces Round #565 (Div. 3) E. Cover it!

题目:https://codeforces.com/contest/1176/problem/E
思路:奇偶染色

#include<bits/stdc++.h>

using namespace std;

#define DEBUG cout<<"DEBUG"<<endl

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int,int>pii;
typedef pair<ll,ll>pll;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pii> vpii;

const int N=2e5+5;

int n,m;
int vis[N];
vi edge[N];
int cnt[3];

void init()
{
    cnt[1]=cnt[2]=0;
    for(int i=1;i<=n;i++) vis[i]=0,edge[i].clear();
}
void dfs(int u,int x)
{
    vis[u]=x;
    cnt[x]++;
    for(auto y:edge[u]) if(!vis[y]) dfs(y,3-x);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    int T;cin>>T;
    while(T--)
    {
        cin>>n>>m;
        init();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            cin>>u>>v;
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        for(int i=1;i<=n;i++) if(!vis[i]) dfs(i,1);
        int t=cnt[1]<cnt[2]?1:2;
        cout<<cnt[t]<<endl;
        for(int i=1;i<=n;i++) if(vis[i]==t) cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/c4Lnn/p/12114927.html

时间: 2024-10-11 19:29:59

Codeforces Round #565 (Div. 3) E. Cover it!的相关文章

Codeforces Round #565 (Div. 3) C. Lose it!

链接: https://codeforces.com/contest/1176/problem/C 题意: You are given an array a consisting of n integers. Each ai is one of the six following numbers: 4,8,15,16,23,42. Your task is to remove the minimum number of elements to make this array good. An a

Codeforces Round #565 (Div. 3) B

B. Merge it! 题目链接:http://codeforces.com/contest/1176/problem/B 题目 You are given an array a consisting of n integers a1,a2,…,an In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not

Codeforces Round #565 (Div. 3) A

A. Divide it! 题目链接:http://codeforces.com/contest/1176/problem/A 题目 You are given an integer n You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace n with n2 if n is divisible by 2;Rep

Codeforces Round #565 (Div. 3)

传送门 A. Divide it! •题意 给定一个数n, 每次可以进行下列一种操作 1.如果n可以被2整除,用n/2代替n 2.如果n可以被3整除,用2n/3代替n 3.如果n可以被5整除,用4n/5代替n 如果可以经过上述操作使得 n 变为 1,输出最小操作次数,反之,输出-1: •思路 n/2 < 2n/3 < 4n/5 要想操作次数最少,优先操作 1 > 2 > 3: •代码 #include<bits/stdc++.h> using namespace std

Codeforces Round #485 (Div. 2) D. Fair

Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description Some company is going to hold a fair in Byteland. There are $n$ towns in Byteland and $m$ two-way roads between towns. Of course, you can reach any t

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F 1296A - Array with Odd Sum 思路: 如果一开始数组的sum和是奇数,那么直接YES, 否则:如果存在一个奇数和一个偶数,答案为YES,否则为NO 代码: int n; int a[maxn]; int main() { //freopen("D:\\code\\text\\input.txt","r",stdin); //freopen(

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我