Codeforces 25 D.Roads not only in Berland(并查集)

柔光的暖阳◎ 2022-06-13 12:19 227阅读 0赞

Description

Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.

Input

The first line contains integer n (2 ≤ n ≤ 1000) — amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers a**i, b**i (1 ≤ a**i, b**i ≤ n, a**i ≠ b**i) — pair of cities, which the road connects. It can’t be more than one road between a pair of cities. No road connects the city with itself.

Output

Output the answer, number t — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.

Sample Input

Input

  1. 2
  2. 1 2

Output

  1. 0

Input

  1. 7
  2. 1 2
  3. 2 3
  4. 3 1
  5. 4 5
  6. 5 6
  7. 6 7

Output

  1. 1
  2. 3 1 3 7

题解:

一题并查集的问题,注意如果是两棵树合并是是把根节点的根节点改变

代码;

  1. #include<stdio.h>
  2. #include<cstring>
  3. #include<string>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<math.h>
  7. #include<queue>
  8. #include<stack>
  9. using namespace std;
  10. int pre[1005];
  11. int p[1005];
  12. int t[1005];
  13. int r1[1005];
  14. int r2[1005];
  15. int find(int x)
  16. {
  17. if(x!=pre[x])
  18. return find(pre[x]);
  19. else
  20. return x;
  21. }
  22. int main()
  23. {
  24. int i,j,n,x,y,ans,num=0,d1,d2;
  25. scanf("%d",&n);
  26. memset(p,0,sizeof(p));
  27. for(i=1;i<=n;i++)
  28. {
  29. pre[i]=i;
  30. }
  31. for(i=1;i<n;i++)
  32. {
  33. scanf("%d%d",&x,&y);
  34. if(x>y)
  35. swap(x,y);
  36. d1=find(pre[x]);
  37. d2=find(pre[y]);
  38. if(d1!=x)
  39. {
  40. pre[d2]=d1;
  41. }
  42. else if(d2!=y)
  43. {
  44. pre[d1]=d2;
  45. }
  46. else
  47. pre[d2]=d1;
  48. if(d1==d2)
  49. {
  50. r1[num]=x;
  51. r2[num]=y;
  52. num++;
  53. }
  54. }
  55. ans=0;
  56. for(i=1;i<=n;i++)
  57. {
  58. if(pre[i]==i)
  59. {
  60. t[ans]=i;
  61. ans++;
  62. }
  63. }
  64. printf("%d\n",num);
  65. for(i=0;i<num;i++)
  66. {
  67. printf("%d %d ",r1[i],r2[i]);
  68. printf("%d %d\n",t[i],t[i+1]);
  69. }
  70. return 0;
  71. }

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces 731C Socks

    1 //题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色, 2 //问要让每天的袜子是相同颜色的,要重新

    相关 codeforces 25C. Roads in Berland

    有n个城市,每个城市都能到达别的城市,n\n的矩阵表明i到j城市的最短距离,现在要建造一些新的道路,在两个城市之间。问每次建造这些道路之后每两个城市之间的距离之和为多少。 如