2019湖南省賽 K 雙向鏈表練習題(list)

https://ac.nowcoder.com/acm/contest/1099/K

這題大概只要會list就會做吧。。。我是真的dd

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #include <bits/stdc++.h>
 4 #define iter ::iterator
 5 #define pa pair<int,ll>
 6 #define pp pair<int,pa>
 7 using namespace  std;
 8 #define ll long long
 9 #define mk make_pair
10 #define pb push_back
11 #define se second
12 #define fi first
13 #define ls o<<1
14 #define rs o<<1|1
15 ll mod=998244353;
16 const int N=2e5+5;
17 list<int>L[N],D[N];
18 int n,m;
19 int main(){
20     while(~scanf("%d%d",&n,&m)){
21         for(int i=1;i<=n;i++){
22             L[i].clear();
23             D[i].clear();
24             L[i].pb(i);
25             D[i].pb(i);
26         }
27         vector<int>ans;
28         while(m--){
29             int x,y;
30             scanf("%d%d",&x,&y);
31             L[x].splice(L[x].end(),L[y]);
32             D[y].splice(D[y].end(),D[x]);
33             swap(L[x],D[y]);
34             swap(D[x],D[y]);
35             L[y].clear();
36             D[y].clear();
37         }
38         for(auto tmp:L[1]){
39             ans.pb(tmp);
40         }
41         int h=ans.size();
42         printf("%d ",h);
43         for(int i=0;i<h;i++){
44             printf("%d ",ans[i]);
45         }
46         printf("\n");
47     }
48 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章