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 在 ...
随机推荐
- Java中的异常处理:何时抛出异常,何时捕获异常?
今天在看hadoop源码时,想想自己最近在做的那个系统,发现很多异常处理的方式不对,还是按照传统的异常处理方式(即:采用返回值来标识程序出现的异常情况).而hadoop中很多方法的声明是有异常抛出的, ...
- Bzoj1823 [JSOI2010]满汉全席
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1640 Solved: 798 Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的 ...
- 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值
一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...
- 企业开发中选择logback而不是log4j的理由
不知道看到这篇文章的Java工程师有没有考虑过这个问题:为什么在企业开发中会选择logback来记录日志,而不是log4j呢? 如果你以前没有考虑过这个问题,那么现在如果让你考虑一下,你可能觉的会是因 ...
- UVa 1025 A Spy in the Metro(动态规划)
传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...
- 强连通分量的Tarjan算法
资料参考 Tarjan算法寻找有向图的强连通分量 基于强联通的tarjan算法详解 有向图强连通分量的Tarjan算法 处理SCC(强连通分量问题)的Tarjan算法 强连通分量的三种算法分析 Tar ...
- django makemigrations的一个特性
Migrations will run the same way on the same dataset and produce consistent results, meaning that wh ...
- 【Alpha阶段】第一次Scrum例会
个人任务报告 姓名 昨日已完成任务 明日计划任务 工作困难 岳桐宇 #20 撰写网站用户界面与体验改进方案文档 无 1. 与邓楚云的制定前端工作流程产生冲突,发生了不愉快的情况,最后在整个团队协商的情 ...
- electron打包
1.全局安装electron-packager npm install -g electron-packager 2.在项目目录下执行命令 electron-packager ./ --platfor ...
- mysql查询区分大小写
Mysql默认查询是不分大小写的,可以在SQL语句中加入 binary来区分大小写: BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区 ...