#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
 
int T;
int V, E;
vector<int> graph[20010];
int visited[20010];
 
void BFS(int start)
{    
    queue<int> q;
    
    q.push(start);
    visited[start] = 1// 빨강(1), 파랑(2), 방문X(0) 
    
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        
        for(int i = 0; i < graph[x].size(); i++)
        {
            // 빨강 
            if(visited[x] == 1)
            {
                // 인접한 곳은 파랑으로 색칠
                if(visited[graph[x][i]] == 0)
                {
                    q.push(graph[x][i]);
                    visited[graph[x][i]] = 2;    
                } 
            }
            // 파랑 
            else if(visited[x] == 2)
            {
                // 인접한 곳은 빨강으로 색칠 
                if(visited[graph[x][i]] == 0)
                {
                    q.push(graph[x][i]);
                    visited[graph[x][i]] = 1;    
                } 
            }
        }
    }
}
 
bool isBinary()
{
    for(int i = 1; i <= V; i++)
    {
        for(int j = 0; j < graph[i].size(); j++)
        {
            // 인접한 정점끼리의 색이 같으면 이분그래프X 
            if(visited[i] == visited[graph[i][j]])
            {
                return false;
            }
        }
    }
    
    return true;
}
 
int main(void)
{
//    freopen("B1981_input.txt", "r", stdin);
    
    cin >> T;
    
    while(T--)
    {
        cin >> V >> E;
        
        for(int i = 1; i <= V; i++)
        {
            graph[i].clear();
            visited[i] = 0;
        }
        
        for(int i = 1; i <= E; i++)
        {
            int from, to;
            cin >> from >> to;
            
            graph[from].push_back(to);
            graph[to].push_back(from);
        }
        
        for(int i = 1; i <= V; i++)
        {
            if(visited[i] == 0)
            {
                BFS(i);    
            }
        }
        
        if(isBinary())
        {
            cout << "YES" << endl;    
        } 
        else
        {
            cout << "NO" << endl;
        }
    }
    
    return 0;
}
cs

+ Recent posts