思路
测试点2:颜色必须为k种
测试点1,3过不去,大概率是dfs写错了(如果是写的dfs),不能先判断走没走过,要先判断颜色是否一样
代码
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define per(i,x,y) for(int i=x; i>=y; --i)
#define pushk push_back
#define popk pop_back
#define mem(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define ll long long
using namespace std;
const int N = 1e5+9;
vector<int> g[N];
int col[N];
bool vis[N];
int v,e,k;
bool dfs(int u) {
vis[u]=1;
for(auto &it : g[u]) {
if(col[it]==col[u]) return 0;
if(vis[it]) continue;
if(!dfs(it)) return false;
}
return true;
}
int main() {
cin>>v>>e>>k;
rep(i,1,e) {
int u,v;
cin>>u>>v;
g[u].pushk(v);
g[v].pushk(u);
}
int q;
cin>>q;
while(q--) {
set<int> se;
bool ok=1;
mem(vis,0);
rep(i,1,v) {
cin>>col[i];
se.insert(col[i]);
}
rep(i,1,v) {
if(vis[i]==0) {
if(dfs(i)==false) ok=0;
}
}
if(ok==false || se.size()!=k ) puts("No");
else puts("Yes");
}
return 0;
}