HDU-1878 欧拉回路 欧拉回路

题目链接:https://cn.vjudge.net/problem/HDU-1878

题意

中文题,而且就是单纯的欧拉回路

思路

  1. 判断连通图
    用并查集会很好,bfs亦可
    一时脑抽用bfs过了这个题,数据还是太弱
  2. 出度==入度

代码

并查集查连通

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1000;
struct Node{
    int parent, rank;
    Node(int parent=0, int rank=0):
        parent(parent), rank(rank) {}
}node[maxn+5];
int n;
int find(int x){
    return (node[x].parent==x)?x:(node[x].parent=find(node[x].parent));
}

void join(int a, int b){
    a=find(a); b=find(b);
    if (a==b) return;
    if (node[a].rank==node[b].rank) node[a].rank++;
    if (node[a].rank>node[b].rank) node[b].parent=a;
    else node[a].parent=b;
}

bool connect(void){
    for (int i=2; i<=n; i++)
        if (find(1)!=find(i)) return false;
    return true;
}

int main(void){
    while (scanf("%d", &n)==1 && n){
        int m, cnt=0, vis[maxn+5]={0};
        bool set[maxn+5]={false};
        for (int i=1; i<=n; i++) node[i]=Node(i, 0);

        scanf("%d", &m);
        for (int i=0, a, b; i<m; i++){
            scanf("%d%d", &a, &b);
            join(a, b);
            vis[a]++; vis[b]++;
        }

        int flag=false;
        for (int i=1; i<=n; i++)
            if (vis[i]%2) {flag=true; break;}
        if (flag==false && !connect()) flag=true;
        printf("%d\n", (flag)?0:1);
    }

    return 0;
}

BFS查连通

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1000;
struct Edge{
    int from, to;
    bool vis;
    Edge(int from=0, int to=0, int vis=false):
        from(from), to(to), vis(vis) {}
};
vector<Edge> edge;
vector<int> G[maxn+5];
int n;
inline void addEdge(int from, int to){
    edge.push_back(Edge(from, to, false));
    G[from].push_back(edge.size()-1);
    G[to].push_back(edge.size()-1);
}

bool connective(void){
    int cnt=1; bool vis[maxn+5]={false};
    queue<int> que;
    que.push(1); vis[1]=true;
    while (que.size()){
        int from=que.front(); que.pop();
        if (cnt==n) return true;
        for (int i=0; i<G[from].size(); i++){
            Edge &e=edge[G[from][i]];
            int to=(e.to==from)?e.from:e.to;
            if (e.vis) continue;
            vis[to]=true; e.vis=true; cnt++;
            que.push(to);
        }
    }return false;
}

int main(void){
    while (scanf("%d", &n)==1 && n){
        int m, vis[maxn+5]={0};
        memset(G, 0, sizeof(G));

        scanf("%d", &m);
        for (int i=0, a, b; i<m; i++){
            scanf("%d%d", &a, &b);
            addEdge(a, b);// G[a][b]++; G[b][a]++;
            vis[a]++; vis[b]++;
        }

        int flag=false;
        for (int i=1; i<=n; i++)
            if (vis[i]%2) {flag=true; break;}
        if (flag==false && connective()==0) flag=true;
        printf("%d\n", (flag)?0:1);
    }

    return 0;
}

并查集

Time Memory Length Lang Submitted
93ms 1524kB 1198 G++ 2018-03-14 17:22:31

BFS

Time Memory Length Lang Submitted
124ms 7016kB 1445 G++ 2018-03-14 17:03:18

posted on 2018-03-14 17:34  糖栗子  阅读(122)  评论(0编辑  收藏  举报

导航