12 October
次小生成树
http://poj.org/problem?id=1679
不难得出,次小生成树可以由最小生成树更换一条边得到。
首先构造原图的最小生成树,然后枚举每一条不在最小生成树中的边 (u, v, w),尝试将这条边加入生成树,因为直接加入边会产生环,所以我们需要在加边之前删去最小生成树上 u 到 v 的路径上权值最大的边。在枚举每一条边时我们都会得到一棵生成树,这些生成树中边权和最小的即为要求的次小生成树。
需要在构造最小生成树时将完整的树结构构造出来,并且使用树上倍增算法查询两点间边权值最大的值。
强连通分量
在一个有向图中,如果某两点间都有互相到达的路径,那么称中两个点强连通,如果任意两点都强连通,那么称这个图为强连通图;一个有向图的极大强连通子图称为强连通分量。
https://oi.men.ci/tarjan-scc-notes/
https://www.cnblogs.com/stxy-ferryman/p/7779347.html
一个强连通分量中的点一定处于搜索树中同一棵子树中。
Tarjan 算法:
low[]
表示这个点以及其子孙节点连的所有点中dfn最小的值.s[]
表示当前所有可能能构成是强连通分量的点.col[]
对强连通分量进行染色.v[to[k]]==false
说明无论如何这个点也不能与u构成强连通分量,因为它不能到达u.low[x]==dfn[x]
说明u点及u点之下的所有子节点没有边是指向u的祖先的了,即u点与它的子孙节点构成了一个最大的强连通图即强连通分量.if (!dfn[i]) tarjan(i);
Tarjan一遍不能搜完所有的点,因为存在孤立点. 所以我们要对一趟跑下来还没有被访问到的点继续跑Tarjan.
均摊时间复杂度 \(O(n)\).
int dfn[N], low[N], t, s[N], st;
int col[N], ct;
bool v[N];
void tarjan(int x) {
dfn[x]=low[x]=++t, s[++st]=x, v[x]=true;
for (int k=head[x]; k; k=nex[k]) {
if (dfn[to[k]]) {if (v[to[k]]) low[x]=min(low[x], dfn[to[k]]); }
else tarjan(to[k]), low[x]=min(low[x], low[to[k]]);
}
if (low[x]==dfn[x]) {
col[x]=++ct, v[x]=false;
while (s[st]!=x) col[s[st]]=ct, v[s[st--]]=false;
--st;
}
}
for (int i=1; i<=n; ++i) if (!dfn[i]) tarjan(i);
for (rint i=1; i<=n; ++i) ++cnt[col[i]];
for (rint i=1; i<=ct; ++i) if (cnt[i]>1) ++ans;
printf("%d\n", ans);
缩点
缩点: 对于 贡献具有传递性 的题,因为强连通分量中的每两个点都是强连通的,可以将一个强连通分量当做一个 超级点,而点权按题意来定.
POJ2186 Popular Cows: 告诉你有n头牛,m个崇拜关系,并且崇拜具有传递性,如果a崇拜b,b崇拜c,则a崇拜c,求最后有几头牛被所有牛崇拜.
显然一个强联通分量内的所有点都是满足条件的,我们可以对整张图进行缩点,然后就简单了.
剩下的所有点都不是强连通的,现在整张图就是一个DAG(有向无环图).
那么就变成一道水题了,因为这是一个有向无环图,不存在所有点的出度都不为零的情况.
所以必然有1个及以上的点出度为零,如果有两个点出度为零,那么这两个点肯定是不相连的,即这两圈牛不是互相崇拜的,于是此时答案为零,如果有1个点出度为0,那么这个点就是被全体牛崇拜的.
这个点可能是一个强联通分量缩成的 超级点,所以应该输出整个强联通分量中点的个数.
/* 以上同 Tarjan 求强连通分量. */
int deg[N], cnt[N];
int tot=0, ans=0;
for (int i=1; i<=n; ++i) {
for (int k=head[i]; k; k=nex[k]) if (col[to[k]]!=col[i]) ++deg[col[i]];
++cnt[col[i]];
}
for (int i=1; i<=ct; ++i) if (!deg[i]) ++tot, ans=cnt[i];
if (!tot || tot>1) printf("0\n"); else printf("%d\n", ans);
割点、桥
无向图.
割点
特判:根节点如果有两个及以上的儿子,那么他也是割点.
int dfn[N], low[N], t, root;
bool cut[N];
void tarjan(int x) {
int flag=0;
dfn[x]=low[x]=++t;
for (int k=head[x]; k; k=nex[k]) {
if (dfn[to[k]]) low[x]=min(low[x], dfn[to[k]]);
else {
tarjan(to[k]), low[x]=min(low[x], low[to[k]]);
if (low[y]>=dfn[x]) {
++flag;
if (x!=root || flag>1) cut[x]=true;
}
}
}
}
for (int i=1; i<=n; ++i) if (!dfn[i]) tarjan(root=i);
for (int i=1; i<=n; ++i) if (cut[i]) printf("%d ", i);
桥
邻接表存图编号从2开始. 即开头 head[0]=1;
.
int dfn[N], low[N], t;
bool bdg[N<<1];
void tarjan(int x, int last) {
dfn[x]=low[x]=++t;
for (int k=head[x]; k; k=nex[k]) {
if (dfn[to[k]]) if (i!=(last^1)) low[x]=min(low[x], dfn[to[k]]);
else {
tarjan(to[k], k), low[x]=min(low[x], low[to[k]]);
if (low[y]>=dfn[x]) bdg[k]=bdg[k^1]=true;
}
}
}
for (int i=1; i<=n; ++i) if (!dfn[i]) tarjan(i, 0);
for (int i=2; i<head[0]; i+=2) if (bdg[i]) printf("%d %d\n", to[i^1], to[i]);
Euler 函数
\]
\]
int phi() {
int m = floor(sqrt(n + 0.5)), ans = n;
for (int i = 2; i <= m; i++) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n != 1) ans = ans / n * (n - 1); // 整体为素数
return ans;
}
https://oi.men.ci/euler-sieve/
模意义下的除法
要求模数为素数。
Fermat 小定理:
\]
\]
inline int pow(long long x, int y) {
long long res=1;
for (; y; x=x*x%mod, y>>=1) if (y&1) res=res*x%mod;
return res;
}
inline int inv(int& x) {return pow(num, mod-2); }
拓展 Euclid 算法:
在对数时间内求出方程 \(ax + by = \gcd(a, b)\) 的一组解。当 b 为素数时,\(\gcd(a, b)=1\),则
\]
式中 \(x\) 即为所求。
void exgcd(int& a, int& b, int &g, int &x, int &y) {
if (!b) g=a, x=1, y=0;
else exgcd(b, a%b, g, y, x), y-=x*(a/b);
}
inline int inv(int& t) {
register int g, x, y; exgcd(t, mod, g, x, y);
retuurn ((x%mod)+mod)%mod;
}
全错位排列递推公式
\]
常数优化
书写优化:
Before | After |
---|---|
x==0 |
!x |
x!=-1 |
~x |
x!=y |
x^y |
x*10 |
(x<<3) + (x<<1) |
x*2+1 |
x<<1|1 |
x%2 |
x&1 |
(x+1)%2 |
x^1 |
x%2==0 |
~(x&1) |
函数参数尽量取地址。手动内联 inline
。
大循环分开来做。register
。(手动 cache)
strlen()
函数提前求好值,避免重复调用。
表达式合并。(手动并行)
重载运算符:
struct mat {
static const int ml=10;
int m[ml][ml];
mat(int x=0) {
memset(m, 0, sizeof(m));
for (int i=0; i<ml; i++) m[i][i]=x;
}
int* operator [] (int& p) {return m[p]; }
};
12 October的更多相关文章
- PHP-----文件系统的交互
本文讲解php中于文件交互中所使用的函数 代码示例 <html> <head> <title> File Detail </title> </he ...
- linux 并发 RCU
What is RCU, Fundamentally? https://lwn.net/Articles/262464/ If you can fill the unforgiving secondw ...
- cg tut
Gesture Drawing with Alex Woo Gesture Drawing with Alex Woo and Louis Gonzales http://eisneim.com/?p ...
- An Implementation of Double-Array Trie
Contents What is Trie? What Does It Take to Implement a Trie? Tripple-Array Trie Double-Array Trie S ...
- Oracle 补丁及opatch 工具介绍
一. CPU(Critical Patch Update) 一个CPU内包含了对多个安全漏洞的修复,并且也包括相应必需的非安全漏洞的补丁.CPU是累积型的,只要安装最新发布的CPU即可,其中包括之前发 ...
- [转帖]Oracle 补丁体系(PSR/PSU/CPU) 及 opatch 工具 介绍
Oracle 补丁体系(PSR/PSU/CPU) 及 opatch 工具 介绍 原文:http://blog.csdn.net/tianlesoftware/article/details/58095 ...
- 转://Oracle补丁及opatch工具介绍
一. CPU(Critical Patch Update) 一个CPU内包含了对多个安全漏洞的修复,并且也包括相应必需的非安全漏洞的补丁.CPU是累积型的,只要安装最新发布的CPU即可,其中包括之前发 ...
- 双数组Trie的一种实现
An Implementation of Double-Array Trie 双数组Trie的一种实现 原文:http://linux.thai.net/~thep/datrie/datrie.htm ...
- ssiOS应用架构谈 本地持久化方案及动态部署
本文转载至 http://casatwy.com/iosying-yong-jia-gou-tan-ben-di-chi-jiu-hua-fang-an-ji-dong-tai-bu-shu.html ...
随机推荐
- 《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week3 Networks and Sockets 12.1 Networked Te ...
- POI读取指定Excel中行与列的数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- protel封装总结(新手必看)
零件封装是指实际零件焊接到电路板时所指示的外观和焊点的位置.是纯粹的空间概念.因此不同的元件可共用同一零件封装,同种元件也可有不同的零件封装.像电阻,有传统的针插式,这种元件体积较大,电路板必须钻孔才 ...
- 【GTS】关于GtsTetheringTestCases模块的几个失败项
GTS---关于GtsTetheringTestCases模块的几个失败项 1.run gts -m GtsTetheringTestCases -t com.google.android.tethe ...
- 20191105 《Spring5高级编程》笔记-【目录】
背景 开始时间:2019/09/18 21:30 Spring5高级编程 版次:2019-01-01(第5版) Spring5最新版本:5.1.9 CURRENT GA 官方文档 Spring Fra ...
- Queen Attack -- 微软2017年预科生计划在线编程笔试第二场
#!/usr/bin/env python # coding:utf-8 # Queen Attack # https://hihocoder.com/problemset/problem/1497 ...
- type动态创建类
在一些特定场合,需要动态创建类,比如创建表单,就会用到type动态创建类,举个例子: class Person(object): def __init__(self,name,age): self.n ...
- DS-二叉排序树的插入、查找和删除
2019-12-02(菜鸡开始学习了...) Data Structure 之 二叉排序树 二叉排序树是给定一个节点后,接下来插入的数如果比它大就会放到它的右孩子那边,比它小就会放到它的左孩子那边. ...
- React.memo
介绍React.memo之前,先了解一下React.Component和React.PureComponent. React.Component React.Component是基于ES6 class ...
- Codeforces - 6E - Exposition - 尺取
https://codeforc.es/problemset/problem/6/E 既然可以多个log,那就直接map伺候.尺取之后要查询区间里面的最大值和最小值的差.众所周知尺取的时候要是不是有序 ...