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 在 ...
随机推荐
- 【BZOJ-3308】九月的咖啡店 最大费用最大流 + 线性筛素数
3308: 九月的咖啡店 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 159 Solved: 56[Submit][Status][Discuss ...
- webapi获取IP的方式
using System.Net.Http; public static class HttpRequestMessageExtensions { private const string HttpC ...
- css中margin-top/margin-bottom失效
要设置这两个值,我的理解应该在这个div的父容器中设置了固定宽高,或者设置了绝对定位,比如position:absolute(绝对定位) 或者压根不用,直接用padding-top/padding-b ...
- 微软注册dll在dotnet开发时起到缓存的作用
经过试验,我发觉只要是注册了dll之后,会在全局的环境中得到很好的体现,比如无需指定具体物理路径的dll引用,搜索即可引用等,同时也得到一点: 1.会缓存起这个dll先,在不重启电脑的情况,本地物理路 ...
- hdu 5229 找规律
假设选择了字符串a和b: 假设两人都按照最聪明的策略,那么观察一下可以找出规律:当a和b的字符串长度之和为奇数的时候zcc会败. 另外当a==b的时候zcc也会败(当时做的时候忘了这个了T^T) 接下 ...
- malware analysis、Sandbox Principles、Design && Implementation
catalog . 引言 . sandbox introduction . Sandboxie . seccomp(short for secure computing mode): API级沙箱 . ...
- Objective-C 利用OC的消息机制,使用Method Swizzling进行方法修改
功能:修改父类不可修改函数方法,函数方法交换 应用场景:假如我们使用的他人提供一个的framework,.m已被打包成二进制.a无法修改源码,只留下.h头文件,那假如代码中某个函数出现了问题可以通过这 ...
- 【Alpha版本】冲刺-Day6
队伍:606notconnected 会议时间:11月14日 会议总结 张斯巍(433) 今天安排:学习UI设计 完成度:100% 明天计划:上传界面设计 遇到的问题:无 感想:刚开始学的时候,都是从 ...
- redshift编译遇到的错误(ubuntu14.04)
1. ./bootstrap: 6: ./bootstrap: autopoint: not found 解决方法: $ sudo apt-get install autopoint 2. autor ...
- Python ValueError: IO operation on closed file
ValueError IO operation on closed file表示处理了已经被关闭的数据,在python 中 with语句的上下文会帮助处理,也就是说,当python的处理代码不对齐的时 ...
