zoukankan      html  css  js  c++  java
  • [USACO]骑马修栅栏 Riding the Fences

    题目链接

    题目简述:欧拉回路,字典序最小。没什么好说的。

    解题思路:插入边的时候,使用multiset来保证遍历出出答案的字典序最小。

    算法模板:for(枚举边)

          删边(无向图删两次)

          遍历到那个点

         将点入栈

    代码

    #include<iostream>
    #include<cstdio>
    #include<fstream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<set>
    using namespace std;
    int read(){
        char ch;
        int res=0,f=1;
        ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            res=res*10+(ch-'0');
            ch=getchar();
        }
        return res*f;
    }
    int n,len[50005],s,e,ans[50005],top;
    multiset<int> u[50005];
    void H(int x){
        for(multiset<int>::iterator iter=u[x].begin();iter!=u[x].end();iter=u[x].begin()){
            int k=*iter;
            u[x].erase(iter);
            u[k].erase(u[k].find(x));
            H(k);
        }
        ans[++top]=x;
    }
    int main(){
        n=read();
        for(int i=1;i<=n;++i){
            int x,y;
            x=read();y=read();
            len[x]++;len[y]++;
            u[x].insert(y);
            u[y].insert(x);
        }
        for(int i=1;i<=1024;++i){
            if(len[i]%2){
                if(!s)s=i;
                else if(!e)e=i;
            }
        }
        if(s==0)s=1;
        H(s);
        while(top>0)printf("%d
    ",ans[top--]);
        return 0;
    }
  • 相关阅读:
    学期总结
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    作业02
    c语言 学习笔记之二 选择题2
  • 原文地址:https://www.cnblogs.com/clockwhite/p/11225045.html
Copyright © 2011-2022 走看看