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 ≤ nui ≠ 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. Ifxi = 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
1 2
2 3
1 1 1
output
3
1 2 3
input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
output
10
2 1 3 4 5 4 5 4 3 1
input
2 0
0 0
output
0

题目大意:

给出一张图,有N个点,M条边,并给出每一个点要求訪问次数的奇偶性。,要求输出訪问路径。

解法:

首先我们能够明白一点,这就是一个图的遍历,找一个点,设为起点,建立一个搜索遍历树,对于树每个点,我们全然能够控制奇偶性,如果:

眼下訪问的点为v。父节点为fa,如若点v不符合当前的奇偶性。则就让父节点到v绕一次,这样 odd[v] ^= 1, fa[v] ^= 1,这样我们能够全然保证全然控制子节点,将不符合要求的奇偶性调整成符合要求的奇偶性。同一时候父节点的奇偶性也在改变。

通过上述的操作,我们能够每次保证子节点的奇偶性符合要求,然而父节点的奇偶性如若不符合要求,则能够通过调整父节点 与 父节点的父节点来调整奇偶性。这样我们就能够奇偶性传递,终于传递到根节点。

根节点如若不符合该怎样调整呢?因为我们是走遍历树。到达叶节点还要回来的,意味着我们要走至少两次根节点。一次是出发。一次是最后一次回归。我们能够将根节点 r1 调整到根节点的当中一个子节点r2,使得奇偶性再次得到调整

代码:

#include <cstdio>
#include <vector>
#define N_max 123456 using namespace std; int n, m, fa[N_max], want[N_max];
int Odd_n, Odd_x, haveOdd[N_max];
vector <int> G[N_max], ans; int getf(int x) {
return (fa[x] == x) ? x : fa[x] = getf(fa[x]);
}
void addedge(int x, int y) {
G[x].push_back(y);
} void init() {
scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y; scanf("%d%d", &x, &y); int tmpx=getf(x);
int tmpy=getf(y);
if (tmpx != tmpy) {
fa[tmpx] = tmpy;
addedge(x, y);
addedge(y, x);
}
} for (int i = 1; i <= n; i++) {
scanf("%d", &want[i]); if (want[i]) haveOdd[getf(i)] = 1;
} for (int i = 1; i <= n; i++)
if (haveOdd[i]) {
Odd_n++;
Odd_x = i;
}
} void dfs(int now, int pre) {
ans.push_back(now);
want[now] ^= 1; for (int i = 0; i < G[now].size(); i++) {
int nex = G[now][i];
if (nex == pre) continue; dfs(nex, now);
ans.push_back(now);
want[now] ^= 1; if (want[nex]) {
ans.push_back(nex);
want[nex] ^= 1;
ans.push_back(now);
want[now] ^= 1;
}
}
} void solve() {
if (Odd_n == 0) {
printf("0\n");
return;
} if (Odd_n > 1) {
printf("-1\n");
return;
} dfs(Odd_x, -1);
int x = 0;
if (want[Odd_x]) x=1; printf("%d\n", ans.size() - x);
for (int i = x; i < ans.size(); i++)
printf("%d ", ans[i]);
} int main() {
init();
solve();
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

codeforces Round #259(div2) E解决报告的更多相关文章

  1. codeforces Round #259(div2) D解决报告

    D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...

  2. codeforces Round #260(div2) D解决报告

    D. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. codeforces Round #259(div2) C解题报告

    C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  4. codeforces Round #258(div2) D解题报告

    D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #259(div2)C(数学期望)

    数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k ...

  6. codeforces Round #258(div2) C解题报告

    C. Predict Outcome of the Game time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  7. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  8. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  9. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

随机推荐

  1. VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT

    VMware虚拟机上网络连接(network type)的三种模式--bridged.host-only.NAT VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换 ...

  2. 利用Javamail接收QQ邮箱和Gmail邮箱(转)

    求大神解答 Java代码: public class SendMailController { //@Autowired private JavaMailSenderImpl mailSender; ...

  3. EF 执行视图

    IEnumerable<V_stocks> summary = db.Database.SqlQuery<V_stocks>("SELECT * FROM dbo.V ...

  4. 飘逸的python - 发送带各种类型附件的邮件

    上一篇博文演示了如何发送简单的邮件,这一篇将演示如何发送各种类型的附件. 基本思路就是,使用MIMEMultipart来标示这个邮件是多个部分组成的,然后attach各个部分.如果是附件,则add_h ...

  5. openGL点精灵PointSprite具体解释: 纹理映射,旋转,缩放,移动

    第一,什么是点精灵 openGL的图形由顶点构成,以后利用顶点进行纹理的映射.点精灵就是,一个顶点被当作一个精灵来处理.特别之处就是,一个顶点也可进行纹理贴出.比如,原来是个顶点构成的一个矩形,如今一 ...

  6. 解决wordpress发表文章,照片不能居中的问题

    最近,随着一个年轻漂亮的女人的帮助(简直美极了.图相当火爆,性格非常好.大家闺秀型,照片给大家看看下一个突发.哈哈)获取她的个人博客,地址似乎是www.okaaok.com遇到发表文章.照片不能反正水 ...

  7. HDU1163 Eddy&#39;s digital Roots【九剩余定理】

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. 第三届蓝桥杯C++本科B组决赛解题报告(更新中)

    <1>星期几 9 package JiaNan; import java.util.Calendar; import java.util.GregorianCalendar; public ...

  9. CSDN markdown 编辑 三 基本语法

    这是一个副本,在准备好了help档,另外补充一些他们自己的解释. 这一次是不说话UML和LaTEX. 准备抽两篇文章专门谈. 1. 标题和字体大小 markdown字号太少了.觉得应该再加些. 在文字 ...

  10. ORACLE触发特定的解释

    ORACLE PL/SQL编程八: 把触发器说透 本篇主要内容例如以下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 ...