Light oj 1201 - A Perfect Murder(树形dp)

1201 - A Perfect Murder
Time Limit: 2 second(s)Memory Limit: 32 MB

"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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;

using namespace std;
#define N 1005

int head[N],num;
int dp[N][2];
int father[N];
int n,m;
struct stud{
    int to,ne;
}e[N*2];

int cha(int x)
{
    if(x!=father[x])
        father[x]=cha(father[x]);
    return father[x];
}

inline void add(int u,int v)
{
   e[num].to=v;
   e[num].ne=head[u];
   head[u]=num++;
}

void dfs(int u,int pre)
{
    dp[u][1]=1;
    dp[u][0]=0;
    for(int i=head[u];i!=-1;i=e[i].ne)
    {
        int to=e[i].to;
        if(to==pre) continue;
        dfs(to,u);
        dp[u][1] +=dp[to][0];
        dp[u][0] +=max(dp[to][1],dp[to][0]);
    }
    //printf("%d %d %d\n",u,dp[u][0],dp[u][1]);
}

int main()
{
    int i,j,t,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        num=0;
        int u,v;
        for(int i=0;i<=n;i++)
            father[i]=i;

        while(m--)
        {
           scanf("%d%d",&u,&v);
           add(u,v);
           add(v,u);
           int uu=cha(u);
           int vv=cha(v);
           father[uu]=vv;
        }
        int ans=0;
        for(i=1;i<=n;i++)
            if(father[i]==i)
        {
            dfs(i,-1);
           // printf("cha %d\n",i);
            ans+=max(dp[i][0],dp[i][1]);
        }
        printf("Case %d: %d\n",++ca,ans);
    }
    return 0;
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值