#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#define N 10010
using namespace std; vector<long int> head[N];
stack<long int> way;
long int n;
bool visited[N][N];
bool vis[N];
long long int d[N]; void addEdge(long int x,long int y) {
head[x].push_back(y);
head[y].push_back(x);
d[x]++;
d[y]++;
} void dfs(int u) {
vis[u]=;
vector<long int>::iterator pos;
for(pos=head[u].begin(); pos!=head[u].end(); pos++) {
long int v=*pos;
if(!vis[v]) {
dfs(v);
}
} } void euler(long int u) {
vector<long int>::iterator pos;
//利用dfs的方法遍历所有的边
for(pos=head[u].begin(); pos!=head[u].end(); pos++) {
long int v=*pos;
if(!visited[u][v]) {
visited[u][v]=visited[v][u]=;
//我这里不用栈,而是在进行下一个点之前就把当前点输出,样例是过了,可是交上去只能得10分
//cout<<' '<<v;
euler(v);
//用栈的话就没问题,可以得90分。
way.push(v);
}
}
} int main() {
long long int m;
cin>>n>>m ;
while(m--) {
long int x,y;
cin>>x>>y;
addEdge(x,y);
}
//以前总是得90分的原因就是没有考虑图不连通的情况
//这里从第一个点出发dfs所有的点,最后检验即可
dfs();
bool flag=;
for(int i=; i<=n; i++)
if(vis[i]==) {
flag=;
break;
}
//如果是不连通的,直接输出-1
if(flag==) cout<<-;
else {
long long int odd_num=;
for(long int i=; i<=n; i++) {
sort(head[i].begin(),head[i].end());
if(d[i]%)
odd_num++;
}
if(odd_num>)
cout<<-;
else {
cout<<;
euler();
while(!way.empty()) {
cout<<' '<<way.top();
way.pop();
}
}
} return ;
}

CCF第四题无向图打印路径的更多相关文章

  1. CCF第四题无向图打印路径 欧拉问题

    #include<iostream> #include<vector> #include<algorithm> #include<stack> #def ...

  2. 第十三次CCF第四题 1803——04 博弈

    我又写了一个简洁版的2.0: 可以作为博弈搜索树的模板  : https://www.cnblogs.com/xidian-mao/p/9389974.html 废话ps: 开始觉得这是一道简单得博弈 ...

  3. CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径

    推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...

  4. UVA624 CD,01背包+打印路径,好题!

    624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...

  5. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. AOE网上的关键路径(最长路径 + 打印路径)

    题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图.     AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...

  7. 经典算法题每日演练——第十四题 Prim算法

    原文:经典算法题每日演练--第十四题 Prim算法 图论在数据结构中是非常有趣而复杂的,作为web码农的我,在实际开发中一直没有找到它的使用场景,不像树那样的频繁使用,不过还是准备 仔细的把图论全部过 ...

  8. SPFA和FLOYD算法如何打印路径

    早晨碰到了一题挺裸的最短路问题需要打印路径:vijos1635 1.首先说说spfa的方法: 其实自己之前打的最多的spfa是在网格上的那种,也就是二维的 一维的需要邻接表+queue 以及对于que ...

  9. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

随机推荐

  1. Markdown常用用法

    很早之前就听过Markdown,一直没用,用过才发现,原来这么好用,迷人,就好比一位知性.大方.成熟.美丽的少妇一样深深吸引着我,特深夜把学习的笔记记录下. 引用 ">"最好 ...

  2. wex5 开机图片时间长

    作用: 控制刚打开图片 时间长 修改config.xml  地址:F:\wex\model\Native\templates\advanced 延迟的时间是在本地app的 config.xml中修改, ...

  3. Google

    1. Google Play: Google Play是谷歌官方的的应用市场, Google Play 服务通常会在 Android 装置上自动更新. http://baike.baidu.com/l ...

  4. The length of the string value exceeds the length configured in the mapping/parameter.

    在NHibernate 3.3 中存储的字符串太长,会抛异常:The length of the string value exceeds the length configured in the m ...

  5. OC- .h与.m

    1.只有利用类名调用类方法的时候,不需要在类名后面写*.其他情况下,类名后面统一加上一个* Circle *c1 = [Circle new]; - (BOOL)isInteractWithOther ...

  6. 初识ActionScript

    <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:fx="h ...

  7. Java Runtime.availableProcessors()方法

    Java Runtime.availableProcessors()方法用法实例教程.   描述 java.lang.Runtime.availableProcessors() 方法返回到Java虚拟 ...

  8. 【8-21】java学习笔记03

    内部类(静态内部类&非静态内部类) 静态外部类成员方法(如main方法)不能直接访问内部类,但是可以通过外部类的方法,在其中创建内部类实例对象,间接使用: 非静态内部类可以直接访问外部类的私有 ...

  9. vim的寄存器和剪贴簿操作?

    vim 复制/ 删除 多行? 有确定序号的行: :10,15m20, 10,15co20 没有确定序号的行: ndd, nyy. 其中的n表示, 从当前行开始算起(当前行本身要包含!!!), 向下共删 ...

  10. HDU4930 Fighting the Landlords 模拟

    Fighting the Landlords Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...