【HDU】HDU5285 贪心+二分染色

基础稍微可以了,来练练手= =

题目意思:

给你N个小朋友,他们之间有M个关系,表示Ai小朋友不认识Aj小朋友

现在需要把他们划为两个集合,要求集合中小朋友不能互不认识

若不能则输出“Poor wyh”

若能,则需要第一个集合尽可能的多,并输出两个集合小朋友数量

思路:

首先是染色判定,这是必要的

然后贪心,当时这一步想了比较长时间

对于一个每一个新加入的节点,总会带来两个颜色集合点数量的增长

我们只需要每次把增长多的那个放进第一个集合就可以了

注意有个坑!当所有小朋友都认识的时候(即M为0)

不能将他们全都放在一个集合里得分出一个到集合2!

附上代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#pragma warning(disable:4996)

#define Zero(a) memset(a, 0, sizeof(a))
#define Neg(a)  memset(a, -1, sizeof(a))
#define All(a) a.begin(), a.end()
#define PB push_back
#define repf(i,a,b) for(i = a;i <= b; i++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,n,1
#define ll long long
#define MAXN 100005
#define mod 1000000007
using namespace std;

vector <int> mp[MAXN];
int col[MAXN];
int n, m, num[3];

bool dfs(int u, int color){
    col[u] = color;
    num[col[u]]++;
    for (int i = 0; i < mp[u].size(); ++i){
        int v = mp[u][i];
        if (col[v]){
            if (col[v] != 3 - col[u]) return false;
        }
        else{
            if (!dfs(v, 3 - color))return false;
        }
    }
    return true;
}

int main(){
    int T;
    int u, v;
    while (~scanf("%d", &T)){
        while (T--){
            scanf("%d%d", &n, &m);
            for (int i = 0; i <= n; ++i){
                mp[i].clear();
                col[i] = 0;
            }
            for (int i = 0; i < m; ++i){
                scanf("%d%d", &u, &v);
                mp[u].push_back(v);
                mp[v].push_back(u);
            }
            num[1] = num[2] = 0;
            int ans1 = 0, ans2 = 0;
            bool flag = true;
            for (int i = 1; i <= n; ++i){
                if (!col[i]){
                    int pt1 = num[1], pt2 = num[2];
                    if (!dfs(i, 1)){
                        flag = false;
                        break;
                    }
                    ans1 += max(num[1] - pt1, num[2] - pt2);
                    ans2 += min(num[1] - pt1, num[2] - pt2);
                    num[1] += pt1, num[2] += pt2;
                }
            }
            if (ans2 == 0) ans1--, ans2++;
            if (!flag || n <= 1)
                printf("Poor wyh\n");
            else
                printf("%d %d\n", ans1, ans2);
        }
    }
    return 0;
}
时间: 2024-10-13 00:08:18

【HDU】HDU5285 贪心+二分染色的相关文章

hdu 1969 Pie(贪心+二分查找)(简单)

Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5628    Accepted Submission(s): 2184 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

Card Game Cheater(贪心+二分匹配)

Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1566    Accepted Submission(s): 822 Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rule

(简单) POJ 2492 A Bug&#39;s Life,二分染色。

Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

hdu 3641 数论 二分求符合条件的最小值数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*==================================================== 二分查找符合条件的最小值 ======================================================*/ ll solve() { __int64 low = 0, high = INF, mid ; while(low <=

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by