题意:找三条同起点同终点的不相交的路径

题解:用tarjan的思想,记录两个low表示最小和次小的dfs序,以及最小和次小的位置,如果次小的dfs序比dfn小,那么说明有两条返祖边,那么就是满足条件的答案

  1. //#pragma GCC optimize(2)
  2. //#pragma GCC optimize(3)
  3. //#pragma GCC optimize(4)
  4. //#pragma GCC optimize("unroll-loops")
  5. //#pragma comment(linker, "/stack:200000000")
  6. //#pragma GCC optimize("Ofast,no-stack-protector")
  7. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  8. #include<bits/stdc++.h>
  9. #define fi first
  10. #define se second
  11. #define db double
  12. #define mp make_pair
  13. #define pb push_back
  14. #define pi acos(-1.0)
  15. #define ll long long
  16. #define vi vector<int>
  17. #define mod 1000000007
  18. #define ld long double
  19. #define C 0.5772156649
  20. #define ls l,m,rt<<1
  21. #define rs m+1,r,rt<<1|1
  22. #define pll pair<ll,ll>
  23. #define pil pair<int,ll>
  24. #define pli pair<ll,int>
  25. #define pii pair<int,int>
  26. //#define cd complex<double>
  27. #define ull unsigned long long
  28. //#define base 1000000000000000000
  29. #define Max(a,b) ((a)>(b)?(a):(b))
  30. #define Min(a,b) ((a)<(b)?(a):(b))
  31. #define fin freopen("a.txt","r",stdin)
  32. #define fout freopen("a.txt","w",stdout)
  33. #define fio ios::sync_with_stdio(false);cin.tie(0)
  34. inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  35. inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
  36. inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
  37. template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
  38. template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
  39. inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
  40. inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
  41. using namespace std;
  42. const double eps=1e-8;
  43. const ll INF=0x3f3f3f3f3f3f3f3f;
  44. const int N=100000+10,maxn=50000+10,inf=0x3f3f3f3f;
  45. vi v[N],road;
  46. int dfn[N],low[N][2],fa[N];
  47. pii pos[N][2];
  48. int ind;
  49. bool ok;
  50. void pr()
  51. {
  52. printf("%d",road.size());
  53. for(int i=0;i<road.size();i++)printf(" %d",road[i]);puts("");
  54. road.clear();
  55. }
  56. void tarjan(int u,int f)
  57. {
  58. if(ok)return ;
  59. dfn[u]=low[u][0]=low[u][1]=++ind;
  60. for(int i=0;i<v[u].size();i++)
  61. {
  62. int x=v[u][i];
  63. if(x==f)continue;
  64. if(!dfn[x])
  65. {
  66. fa[x]=u;
  67. tarjan(x,u);
  68. if(low[x][1]<low[u][0])
  69. {
  70. low[u][1]=low[u][0];
  71. pos[u][1]=pos[u][0];
  72. low[u][0]=low[x][1];
  73. pos[u][0]=pos[x][1];
  74. }
  75. else if(low[x][1]<low[u][1])
  76. {
  77. low[u][1]=low[x][1];
  78. pos[u][1]=pos[x][1];
  79. }
  80. if(low[x][0]<low[u][0])
  81. {
  82. low[u][1]=low[u][0];
  83. pos[u][1]=pos[u][0];
  84. low[u][0]=low[x][0];
  85. pos[u][0]=pos[x][0];
  86. }
  87. else if(low[x][0]<low[u][1])
  88. {
  89. low[u][1]=low[x][0];
  90. pos[u][1]=pos[x][0];
  91. }
  92. }
  93. else if(dfn[x]<dfn[u])
  94. {
  95. if(dfn[x]<low[u][0])
  96. {
  97. low[u][1]=low[u][0];
  98. pos[u][1]=pos[u][0];
  99. low[u][0]=dfn[x];
  100. pos[u][0]=mp(u,x);
  101. }
  102. else if(dfn[x]<low[u][1])
  103. {
  104. low[u][1]=dfn[x];
  105. pos[u][1]=mp(u,x);
  106. }
  107. }
  108. }
  109. if(low[u][1]<dfn[u]&&!ok)
  110. {
  111. ok=1;
  112. printf("%d %d\n",u,pos[u][1].se);
  113. for(int now=u;now!=pos[u][1].se;now=fa[now])road.pb(now);
  114. road.pb(pos[u][1].se);
  115. pr();
  116. road.pb(pos[u][1].se);
  117. for(int now=pos[u][1].fi;now!=u;now=fa[now])road.pb(now);
  118. road.pb(u);
  119. reverse(road.begin(),road.end());
  120. pr();
  121. for(int now=pos[u][1].se;now!=pos[u][0].se;now=fa[now])road.pb(now);
  122. road.pb(pos[u][0].se);
  123. for(int now=pos[u][0].fi;now!=u;now=fa[now])road.pb(now);
  124. road.pb(u);
  125. reverse(road.begin(),road.end());
  126. pr();
  127. // for(int i=1;i<=8;i++)printf("%d %d\n",i,fa[i]);
  128. // printf("%d %d %d %d %d %d %d %d---\n",u,dfn[u],low[u][0],pos[u][0].fi,pos[u][0].se,low[u][1],pos[u][1].fi,pos[u][1].se);
  129. return ;
  130. }
  131. }
  132. int main()
  133. {
  134. freopen("grand.in","r",stdin);
  135. freopen("grand.out","w",stdout);
  136. int T;scanf("%d",&T);
  137. while(T--)
  138. {
  139. int n,m;
  140. scanf("%d%d",&n,&m);
  141. for(int i=1;i<=n;i++)
  142. {
  143. dfn[i]=low[i][0]=low[i][1]=fa[i]=0;
  144. v[i].clear();
  145. }
  146. ind=0;
  147. for(int i=0,x,y;i<m;i++)
  148. {
  149. scanf("%d%d",&x,&y);
  150. v[x].pb(y),v[y].pb(x);
  151. }
  152. ok=0;
  153. for(int i=1;i<=n;i++)
  154. if(!dfn[i])
  155. tarjan(i,-1);
  156. if(!ok)puts("-1");
  157. }
  158. return 0;
  159. }
  160. /********************
  161. 1
  162. 8 9
  163. 1 2
  164. 1 3
  165. 4
  166. 2 5
  167. 3 6
  168. 4 7
  169. 5 8
  170. 6 8
  171. 7 8
  172. ********************/

