题目描述
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.
输入
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.
输出
For each test case, output “Y” if Bob can assign frequences so there is no integerence. Output “N” otherwise.
样例输入
4 6 6 0 3 1 5 3 2 2 5 0 4 1 0 7 12 6 5 0 3 2 6 3 5 5 0 0 4 4 5 6 3 1 4 1 2 3 4 2 3 7 8 6 5 0 3 2 6 3 5 1 4 1 2 3 4 2 3 6 9 0 1 1 2 2 3 5 2 5 3 3 4 2 4 1 4 4 5
样例输出
Y N Y N
题解:题意是让你将n个卫星分为三组,然后会有一些卫星是互斥的,让你判断存不存在这样的解,可以用二分图匹配做,当然队友让我知道了什么叫暴力出奇迹,下面上暴力代码。
#include <bits/stdc++.h>
using namespace std;
int t,n,p,x,y,flag,flags;
int tail[4];
int ans[4][105];
int a[100][100];
void dfs(int x)
{
if(x==n)
{
flags = 1;
return;
}
for(int i=1;i<=3;i++)
{
flag = 0;
for(int j=0;j<tail[i];j++)
{
if(a[x][ans[i][j]]==1)
{
flag = 1;
break;
}
}
if(flag==0)
{
ans[i][tail[i]++] = x;
dfs(x+1);
if(flags)
return;
tail[i]--;
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&p);
flags = 0;
tail[1] = 0;
tail[2] = 0;
tail[3] = 0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j] = 0;
for(int i=0;i<p;i++)
{
scanf("%d%d",&x,&y);
a[x][y] = 1;
a[y][x] = 1;
}
dfs(0);
if(flags==1)
printf("Y\n");
else
printf("N\n");
}
return 0;
}