题目描述
知识点: 链表
思路: 较为简单,遍历三次链表,每一次条件根据题目意思来,然后每次放进vector里最后输出就好了。
#include<iostream>
#include<vector>
using namespace std;
const int N = 1e6+10;
struct Node{
int ad,v,ne;
bool is_get;
}node[N];
int main(){
int h,n,k;
cin>>h>>n>>k;
for(int i = 0;i < n;i++){
int ad,v,ne;
cin>>ad>>v>>ne;
node[ad] = {ad,v,ne,false};
}
int cur = h;
vector<Node> res;
while(cur != -1){
if(node[cur].v < 0){
node[cur].is_get = true;
res.push_back(node[cur]);
}
cur = node[cur].ne;
}
cur = h;
while(cur != -1){
if(node[cur].v <= k && !node[cur].is_get){
node[cur].is_get = true;
res.push_back(node[cur]);
}
cur = node[cur].ne;
}
cur = h;
while(cur != -1){
if(!node[cur].is_get){
node[cur].is_get = true;
res.push_back(node[cur]);
}
cur = node[cur].ne;
}
for(int i = 0;i < res.size();i++){
if(i != res.size()-1)
printf("%05d %d %05d\n",res[i].ad,res[i].v,res[i+1].ad);
else printf("%05d %d -1\n",res[i].ad,res[i].v);
}
return 0;
}