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.
InputThe 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 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — 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#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<set> #include<stack> #include<map> #include<queue> #include<vector> using namespace std; #define ll long long #define INF 0x3fffffff #define maxn 100005 #define mod 998244353 int n,m,k,t,z=0; int fa[maxn]; bool flag[maxn]={0}; vector<int>a,aa,b; int fi(int x){ if(fa[x]==x) return x; return fa[x]=fi(fa[x]); } int x,y,w; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); int fx=fi(x),fy=fi(y); if(fx==fy){ a.push_back(x); aa.push_back(y); z++; } else fa[fy]=fx; } k=fi(1); flag[k]=1; for(int i=2;i<=n;i++) if(flag[fi(i)]==0){ b.push_back(fi(i)); flag[fi(i)]=1; } printf("%d\n",z); for(int i=0;i<z;i++) printf("%d %d %d %d\n",a[i],aa[i],k,b[i]); }