1. 程式人生 > >soj4522 完全二分圖的判斷

soj4522 完全二分圖的判斷

題意
有三個字母a,b,c,只有相鄰字母或者相同字母才連邊。現在給你一個無向圖,問你是否是這個規則建立出來的。
分析
- wa了很多次,還是思路沒有分析好
- 觀察發現只有a,c這對之間不能有邊相連線,圖2著色問題,就是完全二分圖。
- 注意除了孤立點,整個圖是個完全二分圖。

#include <cstdio>
#include <iostream>
#include <vector>
#include <stack>
#define pr(x) std::cout << #x << ": " << x << " "
#define pl(x) std::cout << #x << ": " << x << std::endl class Solution { public: void input() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j < i; j++) Graph_matrix[i][j] = false; for (int i = 0; i < m; i++) { int
x, y; scanf("%d%d", &x, &y); if (x < y) std::swap(x, y); Graph_matrix[x][y] = true; } } void bulid_graph() { for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { if (Graph_matrix[i][j] == false
) { Graph[i].push_back(j); Graph[j].push_back(i); } } } } bool dfs(int cur, int c) { color[cur] = c; cnt++; for (int i = 0; i < (int)Graph[cur].size(); i++) { int nxt = Graph[cur][i]; if (color[nxt] == c) return false; if (color[nxt] == 0 && !dfs(nxt, -c)) return false; } return true; } void getit(int cur) { st.push(cur); if (color[cur] == 1) one++; else two++; vis[cur] = true; for (int i = 0; i < (int)Graph[cur].size(); i++) { int nxt = Graph[cur][i]; if (!vis[nxt]) getit(nxt); } } void detect_BipartiteGraph() { ans = true; for (int i = 1; i <= n; i++) color[i] = 0, vis[i] = false; int key = 0; for (int i = 1; i <= n; i++) { if (!color[i]) { cnt = 0; ans = dfs(i, 1); if (cnt > 1) key++; } one = 0, two = 0; if (!vis[i]) getit(i); while (!st.empty()) { int cur = st.top(); if (color[cur] == -1) { if ((int)Graph[cur].size() != one) ans = false; } else { if ((int)Graph[cur].size() != two) ans = false; } st.pop(); } if (!ans) break; } puts(ans && key <= 1? "Yes" : "No"); } private: static const int maxn = 503; int n, m, one, two, cnt; bool Graph_matrix[maxn][maxn], ans, vis[maxn]; std::vector<int> Graph[maxn]; std::stack<int> st; int color[maxn]; }; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif int T; scanf("%d", &T); while (T--) { Solution work; work.input(); work.bulid_graph(); work.detect_BipartiteGraph(); } }

相關推薦

soj4522 完全二分判斷

題意: 有三個字母a,b,c,只有相鄰字母或者相同字母才連邊。現在給你一個無向圖,問你是否是這個規則建立出來的。 分析: - wa了很多次,還是思路沒有分析好 - 觀察發現只有a,c這對之間不能有邊相連線,圖2著色問題,就是完全二分圖。 - 注意除了孤

[論][二分判斷]The Accomodation of Students

member ali itl 回路 input The turn 匹配 previous Problem Description There are a group of students. Some of them may know each other, while

二分判斷-筆試題

不同 解析 string code max c++ include href ons 第四範式秋招筆試題 題目 - 給定一個具有n個頂點的圖。要給圖上每個頂點染色,並且要使相鄰的頂點顏色不同。判斷是否能最多用兩種顏色進行染色。題目保證沒有重邊和自環。 概念:把相鄰

二分判斷(dfs)

二分圖著色法判斷 #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int maxn =

Bicoloring UVA - 10004 二分判斷

