#include<bits/stdc++.h>
using namespace std;
int a[1005][1005] = {0};
vector<int> b[100005];
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u][v] = 1;
a[v][u] = 1;//标记
b[u].push_back(v);
b[v].push_back(u);//将路径连接的两个点加入动态数组
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cout << a[i][j];
if(j != n) cout << " ";
}
cout << endl;
}//输出邻接表
for(int i = 1; i <= n; i++) {
sort(b[i].begin(), b[i].end());//排序
cout << b[i].size();
for(int j=0;j<b[i].size();j++) {
cout << " " << b[i][j];
}
cout << endl;
}
return 0;
}
U460099 结点的度
#include<bits/stdc++.h>
using namespace std;
vector<int> b[100005];
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
b[u].push_back(v);
b[v].push_back(u);//将路径连接的两个点加入动态数组
}
for(int i = 1; i <= n; i++) {
sort(b[i].begin(), b[i].end());
cout << b[i].size();
cout<<endl;
//输出
}
return 0;
}
U460103 图的类型
#include<bits/stdc++.h>
using namespace std;
vector<int> a[1000005];
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
vector<int> d(n+1);
for(int i = 1; i <= n; i++) {
d[i] = a[i].size();
}
bool f = true;
int c = -1;
for(int i = 1; i <= n; i++) {
if(d[i] == n-1) c = i;
else if(d[i] != 1) f = false;
}
if(c == -1) f = false;
if(f) {
cout << "Flower";//判断是否是菊花图
return 0;
}
int cnt1 = 0, cnt2 = 0;
for(int i = 1; i <= n; i++) {
if(d[i] == 1) {
cnt1++;
}
else if(d[i] == 2) {
cnt2++;
}
}
if(cnt1 == 2 && cnt2 == n-2) {
cout << "Chain";//链
return 0;
}
if(cnt2 == n) {
cout << "Ring";//简单环
return 0;
}
cout << "Neither";//都不是
return 0;
}
B3643 图的存储
#include <bits/stdc++.h>
using namespace std;
int a , n, m, b, c;
vector<long long> d[500010];
int main() {
cin >> a;
while (a--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
d[i].clear();
}
for (int i = 1; i <= m; i++) {
cin >> b >> c;
d[b].push_back(c);
}
for (int i = 1; i <= n; i++) {
sort(d[i].begin(), d[i].end());
for (long long c : d[i]) {
cout << c << " ";
}
cout <<endl;
}
}
return 0;
}
点的邻居
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int>a[200005];
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
if (x != y) {
a[x].push_back(y);
a[y].push_back(x);
}
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}