joj 2672 Hanoi Tower Once More 有向无环图的最小路径覆盖=N-最大匹配

                   2010 Yuanguang Cup ACM-ICPC Collegiate Programming Contest

                 Problem I: Hanoi Tower Once More

                                Input File: i.in  Output: stdout

     Description

     You know the game of Hanoi Tower, right? People stopped moving discs from
peg to peg after they know the number of steps needed to complete the entire task.
But on the other hand, they didn't not stopped thinking about similar puzzles with the
Hanoi Tower. Mr. S invented a little game on it. The game consists of N pegs and a
LOT of balls. Each ball has a different number. The balls look ordinary, but they are
actually magic. If the sum of the numbers on two balls is NOT a square number, they
will push each other with a great force when they're too closed, so they can NEVER
be put together touching each other.

     The player should place one ball on the top of a peg at a time. And we must start
from the smallest number all the way to the largest .So no ball with a smaller number
should be above a ball with a larger number.

     Here comes our question: Given the number on each ball, how many pegs, at
least, are needed to make all the balls placed?

     Input

     The input consists of T test cases. The number of test cases T is given in the first
line of the input. Each test case starts with a line containing one integers n (n≤300),
which is the amount of balls. In the next line, n different positive integers, the number
on each ball, is given.

     Output

     Print the minimum pegs needed to make all the balls placed.

     Sample Input

  2
  4
  1 2 3 4
  4
  1 3 6 10

output

3

1

   

 //

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

const int N=400;//N不能太大 否则超时
int cap[N][N];//初始化要清零
int _link[N];
bool used[N];
int nx,ny;//1->nx
bool _find(int t)
{
    for(int i=1;i<=ny;i++)

    if(!used[i]&&cap[t][i]==1)
    {
        used[i]=true;
        if(_link[i]==-1||_find(_link[i]))
        {
            _link[i]=t;
            return true;
        }
    }
    return false;
}
int MaxMatch()
{
    int num=0;
    memset(_link,-1,sizeof(_link));
    for(int i=1;i<=nx;i++)
    {
        memset(used,false,sizeof(used));
        if(_find(i))   num++;
    }
    return num;
}
int a[N];
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        nx=ny=n;
        memset(cap,0,sizeof(cap));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i]>=a[j]) continue;
                int t=a[i]+a[j];
                int k=sqrt(t+0.5);
                if(k*k==t) cap[i][j]=1;
            }
        }
        int ans=n-MaxMatch();
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值