pat 训练 7-2 卷图 (25分) 二分图判定模板题 染色法

作为优秀大学的优秀学生,小w必然活在一个极度内卷的环境里:,每一次的大报告,每一次的期末考试,每一次的作业…

而在大x的某堂课上,在老师分完分组任务后,小w惊奇地发现班上内卷的同学自动分好组了。精通离散数学的小w将内卷的同学抽象成一个个的点,然后必不可能分到一个组的同学之间连出边,小w发现了这张图有如下性质:

图有n个结点,并且图上的n个节点可以分成A,B两个集合;

A∩B=∅

A集合内的点均无连边

B集合内的点均无连边

小w称这样的图为卷图,问题来了,小w自己突然有了很多奇奇怪怪的图,请你判断一下哪些是卷图,哪些不是卷图。
输入格式:

注意本题有多组数据。

每组测试数据第一行将读入一个数t(1≤t≤20),代表测试数据的组数。

之后的t组数据,每组数据的第一行读入两个数n,m(1≤n≤10​5​​,1≤m≤min(​2​​n⋅(n−1)​​,200000)),代表此图由n个点和m条边组成。

接下来m行,每行两个整数u,v(1≤u,v≤n),u≠v,代表点u,v之间连有一条边。

数据保证给出的图是简单图,即无重边与自环
输出格式:

对于每组样例的每张图,如果是卷图,输出Ye5!juantu

否则,输出N0!
输入样例:

在这里给出一组输入。例如:

2
3 3
1 2
2 3
3 1
3 2
1 2
1 3

输出样例:

在这里给出相应的输出。例如:

N0!
Ye5!juantu

题意:判定一张图是否是二分图,多组输入

  1. 板子题,用dfs每条边对每个点染色即可,若相邻边出现相同颜色则不是二分图
#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;
typedef vector<vector<int> > VVI;

#define show(x...) \
	do { \
		cout << "\033[31;1m " << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
	   inline void read(T &x, T2 &... oth) {
		   x = 0;
		   char ch = getchar();
		   ll f = 1;
		   while (!isdigit(ch)) {
			   if (ch == '-') f *= -1; 
			   ch = getchar();
		   }
		   while (isdigit(ch)) {
			   x = x * 10 + ch - 48;
			   ch = getchar();
		   }
		   x *= f;
		   read(oth...);
	   }
	template <typename T, typename... T2>
	   inline void print(T x, T2... oth) {
		   ll p3=-1;
		   if(x<0) putchar('-'), x=-x;
		   do{
				print_f[++p3] = x%10 + 48;
		   } while(x/=10);
		   while(p3>=0) putchar(print_f[p3--]);
		   putchar(' ');
		   print(oth...);
	   }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

#define RED (32)
#define Paint(fa) (fa ? color[fa]^1 : RED)
int n, m, Q, K, color[MAXN], ok = true;
vector<int> G[MAXN];

bool dfs(int u, int fa_color) {
	for(auto v : G[u]) {
		if(color[u] == color[v]) { ok = false; return false; }
		if(0 == color[v]) {
			color[v] = -fa_color;
			dfs(v, -fa_color); //向下传反色
		}
	}
}

void init() {
	ok = true;
	for(int i=0; i<=n+1; i++)
		G[i].clear(), color[i] = 0;
}

signed main() {
#ifdef debug
	freopen("test.txt", "r", stdin);
	clock_t stime = clock();
#endif
	read(Q);
	while(Q--) {
		read(n, m);
		init();	
		int u, v;
		for(int i=1; i<=m; i++) {
			read(u, v);
			G[u].push_back(v), G[v].push_back(u);
		}
		for(int i=1; i<=n; i++) //可能有多个联通块,多个联通块也可能是二分图
			if(!color[i]) color[i] = 1, dfs(i, 1);
		printf("%s\n", (ok) ? "Ye5!juantu" : "N0!");
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值