//邻接矩阵 #include#include int main() { int map[101][101]; memset(map,0,sizeof(map)); int u, v, w; int m, n ,i; scanf("%d %d",&m,&n);//m个点n条边 for(i = 1;i <= n; i++) { scanf("%d %d %d",&u,&v,&w); map[u][v] = w; map[v][u] = w; } for(i = 1;i <=m; i++) { printf("与%d相关联的点有:\n",i); for(int j = 1;j <= n; j++) { if(map[i][j]!=0) printf("%d ",j); } printf("\n"); } return 0; }//邻接表 #include #include #include #define M 101 int head[M],t=0; struct node { int u,v,w; int next; }g[M]; void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w) { g[t].u=u; g[t].v=v; g[t].w=w; g[t].next=head[u]; head[u]=t; t++; } int main() { int m,n; int i, j; scanf("%d %d",&m,&n); int u,v,w; init(); for(i = 1;i <= n; i++) { scanf("%d %d %d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(i = 1;i <= m; i++) { printf("与%d相关联的点有:\n",i); for(j = head[i];j != -1;j = g[j].next) { printf("%d ",g[j].v); } printf("\n"); } return 0; } //邻接表--链表 #include #include #include #define N 101 struct node { int u,v,w; struct node *next; }*head[N]; void init() { memset(head,NULL,sizeof(head)); } void add(int u,int v,int w) { struct node *p = (struct node*)malloc(sizeof(struct node)); p->v = v; p->w = w; p->next = head[u]; head[u] = p; } int main() { int m,n; scanf("%d %d",&m,&n); init(); int i; int u,w,v; for(i = 1;i <= n; i++) { scanf("%d %d %d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(i = 1;i <= m; i++) { printf("与%d相关联的点有:\n",i); for(struct node *p = head[i];p != NULL;p=p->next) { printf("%d ",p->v); } printf("\n"); } return 0; }