邻接表到邻接矩阵 swust oj#1056

最近感觉自己基础太差存图除了二维矩阵就不会用别的了,二维矩阵又很容易MLE,恶补邻接表。自己写的邻接表,没看板子,反正能过,以后就用它了。
附代码:

#include <bits/stdc++.h>
using namespace std;
typedef struct l{
    int point ;
    l* next;
}dlb;
struct node{
    dlb* head;
    int n,m;
}ljjz[50000];
int A[5000][5000];
void add(char c,int pos,int pos2){
    int t=c-'0';
    if (pos2==0){
        dlb* p=new dlb;
        p->point=t; p->next=NULL;
        ljjz[pos].head=p;
    }
    if(pos2!=0){
        dlb* p=new dlb;
        p->point=t; p->next=NULL;
        dlb* node2=ljjz[pos].head;
        while(node2->next!=NULL){
            node2=node2->next;
        }
        node2->next=p;
    }
}
 signed main(){
    int t; cin>>t;
    getchar();
    for(int i=0;i<t;i++){
        char c; int k=0;
        while((c=getchar())){
            if(c=='\n') break;
            if(c==' ') continue;
            add(c,i,k); k++;
        }
    }
    for(int i=0;i<t;i++){
        dlb* node2=ljjz[i].head;
        while(node2!=NULL){
            A[i][node2->point]=1;
            node2=node2->next;
        }
    }
    for(int i=0;i<t;i++){
        for(int j=0;j<t;j++){
            cout<<A[i][j];
        }
        cout<<endl;
    }
}

发布了25 篇原创文章 · 获赞 6 · 访问量 870

猜你喜欢

转载自blog.csdn.net/chineseherofeng/article/details/104954651