#include <stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;
typedef int InfoType;
#define MAXV 100                //最大顶点个数
#define INF 32767               //INF表示∞
//以下定义邻接矩阵类型
typedef struct
{
    int no;                     //顶点编号
    InfoType info;              //顶点其他信息
} VertexType;                   //顶点类型
typedef struct                  //图的定义
{
    int edges[MAXV][MAXV];      //邻接矩阵
    int n,e;                    //顶点数,边数
    VertexType vexs[MAXV];      //存放顶点信息
} MGraph;                       //图的邻接矩阵类型
//以下定义邻接表类型
typedef struct ANode            //边的节点结构类型
{
    int adjvex;                 //该边的终点位置
    struct ANode *nextarc;      //指向下一条边的指针
    InfoType info;              //该边的相关信息,这里用于存放权值
} ArcNode;
typedef int Vertex;
typedef struct Vnode            //邻接表头节点的类型
{
    Vertex data;                //顶点信息
    ArcNode *firstarc;          //指向第一条边
} VNode;
typedef VNode AdjList[MAXV];    //AdjList是邻接表类型
typedef struct
{
    AdjList adjlist;            //邻接表
    int n,e;                    //图中顶点数n和边数e
} ALGraph;                      //图的邻接表类型

void MatToList(MGraph g,ALGraph *&G)
//将邻接矩阵g转换成邻接表G
{
    int i,j;
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    for (i=0; i<g.n; i++)                //给邻接表中所有头节点的指针域置初值
        G->adjlist[i].firstarc=NULL;
    for (i=0; i<g.n; i++)                //检查邻接矩阵中每个元素
        for (j=g.n-1; j>=0; j--)
            if (g.edges[i][j]!=0)       //存在一条边
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                p->adjvex=j;
                p->nextarc=G->adjlist[i].firstarc;        //采用头插法插入*p
                G->adjlist[i].firstarc=p;
            }
    G->n=g.n;
    G->e=g.e;
}
void ListToMat(ALGraph *G,MGraph &g)
//将邻接表G转换成邻接矩阵g
{
    int i;
    ArcNode *p;
    for (i=0; i<G->n; i++)
    {
        p=G->adjlist[i].firstarc;
        while (p!=NULL)
        {
            g.edges[i][p->adjvex]=1;
            p=p->nextarc;
        }
    }
    g.n=G->n;
    g.e=G->e;
}
void DispMat(MGraph g)
//输出邻接矩阵g
{
    int i,j;
    for (i=0; i<g.n; i++)
    {
        for (j=0; j<g.n; j++)
            printf("%3d",g.edges[i][j]);
        printf("\n");
    }
}
void DispAdj(ALGraph *G)
//输出邻接表G
{
    int i;
    ArcNode *p;
    for (i=0; i<G->n; i++)
    {
        p=G->adjlist[i].firstarc;
        //printf("%3d: ",i);
        cout<<i<<":";
        while (p!=NULL)
        {
            //printf("%3d",p->adjvex);
            cout<<p->adjvex<<" ";
            p=p->nextarc;
        }
        printf("\n");
    }
}
//以下主函数用作调试
int main()
{
    int i,j;
    MGraph g;
    ALGraph *G;
    int m,n;
    cin>>n>>m;
    int A[n][n];
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cin>>A[i][j];
        }
    }

    g.n=n;
    g.e=m;
    for (i=0; i<g.n; i++)
        for (j=0; j<g.n; j++)
            g.edges[i][j]=A[i][j];
    //printf("\n");
    ///printf(" 有向图G的邻接矩阵:\n");
    //DispMat(g);
    G=new ALGraph;
    //printf(" 图G的邻接矩阵转换成邻接表:\n");
    MatToList(g,G);
    DispAdj(G);
    /*  printf(" 图G的邻接表转换成邻接邻阵:\n");
        for (i=0;i<g.n;i++)
            for (j=0;j<g.n;j++)
                g1.edges[i][j]=0;
        ListToMat(G,g1);
        DispMat(g1);
        printf("\n");*/
    return 0;
}