[BZOJ 3033] 太鼓达人

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=3033

[算法]

         欧拉回路

[代码]

        

#include<bits/stdc++.h>
using namespace std;
const int MAXS = 1 << 13;

int k,tot,t;
stack< int > s;
int head[MAXS];
char ans[110];

struct edge
{
        int to,w,nxt;
        bool flag;
} e[MAXS];

template <typename T> inline void read(T &x)
{
    int f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; 
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
inline void addedge(int u,int v,int w)
{
        tot++;
        e[tot] = (edge){v,w,head[u],false};
        head[u] = tot;
}
inline void dfs(int u)
{
        for (int i = head[u]; i; i = e[i].nxt)
        {
                if (!e[i].flag)
                {
                        e[i].flag = true;
                        dfs(e[i].to);
                        s.push(e[i].w);
                }
        }
}

int main() 
{
        
        read(k);
        cout<< (1 << k) << ' ';
        int mask = (1 << (k - 1)) - 1;
        for (int i = 0; i <= mask; i++)
        {
                addedge(i,((i << 1) | 1) & mask,1);
                addedge(i,(i << 1) & mask,0);
        }
        dfs(0);
        t = k - 1;
        for (int i = 1; i < k; i++) ans[i] = '0';
        while (!s.empty())
        {
                ans[++t] = s.top() + '0';
                s.pop();
        }
        for (int i = 1; i < k; i++) ans[t - i + 1] = 0;
        printf("%s\n",ans + 1);
        
        return 0;
    
}

 

posted @ 2018-08-20 20:18  evenbao  阅读(269)  评论(0编辑  收藏  举报