L2-002 链表去重
总结: 要注意条件判断的顺序,分清每一次if所影响的的操作,最后的-1输出在if(cnt)里面,把他写在了外面wa了,非重复的节点也忘了指向下一节点;
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<map>
#include<stack>
#include<string>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const int maxn=1e6+9;
const int mod=1e9+7;
template <class T>
inline void sc(T &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0'), c = getchar();
}
}
ll power(ll n,ll x)
{
ll ans=1;
while(n)
{
if(n&1) ans=ans*x;
n>>=1;
x=x*x;
}
return ans;
}
struct node
{
int nx;
int x;
}p[maxn];
bool vis[maxn];
int us[maxn];
int cnt=0;
int main()
{
int n,st;
cin>>st>>n;
for(int i=0;i<n;i++)
{
int id;
cin>>id;
cin>>p[id].x>>p[id].nx;
}
printf("%05d %d ",st,p[st].x);
vis[abs(p[st].x)]=1;
st=p[st].nx;
while(1)
{
if(st!=-1)
{
if(vis[abs(p[st].x)])
{
//cout<<st<<endl;
us[cnt++]=st;
st=p[st].nx;
}
else
{
printf("%05d\n%05d ",st,st);
printf("%d ",p[st].x);
vis[abs(p[st].x)]=1;
st=p[st].nx;
}
}
else
{
cout<<-1<<endl;
break;
}
}
/*for(int i=0;i<cnt;i++)
{
//cout<<us[i]<<" ";
}*/
if(cnt)
{
printf("%05d ",us[0]);
cout<<p[us[0]].x<<" ";
for(int i=1;i<cnt;i++)
{
printf("%05d\n%05d ",us[i],us[i]);
cout<<p[us[i]].x<<" ";
}
cout<<-1<<endl;
}
return 0;
}