洛谷

Codeforces


建议阅读这篇博客作为预备。无耻地打广告


思路

与bzoj4025很相似,思路也差不多,可以看上面那篇博客。

仍然是用二分图的充要条件:没有奇环。

然而这题难在每条边的存在时间不固定,无法一开始知道。

可以每次在加入这条边的时间点判断能否成功修改,确定接下来一段时间它的颜色是什么。

具体见代码。


代码

  1. #include<bits/stdc++.h>
  2. namespace my_std{
  3. using namespace std;
  4. #define pii pair<int,int>
  5. #define fir first
  6. #define sec second
  7. #define MP make_pair
  8. #define rep(i,x,y) for (int i=(x);i<=(y);i++)
  9. #define drep(i,x,y) for (int i=(x);i>=(y);i--)
  10. #define go(x) for (int i=head[x];i;i=edge[i].nxt)
  11. #define sz 505050
  12. typedef long long ll;
  13. template<typename T>
  14. inline void read(T& t)
  15. {
  16. t=0;char f=0,ch=getchar();
  17. double d=0.1;
  18. while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
  19. while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
  20. if(ch=='.')
  21. {
  22. ch=getchar();
  23. while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();
  24. }
  25. t=(f?-t:t);
  26. }
  27. template<typename T,typename... Args>
  28. inline void read(T& t,Args&... args){read(t); read(args...);}
  29. void file()
  30. {
  31. #ifndef ONLINE_JUDGE
  32. freopen("a.txt","r",stdin);
  33. #endif
  34. }
  35. // inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
  36. }
  37. using namespace my_std;
  38. int n,m,K,Q;
  39. struct hh{int u,v,col;}edge[sz];
  40. int id[sz],las[sz],L[sz],R[sz],pre[sz],col[sz];
  41. struct hhhh{int x,y,col;bool s;};
  42. struct hhh
  43. {
  44. int fa[sz],f[sz],dep[sz];
  45. int getfa(int x){return x==fa[x]?x:getfa(fa[x]);}
  46. int getdis(int x){return x==fa[x]?0:f[x]^getdis(fa[x]);}
  47. void cancel(hhhh a){f[fa[a.x]=a.x]=0;dep[a.y]-=a.s;}
  48. bool connected(int x,int y){return getfa(x)==getfa(y);}
  49. hhhh merge(int x,int y,int col)
  50. {
  51. int fx=getfa(x),fy=getfa(y);
  52. if (dep[fx]>dep[fy]) swap(fx,fy),swap(x,y);
  53. int w=getdis(x)^getdis(y)^1;
  54. hhhh ret=(hhhh){fx,fy,col,0};
  55. f[fx]=w;fa[fx]=fy;
  56. if (dep[fx]==dep[fy]) ++dep[fy],ret.s=1;
  57. return ret;
  58. }
  59. }G[55];
  60. void cancel(stack<hhhh>S){while (!S.empty()) G[S.top().col].cancel(S.top()),S.pop();}
  61. vector<int>v[sz<<2];
  62. #define ls k<<1
  63. #define rs k<<1|1
  64. #define lson ls,l,mid
  65. #define rson rs,mid+1,r
  66. void insert(int k,int l,int r,int x,int y,int e)
  67. {
  68. if (x<=l&&r<=y) return (void)(v[k].push_back(e));
  69. int mid=(l+r)>>1;
  70. if (x<=mid) insert(lson,x,y,e);
  71. if (y>mid) insert(rson,x,y,e);
  72. }
  73. void solve(int k,int l,int r)
  74. {
  75. stack<hhhh>S;
  76. rep(i,0,(int)v[k].size()-1)
  77. {
  78. hh e=edge[v[k][i]];
  79. if (!e.col) continue;
  80. int x=e.u,y=e.v,col=e.col;
  81. if (G[col].connected(x,y)) continue;
  82. S.push(G[col].merge(x,y,col));
  83. }
  84. if (l==r)
  85. {
  86. hh e=edge[id[l]];
  87. int w=G[col[l]].getdis(e.u)^G[col[l]].getdis(e.v)^1;
  88. bool t=G[col[l]].connected(e.u,e.v);
  89. cancel(S);
  90. if (!t||!w) return (void)(puts("YES"),edge[id[l]].col=col[l]);
  91. return (void)puts("NO");
  92. }
  93. int mid=(l+r)>>1;
  94. solve(lson);solve(rson);
  95. cancel(S);
  96. }
  97. int main()
  98. {
  99. file();
  100. int x,y;
  101. read(n,m,K,Q);
  102. rep(i,1,K) rep(j,1,n) G[i].fa[j]=j;
  103. rep(i,1,m) read(x,y),edge[i]=(hh){x,y,0};
  104. rep(i,1,Q)
  105. {
  106. read(x,y);
  107. id[i]=x;col[i]=y;
  108. if (las[x]) R[las[x]]=i-1;
  109. pre[i]=las[x];las[x]=i;
  110. L[i]=i+1,R[i]=Q;
  111. }
  112. rep(i,1,Q) if (L[i]<=R[i]) insert(1,1,Q,L[i],R[i],id[i]);
  113. solve(1,1,Q);
  114. return 0;
  115. }

