hdu 5277(BestCoder Round#46 1002)YJC counts stars

DFS should be use to search answer.But I'm not good at DFS,So I use many times to debug my program.So sad.

The number of subset don't over 4,Because If the subsets contains 5 or even bigger,It must be have cross.

First I think the depth of the subset shouble be use.But finally I think It not useful.It's just a unuseful way to reduce the search numbers.

The  portal:http://acm.hdu.edu.cn/showproblem.php?pid=5277

#include <stdio.h>
#include <string.h>

int f[1005][1005];
int temp[1005];
int templen;
int n,m;
int cnt3,cnt4;

int test(int k){
    int i;
    for(i=0;temp[i];i++){
        if(f[temp[i]][k] == 0)
            return 0;
    }
    temp[i] = k;
    templen ++;
    return 1;
}

void dfs(int s,int c,int si){
    if(c == 3) cnt3 ++;
    if(c == 4) cnt4 ++;
    for(int i=si;i<=n;i++){
        if(f[s][i]){
            if(test(i)){
                dfs(s,c+1,i+1);
                temp[templen-1] = 0;
                templen --;
            }
        }
    }
}

void Deal_with(){
    cnt3 = 0;cnt4 = 0;
    for(int i=1;i<=n;i++){
        memset(temp,0,sizeof(temp));
        temp[0] = i;
        templen = 1;
        dfs(i,1,i+1);
    }
    if(cnt4){
        printf("4 %d\n",cnt4);
        return ;
    }
    if(cnt3){
        printf("3 %d\n",cnt3);
        return ;
    }
    if(m == 0){
        printf("1 %d\n",n);
        return ;
    }
    printf("2 %d\n",m);
}

int main(void){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    while(~scanf("%d %d",&n,&m)){
        memset(f,0,sizeof(f));
        for(int i=0;i<n;i++){
            int tempa,tempb;
            scanf("%d %d",&tempa,&tempb);
        }
        for(int i=0;i<m;i++){
            int tempa,tempb;
            scanf("%d %d",&tempa,&tempb);
            f[tempa][tempb] = 1;
            f[tempb][tempa] = 1;
        }
        Deal_with();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值