编辑代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct edge {
    int to, dis, pre;
}a[maxn];
int head[maxn];
int cnt;
void add_edges(int u, int v, int w) {
    a[++cnt].to= v;
    a[cnt].dis = w;
    a[cnt].pre = head[u];
    head[u] = cnt;
}
void print_edges(int root) {
    for (int i = head[root]; i; i = a[i].pre) {
        cout <<a[i].to << ' ';
    }
}
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        add_edges(u, v, w);
        add_edges(v, u, w);
    }
    for (int i = 1; i <= n; i++) {
        print_edges(i);
    }
}