用邻接列表存储图,排序后输出。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
vector<int> adj[500001];
bool visited[500001];
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
for (int i = 1; i <= n; i++) {
sort(adj[i].begin(),adj[i].end());
for (auto e : adj[i]) {
cout << e << ' ';
}
cout << endl;
adj[i].clear();
}
}
int main() {
int T;
cin >> T;
for (int z = 1; z <= T; z++) {
solve();
}
return 0;
}
网友评论