Portal

Description

初始时有\(n(n\leq10^5)\)个孤立的点,依次向图中加入\(m(m\leq3\times10^5)\)条带权无向边。使得图中每个点的度数均为奇数的边集是合法的,其权值定义为集合中的最大边权。每次加入边后,询问权值最小的合法边集的权值,不存在合法边集时输出\(-1\)。

Solution

存在合法边集 \(\Leftrightarrow\) 每个连通块的大小均为偶数。如果某连通块大小为奇数,那么该块的总度数是奇数,但一条无向边会提供两个度数,所以不存在合法边集。如果大小为偶数,那么可以按照如下的方式构造合法边集:

求出该连通块的一棵生成树。由深到浅观察每个点\(u\),如果\(u\)与\(\{ch_u\}\)的边中有偶数个在边集中,那么将边\((u,fa[u])\)加入边集。

做到根的时候,因为除了根以外有奇数个度数为奇数的点,但总度数为偶数,所以根的度数必然是奇数。

该方法将每个偶数连通块划分为了若干个偶数连通块。对于划分后的每个连通块,其最小生成树就是其权值最小的合法边集。

那么现在我们可以进行操作了。首先开一个set<pair<int,int>>记录lct中的边,first记录边权,second记录边的编号。初始用\(cnt\)表示剩余奇数连通块的个数,在其减至\(0\)前一直输出\(-1\)。

当存在合法边集后,加入边\((u,v)\)时,若\(u,v\)已经连通则维护最小生成树,否则在lct中连接\((u,v)\),同时维护set

如果该边被加入lct,那么尝试获得更优答案。从set中最大的边开始,检查能否移除这条边;若能移除则移除并检查次大,不能则break。因为如果当前的最大边不能移除,答案就不会变的更优了。最后输出set中最大的边权。

时间复杂度\(O(mlog(n+m))\)。

Code

  1. //Pastoral Oddities
  2. #include <cstdio>
  3. #include <set>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef pair<int,int> Ipair;
  7. inline char gc()
  8. {
  9. static char now[1<<16],*S,*T;
  10. if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
  11. return *S++;
  12. }
  13. inline int read()
  14. {
  15. int x=0; char ch=gc();
  16. while(ch<'0'||'9'<ch) ch=gc();
  17. while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
  18. return x;
  19. }
  20. int const N=4e5+10;
  21. int const INF=0x3F3F3F3F;
  22. int n,m;
  23. struct edge{int u,v;} ed[N];
  24. set<Ipair> st;
  25. int fa[N],ch[N][2],siz[N],isiz[N];
  26. int val[N],maxL[N]; bool rev[N];
  27. int wh(int p) {return p==ch[fa[p]][1];}
  28. int notRt(int p) {return p==ch[fa[p]][wh(p)];}
  29. void update(int p)
  30. {
  31. int ch0=ch[p][0],ch1=ch[p][1];
  32. siz[p]=siz[ch0]+1+siz[ch1]+isiz[p];
  33. int maxP=max(val[p],max(val[maxL[ch0]],val[maxL[ch1]]));
  34. if(val[p]==maxP) maxL[p]=p;
  35. else if(val[maxL[ch0]]==maxP) maxL[p]=maxL[ch0];
  36. else if(val[maxL[ch1]]==maxP) maxL[p]=maxL[ch1];
  37. }
  38. void pushdw(int p) {if(rev[p]) swap(ch[p][0],ch[p][1]),rev[ch[p][0]]^=1,rev[ch[p][1]]^=1,rev[p]=false;}
  39. void rotate(int p)
  40. {
  41. int q=fa[p],r=fa[q],w=wh(p);
  42. fa[p]=r; if(notRt(q)) ch[r][wh(q)]=p;
  43. fa[ch[q][w]=ch[p][w^1]]=q;
  44. fa[ch[p][w^1]=q]=p;
  45. update(q),update(p);
  46. }
  47. void pushRt(int p) {if(notRt(p)) pushRt(fa[p]); pushdw(p);}
  48. void splay(int p)
  49. {
  50. pushRt(p);
  51. for(int q=fa[p];notRt(p);rotate(p),q=fa[p]) if(notRt(q)) rotate(wh(p)^wh(q)?p:q);
  52. }
  53. void access(int p) {for(int q=0;p;q=p,p=fa[p]) splay(p),isiz[p]+=siz[ch[p][1]]-siz[q],ch[p][1]=q,update(p);}
  54. void makeRt(int p) {access(p),splay(p),rev[p]^=1;}
  55. void path(int p,int q) {makeRt(p),access(q),splay(q);}
  56. void link(int i)
  57. {
  58. int p=ed[i].u,q=ed[i].v; makeRt(p),makeRt(q);
  59. fa[p]=n+i,isiz[n+i]+=siz[p]; update(n+i);
  60. fa[n+i]=q,isiz[q]+=siz[n+i]; update(q);
  61. }
  62. void clear(int p) {fa[p]=ch[p][0]=ch[p][1]=0,isiz[p]=0; update(p);}
  63. void cut(int i)
  64. {
  65. int p=ed[i].u,q=ed[i].v; path(p,q);
  66. fa[p]=ch[p][1]=0; ch[q][0]=0;
  67. clear(n+i); update(p),update(q);
  68. }
  69. int find(int p) {access(p),splay(p); while(ch[p][0]) p=ch[p][0]; return p;}
  70. int odd(int p) {return siz[p]+1>>1&1;}
  71. int main()
  72. {
  73. n=read(),m=read();
  74. for(int i=1;i<=n;i++) val[i]=-INF,update(i);
  75. for(int i=1,cnt=n;i<=m;i++)
  76. {
  77. int u=read(),v=read(),w=read();
  78. ed[i].u=u,ed[i].v=v; val[n+i]=w,maxL[n+i]=n+i;
  79. if(find(u)==find(v))
  80. {
  81. path(u,v); int id=maxL[v]-n,w0=val[id+n];
  82. if(w<w0) cut(id),link(i),st.erase(make_pair(w0,id));
  83. else {printf("%d\n",cnt?-1:st.rbegin()->first); continue;}
  84. }
  85. else
  86. {
  87. makeRt(u),cnt-=odd(u); makeRt(v),cnt-=odd(v);
  88. link(i),cnt+=odd(v);
  89. }
  90. st.insert(make_pair(val[i+n],i));
  91. if(cnt) {puts("-1"); continue;}
  92. while(true)
  93. {
  94. int id=st.rbegin()->second; u=ed[id].u,v=ed[id].v;
  95. path(u,v); if(odd(u)) break;
  96. cut(id); st.erase(*st.rbegin());
  97. }
  98. printf("%d\n",st.rbegin()->first);
  99. }
  100. return 0;
  101. }

