CCF 201512-4 送货 (并查集+DFS,欧拉路)
任务虽然繁重,但是小明有足够的信心,他拿到了城市的地图,准备研究最好的方案。城市中有n个交叉路口,m条街道连接在这些交叉路口之间,每条街道的
首尾都正好连接着一个交叉路口。除开街道的首尾端点,街道不会在其他位置与其他街道相交。每个交叉路口都至少连接着一条街道,有的交叉路口可能只连接着一
条或两条街道。
小明希望设计一个方案,从编号为1的交叉路口出发,每次必须沿街道去往街道另一端的路口,再从新的路口出发去往下一个路口,直到所有的街道都经过了正好一次。
接下来m行,每行两个整数a, b,表示和标号为a的交叉路口和标号为b的交叉路口之间有一条街道,街道是双向的,小明可以从任意一端走向另一端。两个路口之间最多有一条街道。
如果不存在方案使得小明经过每条街道正好一次,则输出一个整数-1。
1 2
1 3
1 4
2 4
3 4
1 2
1 3
1 4
2 4
3 4
2 3
前50%的评测用例满足:1 ≤ n ≤ 100, n-1 ≤ m ≤ 10000。
所有评测用例满足:1 ≤ n ≤ 10000,n-1 ≤ m ≤ 100000。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> G[maxn];
int p[maxn], in[maxn];
int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); }
stack<int> ans;
bool vis[maxn][maxn]; void dfs(int u){
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(!vis[u][v]){
vis[u][v] = vis[v][u] = 1;
dfs(v);
ans.push(v);
}
}
} bool judge(){
int x = Find(1);
int cnt = 0;
for(int i = 1; i <= n; ++i){
if(x != Find(i)) return false;
if(in[i] & 1) ++cnt;
if(cnt > 2) return false;
sort(G[i].begin(), G[i].end());
} if(cnt == 2 && in[1] % 2 == 0) return false;
return true;
} void print(){
printf("1");
while(!ans.empty()){
printf(" %d", ans.top());
ans.pop();
}
printf("\n");
} int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i) G[i].clear(), p[i] = i;
memset(in, 0, sizeof in);
memset(vis, false, sizeof vis);
int u, v;
for(int i = 0; i < m; ++i){
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
int x = Find(u);
int y = Find(v);
if(x != y) p[y] = x;
++in[u]; ++in[v];
} if(!judge()){ printf("-1\n"); continue; }
dfs(1);
print();
}
return 0;
}
CCF 201512-4 送货 (并查集+DFS,欧拉路)的更多相关文章
- Colored Sticks (并查集+Trie + 欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 37340 Accepted: 9796 Description You ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- POJ1291-并查集/dfs
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- UVA208-Firetruck(并查集+dfs)
Problem UVA208-Firetruck Accept:1733 Submit:14538 Time Limit: 3000 mSec Problem Description The Ce ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- hdu6370 并查集+dfs
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- 简单的ftp服务器
import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandle ...
- LeetCode求能够装得最多的水
费了半天劲还是没想出来,然后跑到网上找答案,明白了是怎么回事儿了,就是不知道为啥自己没有想出来. 明天再搞~~~ 国庆快乐~~~ 一年了~~~~~ 明年今日~~我会在哪儿 =====2017.9.30 ...
- codeforces Looksery Cup 2015 H Degenerate Matrix
The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...
- Java多态特性:重载和覆写的比較
Java重载: 在同一个类中 方法具有同样的名字,同样或不同的返回值,但參数不同的多个方法(參数个数或參数类型) public class MethoDemo{ public static void ...
- warez世界顶级压缩作品网站
http://www.pouet.net/ warez世界顶级压缩作品网站
- CentOS 更换 usr 挂载分区
由于之前挂载在/usr目录的分区空间过小,无法安装更多需要的软件,现在添加一块硬盘重新挂载在/usr目录,并将之前/usr 目录下的内容(包括权限.连接等)完整拷贝到新磁盘分区的/usr目录. 操作系 ...
- LLVM的总结
LLVM 写在前面的话:无意中看到的LLVM的作者Chris Lattner相关的介绍和故事,觉得很有意思就贴上来,如果不感兴趣,可以直接跳入下一章. 关于LLVM 如果你对LLVM的由来陌生,那么我 ...
- Unable to start adb server: adb server version (32) doesn't match this client (39); killing...
关于Android studio 连接不上adb问题,有人说重启机器,有人说重启工具,也有人说adb kill-server.然后我都尝试过依然没有解决.通过各种查询.最终成功的解决!!! adb n ...
- Nginx+Tomcat搭建负载均衡集群
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器, 使用 Nginx 可以使得程序在高并发的情况下依旧可以保持良好的性能.使用 Nginx+Tomcat ...
- php高级技巧总结
通过对<深入理解PHP:高级技巧.面向对象与核心技术>这本书的学习,总结出常用的php高级技巧,也方便自己以后查阅;我认为该书是php高级教程的葵花宝典,哈哈.里面的内容很实用,尤其是在项 ...