1126 Eulerian Path

题意:若图是连通图,且所有结点的度均为偶数,则称为Eulerian;若有且仅有两个结点的度为奇数,则称为semi-Eulerian。现给出一个图,要我们判断其是否为Eulerian,semi-Eulerian还是not-Eulerian。

思路:在数据输入的时候计算各个节点的度;在输出各个节点的度的同时记录度为奇数的结点个数;最后判断判断图是否连通。这种题不考察什么算法,关键要我们理解题意,往往都是很简单的!

代码:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=510;
vector<int> Adj[maxn];
int degree[maxn]={0};
int vis[maxn];
int n,m,u,v;

void dfs(int v)
{
    vis[v]=true;
    for(auto u:Adj[v])
        if(!vis[u]) dfs(u);
}

bool connected()
{
    memset(vis,false,sizeof(vis));
    int cnt=0;//连通块的个数
    for(int v=1;v<=n;v++){
        if(!vis[v]){
            dfs(v);
            cnt++;
        }
    }
    return (cnt > 1 ? false : true);
}

int main()
{
    //freopen("pat.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%d%d",&u,&v);
        Adj[u].push_back(v);
        Adj[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    int oddCnt=0;
    for(int v=1;v<=n;v++){
        if(degree[v]%2!=0) oddCnt++;
        printf("%d",degree[v]);
        if(v==n) printf("\n");
        else printf(" ");
    }
    bool flag=connected();
    if(flag && oddCnt==0) printf("Eulerian\n");
    else if(flag && oddCnt==2) printf("Semi-Eulerian\n");
    else printf("Non-Eulerian\n");
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9574437.html

时间: 2024-10-09 18:10:27

1126 Eulerian Path的相关文章

1126 Eulerian Path (25 分)

1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard

PAT 1126 Eulerian Path

In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the fa

PAT 甲级 1126 Eulerian Path

https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the

Graph | Eulerian path

In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge exactly once. Similarly, an Eulerian circuit or Eulerian cycle is an Eulerian trail which starts and ends on the same vertex. Hierholzer's algorithm 可以

PAT A1126 Eulerian Path (25 分)

In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the fa

PAT_A1126#Eulerian Path

Source: PAT A1126 Eulerian Path (25 分) Description: In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were fi

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

牛人的ACM经验 (转)

一:知识点 数据结构: 1,单,双链表及循环链表 2,树的表示与存储,二叉树(概念,遍历)二叉树的 应用(二叉排序树,判定树,博弈树,解答树等) 3,文件操作(从文本文件中读入数据并输出到文本文 件中) 4,图(基本概念,存储结构,图的运算) 数学知识 1,离散数学知识的应用(如排列组合.简单的图论,数 理逻辑) 2,数论知识 3,线性代数 4,组合代数 5,计算几何 二 算法 1,排序算法(冒抛法,插入排序,合并排序,快速排 序,堆排序) 2,查找(顺序查找,二分发) 3,回溯算法 4,递归算

《算法》第四章部分程序 part 7

? 书中第四章部分程序,包括在加上自己补充的代码,图中找欧拉环 ● 无向图中寻找欧拉环 1 package package01; 2 3 import edu.princeton.cs.algs4.StdOut; 4 import edu.princeton.cs.algs4.StdRandom; 5 import edu.princeton.cs.algs4.GraphGenerator; 6 import edu.princeton.cs.algs4.Graph; 7 import edu.