易错点
- 测试点1、4、5:如果这些点错了,可以尝试测试一下下面这组数据:
00100 9 10
00100 -18 12309
12309 -7 33218
33218 -3 00000
00000 0 99999
99999 -5 68237
68237 -6 23333
23333 -10 27777
27777 -11 48652
48652 -41 -1
代码
#include <iostream>
#include <cstdio>
using namespace std;
long a[100002];
string nxt[100002];
int main()
{
string faddr;
string h,p1,p2;
string f,ff;
string addr;
long data;
long n;
string next;
int k;
string tmpf;
int len=1;
cin>>faddr>>n>>k;
for(long i=0;i<n;i++){
cin>>addr>>data>>next;
a[stoi(addr)]=data;
nxt[stoi(addr)]=next;
if(next=="-1"){
f=addr;
ff=addr;
}
}
tmpf=faddr;
while(nxt[stoi(tmpf)]!="-1"){
len++;
tmpf=nxt[stoi(tmpf)];
}
h="100001";
nxt[stoi(h)]=faddr;
p1=h;
p2=faddr;
string tmp;
string tmp2=h;
string tmp3;
while(true){
if(a[stoi(p2)]<0){
if(p2==faddr&&p2!=ff){
tmp2=p2;
p1=nxt[stoi(p1)];
p2=nxt[stoi(p2)];
}
else{
nxt[stoi(p1)]=nxt[stoi(p2)];
tmp3=nxt[stoi(p2)];
nxt[stoi(p2)]=nxt[stoi(tmp2)];
nxt[stoi(tmp2)]=p2;
tmp2=p2;
if(nxt[stoi(p1)]==p2&&p2!=ff){
nxt[stoi(p2)]=tmp3;
p1=nxt[stoi(p1)];
p2=nxt[stoi(p2)];
}
else if(nxt[stoi(p1)]==p2&&p2==ff){
nxt[stoi(p2)]=tmp3;
break;
}
else if(p2!=ff){
p2=nxt[stoi(p1)];
}
else{
break;
}
}
}
else if(a[stoi(p2)]>k){//***
if(len==1){
break;
}
else{
nxt[stoi(p1)]=nxt[stoi(p2)];
tmp=p2;
nxt[stoi(f)]=tmp;
nxt[stoi(tmp)]="-1";
f=tmp;
if(p2!=ff){
p2=nxt[stoi(p1)];
}
else{
break;
}
}
}
else{
if(p2!=ff){
p1=nxt[stoi(p1)];
p2=nxt[stoi(p2)];
}
else{
break;
}
}
}
tmp=nxt[stoi(h)];
while(true){
cout<<tmp<<" "<<a[stoi(tmp)]<<" "<<nxt[stoi(tmp)]<<endl;
if(nxt[stoi(tmp)]!="-1"){
tmp=nxt[stoi(tmp)];
}
else{
break;
}
}
return 0;
}