B3613 图的存储与出边的排序
2024-10-06 本文已影响0人
louyang
用邻接列表存储图,排序后输出。
#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;
}