[关闭]
@xiaoziyao 2021-08-13T13:36:18.000000Z 字数 2003 阅读 699

CF1142E Pink Floyd 解题报告

解题报告


CF1142E Pink Floyd解题报告:

更好的阅读体验

题意

分析

比较让人迷惑的题。

考虑没有粉色边的情况,我们维护若干个绿色叶向树,每次合并两个根结点就好了,询问次数

然后是粉色边形成一个 DAG 的情况,我们考虑对粉色边的图拓扑排序,同时维护一个集合 。一开始把所有入度为 的结点放到 中,每次取其中两个结点合并,找到删除的那个结点它所有的出边到达的结点放入 中,直到

那么最后的点要么在剩下的那个结点的绿色叶向树中,要么可以被剩下的那个结点经过粉色的边到达。

最后是原题的情况,显然我们要对粉色图进行缩点,删除同一个强联通分量之间的边,形成一个 DAG。考虑将入度为 的 scc 中任取一个点放入 中,每次取 中两个结点合并,设删除的结点为 ,我们让 的 scc 中没有删除过且当前入度为 的的任一结点放入 中,然后再把 所有到达的且当前 scc 没有点在 中的结点放入 中。不断重复上述步骤直到

设剩下的位置为 ,那么被删除的结点一定是 绿色叶向树内的。如果没有被删除,那么 所在连通块(注意,不是强联通块)之外的连通块的结点一定删完了,且 所在连通块其他结点一定可以被 通过粉色边到达。

时间复杂度:

代码

  1. #include<stdio.h>
  2. #include<vector>
  3. #include<queue>
  4. using namespace std;
  5. const int maxn=100005;
  6. int n,m,top,dfns,tot;
  7. int dfn[maxn],low[maxn],stk[maxn],vis[maxn],bel[maxn],deg[maxn],inque[maxn];
  8. vector<int>g[maxn],v[maxn],ok[maxn];
  9. queue<int>q;
  10. void tarjan(int x){
  11. dfn[x]=low[x]=++dfns,stk[++top]=x,vis[x]=1;
  12. for(int i=0;i<g[x].size();i++){
  13. int y=g[x][i];
  14. if(dfn[y]==0)
  15. tarjan(y),low[x]=min(low[x],low[y]);
  16. else if(vis[y])
  17. low[x]=min(low[x],dfn[y]);
  18. }
  19. if(dfn[x]==low[x]){
  20. int y=0;
  21. tot++;
  22. while(y!=x)
  23. y=stk[top--],bel[y]=tot,vis[y]=0;
  24. }
  25. }
  26. int main(){
  27. scanf("%d%d",&n,&m);
  28. for(int i=1;i<=m;i++){
  29. int x,y;
  30. scanf("%d%d",&x,&y);
  31. g[x].push_back(y);
  32. }
  33. for(int i=1;i<=n;i++)
  34. if(dfn[i]==0)
  35. tarjan(i);
  36. for(int i=1;i<=n;i++)
  37. for(int j=0;j<g[i].size();j++){
  38. int k=g[i][j];
  39. if(bel[i]!=bel[k])
  40. v[i].push_back(k),deg[k]++;
  41. }
  42. for(int i=1;i<=n;i++)
  43. if(deg[i]==0){
  44. if(inque[bel[i]]==0)
  45. q.push(i),inque[bel[i]]=1;
  46. else ok[bel[i]].push_back(i);
  47. }
  48. while(q.size()>1){
  49. int x=q.front();
  50. q.pop();
  51. int y=q.front();
  52. q.pop();
  53. printf("? %d %d\n",x,y),fflush(stdout);
  54. int z;
  55. scanf("%d",&z);
  56. if(z==1)
  57. swap(x,y);
  58. q.push(y);
  59. for(int i=0;i<v[x].size();i++){
  60. int y=v[x][i];
  61. deg[y]--;
  62. if(deg[y]==0){
  63. if(inque[bel[y]]==0)
  64. q.push(y),inque[bel[y]]=1;
  65. else ok[bel[y]].push_back(y);
  66. }
  67. }
  68. inque[bel[x]]=0;
  69. if(ok[bel[x]].size())
  70. inque[bel[x]]=1,q.push(ok[bel[x]].back()),ok[bel[x]].pop_back();
  71. }
  72. printf("! %d\n",q.front()),fflush(stdout);
  73. return 0;
  74. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注