题目大意:给定一个链,链上有权值,要求对这个链去重,绝对值一样的算重复,对特定的值只保留首节点.
把去重后的链和删除的节点组成的链都输出出来
#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define ull unsigned long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)#define base 2333
const int mod=1e9+7;
const int maxn=1e5+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);
}
string s,nxt;
map<string,int> mp;
map<int,string> mp2;
int val,n,tot=0,flag[maxn];
vector<pii> g[maxn];
vector<pii> ans1,ans2;
int solve(string s){if(mp[s]==0) mp[s]=++tot;mp2[mp[s]]=s;return mp[s];
}
int main(){ios::sync_with_stdio(false);cin>>s>>n;int init=solve(s);rep(i,0,n){cin>>s>>val>>nxt;int idx1=solve(s),idx2=solve(nxt);g[idx1].push_back(mk(idx2,val));}while(g[init].size()){int val=g[init][0].se;if(val<0) val=-val;if(flag[val]==0){ans1.push_back(mk(init,g[init][0].se));flag[val]=1;}else ans2.push_back(mk(init,g[init][0].se));init=g[init][0].fi;}rep(i,0,ans1.size()){cout<<mp2[ans1[i].fi]<<" "<<ans1[i].se<<" ";if(i+1==ans1.size()) cout<<"-1\n";else cout<<mp2[ans1[i+1].fi]<<"\n";}rep(i,0,ans2.size()){cout<<mp2[ans2[i].fi]<<" "<<ans2[i].se<<" ";if(i+1==ans2.size()) cout<<"-1\n";else cout<<mp2[ans2[i+1].fi]<<"\n";}return 0;
}