跳到正文

个人记录

结总

sbl_xh约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟

版本

  1. v1当前

B3643 图的存储

C++
#include<bits/stdc++.h>
#define ll long long
#define u unsigned
#define ull u ll
using namespace std;
int n,m;
int a[1003][1003];
vector<int> p[1003];
int main(){
	cin >> n >> m;
	for(int i = 1;i<=m;i++){
		int x,y;
		cin >> x >> y;
		p[x].push_back(y);
		p[y].push_back(x);
		a[x][y]=a[y][x]=1;
	}
	for(int i=  1;i<=n;i++){
		for(int j = 1;j<=n;j++){
			cout << a[i][j] << ' ';
		}
		cout << endl;
	}
	for(int i = 1;i<=n;i++){
		cout << p[i].size()<<' ';
		sort(p[i].begin(),p[i].end());
		for(int j = 0;j<p[i].size();j++){
			cout << p[i][j] << ' ';
		}
		cout << endl;
	}
	return 0;
}

U460099 结点的度

C++
#include<bits/stdc++.h>
#define ll long long
#define u unsigned
#define ull u ll
using namespace std;
int n,m;
int a[1003][1003];
vector<int> p[1003];
int main(){
	cin >> n >> m;
	for(int i = 1;i<=m;i++){
		int x,y;
		cin >> x >> y;
		p[x].push_back(y);
		p[y].push_back(x);
		a[x][y]=a[y][x]=1;
	}
	for(int i = 1;i<=n;i++){
		cout << p[i].size()<<'\n';
	}
	return 0;
}

U460103 图的类型

C++
#include<bits/stdc++.h>
#define int long long
#define u unsigned
#define ull u ll
using namespace std;
int n,m;
int du[1000006];
int one,two,nn;
signed main(){
	cin >> n >> m;
	for(int i = 1;i<=m;i++){
		int x,y;
		cin >> x >> y;
		du[x]++;
        du[y]++;
	}
	for(int i = 1;i<=n;i++){
		if(du[i]==1) one++;
		else if(du[i]==2) two++;
		else if(du[i]==n-1) nn++;
	}
	if(one==n-1) cout << "Flower";
	else if(two==n-2&&one==2)cout << "Chain";
	else if(two==n) cout << "Ring";
	else cout << "Neither";
	return 0;
}

佐野

B3613 图的存储与出边的排序

C++
#include <bits/stdc++.h>
using namespace std;
int t;
int n, m, u, v;
vector<unsigned long long int> w[500010];
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (int i = 1; i <= n; i++) { // 清除上一次的数据。
            w[i].clear();
        }
        for (int i = 1; i <= m; i++) {
            cin >> u >> v;
            w[u].push_back(v);
        }
        for (int i = 1; i <= n; i++) {
            sort(w[i].begin(), w[i].end());
            for (auto v : w[i]) {
                cout << v << " ";
            }
            cout << '\n';
        }
    }
    return 0;
}

U460102 点的邻居

C++
#include<bits/stdc++.h>
#define ll long long
#define u unsigned
#define ull u ll
using namespace std;
int n,m;
vector<int> p[1003];
int main(){
	cin >> n >> m;//输入
	for(int i = 1;i<=m;i++){
		int x,y;
		cin >> x >> y;
		p[x].push_back(y);
		p[y].push_back(x);
	}

	for(int i = 1;i<=n;i++){//输出
		cout << p[i].size()<<' ';//长度
		sort(p[i].begin(),p[i].end());//排序
		for(int j = 0;j<p[i].size();j++){
			cout << p[i][j] << ' ';//连接
		}
		cout << endl;
	}
	return 0;
}
原文