首页 > 其他分享 >D - Make Bipartite 2

D - Make Bipartite 2

时间:2022-12-18 00:55:05浏览次数:66  
标签:node ll graph Make bipartite https using Bipartite

D - Make Bipartite 2

https://atcoder.jp/contests/abc282/tasks/abc282_d

 

Simple Graph

https://mathworld.wolfram.com/SimpleGraph.html

 

SimpleGraph

A simple graph, also called a strict graph (Tutte 1998, p. 2), is an unweighted, undirected graph containing no graph loops or multiple edges (Gibbons 1985, p. 2; West 2000, p. 2; Bronshtein and Semendyayev 2004, p. 346). A simple graph may be either connected or disconnected.

Unless stated otherwise, the unqualified term "graph" usually refers to a simple graph. A simple graph with multiple edges is sometimes called a multigraph (Skiena 1990, p. 89).

Multigraph

https://mathworld.wolfram.com/Multigraph.html

 

Multigraph

The term multigraph refers to a graph in which multiple edges between nodes are either permitted (Harary 1994, p. 10; Gross and Yellen 1999, p. 4) or required (Skiena 1990, p. 89, Pemmaraju and Skiena 2003, p. 198; Zwillinger 2003, p. 220). West (2000, p. xiv) recommends avoiding the term altogether on the grounds of this ambiguity.

 

Bipartite Graph -- 二分图

https://mathworld.wolfram.com/BipartiteGraph.html

 

BipartiteGraph

A bipartite graph, also called a bigraph, is a set of graph vertices decomposed into two disjoint sets such that no two graph vertices within the same set are adjacent. A bipartite graph is a special case of a k-partite graph with k=2. The illustration above shows some bipartite graphs, with vertices in each graph colored based on to which of the two disjoint sets they belong.

Bipartite graphs are equivalent to two-colorable graphs. All acyclic graphs are bipartite. A cyclic graph is bipartite iff all its cycles are of even length (Skiena 1990, p. 213).

二分图判断

code

https://www.geeksforgeeks.org/bipartite-graph/

 

https://www.zhihu.com/question/292465499

如何判定

相信大家一定对二分图有了一个初步的了解,那么我们该如何判定二分图呢?

首先我们要引进一个概念,染色

 

判断二分图的常见方法是染色法:用两种颜色,对所有顶点逐个染色,且相邻顶点染不同的颜色

如果发现相邻顶点染了同一种颜色,就认为此图不为二分图

当所有顶点都被染色,且没有发现同色的相邻顶点,就退出

参考

https://atcoder.jp/contests/abc282/submissions/37362401

需要处理存在多个孤立图的情况。

对于每个独立图, 分别计算 同色的点的数量,

独立图之间任意连线是有效的。

 

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
using ll = long long;
using P = pair<int,int>;
using Graph = vector<vector<int>>;
using mint = modint1000000007;

map<ll,vector<ll>> uv;
vector<ll> node;
vector<ll> black;
vector<ll> white;
bool color(int current, int col){
  node[current] = col;
  if(col == 1){
    black.push_back(current);
  }
  else{
    white.push_back(current);
  }
  ll rev;
  if(col == 1){
    rev = -1;
  }
  else{
    rev = 1;
  }
  for(ll nod: uv[current]){
    if(node[nod] == col){
      return false;
    }
    else if(node[nod] == 0){
      if (!color(nod, rev)){
        return false;
      }
    }
  }
  return true;
}

int main() {
  ll n,m;
  cin >> n >> m;
  node.assign(n+1,0);
  rep(i,0,m){
    int u,v;
    cin >> u >> v;
    uv[u].push_back(v);
    uv[v].push_back(u);
  }
  ll sameColors = 0;
  rep(i,1,n+1){
    if(node[i] == 0){
      black.clear();
      white.clear();
      if(!color(i,1)){
        cout << "0" << endl;
        return 0;
      }
      ll bSize = (int)black.size();
      ll wSize = (int)white.size();
      sameColors += bSize*(bSize-1)/2 + wSize*(wSize-1)/2;
    }
  }
  //同じ色
  cout << n*(n-1)/2-m-sameColors << endl;
  return 0;
}

 

标签:node,ll,graph,Make,bipartite,https,using,Bipartite
From: https://www.cnblogs.com/lightsong/p/16989898.html

相关文章

  • gnu build tools(automake ...) 指导
     ​​http://autotoolset.sourceforge.net/tutorial.html#SEC39​​ ​​http://en.wikipedia.org/wiki/GNU_build_system​​  ​​http://autotoolset.sourceforge.ne......
  • ubuntu 11.10(32位系统)下编译android源码 make错误解决办法
    本文介绍在ubuntu11.10系统下编译android2.3.3源码,编译之前请确定上两篇文章中所需的准备工作已经成功完成。编译完成生成系统镜像文件,并在模拟器中运行。准备工作完成......
  • 「REMAKE C++」Day 2
    Day2今天的学习主要是阅读C++Primer第2章结尾以及第3章。decltype类型指示符decltype(f())sum=x;sum类型与f返回类型相同。decltype((variable)),......
  • make学习
    make学习,参考「Makefile20分钟入门,简简单单,展示如何使用Makefile管理和编译C++代码」程序见:https://github.com/ShiqiYu/CPP/tree/main/week03/examples/lab文件结构......
  • Opencv3.4.10 (CMake 编译)windows
    准备工作:下载opencv以及opencv_contrib(包括一些附加功能)源码或opencv下载(下载后解压即可)opencv_contrib下载(下载后解压即可)cmake下载安装MinGW下载(下载后解......
  • 浅谈CMakeLists.txt 增加软件版本信息(很方便)
    1.从一个CMakeLists.txt下手,如下:include_directories(${CMAKE_CURRENT_BINARY_DIR})应该放在最后,但是在引用lib前。#@warninghere:addthevariablesweneedand......
  • When executing step qmake
       QtCreator2.5.0运行其它机器建立的工程文件,总会报错 Whenexecutingstep'qmake'. 一.项目路径中有中文   QtCreator对中文路径处理不太好,改变路径......
  • 在linux下使用CMake构建应用程序
    本文介绍了一个跨平台的自动化构建系统CMake在linux上的使用方法。CMake是一个比automake更加容易使用的工具,能够使程序员从复杂的编译连接过程中解脱出来。文中通......
  • 「REMAKE C++」Day 1
    Day1阅读了一些《C++那些事儿》的基础知识GoogleC++命名风格简单学习。const关键字const对象默认为文件内局部变量//file1intv;externconstintcv;//......
  • 万能makefile深入浅出 - 第四篇
    1.本示例演示的是编译多个可执行程序,库文件,需链接动态库静态库,且需先编译库,并且库与库之间,可执行程序之间皆存在依赖关系的makefile的编写方式(自己写的简单动态库编译和使......