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
- There are at most 85 test cases
- 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;
}