Assigning Frequencies VJ 补题(记忆化)

Bob wants to assign frequencies to n satellites, numbered from 0 to n−1. Two distinct satellites are said to be adjacent if, when assigned the same frequency, they interfere with each other. Sensibly, Bob’s assignment of frequencies must avoid interference altogether. However, only three frequencies are available to him.
Please determine whether Bob can assign frequencies to all satellites to avoid any interference.
Technical Specification

  1. There are at most 85 test cases
  2. The number of satellites is n, n ≤ 25.
    Input
    The first line of the input contains an integer indicating the number of input cases.
    For each test case, the first line contains an integer n, denoting the number of satellites. The second line contains an integer p, denoting the number of adjacent pairs of satellites. The next p lines each contains two integers i, j, indicating the satellite i may intefer with satelite j when both are assigned the same frequency.
    Output
    For each test case, output ‘Y’ if Bob can assign frequences so there is no integerence. Output ‘N’ otherwise.
    Sample Input
    4 6 6 03 15 32 25 04 10 7 12 65 03 26 35 50 04 45 63 14 12 34 23 7 8 65 03 26 35 14 12 34 23 6 9 01 12 23 52 53 34 24 14 45
    Sample Output
    Y N Y N
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[505][505], b[505], n;
bool dfs(int x)
{
    for(int i = 0; i < x; i++)
    {
        if(a[i][x] && b[i] == b[x])
        {
            return  false;
        }
    }
    if(x == n - 1)
        return true;
    for(int i = 0; i < 3; i++)
    {
        b[x+1] = i;
        if(dfs(x+1))
            return true;
        
    }
    return  false;
}
int main()
{
    int m, i, t;
    scanf("%d",&t);
    while(t--)
    {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        scanf("%d %d",&n,&m);
        for(i = 1; i <= m; i++)
        {
            int x, y;
            scanf("%d %d",&x,&y);
            a[x][y] = a[y][x] = 1;
            
        }
        if(dfs(0))
        {
            printf("Y\n");
        }
        else
        {
            printf("N\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值