zoukankan      html  css  js  c++  java
  • codeforces #541 F Asya And Kittens(并查集+输出路径)

    F. Asya And Kittens

    Asya loves animals very much. Recently, she purchased nn kittens, enumerated them from 11 and nn and then put them into the cage. The cage consists of one row of nn cells, enumerated with integers from 11 to nn from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n1n−1 partitions originally. Initially, each cell contained exactly one kitten with some number.

    Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day ii, Asya:

    • Noticed, that the kittens xixi and yiyi, located in neighboring cells want to play together.
    • Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells.

    Since Asya has never putted partitions back, after n1n−1 days the cage contained a single cell, having all kittens.

    For every day, Asya remembers numbers of kittens xixi and yiyi, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into nn cells.

    Input

    The first line contains a single integer nn (2n1500002≤n≤150000) — the number of kittens.

    Each of the following n1n−1 lines contains integers xixi and yiyi (1xi,yin1≤xi,yi≤n, xiyixi≠yi) — indices of kittens, which got together due to the border removal on the corresponding day.

    It's guaranteed, that the kittens xixi and yiyi were in the different cells before this day.

    Output

    For every cell from 11 to nn print a single integer — the index of the kitten from 11 to nn, who was originally in it.

    All printed integers must be distinct.

    It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.

    Example
    input
    5
    1 4
    2 5 
    3 1
    4 5
    output
    3 1 4 2 5
     
    题意:模拟合并两个连通块
    思路:用链表的思想去合并
    #include <cstdio>
    #include <map>
    #include <iostream>
    #include<cstring>
    #include<bits/stdc++.h>
    #define ll long long int
    #define M 6
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    int f[200000],n;
    int path[200000],last[200000]; //path记录路径 last记录最后一个数 
    int find(int x){
        if(x!=f[x])
        f[x]=find(f[x]);
        return f[x];
    } 
    void join(int x,int y){
        int xx=find(x);
        int yy=find(y);
        if(xx!=yy)
            f[yy]=xx;
    }
    int main(){
        ios::sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++) f[i]=i,last[i]=i;
        n--;
        while(n--){
            int x,y; cin>>x>>y;
            int xx=find(x); int yy=find(y);
            if(xx==yy) continue;
            path[last[xx]]=yy;
            last[xx]=last[yy];
            join(x,y);
        }
        for(int i=find(1);i;i=path[i])
        cout<<i<<" ";
        return 0;
    }
     
  • 相关阅读:
    【Wyn Enterprise BI知识库】 什么是商业智能 ZT
    Wyn BI的机会在哪里:越靠近消费者的行业,比如零售、文娱和金融,信息化投入越大 ZT
    客户化软件时代的前夜 ZT
    在“非软件企业”开发软件的困局 ZT
    行业观察报告:从SAAS困局看行业趋势 ZT
    超级干货 :一文读懂数据可视化 ZT
    传统BI还是自助式BI---BI与数据分析 ZT
    【BI学习笔记】在Linux上安装Wyn Enterprise商业智能报表服务器
    MAMP 配置: Mac with OSX 10.8 + (Mac + Apache + MySQL + Php)
    Emule Xtreme Kid eD2K 设置
  • 原文地址:https://www.cnblogs.com/wmj6/p/10429839.html
Copyright © 2011-2022 走看看