CF453C Little Pony and Summer Sun Celebration (DFS)
http://codeforces.com/contest/456
Codeforces Round #259 (Div. 1) C
Codeforces Round #259 (Div. 2) E
| Little Pony and Summer Sun Celebration
time limit per test
1 second memory limit per test
256 megabytes input
standard input output
standard output Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information? Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places. Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi. The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times. Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct. If there is no required path, output -1. If there multiple possible paths, you can output any of them. Sample test(s)
Input
3 2 Output
3 Input
5 7 Output
10 Input
2 0 Output
0 |
题意:给出一个无向图,无自环、多边(指两个点之间有多条边),有N个点,编号1~N,给出各个点需要经过奇数次还是偶数次,每条边最多经过4n次,求路线,或得出无解。
题解:深搜。
找一个需要经过奇数次的点开始深搜。不用担心环,比如1-2-3-1是环,走1-2-3-2-1就行。进入一个点,先把它加进队列,经过次数为1;每次探完一条边回溯的时候,先把当前点再加入队列一次(走回来了嘛),然后看这条边连的那个点的经过次数够不够,不够的话再走它一下,再回来。(把那个点加入队列,再把这个点加入队列)。
这样啪啪啪就走完了,只有起点可能次数不太对,如果不对的话就不走最后回起点的那一步了(或者不走一开始从起点出发的那一步,相当于从下一个点出发)。也就是删掉队尾或队首。
这样其实每条边不可能走超过4n次。最后把队列输出就好了。
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back const int maxn=;
const int maxm=; struct Edge {
int v,next;
} e[maxm];
int en;
int head[maxn]; void add(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} void init() {
memset(head,-,sizeof(head));
en=;
} bool a[maxn];
int d[maxn];
int n,m; int st,ed;
int vis[maxn];
vector<int>b;
void dfs(int x){
vis[x]=;
b.pb(x);
for(int i=head[x];i!=-;i=e[i].next){
if (!vis[e[i].v]){
dfs(e[i].v);
b.pb(x);
vis[x]++;
if(vis[e[i].v]%!=a[e[i].v]){
vis[e[i].v]++;
vis[x]++;
b.pb(e[i].v);
b.pb(x);
}
}
}
return;
} int farm() {
int i;
int st=-;
for(i=; i<=n; i++) {
if(a[i]==){st=i;break;}
}
if(st==-)return ;
mz(vis);
b.clear();
dfs(st);
if(vis[st]%!=a[st]){
vis[st]--;
b.erase(b.begin());
}
int maxi=b.size();
for(i=;i<=n;i++)
if(vis[i]%!=a[i])return -;
return b.size();
} int main() {
int i,j,x,y;
init();
scanf("%d%d",&n,&m);
mz(d);
REP(i,m) {
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
d[x]++;
d[y]++;
}
for(i=; i<=n; i++)
scanf("%d",&a[i]);
int ans=farm();
printf("%d\n",ans);
if(ans!=-) {
int maxi=b.size();
if(maxi>)printf("%d",b[]);
for(i=;i<maxi;i++){
printf(" %d",b[i]);
}
puts("");
}
return ;
}
CF453C Little Pony and Summer Sun Celebration (DFS)的更多相关文章
- CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))
CF453C Little Pony and Summer Sun Celebration 题解 这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会. 我这里给出一种构造思路 ...
- CF453C Little Pony and Summer Sun Celebration
如果一个点需要经过奇数次我们就称其为奇点,偶数次称其为偶点. 考虑不合法的情况,有任意两个奇点不连通(自己想想为什么). 那么需要处理的部分就是包含奇点的唯一一个连通块.先随意撸出一棵生成树,然后正常 ...
- codeforces 454 E. Little Pony and Summer Sun Celebration(构造+思维)
题目链接:http://codeforces.com/contest/454/problem/E 题意:给出n个点和m条边,要求每一个点要走指定的奇数次或者是偶数次. 构造出一种走法. 题解:可能一开 ...
- codeforces 453C Little Pony and Summer Sun Celebration
codeforces 453C Little Pony and Summer Sun Celebration 这道题很有意思,虽然网上题解很多了,但是我还是想存档一下我的理解. 题意可以这样转换:初始 ...
- [CF453C] Little Poney and Summer Sun Celebration (思维)
[CF453C] Little Poney and Summer Sun Celebration (思维) 题面 给出一张N个点M条边的无向图,有些点要求经过奇数次,有些点要求经过偶数次,要求寻找一条 ...
- CF 453C. Little Pony and Summer Sun Celebration
CF 453C. Little Pony and Summer Sun Celebration 构造题. 题目大意,给定一个无向图,每个点必须被指定的奇数或者偶数次,求一条满足条件的路径(长度不超\( ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
随机推荐
- 【faster-rcnn】训练自己的数据集时的坑
既然faster-rcnn原版发表时候是matlab版代码,那就用matlab版代码吧!不过遇到的坑挺多的,不知道python版会不会好一点. ======= update ========= 总体上 ...
- 【BZOJ-2937】建造酿酒厂 前缀和 + 展环为链 + 乱搞
2937: [Poi2000]建造酿酒厂 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 70 Solved: 24[Submit][Status][D ...
- 【poj3764】 The xor-longest Path
http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...
- 数据结构算法C语言实现(五)---2.3重新定义线性链表及其基本操作
一.简述 ...由于链表在空间的合理利用上和插入.删除时不需要移动等的优点,因此在很多场合下,它是线性表的首选存储结构.然而,它也存在着实现某些基本操作,如求线性表的长度时不如顺序存储结构的缺点:另一 ...
- Objective-C 利用OC的消息机制,使用Method Swizzling进行方法修改
功能:修改父类不可修改函数方法,函数方法交换 应用场景:假如我们使用的他人提供一个的framework,.m已被打包成二进制.a无法修改源码,只留下.h头文件,那假如代码中某个函数出现了问题可以通过这 ...
- alias重启终端失效的问题
如果使用命令 alias xx='xxxx' 那么登出以后,别名就会失效.下次登入的时候就不能用了. 为了保持别名可以把它写入.bashrc 在.bashrc的最后写入想要的别名,比如 alias z ...
- 基本概率分布Basic Concept of Probability Distributions 1: Binomial Distribution
PDF下载链接 PMF If the random variable $X$ follows the binomial distribution with parameters $n$ and $p$ ...
- hihocoder 1356 分隔相同整数
时间限制:10000ms单点时限:1000ms内存限制:256MB 描述 给定一个包含N个整数的数组A.你的任务是将A重新排列,使得任意两个相等的整数在数组中都不相邻. 如果存在多个重排后的数组满足条 ...
- OpenGLES入门笔记四
原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...
- WinForm------BarManager中各种属性设置
1.offset:红色Tool距离左边Tool的偏移量
