图论-图的基本概念及存储方式

简单图判定

//简单图判定
#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[1001][1001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        cin >> x >> y;
        if (x == y || a[x][y]){
            cout << "No" << '\n';
            return 0;
        }
        a[x][y] = a[y][x] = 1;
    }
    cout << "Yes" << '\n';
} 

竞赛图数量

竞赛图基于n阶无向完全图,给每条边任意赋予一个方向的图就是竞赛图
//竞赛图数量
#include<bits/stdc++.h>
using namespace std;
int n;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    long long ans = 1LL << ((n * (n - 1)) / 2);
    cout << ans << '\n';
} 

顶点度数统计

//顶点度数统计
#include<bits/stdc++.h>
using namespace std;
int n, m;
int d[1001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        cin >> x >> y;
        d[x]++;
        d[y]++;
    }
    for (int i = 1; i <= n; i++){
        cout << d[i] << ' ';
    }
    cout << '\n';
} 

判断图是否同构

//判断图是否同构
#include<bits/stdc++.h>
using namespace std;
int n, m, ans;
int a[9][9], b[9][9], c[9];
bool vis[9];

inline void dfs(int x){
    if (x == n + 1){
        bool ok = 1;
        for (int i = 1; i <= n; i++){
            for (int j = i + 1; j <= n; j++){
                if (a[i][j] != b[c[i]][c[j]]){
                    ok = 0;
                }
            }
        }
        if (ok){
            ans++;
        }
    }
    for (int i = 1; i <= n; i++){
        if (!vis[i]){
            vis[i] = 1;
            c[x] = i;
            dfs(x + 1);
            vis[i] = 0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        cin >> x >> y;
        a[x][y] = a[y][x] = 1;
    }
    for (int i = 1; i <= m; i++){
        int x, y;
        cin >> x >> y;
        b[x][y] = b[y][x] = 1;
    }
    ans = 0;
    dfs(1);
    if (ans){
        cout << "Yes" << '\n';
    }
    else {
        cout << "No" << '\n';
    }
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值