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 - 
  • 相关阅读:
    Centos6.5环境中安装vsftp服务
    MySQL数据库的数据备份和恢复(导入和导出)命令操作语法【转】
    linux系统被入侵后处理经历【转】
    Linux lsof命令详解和使用示例【转】
    Oracle 表空间和用户权限管理【转】
    如何在 Linux 中找出最近或今天被修改的文件
    Linux 服务器系统监控脚本 Shell【转】
    1张图看懂RAID功能,6张图教会配置服务器【转】
    简析TCP的三次握手与四次分手【转】
    TCP协议中的三次握手和四次挥手(图解)【转】
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/5052205.html
Copyright © 2011-2022 走看看