Description
Berland government decided to improve relations with neighboring countries. First of all, it is decided to build new roads so the from each city of Berland and neighboring countries it became poss Ible to reach all the others. There is 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 ha s to close some of the existing ones. Every day it's possible to close one existing road and immediately build a new one. Your task is to determine what many days would being needed to rebuild roads so, from each city it became possible to REAC H all the others, and to draw a plan of closure of old roads and building of new ones.
Input
The first line contains an integer n (2≤n≤1000)-amount of cities in Berland and neighboring countries. Next n-1 lines contain the description of roads. Each road was described by space-separated integers ai, bi (1≤ai, bi≤n, Ai≠bi)-pair of cities, which the road C Onnects. It can ' t be more than one road between a pair of cities. No Road connects the city with itself.
Output
Output of the answer, number t-what is the least amount of days needed to rebuild roads so that from each city it became PO Ssible to reach all the others. Then output T lines-the Plan of closure of the 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 NE W Road between Cities U and V is built. Cities is numbered from 1. If The answer is isn't unique, output any.
Sample Input
Input
2
1 2
Output
0
Input
7
1 2
2 3
3 1
4 5
5 6
6 7
Output
1
3 1 3 7
Ideas:
First what kind of side needs to be torn down.
Adding to the collection will produce the edges of the ring.
How it will produce rings.
Using and checking the set is the case that if the same points of the two parent nodes are connected together, a ring will be generated.
Use and check the set to do. The parent node of each node is recorded, and if the parent of the two points to be put into the unified collection is already the same in the build-up phase, then a ring will be generated if the two nodes are joined together-that is, the two nodes are not necessarily linked together, that is, the edges to be torn down.
What kind of side needs to be connected together.
In and out of focus, we just need to pick one point from each of the two sets with the edge, and connect the two points together. As for which point to choose, of course, the root node in the collection is the most convenient, that is, itself is the point of their own parent node.
#include <cstdio> using namespace std; int pre[1090]; int chai[1090]; int jian[1090]; int u
[1090],v[1090];
int n;
int fin (int f) {return F==pre[f]?f:pre[f]=fin (pre[f]);} int main () {scanf ("%d", &n);
int UU,VV;
for (int i=1;i<=n;i++) pre[i]=i;
int cnt=0;
for (int i=1;i<n;i++) {scanf ("%d%d", &u[i],&v[i]);
Uu=fin (U[i]);
Vv=fin (V[i]);
if (UU==VV)//indicates that the edge needs to be torn down chai[++cnt]=i;
else PRE[UU]=VV;
} int ii=0;
for (int i=1;i<=n;i++) {if (pre[i]==i)///The root node in the collection jian[++ii]=i;
} printf ("%d\n", CNT);
for (int i=1;i<=cnt;i++) {printf ("%d%d%d%d\n", u[chai[i]],v[chai[i]],jian[i],jian[i+1]);
} return 0; }