版本比较
一些神秘题的笔记
正文
增加 474 行删除 1 行
6 行未改动7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911719219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449569 行未改动6 行未改动钦定 $G'$ 为 $G$ 的补图(即边集为 $G$ 边集对于完全图边集的补集),其边权 $v(G',x,y)=val(G,x,y)$,求 $val(G',x,y)$。
先考虑暴力,这样询问很难不让人想到 kruskal 重构树。对 $G$ 跑一遍重构树,然后重构树维护暴力建补图,再跑一遍重构树即可获得 $O(n+n^2\log n+m\log n)$ 的高昂时间复杂度解法。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 2e5+5;
struct node{ int idx,x,y,z;
bool operator <(node x) const { return this->z < x.z; }}a[N],b[N<<1];
int n,m,f[N<<1];vector<int > e[N<<1];map<pair<int,int >,bool > table;int cntNode,kurskalVal[N<<1];
int fa[N<<1],dep[N<<1];int sz[N<<1],wson[N<<1];int cntId,id[N<<1];int top[N<<1];
inline void init(int n);
inline bool cmp(node x,node y){ return x.idx < y.idx;}
inline int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]);}
inline void merge(int x,int y){ int fx = find(x); int fy = find(y); if(fx != fy) f[fx] = fy;}
inline void dfs1(int x,int father){ fa[x] = father; dep[x] = dep[father] + 1;
sz[x] = 1; for(auto y : e[x]){ if(y != father){ dfs1(y,x); sz[x] += sz[y]; if(sz[y] > sz[wson[x]]) wson[x] = y; } }}
inline void dfs2(int x,int TOP){ id[x] = ++cntId; top[x] = TOP;
if(wson[x]) dfs2(wson[x],TOP); for(auto y : e[x]){ if(y != fa[x] && y != wson[x]) dfs2(y,y); }}
inline int lca(int x,int y){ while(top[x] != top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); x = fa[top[x]]; } return dep[x] < dep[y] ? x : y;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); // freopen("app.in","r",stdin); // freopen("app.out","w",stdout); int T,G; cin >> T >> G; while(T--){ cin >> n >> m;
init(n);
int x,y,z; for(int i = 1; i <= m; i++){ cin >> x >> y >> z; a[i] = (node){i,x,y,z}; table[make_pair(x,y)] = true; table[make_pair(y,x)] = true; }
sort(a+1,a+1+m);
for(int i = 1; i <= m; i++){ x = a[i].x,y = a[i].y,z = a[i].z; int fx = find(x),fy = find(y);
if(fx != fy){ kurskalVal[++cntNode] = z; merge(fx,cntNode),merge(fy,cntNode); e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); } }
dfs1(cntNode,0); dfs2(cntNode,cntNode);
int cntB = 0; for(int i = 1; i < n; i++) for(int j = i+1; j <= n; j++) if(table.find(make_pair(i,j)) == table.end()) b[++cntB] = (node){0,i,j,kurskalVal[lca(i,j)]};
sort(b+1,b+cntB+1);
memset(wson,0,sizeof(wson)); cntNode = n,cntId = 0; for(int i = 1; i <= (n<<1); i++){ f[i] = i; e[i].clear(); kurskalVal[i] = -1; }
for(int i = 1; i <= cntB; i++){ x = b[i].x,y = b[i].y,z = b[i].z; int fx = find(x),fy = find(y);
if(fx != fy){ kurskalVal[++cntNode] = z; merge(fx,cntNode),merge(fy,cntNode); e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); } }
dfs1(cntNode,0); dfs2(cntNode,cntNode);
sort(a+1,a+1+m,cmp);
for(int i = 1; i <= m; i++) cout << kurskalVal[lca(a[i].x,a[i].y)] << " "; cout << endl; } return 0;}
inline void init(int n){ for(int i = 1; i <= (n<<1); i++){ f[i] = i; e[i].clear(); kurskalVal[i] = -1; }
table.clear();
cntId = 0; cntNode = n; memset(wson,0,sizeof(wson));}
```::::
发现难点在于暴力建补图,量级是极大的。那不妨考虑直接求解最小生成树,然后根据其构建 kruskal 重构树。
使用 Boruvka 算法进行构建最小生成树,遍历 $n$ 个点至多 $\log n$ 轮,发现每个点的连边最高可达 $n-1$ 条,考虑如何优化。
算法进行时考虑一个联通块如何连边。显然的是,需要保证连接对象不处于同一个联通块,且在原图中没有连边。
第二个显然是好考虑的,因为在枚举过程中因为原图有边失配是 $O(m)$ 级的,可以接受。不妨令每一个点都要连出去恰好一条边,每轮一个联通块至多连出去或者被连一条边,每轮结束后把连边的联通块合并。
思考如何连,显然可以从点 $x$ 往上倍增到第一个不包含 $x$ 的祖先,连边即可。
一种可行的方法就是在 dfs 序上往前或往后找,找到第一个不在 $x$ 同一个联通块的结点,取两者 lca 更小者即可。
不妨先记录一下,记录 $n$ 叶子结点的 dfs 序和颜色,再 $O(n\log n)$ 按 dfs 序升序排序,然后 $O(n)$ 预处理一下前后缀的最值和次值。
对于前缀坐标最大值、前缀坐标次大值,强制钦定这两者的颜色是不同的,后缀最小和次小是同样的。然后对于一个点 $x$,可以直接 $O(1)$ 跳到一个满足的前后缀,因为两者必有一个不是同一个颜色的。
第二个显然是好考虑的,因为在跳的过程中触发原图有边的失配是 $O(m)$ 级的,可以接受。
那么一轮的时间复杂 $O(n+m)$。
在每一轮选出边的时候,同步构建一个新的 kruskal 重构树。最后 $m$ 次 $O(\log n)$ 询问即可。
总时间复杂度为 $O(n+m\log n+n\log^2 n+(n+m)\log^2 n)$。
有两种优化方法。
第一个将边映射的 map 优化掉。将原图存下来后跑一遍 dfs 求 dfn,拍平在序列上,然后跳的时候判断。
第二种是优化一下最小生成树,用启发式合并去做。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 2e5+5;
struct node{ int idx,x,y,z;
bool operator <(const node x) const { return this->z < x.z; }}a[N];
struct point{ int idx,dfs,col; bool operator <(const point x) const { return this->dfs < x.dfs; }};
int n,m,f[N<<1];int cntBlock;vector<int > e[N<<1];int cntNode,kurskalVal[N<<1];int newKurskalVal[N<<1];vector<node > edgeVec;vector<node > NewEdgeVec;vector<node > edgeAns;
int fa[N<<1],dep[N<<1];int sz[N<<1],wson[N<<1];int cntId,id[N<<1];int g[N<<1],top[N<<1];
vector<point > arr;int preMax[N],preSecMax[N];int sufMin[N],sufSecMin[N];bitset<1> tickNode[N<<1];
unordered_map<lint,bool > hashTable;
inline void init(int n);
inline lint checkHash(int x,int y){ return 1ll * x * 1000000 + y;}
inline bool cmp(node x,node y){ return x.idx < y.idx;}
inline int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]);}
inline void merge(int x,int y){ int fx = find(x); int fy = find(y); if(fx != fy) f[fx] = fy;}
inline void dfs1(int x,int father){ fa[x] = father; dep[x] = dep[father] + 1;
sz[x] = 1; for(auto y : e[x]){ if(y != father){ dfs1(y,x); sz[x] += sz[y]; if(sz[y] > sz[wson[x]]) wson[x] = y; } }}
inline void dfs2(int x,int TOP){ id[x] = ++cntId; g[cntId] = x; top[x] = TOP;
if(wson[x]) dfs2(wson[x],TOP); for(auto y : e[x]){ if(y != fa[x] && y != wson[x]) dfs2(y,y); }}
inline int lca(int x,int y){ while(top[x] != top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); x = fa[top[x]]; } return dep[x] < dep[y] ? x : y;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); // freopen("app.in","r",stdin); // freopen("app.out","w",stdout); int T,G; cin >> T >> G; while(T--){ cin >> n >> m;
init(n);
int x,y,z; for(int i = 1; i <= m; i++){ cin >> x >> y >> z; a[i] = (node){i,x,y,z}; hashTable[checkHash(x,y)] = true; hashTable[checkHash(y,x)] = true; }
sort(a+1,a+1+m);
for(int i = 1; i <= m; i++){ x = a[i].x,y = a[i].y,z = a[i].z; int fx = find(x),fy = find(y);
if(fx != fy){ kurskalVal[++cntNode] = z; merge(fx,cntNode),merge(fy,cntNode); e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); } }
dfs1(cntNode,0); dfs2(cntNode,cntNode);
cntId = 0,cntNode = n; for(int i = 1; i <= (n<<1); i++){ f[i] = i,wson[i] = 0; e[i].clear(); newKurskalVal[i] = -1; }
edgeAns.clear(); while(cntBlock > 1){ arr.clear(),edgeVec.clear(); NewEdgeVec.clear();
for(int i = 1; i <= n; i++){ preMax[i] = preSecMax[i] = 0; sufMin[i] = sufSecMin[i] = 0; f[i] = find(i); tickNode[i].reset(); arr.push_back((point){i,id[i],f[i]}); }
sort(arr.begin(),arr.end()); for(int i = 1; i <= n; i++){ int idx = i - 1; preMax[i] = i; if(i == 1){ preSecMax[i] = 0; continue; } if(arr[idx].col != arr[preMax[i-1]-1].col) preSecMax[i] = preMax[i-1]; else preSecMax[i] = preSecMax[i-1]; }
for(int i = n; i >= 1; i--){ int idx = i - 1; sufMin[i] = i; if(i == n){ sufSecMin[i] = n+1; continue; } if(arr[idx].col != arr[sufMin[i+1]-1].col) sufSecMin[i] = sufMin[i+1]; else sufSecMin[i] = sufSecMin[i+1]; }
for(int i = 1; i <= n; i++){ int idx = i-1,l = i-1,r = i+1;
while(l >= 1){ if(arr[idx].col == arr[l-1].col){ l = preSecMax[l]; continue; } if(hashTable.find(checkHash(arr[idx].idx,arr[l-1].idx)) != hashTable.end()) l--; else break; } while(r <= n){ if(arr[idx].col == arr[r-1].col){ r = sufSecMin[r]; continue; } if(hashTable.find(checkHash(arr[idx].idx,arr[r-1].idx)) != hashTable.end()) r++; else break; }
int lca1 = -1,lca2 = -1,lIdx = -1,rIdx = -1,Idx = arr[idx].idx; if(1 <= l && l <= n) lIdx = arr[l-1].idx,lca1 = lca(lIdx,Idx); else l = -1; if(1 <= r && r <= n) rIdx = arr[r-1].idx,lca2 = lca(rIdx,Idx); else r = -1; if(l == -1 && r == -1) continue; if(l != -1 && r != -1){ if(kurskalVal[lca1] < kurskalVal[lca2]) edgeVec.push_back((node){f[i],Idx,lIdx,kurskalVal[lca1]}); else edgeVec.push_back((node){f[i],Idx,rIdx,kurskalVal[lca2]}); }else if(l != -1) edgeVec.push_back((node){f[i],Idx,lIdx,kurskalVal[lca1]}); else if(r != -1) edgeVec.push_back((node){f[i],Idx,rIdx,kurskalVal[lca2]}); } sort(edgeVec.begin(),edgeVec.end()); for(auto val : edgeVec){ x = val.x,y = val.y,z = val.z; int fx = find(x),fy = find(y); if(fx != fy && tickNode[fx] == false){ cntBlock--,merge(fx,fy); edgeAns.push_back(val); } tickNode[fx].set(); } }
for(int i = 1; i <= (n<<1); i++) f[i] = i;
sort(edgeAns.begin(),edgeAns.end());
for(auto val : edgeAns){ x = val.x,y = val.y,z = val.z; int fx = find(x),fy = find(y); newKurskalVal[++cntNode] = z; merge(fx,cntNode),merge(fy,cntNode); e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); }
dfs1(cntNode,0); dfs2(cntNode,cntNode);
sort(a+1,a+1+m,cmp);
for(int i = 1; i <= m; i++) cout << newKurskalVal[lca(a[i].x,a[i].y)] << " "; cout << endl; } return 0;}
inline void init(int n){ for(int i = 1; i <= (n<<1); i++){ f[i] = i; wson[i] = 0; e[i].clear(); kurskalVal[i] = -1; }
hashTable.clear(); cntId = 0; cntBlock = cntNode = n;}```::::
### P3600 随机数生成器
前置知识点是 $\texttt{min-max}$ 容斥。
69 行未改动