首页 > 其他分享 >HDU 2444

HDU 2444

时间:2023-08-15 17:31:48浏览次数:35  
标签:pre HDU 2444 int flag Edge know each


The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3561    Accepted Submission(s): 1656

Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
 
Input
For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.

Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
 
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
 
Sample Output
No

3

//二分染色+匈牙利


#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct Node
{
    int v;
    int next;
}Edge[200*200*2+10];

int pre[210];

void add(int u,int v,int index)
{
    Edge[index].v=v;
    Edge[index].next=pre[u];
    pre[u]=index;
}
int flag[210],vis[210];
int link[210];
int c[210];


int bfs(int v)
{
    queue <int> q;
    vis[v]=1;
    flag[v]=1;
    q.push(v);
    while(!q.empty())
    {
        int b=q.front();
        q.pop();
        for(int i=pre[b];i!=-1;i=Edge[i].next)
        {
            int e=Edge[i].v;
            if(flag[e]==flag[b])
                return 0;
            if(!flag[e])
            {
                flag[e]=flag[b]==1?2:1;
                q.push(e);
            }
        }
    }
    return 1;
}

bool Find(int v)
{
    for(int i=pre[v];i!=-1;i=Edge[i].next)
    {
        int e=Edge[i].v;
        if(!vis[e])
        {
            vis[e]=1;
            if(link[e]==-1||Find(link[e]))
            {
                link[e]=v;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int a,b,index=1;
        memset(flag,0,sizeof(flag));
        memset(pre,-1,sizeof(pre));
        memset(link,-1,sizeof(link));
        memset(c,0,sizeof(c));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b,index++);
            add(b,a,index++);
            c[a]++,c[b]++;
        }
        int state=0;
        for(int i=1;i<=n;i++)
        {
            if(!flag[i]&&c[i])
            {
                if(!bfs(i))
                {
                    state=1;
                    break;
                }
            }
        }
        int cnt=0;
        if(state)
            printf("No\n");
        else
        {
            for(int i=1;i<=n;i++)
            {
                memset(vis,0,sizeof(vis));
                if(Find(i))
                    cnt++;
            }
            printf("%d\n",cnt/2);  //除以2
        }
    }
    return 0;
}




标签:pre,HDU,2444,int,flag,Edge,know,each
From: https://blog.51cto.com/u_3936220/7091431

相关文章

  • HDU 1698(线段树 区间更新)
    JustaHookTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):23481    AcceptedSubmission(s):11758ProblemDescriptionInthegameofDotA,Pudge’smeathookisactuallythemosthorr......
  • HDU 3829 Cat VS Dog 猫和狗(二分图)结题报告
    听学长说这道题很ex,但是思路想到的话还是挺简单的。可能是受上一道题(放置机器人)的启发,也是找互相冲突的点连线。但是并不是完全一样(废话)放置机器人那道题是找到冲突点连线后直接求最大匹配即可。这道题稍微把思路变换一下,求出最大完美匹配数\(n\)后,说明有\(n*2\)个人的喜好......
  • HDU7326 string magic(Easy Version)
    HDU7326stringmagic(EasyVersion)tag:回文自动机题目链接题意:多组样例,每组输入一字符串(长度1e5以内),输出满足下列条件的子串个数:该串由两个完全相同的回文串拼接而成做法:字符串的题目一般都比较板,洛谷的P4287可以说是这道题目的原题,我们先看看原题是怎么做的P4287双......
  • hdu7365 0 vs 1
    0vs1首先如果两端不同肯定只能直接选。两端都选不了直接失败。不妨设现在是zero在选,从左边来010101交替,如果先出现了一个00比如01010100.....10那么我们就能从这边选,因为one只能跟着我们选这边,最后会出现两边都是0的情况。从右边来同理。假如是0101010交替,那么肯定是平......
  • HDU 多校 Round #6 题解
    HDU多校Round#6题解\(\text{ByDaiRuiChen007}\)A.CountProblemLink题目大意求有多少个长度为\(n\),字符集大小为\(m\)的字符串有长度为\(n-k\)的周期。数据范围:\(n,m,k\le10^{18}\)。思路分析\(k=n\)时答案为\(m^n\),否则转为有长度为\(k\)的Border,答案......
  • ## HDU7328 Snake
    HDU7328Snaketag:容斥,生成函数题目链接题意:1到n个数,分成m组,组队元素排列顺序不同则为不同的组,且每组元素个数不能超过k,问有多少种方案。容斥做法:暂且不管排列的事情,先把问题看成求n个球,m个盒子,盒子不能为空,且每个盒子中球数不能超过k。要保证每盒球数不超过k,我们可通过容......
  • HDU 暑假多校 2023 第六场
    目录写在前面733673417345733773397338写在最后写在前面补题地址:https://acm.hdu.edu.cn/listproblem.php?vol=64,题号7336~7346。哈哈,单刷5题,我是只会做套路题的飞舞。Boc'z那个单曲太牛逼了,最喜欢的一首,但是live上唱的这首是真难绷。以下按个人向难度排序。7336显然......
  • #轮廓线dp#HDU 1400 Mondriaan's Dream
    题目传送门分析状压dp会TLE,考虑用轮廓线dp,设\(dp[i][j][S]\)表示现在处理到\((i,j)\)这个位置轮廓线上状态为\(S\)的情况二进制位为1表示左边或者上方有骨牌跨过轮廓线,然后分类讨论转移一下即可代码#include<cstdio>#include<cstring>usingnamespacestd;con......
  • HDU7331 另解
    \[\begin{aligned}ANS&=\sum_{i=1}^n\binom{n}{i}p^i(1-p)^{n-i}\left(\sum_{j=1}^ij^m\right)&p=\frac{a}{b}\\&=\sum_{j=1}^nj^m\sum_{i=j}^n\binom{n}{i}p^i(1-p)^{n-i}\\&=\sum_{j=1}^n\sum_{k=0}^m{m\bracek}\binom{j}{k}k!\su......
  • HDU1151—Air Raid(最小路径覆盖)
    【\(HDU1151\)】—\(Air\)\(Raid\)(最小路径覆盖)题解描述给定一个\(DAG\)(有向无环图),选定最少的点,使得从这些点出发可以覆盖每一条路径(即每个点都经过至少一遍)。输入:24334132333131223输出21以测试数据为例,\(4\)个路口,\(3\)条路。现派伞兵经过所有......