Codeforces 91C Ski Base 加边求欧拉回路数量
题目链接:点击打开链接
题意:
给出n个点m条无向边的图
開始图里没有边。每次加一条边,然后输出图里欧拉回路的条数。
思路:
We will count the number of ski bases including the base consisted of empty subset of edges (before printing just subtract one). In the beginning the number of bases is equal to 1.
If we connect vertexes in the same connected components then the result should be multiplied by 2 else do nothing. You should use DJS data structure to know information
about connected components where vertexes are and to unite them.
Why is it correct?
To prove it we will use the matrix of incidence I, rows in it will be edges and columns will be vertexes. Let's define xor of
two rows. Xor of two rows a и b will
be row c such that ci = ai xor bi.
Notice if xor of some subset of rows is equal to a zero row then this subset form the ski base. It's correct because, the degree of contiguity of every
vertex is even, so we can form an Euler cycle in every connected component. The answer is 2(m - rank(I)).
Why it is correct? Let's write the number of edge from the right of each row which suit this row. While finding the matrix rank using gauss method with xor operation,
we will xor the subsets from the right of the strings. In the end the subsets of edges written from the right of the zero rows will form the basis of the
linear space. Thats why we can take any subset of vectors from basis and make up a new ski base. The number of these subsets is equal to 2k = 2(m - rank(I)),
where k is the number of zero rows.
The last thing we should notice that the adding row is liner depended if and only if there is exist a way between the vertexes a and b (aand b are
the ends of the adding edge).
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
const int N = 100100;
const int mod = 1000000009;
int f[N];
int find(int x){ return x == f[x] ? x : f[x] = find(f[x]); }
bool Union(int x, int y){
int fx = find(x), fy = find(y);
if (fx == fy)return false;
if (fx > fy)swap(fx, fy);
f[fx] = fy;
return true;
}
int n, m;
int main(){
while (cin >> n >> m){
for (int i = 1; i <= n; i++)f[i] = i;
int ans = 1;
while (m--){
int u, v; scanf("%d %d", &u, &v);
if (Union(u, v)==false)
ans = (ans + ans) % mod;
printf("%d\n", ans-1);
}
}
return 0;
}
Codeforces 91C Ski Base 加边求欧拉回路数量的更多相关文章
- POJ 1637 混合图求欧拉回路 最大流实现
前面讲过了无向图,有向图求欧拉回路,欧拉通路的做法.可以直接根据度数来判断,当然前提是这是一个连通图. 这道题既有无向边,又有有向边,然后求欧拉回路. 采用的方法是最大流. 具体处理方法. 首先,我们 ...
- 求欧拉回路 UOJ117
传送门什么是欧拉回路呢……?欧拉回路的定义就是从vi出发到vi,经过每条边有且只有一次的路径. 就很像一笔画. 欧拉回路的性质较多……定理也很多……直接证明很长……我们还是直接说怎么判定,怎么求欧拉回 ...
- 【新知识】队列&bfs【洛谷p1996约瑟夫问题&洛谷p1451求细胞数量】
(是时候为五一培训准备真正的技术了qwq) part1 队列(FIFO) 算法简介: FIFO:First In First Out(先进先出) 队列是限定在一端进行插入,另一端进行删除的特殊线性表 ...
- 利用arguments求任意数量数字的和/最大值/最小值
文章地址 https://www.cnblogs.com/sandraryan/ arguments是函数内的临时数据,用完销毁,有类似于数组的操作,但不是数组. 举个栗子1:利用arguments求 ...
- 洛谷 P1451 求细胞数量
题目链接 https://www.luogu.org/problemnew/show/P1451 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字 ...
- 洛谷P1451 求细胞数量
求细胞数量 题目链接 这道题大概是一个最简单的联通块的题了qwq 注意枚举起点的时候 一定不要从0开始不然你就会从0进入到了其他联通块中从而多查. 一定看清题意这道题不是同色为联通块!!! AC代码如 ...
- 洛谷——P1451 求细胞数量
P1451 求细胞数量 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.(1<=m,n<=10 ...
- POJ1144 Network 题解 点双连通分量(求割点数量)
题目链接:http://poj.org/problem?id=1144 题目大意:给以一个无向图,求割点数量. 这道题目的输入和我们一般见到的不太一样. 它首先输入 \(N\)(\(\lt 100\) ...
- CodeForces - 547D: Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)
As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange ...
随机推荐
- 上传一个npm包
1.先创建一个npm账号 https://www.npmjs.com/signup 2.在cmd里输入命令进入项目文件夹 3.使用npm init 命令创建一个package.json(确保nodej ...
- 我的DBDA类
<?php class DBDA { public $host="localhost"; public $uid="root"; public $pwd= ...
- shell编程中一个空格引起的异常
最近使用shell编写一个备份和替换f服务器程序的脚本replace.sh,初衷是为了节约人力的重复机械的劳动.在脚本编写过程中,由于疏忽多打了一个空格,出现了一个不符合预期的情况. 1.先看一下出问 ...
- StyleAI厚积薄发: Android网络图片数据传输
在StyleAI上厚积了这么长时间,憋了这么久,本来想憋个更大的,不过还是薄发一次的好. 三.直接使用别人的工程 文章:Android学习之客户端上传图片到服务器 下载地址:https://downl ...
- 执行join_paired_ends.py报错Cannot find fastq-join
通过 conda 安装 qiime 1后,在执行join_paired_ends.py时报错: burrito.util.ApplicationNotFoundError: Cannot find f ...
- Vue渲染原理
现在基本所有的框架都已经认同这个看法——DOM应尽可能是一个函数式到状态的映射.状态即是唯一的真相,而DOM状态只是数据状态的一个映射.如下图所示,所有的逻辑尽可能在状态的层面去进行,当状态改变的时候 ...
- <东方梦符祭> N2无尽40波通关
先上图吧 阵容:纯粹 + 伪魔法队 主C:神妈 露米娅(我觉得不厉害了) 灵梦 控制:琪露诺 + 蕾蒂 永江依玖(听说很厉害 没培育满 没看到效果) 挂件:铃仙挂机 帕秋莉 大妖精(链神妈) 圣今天才 ...
- Scrapy 组件的具体用法
一.Spider 用法 在 Scrapy 中,要抓取网站的链接配置.抓取逻辑.解析逻辑都是在 Spider 里完成的.Spider 的一些基础属性和基础方法: name:爬虫名字,Spider的名字定 ...
- __cdecl、__stdcall、__fastcall 与 __pascal 浅析
call 指令与 retn 指令 首先我们得了解 CALL 和 RETN 指令的作用,才能更好地理解调用规则,这也是先决条件. 实际上,CALL 指令就是先将下一条指令的 EIP 压栈,然后 JMP ...
- 常用Linux命令_20190211
1.创建文件夹:mkdir 文件夹名称 2.查看IP地址信息:ipconfig -a 3.查看内存使用情况:free -m 4.查看CPU使用情况:top 5.查看磁盘使用情况:df -a/-h 6. ...