邻接表存储树与图;
struct Node{
int ej;
Node * nextj;
} j ;
e[j] 存储了节点值;
ne[j]存储了节点的next指针;
如果带权值,则新开数组w[j]
j本身的值从idx递增而来‘’
多权值时,多加权值数组
include [HTML_REMOVED]
include [HTML_REMOVED]
using namespace std;
const int N = 100010, M = 2*N;
int e[M], ne[M] , h[N] , idx , w[M];
void add(int a, int b ,int c)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ,w[idx] = c,idx++;
}
int main()
{
int n ;
cin >> n;
memset(h,-1,sizeof h);
for(int i = 1; i <n ;i++)
{
int a, b ,c;
cin >> a >> b >>c;
add(a,b,c);
add(b,a,c);
}
for(int i=1; i< n ; i++)
{
cout<< i <<":";
for(int j=h [i] ;j!=-1;j=ne[j])
{
cout<< "->" <<e[j] << 'q' << w[j];
}
cout << endl;
}
return 0;
}