2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test的更多相关文章

  1. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  2. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  3. 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...

  4. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  5. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  6. 模拟赛小结:2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest 2019年10月11日 15:35-20:35(Solved 8,Penalty 675 ...

  7. 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest (9/12)

    $$2015-2016\ ACM-ICPC,\ NEERC,\ Northern\ Subregional\ Contest$$ \(A.Alex\ Origami\ Squares\) 签到 //# ...

  8. ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...

  9. 2018.10.20 2018-2019 ICPC,NEERC,Southern Subregional Contest(Online Mirror, ACM-ICPC Rules)

    i207M的“怕不是一个小时就要弃疗的flag”并没有生效,这次居然写到了最后,好评=.= 然而可能是退役前和i207M的最后一场比赛了TAT 不过打得真的好爽啊QAQ 最终结果: 看见那几个罚时没, ...

随机推荐

  1. cmd使用管理员权限运行,启动路径不是当前目录

    https://stackoverflow.com/questions/672693/windows-batch-file-starting-directory-when-run-as-admin B ...

  2. First Steps: Command-line

    This brief tutorial will teach how to get up and running with the Flyway Command-line tool. It will ...

  3. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)

    是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...

  4. methods 方法选项

    最简单的使用方法,一个数字,每点击一下按钮加1 html <div id="app"> <span v-text="number">&l ...

  5. Bytom猜谜合约使用指南

    准备工作: 1.安装全节点钱包V1.0.5以上并同步完成: 2.已经发行一种资产,发行资产的方法具体见文章<如何在Bytom上发布资产?> 3.准备好一些BTM作为手续费: 设置谜语(锁定 ...

  6. Vs Code搭建 TypeScript 开发环境

    一.npm install -g typescript 全局安装TypeScript   二.使用Vs Code打开已创建的文件夹,使用快捷键Ctrl+~启动终端输入命令 tsc --init 创建t ...

  7. CART决策树

     CART(Classification and Regression tree)分类回归树由L.Breiman,J.Friedman,R.Olshen和C.Stone于1984年提出.ID3中根据属 ...

  8. ArcGis连接oracle、oracle配置

    服务器:Oracle 11g(我是默认路径安装,自定义路径没成功,不知道为什么) 客户端:arcgis desktop 10.2.oracle 11g 32位客户端 客户端:arcgis server ...

  9. java io 好文传送

    转自:白大虾 地址:https://www.cnblogs.com/baixl/p/4170599.html 主要内容 java.io.File类的使用 IO原理及流的分类 文件流 FileInput ...

  10. MVC数据列表展示【三】

    一.控制器向视图传递参数的两种形式:使用到的技术有EF,linq表达式,StringBuilder,相关技术都可以在我的博客园中找到详细的技术介绍. 1. 第一种是通过字符通过foreach循环拼接将 ...