题解 CF847A 【Union of Doubly Linked Lists】
Union of Doubly Linked Lists
给你一堆双向链表,让你把这些双向链表连成一条
解法
每次找到一条链表,将头连向上一个尾,将尾存下
最后输出即可
ac代码
#include<bits/stdc++.h>
using namespace std;
int n,lst,a[110],b[110];
int main()
{
cin >> n;
for(int i=1;i<=n;i++)
cin >> a[i] >> b[i];
for(int i=1;i<=n;i++)if(a[i]==0)
{
b[lst]=i,a[i]=lst,lst=i;
while(b[lst])lst=b[lst];
}
for(int i=1;i<=n;i++)
cout << a[i] << " " << b[i] << endl;
return 0;
}