SGU101 欧拉回路 弱智水题

开始版刷SGU啦,SGU的题都是几十个点一道。。。MD一道题卡死你

#include <bits/stdc++.h>

using namespace std;

#define begin Begin
#define next Next
#define count Count

#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i)
#define DR(i, a, b) for (int i = (a), _begin_ = (b); i >= _begin_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b){ return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b){ return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int dmax = 310, maxn = 10, oo = 0x3f3f3f3f;

int N, M;

int a[dmax][dmax], d[maxn], cnt, S;

struct edge
{
    int x, y;
    edge(const int &_x = 0, const int &_y = 0) : x(_x), y(_y) {}
}c[dmax], s[dmax];

void dfs(int x)
{
    REP(i, 0, 6)
        if (a[x][i])
        {
            --a[x][i], --a[i][x];
            dfs(i);
            s[++cnt] = edge(x, i);
        }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d", &N);
    REP(i, 1, N)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        ++a[x][y], ++a[y][x];
        ++d[x], ++d[y];
        c[i] = edge(x, y);
    }
    REP(i, 0, 6)
        if (d[i] & 1)
        {
            ++cnt;
            S = i;
        } else if (d[i] && !S) S = i;
    if (cnt > 2)
    {
        puts("No solution");
        return 0;
    }
    cnt = 0;
    dfs(S);
    if (cnt < N)
    {
        puts("No solution");
        return 0;
    }
    DR(i, cnt, 1)
        REP(j, 1, N)
            if (s[i].x == c[j].x && s[i].y == c[j].y)
            {
                printf("%d +\n", j);
                c[j].x = -1;
                break;
            } else if (s[i].x == c[j].y && s[i].y == c[j].x)
            {
                printf("%d -\n", j);
                c[j].x = -1;
                break;
            }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值