int head[maxn], ver[maxm], edge[maxm], nxt[maxm], tot; bool vis[maxn]; int n, m;
voidadd( int u, int v, int w ) { ver[++tot] = v; edge[tot] = w; nxt[tot] = head[u]; head[u] = tot; }
voidsolve( ) { // 1. 初始化与读图 tot = 1; // 关键:从 1 开始,方便 i^1 找反向边 for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; add(u, v, w); // add(v, u, w); // 无向图 } }
// 2. DFS voiddfs( int u ) { vis[u] = true; for( int i = head[u]; i; i = nxt[i] ) { int v = ver[i]; if( vis[v] ) continue; dfs( v ); } }
// 3. BFS voidbfs( int s ) { queue< int > q; q.push( s ); vis[s] = true; while( !q.empty( ) ) { int u = q.front( ); q.pop( ); for( int i = head[u]; i; i = nxt[i] ) { int v = ver[i]; if( vis[v] ) continue; vis[v] = true; q.push( v ); } } }