LOJ 1201 - A Perfect Murder(二分匹配 最大独立集)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1201

"Yes, I am the murderer. No doubt" I had to confess it in front of all. But wait, why I am confessing? Nobody wants to go to jail, neither do I. As you have suspected there is something fishy. So, let me explain a bit.

The murder was happened in 19th June, at 11:30 pm this year (2009) according to the medical report. So, I was asking the judge "Can you find the time 19th June 11:30 pm in Bangladesh?" The judge informed other reporters to find the time. But alas! There
was no time - "2009, 19th June, 11:30 pm". So, the judge got a bit confused about my confession. So, I began to tell them, "The time the murder was happened, is not a valid time according to you. So, how can you claim that I am the murderer?"

And what happened next, you all know. I am in the streets again with a clean sheet.

But now I have planned to kill again. I have a list of N mosquitoes which are to be killed. But there is a small problem. If I kill a mosquito, all of his friends will be informed, so they will be prepared for my attack, thus they will be
impossible to kill. But there is a surprising fact. That is if I denote them as a node and their friendship relations as edges, the graph becomes acyclic.

Now I am planning when and how to kill them (how to get rid of the law!) and you have to write a program that will help me to find the maximum number of mosquito I can kill. Don‘t worry too much, if anything goes wrong I will not mention your name, trust
me!

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a blank line and two integers N (1 ≤ N ≤ 1000) denoting the number of mosquito I want to kill and M denoting the number of friendship configurations. Each of the next M lines contains
two integers a and b denoting that ath and bth mosquitoes are friends. You can assume that(1 ≤ a, b ≤ N, a ≠ b) and each friendship relation is given only once. As I have already mentioned,
you will not find any cycle in the relations.

Output

For each case, print the case number and the maximum number of mosquitoes I can kill considering the conditions described above.

Sample Input

Output for Sample Input


3

4 3

1 2

1 3

1 4

3 2

1 2

2 3

5 4

1 2

1 3

2 4

2 5


Case 1: 3

Case 2: 2

Case 3: 3



PROBLEM SETTER: JANE ALAM JAN

PS:

二分图最大独立集=顶点数-二分图最大匹配

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
//顶点编号从1开始的
#define MAXN 1017
int LN, RN;//L,R数目
int g[MAXN][MAXN], linker[MAXN];
bool used[MAXN];
int dfs(int L)//从左边开始找增广路径
{
    int R;
    for(R = 1; R <= RN; R++)
    {
        if(g[L][R]!=0 && !used[R])
        {
            //找增广路,反向
            used[R]=true;
            if(linker[R] == -1 || dfs(linker[R]))
            {
                linker[R]=L;
                return 1;
            }
        }
    }
    return 0;//这个不要忘了,经常忘记这句
}
int hungary()
{
    int res = 0 ;
    int L;
    memset(linker,-1,sizeof(linker));
    for( L = 1; L <= LN; L++)
    {
        memset(used,0,sizeof(used));
        if(dfs(L) != 0)
            res++;
    }
    return res;
}
int main()
{
    int t;
    int n, m;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(g,0,sizeof(g));

        for(int i = 1; i <= m; i++)
        {
            int x, y;
            scanf("%d%d",&x,&y);
            g[x][y] = 1;
            g[y][x] = 1;
        }
        LN = n;
        RN = n;
        int ans = hungary();//最大匹配数

        printf("Case %d: %d\n",++cas,n-ans/2);
    }
    return 0 ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-02 04:47:05

LOJ 1201 - A Perfect Murder(二分匹配 最大独立集)的相关文章

LightOJ 1201 - A Perfect Murder【二分图最大独立集】

链接:http://www.lightoj.com/volume_showproblem.php?problem=1201 最大独立集= 顶点数- 最大匹配 代码: #include <iostream> #include <algorithm> #include <set> #include <map> #include <string.h> #include <queue> #include <sstream> #in

poj 1274 The Perfect Stall (二分匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 8104 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

hdu 4169 二分匹配最大独立集 ***

题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #incl

Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子可以共存 阴阳离子都至少需要存在一种 那么可以枚举哪2种离子共存 假设枚举a b 然后找到所有的和a可以共存的阴离子(设为x集合)以及和b共存的阳离子(设为y集合) 现在只需求x集合中和y集合中最多有多少个离子可以共存 这个求最大独立集就行了 枚举所有的a b 取最大值 #include <cstdio> #include

lightoj 1201 - A Perfect Murder(树形dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1201 题解:简单的树形dp,dp[0][i]表示以i为根结点不傻i的最多有多少dp[0][i]+=max(dp[0][j],dp[1][i]),dp[1][i]表示i傻的最多有多少dp[1][i]+=dp[0][j]. 注意这些点不一定是全联通的. #include <iostream> #include <cstring> #include <cstdi

POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题

有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开始想,把人看成顶点,若有暗恋的关系,就连一条边,构成一个图 独立集的概念:一个图中两两互不相连的顶点集合 所以这道题,就是要求最大独立集 有:最大独立集+最小顶点覆盖=|V|(顶点的总个数) 那就求最小顶点覆盖了 根据题意: 暗恋的对象性别不同,所以a暗恋b,b暗恋c,c暗恋a这种关系不可能存在 也

POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 10695 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering p

hdu1068Girls and Boys(二分匹配,最大独立集)

Problem Description the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to

POJ 1274 The Perfect Stall 水二分匹配

题目链接:点击打开链接 嘿嘿 #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<queue> #include<functional> #define N 2011 using namespace std; int lef[N], pn;//lef[v]表示Y集的点v 当前连接