E. Clockwork Bomb

题目连接:

http://www.codeforces.com/contest/650/problem/E

Description

My name is James diGriz, I'm the most clever robber and treasure hunter in the whole galaxy. There are books written about my adventures and songs about my operations, though you were able to catch me up in a pretty awkward moment.

I was able to hide from cameras, outsmart all the guards and pass numerous traps, but when I finally reached the treasure box and opened it, I have accidentally started the clockwork bomb! Luckily, I have met such kind of bombs before and I know that the clockwork mechanism can be stopped by connecting contacts with wires on the control panel of the bomb in a certain manner.

I see n contacts connected by n - 1 wires. Contacts are numbered with integers from 1 to n. Bomb has a security mechanism that ensures the following condition: if there exist k ≥ 2 contacts c1, c2, ..., ck forming a circuit, i. e. there exist k distinct wires between contacts c1 and c2, c2 and c3, ..., ck and c1, then the bomb immediately explodes and my story ends here. In particular, if two contacts are connected by more than one wire they form a circuit of length 2. It is also prohibited to connect a contact with itself.

On the other hand, if I disconnect more than one wire (i. e. at some moment there will be no more than n - 2 wires in the scheme) then the other security check fails and the bomb also explodes. So, the only thing I can do is to unplug some wire and plug it into a new place ensuring the fact that no circuits appear.

I know how I should put the wires in order to stop the clockwork. But my time is running out! Help me get out of this alive: find the sequence of operations each of which consists of unplugging some wire and putting it into another place so that the bomb is defused.

Input

The first line of the input contains n (2 ≤ n ≤ 500 000), the number of contacts.

Each of the following n - 1 lines contains two of integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi) denoting the contacts currently connected by the i-th wire.

The remaining n - 1 lines contain the description of the sought scheme in the same format.

It is guaranteed that the starting and the ending schemes are correct (i. e. do not contain cicuits nor wires connecting contact with itself).

Output

The first line should contain k (k ≥ 0) — the minimum number of moves of unplugging and plugging back some wire required to defuse the bomb.

In each of the following k lines output four integers ai, bi, ci, di meaning that on the i-th step it is neccesary to unplug the wire connecting the contacts ai and bi and plug it to the contacts ci and di. Of course the wire connecting contacts ai and bi should be present in the scheme.

If there is no correct sequence transforming the existing scheme into the sought one, output -1.

Sample Input

3

1 2

2 3

1 3

3 2

Sample Output

1

1 2 1 3

Hint

题意

给你两棵树,你只能操作第一棵树,你每次操作是删除一条边,加一条边

但是都不能构成环,然后问你最少多少步。

题解:

可以强行用LCT做动态最小生成树无脑肝过去就好了。

我推荐rng58的做法:

我们从第一棵树的dfs顺序开始考虑,做到第u个点了,v是u的父亲,如果边(v,u)存在在第二棵树,显然我们就不用动这条边。否则的话,我们将u点连向第二棵树u点的父亲fa[u]就好了。

现在有一个问题,如果第二棵树中u点已经连了父亲fa[u]了,怎么办?

把(u,w)这条边变成(find(u),fa[find(u)])就好了。find是并查集,找到第一个在第一棵树不和自己第二棵树fa[u]相连接的点。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 5e5+7;
  4. vector<int> E[2][maxn];
  5. int fa[2][maxn];
  6. int father[maxn];
  7. int tot=0;
  8. int ans[maxn][4];
  9. void addans(int x,int y,int z,int p)
  10. {
  11. ans[tot][0]=x,ans[tot][1]=y,ans[tot][2]=z,ans[tot][3]=p;
  12. tot++;
  13. }
  14. int fi(int u){
  15. return u != father[u] ? father[u] = fi( father[u] ) : u;
  16. }
  17. bool check(int id,int x,int y)
  18. {
  19. if(x==0||y==0)return false;
  20. if(fa[id][x]==y||fa[id][y]==x)return true;
  21. return false;
  22. }
  23. void dfs(int id,int x,int f)
  24. {
  25. fa[id][x]=f;
  26. for(int i=0;i<E[id][x].size();i++)
  27. {
  28. int v = E[id][x][i];
  29. if(v==f)continue;
  30. dfs(id,v,x);
  31. }
  32. }
  33. void solve(int x,int f)
  34. {
  35. for(int i=0;i<E[0][x].size();i++)
  36. {
  37. int v = E[0][x][i];
  38. if(v==f)continue;
  39. solve(v,x);
  40. if(fa[1][v]!=x&&fa[1][x]!=v)
  41. {
  42. int p = fi(v);
  43. addans(x,v,p,fa[1][p]);
  44. }
  45. }
  46. }
  47. int main()
  48. {
  49. int n;
  50. scanf("%d",&n);
  51. for(int i=1;i<n;i++)
  52. {
  53. int x,y;scanf("%d%d",&x,&y);
  54. E[0][x].push_back(y);
  55. E[0][y].push_back(x);
  56. }
  57. for(int i=1;i<n;i++)
  58. {
  59. int x,y;scanf("%d%d",&x,&y);
  60. E[1][x].push_back(y);
  61. E[1][y].push_back(x);
  62. }
  63. dfs(0,1,0);
  64. dfs(1,1,0);
  65. for(int i=1;i<=n;i++)
  66. {
  67. if(!check(0,i,fa[1][i]))
  68. father[i]=i;
  69. else
  70. father[i]=fa[1][i];
  71. }
  72. solve(1,0);
  73. cout<<tot<<endl;
  74. for(int i=0;i<tot;i++,cout<<endl)
  75. for(int j=0;j<4;j++)
  76. printf("%d ",ans[i][j]);
  77. }

Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集的更多相关文章

  1. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  2. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #345 (Div. 1) C. Table Compression (并查集)

    Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...

  4. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  5. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  6. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

  7. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  8. Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)

    题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. python windows下安装celery调度任务时出错

    由于celery 4.0不支持windows系统.所以用命令pip install Celery安装的celery是最新版4.0的不能在windows下运行. 在windows命令窗口运行: cele ...

  2. IE中部分版本的浏览器对Select标签设置innerHTML无效的问题

    这样写的代码:document.getElementById('data_list').innerHTML = data;//data是ajax返回的数据 结果发现在ie10的兼容模式下面下拉框没有内 ...

  3. LCT 文档

    file:///C:/Users/Frank/Downloads/QTREE%E8%A7%A3%E6%B3%95%E7%9A%84%E4%B8%80%E4%BA%9B%E7%A0%94%E7%A9%B ...

  4. Myeclipse实用快捷键总结

    alt+shift+J 为选中的类/方法添加注释 ctrl+T 显示选中类的继承树 ctrl+shift+X/Y 将选中的字符转换为大写/小写 ctrl+shift+R 打开资源 ctrl+shift ...

  5. Nginx-进程模型

    1.整体框架 正常执行起来的Nginx有很多进程,有master_process和worker_process进程,master_process是监控进程即主线程,worker_process是工作进 ...

  6. POJ-1830

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6294   Accepted: 2393 Description ...

  7. Freemarker的页面和JS遍历后台传入的Map

    后端传到前端的Map Freemarker页面遍历Map: JS遍历Map:

  8. f'lask源码

    上下文本质 ? 1 2 3 4 5 6 7 8 - 当请求过来后,将请求相关数据添加到 Local()类中     {         线程或协程唯一标识:{"stack":[re ...

  9. AC日记——[Hnoi2017]影魔 bzoj 4826

    4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...

  10. AC日记——文化之旅 洛谷 P1078

    文化之旅 思路: 暴搜,倒搜: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 105 #define max ...