标记一下地址就可以
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,fir,sec,ne[N];
char e[N];
vector<int>a,b;
int st[N];
int main(){
cin>>fir>>sec>>n;
for(int i=1;i<=n;i++){
int add,next;
char val;
cin>>add>>val>>next;
//cout<<add<<" "<<val<<" "<<next<<endl;
e[add]=val;
ne[add]=next;
}
for(int i=fir;~i;i=ne[i]){
a.push_back(i);
st[i]=1;
}
for(int i=sec;~i;i=ne[i]){
if(st[i]){
printf("%05d\n",i);
return 0;
}
b.push_back(i);
}
cout<<-1<<endl;
return 0;
}
高频考点了属于是
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int fir,n,k,e[N],ne[N];
int main(){
cin>>fir>>n>>k;
for(int i=1;i<=n;i++){
int add,val,next;
cin>>add>>val>>next;
e[add]=val,ne[add]=next;
}
vector<int>v;
for(int i=fir;~i;i=ne[i]){
v.push_back(i);
}
for(int i=0;i+k-1<v.size();i+=k){
reverse(v.begin()+i,v.begin()+i+k);
}
for(int i=0;i<v.size();i++){
if(i+1==v.size()){
printf("%05d %d -1\n",v[i],e[v[i]]);
}
else{
printf("%05d %d %05d\n",v[i],e[v[i]],v[i+1]);
}
}
}
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int st[N];
int fir,n,e[N],ne[N];
vector<int>a,b;
int main(){
cin>>fir>>n;
for(int i=1;i<=n;i++){
int add,val,next;
cin>>add>>val>>next;
e[add]=val,ne[add]=next;
}
for(int i=fir;~i;i=ne[i]){
int val=e[i];
if(st[abs(val)]){
b.push_back(i);
}
else{
st[abs(val)]=1;
a.push_back(i);
}
}
for(int i=0;i<a.size();i++){
if(i+1==a.size()){
printf("%05d %d -1\n",a[i],e[a[i]]);
}
else printf("%05d %d %05d\n",a[i],e[a[i]],a[i+1]);
}
for(int i=0;i<b.size();i++){
if(i+1==b.size()){
printf("%05d %d -1\n",b[i],e[b[i]]);
}
else printf("%05d %d %05d\n",b[i],e[b[i]],b[i+1]);
}
}
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int fir,n,k,e[N],ne[N];
vector<int>a,b,c;
int main(){
cin>>fir>>n>>k;
for(int i=1;i<=n;i++){
int add,val,next;
cin>>add>>val>>next;
e[add]=val,ne[add]=next;
}
for(int i=fir;~i;i=ne[i]){
if(e[i]<0)a.push_back(i);
else if(e[i]<=k)b.push_back(i);
else c.push_back(i);
}
a.insert(a.end(),b.begin(),b.end());
a.insert(a.end(),c.begin(),c.end());
for(int i=0;i<a.size();i++){
if(i+1==a.size()){
printf("%05d %d -1\n",a[i],e[a[i]]);
}
else printf("%05d %d %05d\n",a[i],e[a[i]],a[i+1]);
}
}