版本比较
一些神秘题的笔记
正文
增加 282 行删除 0 行
205 行未改动全部展开20620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449569 行未改动205 行未改动全部展开在每一轮选出边的时候,同步构建一个新的 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 行未改动