P.S.

第一次用set,不大懂...%%%zhx

Codeforces603E - Pastoral Oddities的更多相关文章

  1. CF603E Pastoral Oddities

    CF603E Pastoral Oddities 度数不好处理.转化题意:不存在连通块为奇数时候就成功了(自底向上调整法证明) 暴力:从小到大排序加入.并查集维护.全局变量记录奇数连通块的个数 答案单 ...

  2. 【CF603E】Pastoral Oddities cdq分治+并查集

    [CF603E]Pastoral Oddities 题意:有n个点,依次加入m条边权为$l_i$的无向边,每次加入后询问:当前图是否存在一个生成子图,满足所有点的度数都是奇数.如果有,输出这个生成子图 ...

  3. Codeforces 603E Pastoral Oddities

    传送门:http://codeforces.com/problemset/problem/603/E [题目大意] 给出$n$个点,$m$个操作,每个操作加入一条$(u, v)$长度为$l$的边. 对 ...

  4. CF603E Pastoral Oddities 优先队列+结论+LCT维护生成树

    首先,一个神奇的结论:一个合法的方案存在的条件是每一个联通块的节点数都是偶数个的. 这个可以用数学归纳法简单证一证. 证出这个后,我们只需动态加入每一个边,并查看一下有哪些边能够被删除(删掉后联通块依 ...

  5. cf Round 603

    A.Alternative Thinking(思维) 给出一个01串,你可以取反其中一个连续子串,问取反后的01子串的最长非连续010101串的长度是多少. 我们随便翻一个连续子串,显然翻完之后,对于 ...

  6. CF603EPastoral Oddities

    /* LCT管子题(说的就是你 水管局长) 首先能得到一个结论, 那就是当且仅当所有联通块都是偶数时存在构造方案 LCT动态加边, 维护最小生成联通块, 用set维护可以删除的边, 假如现在删除后不影 ...

  7. ccpc 2018 final G - Pastoral Life in Stardew Valley

    #include <iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  8. On having layout

    英文原文在此:http://www.satzansatz.de/cssd/onhavinglayout.htm 介绍 Internet Explorer 中有很多奇怪的渲染问题可以通过赋予其“layo ...

  9. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

随机推荐

  1. CAS分析

    CAS:Compare and Swap, 翻译成比较并交换.   CAS 指的是现代 CPU 广泛支持的一种对内存中的共享数据进行操作的一种特殊指令.这个指令会对内存中的共享数据做原子的读写操作.简 ...

  2. CF758C Unfair Poll

    题意: On the Literature lesson Sergei noticed an awful injustice, it seems that some students are aske ...

  3. vue中引入字体图标报错,找不到字体文件

    在用vue + webpack进行开发的时候,在引用字体图标遇到字体无法加载的问题: 报以下错误 搞了好久没搞定,最后才找到解决方法(还是没有找到原因) 修改字体图标的css中引入字体文件的路径 以前 ...

  4. javajsp,Servlet:Property 'Id' not found

    avax.el.PropertyNotFoundException: Property 'Id' not found on type  org.androidpn.server.model.CarSo ...

  5. 【java基础】Java锁机制

    在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁(广义) 互斥锁/读写锁(独享锁/共享锁的实现) 乐观锁 ...

  6. How the performance impacts your revenue-性能影响营收

    看完国外一个APM厂商最后的一个业务介绍视频,终于想通了PE领域中最顶层的应用目标,也就是如标题所云.那么这个影响效果是如何做到的?最终的步骤其实很简单,也就是利用大数据进行分析.而自己先前还没有想到 ...

  7. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'needDao' defined in URL

    这个是我修改过后的mapper,是我的mapper中的空间地址写错了呢

  8. 云原生技术图谱 (CNCF Landscape)

    转自:https://raw.githubusercontent.com/cncf/landscape/master/landscape/CloudNativeLandscape_latest.jpg

  9. SQLite -创建数据库

    SQLite -创建数据库 SQLite sqlite3命令用于创建新的SQLite数据库.你不需要有任何特权来创建一个数据库. 语法: sqlite3命令的基本语法如下: $sqlite3 Data ...

  10. android打包需要的图标

    ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12 大小: 32x32.png 48 72 96 144