以起始点为划分依据,以边为基础单位。
Template
#include <bits/stdc++.h>
#define MAX 10005
using namespace std;
struct edge { int to, next, w; };
edge es[MAX];
int cnt, head[MAX];
void add(int u, int v, int w)
{
es[++cnt].next = head[u];
es[cnt].w = w;
es[cnt].to = v;
head[u] = cnt;
}
void print(int st) //以st为起始结点的边
{
for (int i = head[st]; i != 0; i = es[i].next)
cout << st << " " << es[i].to << " " << es[i].w << endl;
}
int main()
{
int n, m, u, v, w;
cin >> n >> m;
cnt = 0;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> w;
add(u, v, w);
}
for (int i = 1; i <= n; i++)
print(i);
return 0;
}
Test Data
4 3
1 2 3
1 3 4
1 4 5
4 4
1 2 3
1 3 4
1 4 5
2 3 3