SGU 101 AC

总算AC了,好帅气的算法,同时解决了自环跟非连通,一种自下向上找环的算法过程,这里把欧拉回路讲得很清楚,赞。

#include <iostream>

#include <vector>

#include <string>

#include <queue>

#include <map>

#include <string.h>

using namespace std;



class Edge {

public:

    int no, u, v;

    char d;

    Edge() {

        no = u = v = d = -1;

    }

    Edge reverse() {

        Edge rev;

        rev.no = no;

        rev.u = v;

        rev.v = u;

        rev.d = d == '+' ? '-' : '+';

        return rev;

    }

};



class Graph {

private:

    map<int, int> u2i;

    vector<vector<Edge> > G;

    int deg[210], n, no, use[105], vUse[210];

    vector<Edge> solution;

public:

    Graph(vector<Edge>& edges) {

        n = edges.size();

        makeU2I(edges);

        memset(deg, 0, sizeof(deg));

        G.clear();

        G.resize(no);

        for (int i = 0; i < edges.size(); i++) {

            G[u2i[edges[i].u]].push_back(edges[i]);

            G[u2i[edges[i].v]].push_back(edges[i].reverse());

            deg[u2i[edges[i].u]]++;

            deg[u2i[edges[i].v]]++;

        }

    }

    void makeU2I(vector<Edge>& edges) {

        u2i.clear();

        for (int i = 0; i < edges.size(); i++) {

            u2i[edges[i].u] = u2i[edges[i].v] = 0;

        }

        no = 0;

        for (map<int, int>::iterator it = u2i.begin(); it != u2i.end(); it++) {

            it->second = no++;

        }

    }

    int solve() {

        int beg = -1, end = -1;

        for (int i = 0; i < no; i++) {

            if (deg[i] & 1) {

                if (beg == -1) {

                    beg = i;

                } else if (end == -1) {

                    end = i;

                } else {

                    return -1;

                }

            }

        }

        if (beg == -1) {

            beg = 0;

        }

        memset(use, 0, sizeof(use));

        Edge begEdge;

        dfs(beg, -1, begEdge);

        if (solution.size() < n) {

            // 判连通

            return -1;

        }

        return 0;

    }

    void dfs(int u, int sign, Edge lastEdge) {

        for (int i = 0; i < G[u].size(); i++) {

            if (use[G[u][i].no] == 0) {

                use[G[u][i].no] = 1;

                dfs(u2i[G[u][i].v], 1, G[u][i]);

            }

        }



        if (sign > 0) {

            solution.push_back(lastEdge);

        }

    }

    void getSolution() {

        for (int i = solution.size() - 1; i >= 0; i--) {

            printf("%d %c\n", solution[i].no, solution[i].d);

        }

    }

};



int main()

{

    int n;

    scanf("%d", &n);



    vector<Edge> edges;

    for (int i = 0; i < n; i++) {

        int a, b;

        scanf("%d%d", &a, &b);



        Edge e;

        e.no = i + 1;

        e.u = a; e.v = b;

        e.d = '+';



        edges.push_back(e);        

    }



    Graph graph(edges);

    if (graph.solve() == -1) {

        printf("No solution\n");

    } else {

        graph.getSolution();

    }



    //system("pause");

}

 

你可能感兴趣的:(c)