Codeforces 576E Painting Edges [分治,并查集]的更多相关文章

  1. Codeforces 1140F 线段树 分治 并查集

    题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...

  2. 【CF576E】Painting Edges 线段树按时间分治+并查集

    [CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...

  3. 2018.09.30 bzoj4025: 二分图(线段树分治+并查集)

    传送门 线段树分治好题. 这道题实际上有很多不同的做法: cdq分治. lct. - 而我学习了dzyo的线段树分治+并查集写法. 所谓线段树分治就是先把操作分成lognlognlogn个连续不相交的 ...

  4. BZOJ_4025_二分图_线段树按时间分治+并查集

    BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...

  5. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  6. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  7. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  8. hdu_5354_Bipartite Graph(cdq分治+并查集判二分图)

    题目链接:hdu_5354_Bipartite Graph 题意: 给你一个由无向边连接的图,问对于每一个点来说,如果删除这个点,剩下的点能不能构成一个二分图. 题解: 如果每次排除一个点然后去DFS ...

  9. BZOJ 4025: 二分图 [线段树CDQ分治 并查集]

    4025: 二分图 题意:加入边,删除边,查询当前图是否为二分图 本来想练lct,然后发现了线段树分治的做法,感觉好厉害. lct做法的核心就是维护删除时间的最大生成树 首先口胡一个分块做法,和hno ...

随机推荐

  1. Spring AutoWire

    AutoWire 有 ByType ,ByName两种主要使用方式 public class Boss { @Autowired private Car car; public Car getCar( ...

  2. tcp_listen函数

    #include <netdb.h> #include <unistd.h> #include <stddef.h> #include <strings.h& ...

  3. ****** 五十 ******、软设笔记【UML分析和意义】-类图、对象图、状态图、活动图、顺序图、协作图、构件图、部署图,动静态模式

    一.类图(Class Diagram) 描述一组类.接口.协作已经它们之间的图,用来显示系统中各个类的静态结构图. 类之间的关系(relationship) *依赖(dependency) *泛化(g ...

  4. java8 从对象集合中取出某个字段的集合

    public class FeildTest { public static void main(String[] args) { //定义list集合 List<P> list = Ar ...

  5. js call使用

    call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, [,.argN ...

  6. [C++]Linux之C编程异常[true未定义解决方案]

    C语言里面是没有bool(布尔)类型的,C++里面才有,这就是说,在C++里面使用bool类型是没有问题的.bool类型有只有两个值:true =1 .false=0. 但是,C99标准里面,又定义了 ...

  7. Spark SQL自定义外部数据源

    1 涉及到的API BaseRelation: In a simple way, we can say it represents the collection of tuples with know ...

  8. Python 9 进程,线程

    本节内容 python GIL全局解释器锁 线程 进程 Python GIL(Global Interpreter Lock) In CPython, the global interpreter l ...

  9. MSSQL无法启动-原来电脑登录密码改了,重启后要设置

    Sql Server (MSSQLSERVER) 服务无法启动 - 晓菜鸟 - 博客园  http://www.cnblogs.com/52XF/p/4230578.html  --摘抄如下: 一.是 ...

  10. Light oj 1018 - Brush (IV) 状态压缩

    题目大意: 给出n个点的坐标,求至少画多少掉直线才能连接所有点. 题目思路:状态压缩 首先经行预处理,求出所有状态下,那些点不在该状态内 以任意两点为端点求出这条直线的状态 枚举所有状态,找出不在当前 ...