#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define stream(line) istringstream in((line))
#define endl "\n"
#define F first
#define S second
#define clr(a, d) memset((a),d,sizeof(a))
const int MOD = 1e9 + 7;
const int N = 1000 + 5;
const int M = 2000 + 5;
void print_vector(vector<ll> &v) {
for (ll i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
}
int head[N],to[M], nxt[M], cost[M], vis[N],n,m, mn, last, ne, indeg[N], have[N];
void addEdge(int f,int t, int c){
to[ne]=t;
cost[ne] = c;
nxt[ne]=head[f];
head[f]=ne++;
}
void init(){
memset(head,-1,sizeof(head));
ne=0;
}
void addBiEdge(int u,int v, int w){
addEdge(u,v,w);
}
void dfs(int u){
vis[u] = 1;
last = u;
for (int e = head[u],v,w; ~e&&(v=to[e],1) && (w = cost[e],1) ; e=nxt[e]) {
if(!vis[v]){
dfs(v);
if(w < mn){
mn = w;
}
}
}
}
int main() {
fast
int tc =1;
//cin >> tc;
while(tc--){
cin >> n >> m;
init();
for (int i = 0; i < m; ++i) {
int u,v,w;
cin >> u >> v >> w;
addBiEdge(u,v,w);
indeg[v]++;
have[u]++;
have[v]++;
}
vector<pair<pair<int,int>,int>> res;
for (int i = 1; i <= n; ++i) {
if(indeg[i] == 0 && have[i] != 0){
mn = 1e7;
dfs(i);
res.push_back({{i,last}, mn});
}
}
cout << res.size() << endl;
for(auto x : res){
cout << x.first.first << " " << x.first.second << " " << x.second << endl;
}
}
return 0;
}
Two Strings | Anagrams |
Prime Number | Lexical Sorting Reloaded |
1514A - Perfectly Imperfect Array | 580A- Kefa and First Steps |
1472B- Fair Division | 996A - Hit the Lottery |
MSNSADM1 Football | MATCHES Playing with Matches |
HRDSEQ Hard Sequence | DRCHEF Doctor Chef |
559. Maximum Depth of N-ary Tree | 821. Shortest Distance to a Character |
1441. Build an Array With Stack Operations | 1356. Sort Integers by The Number of 1 Bits |
922. Sort Array By Parity II | 344. Reverse String |
1047. Remove All Adjacent Duplicates In String | 977. Squares of a Sorted Array |
852. Peak Index in a Mountain Array | 461. Hamming Distance |
1748. Sum of Unique Elements | 897. Increasing Order Search Tree |
905. Sort Array By Parity | 1351. Count Negative Numbers in a Sorted Matrix |
617. Merge Two Binary Trees | 1450. Number of Students Doing Homework at a Given Time |
700. Search in a Binary Search Tree | 590. N-ary Tree Postorder Traversal |