uva 11069 A Graph Problem

原题:
Given an undirected graph of the following form with n nodes, 1 ≤ n ≤ 76:
这里写图片描述
Your task is to calculate the number of subsets of nodes of the graph with the following properties:
• no nodes in the subset should be connected
• it shouldn’t be possible to add further nodes to the subset without violating the first condition For a graph with 5 nodes the number of subsets which fulfill the above conditions is 4. The subsets are {1,3,5},{2,4},{2,5},{1,4}.
Input
The input will consist of a sequence of numbers n, 1 ≤ n ≤ 76. Each number will be on a separate line.
The input will be terminated by EOF.
Output
Output the number of subsets as described above on a single line. The number of all subsets will be
less than 2 31 .
Sample Input
1
2
3
4
5
30
Sample Output
1
2
2
3
4
4410
中文:
给你一个单节点的无向图如图,现在让你满足如下:
集合里不能有两个相邻的点。 例如图形中有n=3个节点,则集合 {1,2} 是违法的,而集合 {1,3} 是合法的。
当这个集合能再加入任一节点,却可以不和其它节点相邻,则这个集合是违法的。 例如图形中有n=5个节点,则集合 {1,5} 是违法的,因为这个集合再加入节点3仍不和其它节点相邻,而集合 {1,3,5} 则是合法的。
给你n,问你n个节点时的结果是多少?

#include <bits/stdc++.h>
using namespace std;
int ter[100];

int main()
{
    ios::sync_with_stdio(false);
    ter[0]=0;
    ter[1]=1;
    ter[2]=1;
    for(int i=3;i<=76;i++)
    ter[i]=ter[i-3]+ter[i-2];
    int n;
    while(cin>>n)
    {
        if(n==1)
        {
            cout<<1<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<2<<endl;
            continue;
        }
        cout<<ter[n-1]+ter[n-2]+ter[n-3]<<endl;
    }
    return 0;
}

解答:
题目要求比较奇怪,首先要求不能有两个相邻的点,而且得到的集合必须是随意再添加一个节点就能构成相邻。
那么,只有如下可能
每隔一个节点选一个构成集合,每个两个节点选择一个构成集合,或者每隔一个或者两个节点构成集合
现在设ter[n]表示以n为最后一个节点的情况数,比如n=5的时候
ter[5]=2 也就是说只有
2 5和1 3 5满足最后一个节点是5,而且满足题目中的两个条件
那么最后的答案就是ter[n-1]+ter[n-2]+ter[n-3]
表示ter[n-1]表示不选最后一个节点,但是选择第n-1个节点作为结尾的个数,ter[n-2]和ter[n-3]表示选择最后一个节点n作为最后一个节点的个数,分别表示第n个元素与第n-2个元素之间空下的选法和第n个元素与第n-3个元素空下的选法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值