zoukankan      html  css  js  c++  java
  • SGU 101.Domino( 欧拉路径 )

    求欧拉路径...直接dfs即可,时间复杂度O(N)

    ---------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
     
    using namespace std;
     
    #define X(i) Edge[i].first
    #define Y(i) Edge[i].second
    typedef pair<int, int> pii;
     
    const int maxn = 10;
    const int maxm = 109;
    const int n = 7;
     
    struct edge {
    int to, F, Id;
    edge* next;
    } E[maxm << 1], *pt = E, *head[maxn];
     
    void AddEdge(int u, int v, int Id, int f) {
    pt->to = v;
    pt->F = f;
    pt->Id = Id;
    pt->next = head[u];
    head[u] = pt++;
    }
     
    inline edge* Rev(edge* e) {
    return E + ((e - E) ^ 1);
    }
     
    pii Edge[maxm];
    int N, cnt[maxn], par[maxn];
    stack<pii> S;
     
    int Find(int x) {
    return x == par[x] ? x : par[x] = Find(par[x]);
    }
     
    bool chk() {
    int c = 0;
    for(int i = 0; i < n; i++)
    if((cnt[i] & 1) && ++c > 2) return false;
    for(int i = 0; i < n; i++) par[i] = i;
    for(int i = 0; i < N; i++)
    par[Find(X(i))] = Find(Y(i));
    c = -1;
    for(int i = 0; i < n; i++) if(cnt[i]) {
    if(!~c) 
    c = par[i];
    else if(par[i] != c)
    return false;
    }
    return true;
    }
     
    void Euler(int x) {
    for(edge*&e = head[x]; e; ) if(e->F) {
    int F = e->F, t = e->to, Id = e->Id;
    e->F = Rev(e)->F = 0;
    e = e->next;
    Euler(t);
    S.push(make_pair(Id, F));
    } else
    e = e->next;
    }
     
    int main() {
    scanf("%d", &N);
    memset(cnt, 0, sizeof cnt);
    for(int i = 0; i < N; i++) {
    scanf("%d%d", &X(i), &Y(i));
    cnt[X(i)]++;
    cnt[Y(i)]++;
    AddEdge(X(i), Y(i), i, 1);
    AddEdge(Y(i), X(i), i, -1);
    }
    if(!chk()) {
    puts("No solution");
    return 0;
    }
    for(int i = 0; i < n; i++) if(cnt[i] & 1) {
    Euler(i);
    while(!S.empty()) {
    pii p = S.top(); S.pop();
    printf("%d %c ", ++p.first, p.second != 1 ? '-' : '+');
    }
    return 0;
    }
    for(int i = 0; i < n; i++) if(cnt[i]) {
    Euler(i);
    while(!S.empty()) {
    pii p = S.top(); S.pop();
    printf("%d %c ", ++p.first, p.second != 1 ? '-' : '+');
    }
    return 0;
    }
    return 0;
    }

    --------------------------------------------------------------------------- 

    101. Domino

    time limit per test: 0.25 sec. 
    memory limit per test: 4096 KB

    Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.
    The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

    The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

    ENCYCLOPÆDIA BRITANNICA

    Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

    Input

    The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

    Output

    Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

    Sample Input

    5 1 2 2 4 2 4 6 4 2 1 

    Sample Output

    2 - 5 + 1 + 3 + 4 - 
  • 相关阅读:
    013.ES6 -对象字面量增强型写法
    012. ES6
    011. ES6 语法
    10. 9. Vue 计算属性的setter和getter 以及 计算属性的缓存讲解
    4. Spring MVC 数据响应方式
    3. SpringMVC 组件解析
    9. Vue 计算属性
    【洛谷 2984】给巧克力
    【洛谷 1821】捉迷藏 Hide and Seek
    【洛谷 1821】银牛派对Silver Cow Party
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/5052205.html
Copyright © 2011-2022 走看看