当前位置:网站首页>Code shoe set - mt3435 · assignment - bipartite graph problem - Graphic explanation
Code shoe set - mt3435 · assignment - bipartite graph problem - Graphic explanation
2022-06-30 19:31:00 【Tisfy】
Portal
assignment
The time limit :1 second
Space restriction :256M
Title Description
Given a picture containing n Nodes m Undirected graph of strip and edge , You must assign a point weight to each point ( Must be 1 2 3 One of them ), How many different assignment methods are there ( Isn't that what 3^n Do you ???, To look down )
But there is one limitation : That is, the parity of the two endpoints of each edge must be different
Input description
Enter two integers on the first line n,m
Next m Two integers per line u,v representative u and v There is a side between
( Data guarantees that there is no self loop , There is no double edge , It is not guaranteed to be a connected graph )
(1<=n,m<=300000,1<=u,v<=n)
Output description
Enter the total number of schemes , The answer could be very big , The result is right 1e9+7 modulus
Example 1
Input
4 4
1 2
2 3
3 4
1 4
Output
8
Topic analysis
Brief description of bipartite graph
This problem is essentially a bipartite graph problem .
Bipartite graph is to divide all nodes of a graph into two sets A A A、 B B B in , So that the nodes at both ends of each edge are not in a set .

informally , All red nodes cannot have edges directly connected , All blue nodes cannot have edges directly connected .
If we divide a connected graph into the above set A A A and B B B, Then we can put A A A All nodes in are assigned odd numbers , B B B All nodes in the are assigned an even number .
Because odd numbers have 1 1 1 and 3 3 3 Two kinds of , Even numbers only 2 2 2 This kind of , So the number of assignment schemes is 2 a 2^a 2a, among a a a yes A A A Number of nodes in .
Empathy , We can also put A A A The nodes in the are assigned an even number , B B B The nodes in the are assigned an odd number , Then there are 2 b 2^b 2b Two assignment schemes , among b b b Is a collection B B B Number of nodes in .
in summary , One can divide nodes into sets A A A and B B B The bipartite graph of , The node is assigned as 1 、 2 、 3 1、2、3 1、2、3 The assignment scheme with different parity of adjacent nodes is 2 a + 2 b 2^a+2^b 2a+2b
Then the problem becomes how to divide the given graph into different bipartite graphs .
Divided into bipartite graphs
We can use c o l o r [ i ] color[i] color[i] Representation node i i i Grouping of . 0 0 0 Represents a group , 1 1 1 Represents that the node is divided into sets A A A in , 2 2 2 The delegate is divided into sets B B B in .
Traverse every node , If this node has not been partitioned , Take this node as a new bipartite graph “ Source point ”, B F S BFS BFS Go over and count the set of subgraphs A 、 B A、B A、B The number of nodes in .
Be careful : if B F S BFS BFS In the process, I found “ The nodes at both ends of an edge belong to the same set ” The situation of , The graph cannot be divided into bipartite graphs , The number of assignment schemes is 0 0 0.( As shown by the green edge in the following figure )

It turns out that
When we divide this graph into different bipartite graphs , The product of the number of schemes of each bipartite graph , Is the total number of schemes .

For specific implementation, please refer to code comments
AC Code
// Given a picture containing n Nodes m Undirected graph of strip and edge , The parity of the two endpoints of each edge must be different , How many different assignment methods are there
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
const ll mod = 1e9 + 7;
vector<int> a[300010]; // Save map
ll Pow[300010] = {
1}; // Pow[i] = 2^i
int color[300010] = {
0}; // Grouping , The default value is 0 Indicates ungrouped
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
// Preprocessing , Calculation 2^i
Pow[i] = (Pow[i - 1] * 2) % mod;
}
for (int i = 0; i < m; i++) {
// Read in the picture
int l, r;
scanf("%d%d", &l, &r);
a[l].push_back(r);
a[r].push_back(l);
}
ll ans = 1; // answer
for (int i = 1; i <= n; i++) {
// Traverse all nodes
if (!color[i]) {
// Not dyed yet ( The description has not been divided into some sub graph )
int aNode = 0, bNode = 0; // Bipartite graph A、B The number of nodes in
queue<int> q; // bfs queue
function<void(int, int)> addOneNode = [&](int thisNode, int thisColor) {
// Handle a new node
color[thisNode] = thisColor; // Label grouping
q.push(thisNode); // The team
if (thisColor == 1) // Count the number of nodes in the set
aNode++;
else
bNode++;
};
addOneNode(i, 1); // Let's deal with the... Of this subgraph first “ Source point ”
while (q.size()) {
// Start BFS
int thisNode = q.front();
q.pop(); // Team leader
int toColor = (color[thisNode] == 1 ? 2 : 1); // The set to which its adjacent nodes belong
for (int& toNode : a[thisNode]) {
// Traverse all the edges of this node
if (!color[toNode]) {
// Adjacent nodes have not been grouped
addOneNode(toNode, toColor); // Handle this neighboring node ( Partition sets 、 The team 、 Statistics )
}
else {
// This neighboring node has been grouped
if (color[toNode] == color[thisNode]) {
// And this node is also divided into a group
puts("0"); // Can not be divided into bipartite graphs , The number of programmes is 0
return 0;
}
}
}
}
ans = (ans * (Pow[aNode] + Pow[bNode])) % mod; // The product of the number of schemes of each subgraph
}
}
cout << ans << endl;
return 0;
}
Update the weekly competition solution of elite class in advance every week , Focus , Neverlost
Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125537979
边栏推荐
- Code shoe set - mt3111 · assignment
- CODING 正式入驻腾讯会议应用市场!
- Large file transfer software based on UDP protocol
- Practical application of "experience" crawler in work "theory"
- go之web框架 iris
- Iris, the web framework of go
- 商业智能BI与业务管理决策思维之四:业务成本分析
- 基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
- sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户
- Why do more and more people choose cloud rendering?
猜你喜欢
随机推荐
mysql中union和union all的区别
How JS correctly clears all child elements under an element
码蹄集 - MT3111· 赋值
Opencv data type code table dtype
Task04: set operation - addition and subtraction of tables, join, etc. - learning notes of Tianchi Longzhu project SQL training camp
Nodejs 安装与介绍
「经验」爬虫在工作中的实战应用『理论篇』
项目配置了eslint,编辑器没有关闭eslint功能的情况下,eslint没有生效
MQ优缺点(2022.5.2-5.8)
详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
Browser window switch activation event visibilitychange
【UML】UML类图
How to open a futures account safely? Which futures companies are more reliable now?
Develop those things: how to add text watermarks to videos?
com. alibaba. fastjson. Jsonobject tojsonstring eliminate circular reference
torch. roll
Final chapter of binary tree
Build graphql service based on Actix, async graphql, rbatis, pgsql/mysql (4) - change service
CODING 正式入驻腾讯会议应用市场!
Huaxing Securities: kitex practice under the original hybrid Cloud Architecture









