CodeforcesRound #259 (div2) E solution report

Source: Internet
Author: User
E. Vendor

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 learned that the edevil Nightmare Moon wocould return during the upcoming Su

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 learned that the edevil Nightmare Moon wocould 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 maid 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 4NPlaces.

Input

The first line contains two integersNAndM(2? ≤?N? ≤? 105; 0? ≤?M? ≤? 105)-the number of places and the number of roads in Ponyville. Each of the followingMLines contains two integersUI,?VI(1? ≤?UI,?VI? ≤?N;UI? ?VI), These integers describe a road between placesUIAndVI.

The next line containsNIntegers:X1 ,?X2 ,?...,?XN(0? ≤?XI? ≤? 1)-the parity of the number of times that each place must be visited. IfXI? =? 0, thenI-Th place must be visited even number of times, else it must be visited odd number of times.

Output

Output the number of visited placesKIn the first line (0? ≤?K? ≤? 4N). Then outputKIntegers-the numbers of places in the order of path. IfXI? =? 0, thenI-Th place must appear in the path even number of times, elseI-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 21 22 31 1 1

Output

31 2 3

Input

5 71 21 31 41 53 43 54 50 1 0 1 0

Output

102 1 3 4 5 4 5 4 3 1 

Input

2 00 0

Output

0

Question:

A graph with N vertices and M edges is given, and the parity of the number of visits required for each vertex is given ., The output access path is required.

Solution:

First, we can make it clear that this is the traversal of a graph. We can find a vertex and set it as the starting point to create a search traversal tree. For each vertex in the tree, we can completely control the parity. Suppose:

The currently accessed vertex is v and the parent node is fa. If vertex v does not conform to the current parity, the parent node is redirected to v. In this way, odd [v] ^ = 1, fa [v] ^ = 1, so that we can completely control the sub-nodes and adjust the non-conforming parity to the required parity. At the same time, the parity of the parent node is changing.

Through the above operations, we can ensure that the child node's parity meets the requirements each time. However, if the child node's parity does not meet the requirements, you can adjust the parity by adjusting the parent node and the parent node of the parent node, so that we can pass the parity and finally pass it to the root node. How should I adjust the root node if it does not match? Because we need to traverse the tree and return to the leaf node, it means we have to go through the root node at least twice, one is the departure, and the other is the last regression. We can adjust the root node r1 to one of the root nodes r2, so that the parity can be adjusted again.

Code:
#include 
 
  #include 
  
   #define N_max 123456using namespace std;int n, m, fa[N_max], want[N_max];int Odd_n, Odd_x, haveOdd[N_max];vector 
   
     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();}
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.