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 im*ment 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 ;
}