Codeforces Round #694 (Div. 2)F. Strange Housing(染色)

题意:

给你n个点和m条双向边,让你在满足两个老师不相邻的情况下输出老师的点的集合。

思路

可以发现是一个染色问题,让你输出染色为某个值的所有点。

\(vis[i] = 1\)表示染成学生,\(vis[i] = 2\)表示染成老师。

假设将u染色为2,则与其相邻的边必须全部染为1

如果图不联通输出NO。

#include<bits/stdc++.h>

using namespace std;
const int N = 3e5 + 100;

//#define int long long
typedef long long LL;

struct NODE {
	int to, next;
}edge[N << 1];
int head[N], tot;
void add(int u, int v) {
	edge[++tot].to = v;
	edge[tot].next = head[u];
	head[u] = tot;
}

int vis[N];

void dfs(int u) {
	int f = 0;
	for (int i = head[u]; i; i = edge[i].next) {
		int v = edge[i].to;
		if (vis[v] == 2) f = 1;
	}
	if (f) vis[u] = 1;
	else vis[u] = 2;
	for (int i = head[u]; i; i = edge[i].next) {
		int v = edge[i].to;
		if (vis[v])continue;
		dfs(v);
	}
}


void solve() {
	int n, m; scanf("%d%d", &n, &m);
	for (int i = 0 ; i <= n; ++i) head[i] = 0,vis[i] = 0;
	for (int i = 1; i <= m; ++i) {
		int u, v; scanf("%d%d", &u, &v);
		add(u, v);add(v, u);
	}
	dfs(1);
	int cnt = 0;
	vector<int>ans;
	for (int i = 1; i <= n; ++i) {
		if (vis[i] == 2)ans.push_back(i);
		if (vis[i])++cnt;
	}
	if (cnt < n) {
		puts("NO");
		return ;
	}
	printf("YES\n%d\n", ans.size());
	for (int i = 0; i < int(ans.size()); ++i) {
		printf("%d ", ans[i]);
	}
	puts("");

}


signed main() {

    int T = 1;
	scanf("%d",&T);
    // cin >> T;
    while (T--) {
        solve();
    }

}
posted @ 2021-01-17 19:25  waryan  阅读(37)  评论(0编辑  收藏  举报