\(\color{#0066ff}{題目描述}\) 多組資料,n=0結束,每次一個n,m,之後是邊,問你是不是二分圖 \(\color{#0066ff}{輸入樣例}\) 3 3 0 1 1 2 2 0 3 2 0 1 1 2 9 8 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0

洛谷P1330 封鎖陽光大學 二分判斷+dfs

傳送門 如果這個圖不是二分圖就輸出impossible 否則對圖進行染色(圖可能不是聯通的),對每一個聯通塊染成兩種顏色(比如-1和1),然後統計-1和1的個數,用一個數組儲存他們中的較小值,最後對這個陣列求和就行了 #include<iostream> #include<vec

UVA1627-Team them up!(二分判斷+動態規劃)

Problem UVA1627-Team them up! Total Submissions:1228  Solved:139 Time Limit: 3000 mSec Problem Description Your task is to divide a numbe

二分判斷

二分圖又稱為二部圖,其定義是:設G=(V,E)是一個無向圖。如頂點集V可分割為兩個互不相交的子集,並且圖中每條邊依附的兩個頂點都分屬兩個不同的子集。則稱圖G為二分圖。也就是說在二分圖中,頂點可以分為兩個集合X和Y,每一條邊的兩個頂點都分別位於X和Y集合中。它滿足這樣一個特性,

二分匹配入門專題1】M - Cyclic Tour hdu1853【km算法--判斷自重邊】

初始化 case test case 思路 contain first rst ant eve There are N cities in our country, and M one-way roads connecting them. Now Little Tom wa

HDU 2444 (判斷二分+二分最大匹配)

題目連結 題意:首先判斷這個圖是否是二分圖,如果不是二分圖就輸出no,如果是二分圖就輸出最大匹配數 判斷二分圖可以用染色法。。bfs一次就可以 求二分圖最大匹配我用的匈牙利 #include<stdio.h> #include<iostream> #inclu

[leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判斷二分

Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0,2], [1,3], [0,2]] Output: true Explanat

HDU2444(二分的最大匹配+染色法判斷二分

HDU2444   n=200,邊要開到1e5才能過。。。。。。   #include<iostream> #include<cstdio> #include<cstring> #include<queue> #

785. 判斷二分

題目 給定一個無向圖graph,當這個圖為二分圖時返回true。 如果我們能將一個圖的節點集合分割成兩個獨立的子集A和B,並使圖中的每一條邊的兩個節點一個來自A集合,一個來自B集合,我們就將這個圖稱為二分圖。 graph將會以鄰接表方式給出,graph[i]表示圖中與節點i相連的

二分 板題 HDU2444 判斷是否為二分&求二分最大匹配

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss

hdu 2444 The Accomodation of Students (判斷是否是二分,最大匹配)

小記:這題主要是判斷給定的圖是否是二分圖匹配,如果是的,求出最大匹配。這題資料比較水。 思路:求圖是否可以二分,可以使用dfs染色,或者bfs染色,或者並查集, 這裡我使用的是dfs染色,比較簡單的一種。 程式碼: #include <iostream> #

Bad Horse -google-判斷是否是二分

Problem As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too

判斷一個無向是否為二分

要求:先設計演算法,然後用程式實現。程式可允許輸入一個無向圖,然後自動判斷是否為二分圖 注:一個圖G=(V,E)是二分圖如果存在V的一個劃分V= XY,其中XY=空集。 話不多說,直接上程式碼 package test; import java.util.Scanner

NYOJ-1015(判斷是否為二分)

二部圖 時間限制:1000 ms | 記憶體限制:65535 KB 難度:1 描述 二部圖又叫二分圖,我們不是求它的二分圖最大匹配,也不是完美匹配,也不是多重匹配,而是證明一個圖是不是二部圖

BFS/DFS 判斷是否是二分

package Graph; import java.util.ArrayDeque; import java.util.LinkedList; import java.util.Queue; impo

二分判斷 bfs+dfs兩種搜尋方法判斷

二分圖的定義是:給定一個具有n個頂點的圖,要給每個頂點上色,並且使相鄰的頂點顏色不相同。是否能用最多兩種顏色進行染色? 首先我們用鄰接矩陣來模擬圖,使用bfs對整個圖遍歷一遍 #include &l