Skip to content

Instantly share code, notes, and snippets.

@kukukeke
Created April 8, 2018 06:34
Show Gist options
  • Save kukukeke/06f408b4803afc3074af5ac09edf3212 to your computer and use it in GitHub Desktop.
Save kukukeke/06f408b4803afc3074af5ac09edf3212 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <iostream>
#include <vector>
#include <memory.h>
using namespace std;
int t, v, e; int color[20001]; bool check;
vector<int> adj[20001];
void search(int now, int col) {
//visited[now] = true;
color[now] = col;
for (int i = 0; i < adj[now].size(); i++) {
int next = adj[now][i];
if (color[next] == 0) {
if (col == 1) search(next, 2);
else if (col == 2) search(next, 1);
}
}
}
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d %d", &v, &e);
check = true;
for (int i = 1; i <= v; i++) adj[i].clear();
memset(color, 0, sizeof(int) * 20001);
for (int i = 0; i < e; i++) {
int a, b; scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= v; i++) {
if (color[i] == 0) search(i, 1);
}
for (int i = 1; i <= v; i++) {
for (int j = 0; j < adj[i].size(); j++) {
int next = adj[i][j];
if (color[i] == color[next]) {
check = false;
break;
}
}
}
if (check) printf("YES\n");
else printf("NO\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment