• Codeforces #541 (Div2)


    Problem   Codeforces #541 (Div2) - F. Asya And Kittens

    Time Limit: 2000 mSec

    Problem Description

    Input

    The first line contains a single integer nn (2n150000) — the number of kittens.

    Each of the following n1lines contains integers xi and yi (1xi,yin, xiyi) — indices of kittens, which got together due to the border removal on the corresponding day.

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

    Output

    For every cell from 1 to n print a single integer — the index of the kitten from 1 to n, 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.

    Sample Input

    5
    1 4
    2 5
    3 1
    4 5

    Sample Output

    3 1 4 2 5

    题解:并查集+链表,始终把y所在的集合加到x所在集合的右边,让每个集合的根始终保持在最左边,维护一下每个集合最左边元素,链表连一连即可,这种题拼的就是手速。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define REP(i, n) for (int i = 1; i <= (n); i++)
     6 #define sqr(x) ((x) * (x))
     7 
     8 const int maxn = 150000 + 100;
     9 const int maxm = 200000 + 100;
    10 const int maxs = 10000 + 10;
    11 
    12 typedef long long LL;
    13 typedef pair<int, int> pii;
    14 typedef pair<double, double> pdd;
    15 
    16 const LL unit = 1LL;
    17 const int INF = 0x3f3f3f3f;
    18 const LL mod = 1000000007;
    19 const double eps = 1e-14;
    20 const double inf = 1e15;
    21 const double pi = acos(-1.0);
    22 
    23 int n;
    24 int fa[maxn], ri[maxn];
    25 int ans[maxn];
    26 int edge[maxn];
    27 
    28 int findn(int x)
    29 {
    30     return x == fa[x] ? x : fa[x] = findn(fa[x]);
    31 }
    32 
    33 void init()
    34 {
    35     for (int i = 0; i < maxn; i++)
    36     {
    37         fa[i] = edge[i] = ri[i] = i;
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     ios::sync_with_stdio(false);
    44     cin.tie(0);
    45     //freopen("input.txt", "r", stdin);
    46     //freopen("output.txt", "w", stdout);
    47     init();
    48     cin >> n;
    49     int x, y;
    50     for (int i = 0; i < n - 1; i++)
    51     {
    52         cin >> x >> y;
    53         int fx = findn(x), fy = findn(y);
    54         fa[fy] = fx;
    55         ri[edge[fx]] = fy;
    56         edge[fx] = edge[fy];
    57     }
    58     int root = -1;
    59     for (int i = 1; i <= n; i++)
    60     {
    61         if (findn(i) == i)
    62         {
    63             root = i;
    64         }
    65     }
    66     ans[1] = root;
    67     int cnt = 1;
    68     for (int i = ri[root]; cnt++; i = ri[i])
    69     {
    70         ans[cnt] = i;
    71         if (cnt == n)
    72             break;
    73     }
    74     for(int i = 1; i <= n; i++)
    75     {
    76         cout << ans[i];
    77         if(i != n)
    78         {
    79             cout << " ";
    80         }
    81         else
    82         {
    83             cout << endl;
    84         }
    85         
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    快速搭建http server
    cwmp part2 测试基础RPC
    Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)
    Leetcode-5222 Split a String in Balanced Strings(分割平衡字符串)
    Leetcode-5224 Dice Roll Simulation(掷骰子模拟)
    P2604-[ZJOI2010]网络扩容
    P2053-[SCOI2007]修车
    P2153-[SDOI2009]晨跑
    P2774 方格取数问题
    P2763-试题库问题
  • 原文地址:https://www.cnblogs.com/npugen/p/10780000.html
Copyright © 2020-2023  润新知