zoukankan      html  css  js  c++  java
  • 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.

    这道题卡了好久(看题解会的嘻嘻) 就是先找到成环的那些,最后在把那些不是同一根节点的东西连到一起就好了.

     1 #include <algorithm>
     2 #include <stack>
     3 #include <istream>
     4 #include <stdio.h>
     5 #include <map>
     6 #include <math.h>
     7 #include <vector>
     8 #include <iostream>
     9 #include <queue>
    10 #include <string.h>
    11 #include <set>
    12 #include <cstdio>
    13 #define FR(i,n) for(int i=0;i<n;i++)
    14 #define MAX 2005
    15 #define mkp pair <int,int>
    16 using namespace std;
    17 const int maxn = 1e6+5;
    18 typedef long long ll;
    19 const int  inf = 0x3fffff;
    20 void read(int  &x) {
    21     char ch; bool flag = 0;
    22     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    23     for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
    24     x *= 1 - 2 * flag;
    25 }
    26 
    27 int l,r;
    28 int fa[maxn];
    29 int fin(int x)
    30 {
    31     int tmp = x;
    32     while(x!=fa[x])x=fa[x];
    33     while(tmp!=x)
    34     {
    35         int temp = fa[tmp];
    36         fa[tmp]=x;
    37         tmp = temp;
    38     }
    39     return x;
    40 }
    41 pair<int,int> head[maxn];int top = 0;
    42 int P[maxn],p=0;
    43 int vis[maxn];
    44 int main() {
    45     int n;
    46     read(n);
    47     for(int i=0;i<=n;i++)fa[i]=i;
    48     for(int i=0;i<n-1;i++)
    49     {
    50         read(l),read(r);
    51         int x = fin(l), y = fin(r);
    52         if(fa[x]==fa[y])
    53         {
    54             head[++top].first=l,head[top].second=r;
    55         }
    56         else {
    57             fa[x]=y;
    58         }
    59     }
    60    // cout<<1<<endl;
    61     for(int i=1;i<=n;i++)
    62     {
    63         vis[fa[i]]++;
    64     }
    65     for(int i=1;i<=n;i++){
    66         if(fa[i]==i){
    67             P[p++]=i;
    68         }
    69     }
    70     cout<<top<<endl;
    71     for(int i=1;i<=top;i++){
    72         printf("%d %d %d %d
    ",head[i].first,head[i].second,P[i],P[i-1]);
    73     }
    74     return 0;
    75 }
    我身后空无一人,我怎敢倒下
  • 相关阅读:
    C# winform 数据库链接
    Second easyui框架学习
    First,映射数据库到项目
    Mybatis随笔
    spring注解简单了解
    SSH Mybatis 框架
    Maven pom.xml
    Spring
    LayaBox 摄像机Unit8Array数据获取、截图
    lvs和keepalived搭建高可用性系统
  • 原文地址:https://www.cnblogs.com/DreamKill/p/9388149.html
Copyright © 2011-2022 走看看