527E Data Center Drama(欧拉回路)
The project of a data center of a Big Software Company consists of n computers connected by m cables. Simply speaking, each computer can be considered as a box with multiple cables going out of the box. Very Important Information is transmitted along each cable in one of the two directions. As the data center plan is not yet approved, it wasn't determined yet in which direction information will go along each cable. The cables are put so that each computer is connected with each one, perhaps through some other computers.
The person in charge of the cleaning the data center will be Claudia Ivanova, the janitor. She loves to tie cables into bundles using cable ties. For some reasons, she groups the cables sticking out of a computer into groups of two, and if it isn't possible, then she gets furious and attacks the computer with the water from the bucket.
It should also be noted that due to the specific physical characteristics of the Very Important Information, it is strictly forbidden to connect in one bundle two cables where information flows in different directions.
The management of the data center wants to determine how to send information along each cable so that Claudia Ivanova is able to group all the cables coming out of each computer into groups of two, observing the condition above. Since it may not be possible with the existing connections plan, you are allowed to add the minimum possible number of cables to the scheme, and then you need to determine the direction of the information flow for each cable (yes, sometimes data centers are designed based on the janitors' convenience...)
The first line contains two numbers, n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of computers and the number of the already present cables, respectively.
Each of the next lines contains two numbers ai, bi (1 ≤ ai, bi ≤ n) — the indices of the computers connected by the i-th cable. The data centers often have a very complex structure, so a pair of computers may have more than one pair of cables between them and some cables may connect a computer with itself.
In the first line print a single number p (p ≥ m) — the minimum number of cables in the final scheme.
In each of the next p lines print a pair of numbers ci, di (1 ≤ ci, di ≤ n), describing another cable. Such entry means that information will go along a certain cable in direction from ci to di.
Among the cables you printed there should be all the cables presented in the original plan in some of two possible directions. It is guaranteed that there is a solution where p doesn't exceed 500 000.
If there are several posible solutions with minimum possible value of p, print any of them.
4 6
1 2
2 3
3 4
4 1
1 3
1 3
6
1 2
3 4
1 4
3 2
1 3
1 3
3 4
1 2
2 3
1 1
3 3
6
2 1
2 3
1 1
3 3
3 1
1 1
Picture for the first sample test. The tied pairs of cables are shown going out from the same point.
Picture for the second test from the statement. The added cables are drawin in bold.
Alternative answer for the second sample test:
题意:给你n个点和m条边,你可以添加边,求添加最少的边能否使每个点的边出度为偶数。
思路:可以想到对于一个无向图,当所有点的度数为偶数时,图中存在欧拉回路。那么对于一个存在欧拉路的无向图似乎可以以某种方式构造出满足条件的有向边。假设图中有欧拉回路1 2 3 4 1, 可以构造边2->1,2->3,4->3,4->1满足条件。
而对于不存在欧拉回路的图,可以在度数为奇数的两个节点间加一条边,或者添加自环使图中构成欧拉回路。
用邻接表会超时,用set维护边集,每次用过的边删除,能极大地节省时间。
找到欧拉路后,用上面的方法构造有向边输出就好了。
代码:
#include<set>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1000010;
multiset<int> e[N];
vector<int> odd;
int d[N],ans[N];
int n,m,tot_edge,top;
void dfs(int x)
{
while(!e[x].empty())
{
int v = *e[x].begin();
e[x].erase(e[x].begin());
e[v].erase(e[v].find(x));
dfs(v);
}
ans[++top] = x;
}
int main()
{
//freopen("in.txt","r",stdin);
int x,y;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
e[x].insert(y);
e[y].insert(x);
d[x]++,d[y]++;
tot_edge++;
}
for(int i=1; i<=n; i++)
if(d[i] % 2)
odd.push_back(i);
for(int i=0; i<odd.size(); i+=2)
{
int u = odd[i],v = odd[i+1];
e[u].insert(v);
e[v].insert(u);
tot_edge++;
}
if(tot_edge % 2)//需要加一条自环边
tot_edge++;
dfs(1);
printf("%d\n",tot_edge);
for(int i=1; i<top; i++)
{
if(i&1) printf("%d %d\n",ans[i],ans[i+1]);
else printf("%d %d\n",ans[i+1],ans[i]);
}
if(!(top % 2))puts("1 1");
return 0;
}