题意是:,往这个图中的点放置频率,总共有三种频率,如果这两个点连通,并且频率一样,则与题目不符,反之任意两个连通的点频率都要不一样;
思路:dfs暴力枚举,类似于棋盘问题,用flag存频率种类,从0遍历到最后一个点看是否符合题意(注意只有三种频率)
代码:
//Full of love and hope for the life
//https://paste.ubuntu.com/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define BIG 10000
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
//感觉就是一个暴力枚举dfs,将三种频率覆盖这个图(类似于那个棋盘问题)
int a,b,c,x,y;
int flag[100];
int n[100][100];
int dfs(int t,int x){
flag[t]=x;//可以移到下面
for(int i=0;i<t;i++){
if(n[i][t]&&flag[i]==flag[t]){//如果两个点相连,说明,两个点联通并且两个点的频率也一样,直接return0;;
return 0;
}
}
if(t==b-1){//这个地方一开始用的b,有试了b-1,结果都A了;
return 1;
}
for(int i=0;i<3;i++){//题干说只有三种频率
if(dfs(t+1,i)){//如果以这种方式放置三种频率,如果两个点是相同的频率,那么不进if,反之可行的法,return1;
return 1;
}
}
return 0;//如果以这种方式放频率,不可行,那么return0;
}
int main(){
cin >> a;
while(a--){
memset(n,0,sizeof(n));//初始化
memset(flag,0,sizeof(flag));//初始化
cin >> b >> c;
while(c--){
cin >> x >> y;
n[x][y]=1;
n[y][x]=1;
}
if(dfs(0,0)){
cout << "Y" << endl;
}
else{
cout << "N" << endl;
}
}
return 0;
}
//Full of love and hope for the life
//https://paste.ubuntu.com/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define BIG 10000
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
//感觉就是一个暴力枚举dfs,将三种频率覆盖这个图(类似于那个棋盘问题)
int a,b,c,x,y;
int flag[100];
int n[100][100];
int dfs(int t){
for(int i=0;i<t;i++){
if(n[i][t]&&flag[i]==flag[t]){//如果两个点相连,说明,两个点联通并且两个点的频率也一样,直接return0;;
return 0;
}
}
if(t==b-1){//这个地方一开始用的b,有试了b-1,结果都A了;
return 1;
}
for(int i=0;i<3;i++){//题干说只有三种频率
flag[t+1]=i;//可以移到上面去
if(dfs(t+1)){//如果以这种方式放置三种频率,如果两个点是相同的频率,那么不进if,反之可行的法,return1;
return 1;
}
}
return 0;//如果以这种方式放频率,不可行,那么return0;
}
int main(){
cin >> a;
while(a--){
memset(n,0,sizeof(n));//初始化
memset(flag,0,sizeof(flag));//初始化
cin >> b >> c;
while(c--){
cin >> x >> y;
n[x][y]=1;
n[y][x]=1;
}
if(dfs(0)){
cout << "Y" << endl;
}
else{
cout << "N" << endl;
}
}
return 0;
}