思路简单,话不多说直接上ACcode(详细注释):
ACcode:(安全饲用):
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e3+10;
int mp[N][N],n,m;
vector<int>v[N];
void fun1(){//邻接矩阵打印
for(int i=1;i<=n;i++){//打印
for(int j=1;j<=n;j++){
cout<<mp[i][j]<<" ";
}
cout<<"\n";
}
return;//好习惯
}
void fun2(){//邻接表打印
for(int i=1;i<=n;i++){
sort(v[i].begin(),v[i].end());//排序,从小到大
}
for(int i=1;i<=n;i++){//打印
cout<<v[i].size();
for(auto x:v[i]){
cout<<" "<<x;
}
cout<<"\n";
}
return; //好习惯
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){//建图
int u,vv;
cin>>u>>vv;
mp[u][vv]=1,mp[vv][u]=1;//邻接矩阵
v[u].push_back(vv),v[vv].push_back(u);//邻接表
}
fun1();//邻接矩阵打印
fun2();//邻接表打印
}
signed main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);//加速输入输出
int tt=1;
//cin>>t;
while(tt--){
solve();
}
return 0;//好习惯
}
over~好评点赞!~