• Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland


    D. Roads not only in Berland
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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, 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

    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 test(s)
    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

     并查集

    先跑一遍并查及,分成几个集合。在查询父节点的时候,如果x=findfa(a),y=findfa(b) x=y那么,这条道路a-b就应该封闭

    再跑一遍并查集,把这几个集合连起来....就得到要修的道路编号了

    /* ***********************************************
    Author        :pk29
    Created Time  :2015/8/23 11:19:23
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    bool cmp(int a,int b){
        return a>b;
    }
    int fa[maxn],n;
    int a,b;
    vector<pair<int ,int> >v1;
    vector<pair<int,int> >v2;
    
    void init(){
        for(int i=0;i<=n;i++){
            fa[i]=i;
        }
        v1.clear();
        v2.clear();
    }
    int findfa(int x){
        if(x==fa[x])return x;
        else return fa[x]=findfa(fa[x]);
    }
    void Union(int a,int b){
        int x=findfa(a);
        int y=findfa(b);
        if(x!=y){
            if(x<y)fa[y]=x;
            else fa[x]=y;
            return ;
        }
        else v1.push_back(make_pair(a,b));
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        
        while(cin>>n){
            init();
            for(int i=1;i<=n-1;i++){
                scanf("%d %d",&a,&b);
                Union(a,b);
            }
            for(int i=1;i<=n;i++){
                int x=findfa(i);
                for(int j=1+i;j<=n;j++){
                    int y=findfa(j);
                    if(x!=y){
                        fa[y]=x;
                        v2.push_back(make_pair(i,j));
                    }
                }
    
            }
            cout<<v1.size()<<endl;
            for(int i=0;i<v1.size();i++){
                cout<<v1[i].first<<" "<<v1[i].second<<" ";
                cout<<v2[i].first<<" "<<v2[i].second<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    Struts2 动态方法调用
    Struts2 NameSpace空间的使用
    Struts2基本结构
    Android TextView Button按钮 属性
    【转】vue 手动挂载$mount() 获取 $el
    【转】逻辑架构和物理架构
    EntityFramework Code First 构建外键关系,数据库不生成外键约束
    HTML Document 头
    CSS 浏览器兼容
    PageMethods
  • 原文地址:https://www.cnblogs.com/pk28/p/4755746.html
Copyright © 2020-2023  润新知