Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

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 }

 

转载于:https://www.cnblogs.com/npugen/p/10780000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值