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 aibi (1 ≤ ai, bin, aibi) — 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 iand 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.
题意:给n个点,n-1条边,每天通过加边减边得到一颗覆盖所有点的树,问最少多少天。
并查集裸题,遇到环就拆,然后在不同集合中建边
下面是代码:
#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]);
}