[蓝桥杯][算法提高VIP]卡勒沃夫之弱水路三千(提高型)

[蓝桥杯][算法提高VIP]卡勒沃夫之弱水路三千(提高型)

## 代码：

```#include<bits/stdc++.h>
using namespace std;
const int maxn=1e2+9;
vector<int>edge[maxn];
map<int,string>tran;
map<string,int>change;
int in[maxn];
vector<int>w;
queue<int>q;
void toop(int x)
{
q.push(x);
while(!q.empty())
{
int u=q.front();
q.pop();
w.push_back(u);
for(int i=0;i<edge[u].size();i++)
{
int v=edge[u][i];
in[v]--;
if(in[v]==0)q.push(v);
}
}
for(int i=0;i<w.size();i++)
{
if(i==w.size()-1)cout<<tran[w[i]];
else cout<<tran[w[i]]<<" ";
}
cout<<endl;
return ;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int sum=0;
memset(in,0,sizeof(in));
tran.clear();
change.clear();
for(int i=0;i<maxn;i++)edge[i].clear();
for(int i=0;i<w.size();i++)w.clear();
for(int i=1;i<=n;i++)
{
string a,b;
cin>>a>>b;
if(change[a]==0)
{
change[a]=++sum;
tran[sum]=a;
}
if(change[b]==0)
{
change[b]=++sum;
tran[sum]=b;
}

edge[change[a]].push_back(change[b]);
in[change[b]]++;
}
int i;
for(i=1;i<=sum;i++)if(in[i]==0)break;
toop(i);
}
}
```

2022-12-10 15:01

2022-12-19 19:04

01-13 18:31

2022-12-09 18:39

2022-12-20 17:21

2022-12-30 21:00

2022-12-18 02:29

2022-12-17 23:57

2022-12-20 07:39

01-16 10:27

1 收藏 评论