Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3043 Accepted Submission(s): 704
Special Judge
Problem Description
The Wall has down and the King in the north has to send his soldiers to sentinel.
The North can be regard as a undirected graph (not necessary to be connected), one soldier can cover one path. Today there's no so many people still breathing in the north, so the King wants to minimize the number of soldiers he sent to cover each edge exactly once. As a master of his, you should tell him how to arrange soldiers.
Input
There might be multiple test cases, no more than 20. You need to read till the end of input.
In the first line, two integers n and m, representing the number of nodes and edges in the graph.
In the following m lines, each contain two integers, representing two ends of an edge.
There are no parallel edges or self loops.
1≤n,m≤100000
Output
For each test case, the first line contains number of needed routes, p.
For the following p lines, an integer x in the beginning, followed by x integers, representing the list of used edges. Every integer should be a positive or negative integer. Its absolute value represents the number of chosen edge (1~n). If it's positive, it shows that this edge should be passed as the direction as the input, otherwise this edge should be passed in the direction different from the input. Edges should be in correct order.
Sample Input
3 3 1 2 1 3 2 3
Sample Output
1 3 1 3 -2
Source
2018 Multi-University Training Contest 2
解题思路
题目意思是求对给定的图,至少需要几条不重复的欧拉路径才能完全覆盖,输出路径条数以及打印路径(打印经过的边的编号,如果边走的边的方向和输入一样就是正数,不一样输出编号的负数)。
每个连通图的路径条数为max(1,奇数度点个数/2).
对于每个连通图,给图中每一对度数为奇数的点,增加一条边,剩下一对不增加,作为起点和终点,然后求出一条欧拉路径,再在求出的路径中删除这些自己增加的边,即可得出答案。
代码如下
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define maxn 100005
using namespace std;
struct T{
int l, r;
T(int l , int r): l(l), r(r){ }
};
int n, m;
vector g[maxn];
vector line;
bool vl[4*maxn]; //记录边是否走过
bool vis[maxn]; //记录点是否走过
queue que; //存度为奇数的点
//找此连通图中度为奇数的点
void dfs1(int x)
{
if(g[x].size() % 2){
que.push(x);
}
for(int i = 0; i < g[x].size(); i ++){
int r = line[g[x][i]].r;
if(!vis[r]){
vis[r] = true;
dfs1(r);
}
}
}
stack sta; //存放路径
//求欧拉路径
void dfs(int x, int e)
{
if(e == INF) //新的连通图,隔开
sta.push(e);
for(int i = 0; i < g[x].size(); i ++){
int l = g[x][i];
if(!vl[l]){
vl[l] = true;
vl[l^1] = true;
if(l % 2)
dfs(line[l].r, -(l+2)/2);
else {
dfs(line[l].r, (l+2)/2);
}
}
}
if(e != INF)
sta.push(e);
}
inline void add_line(int x, int y)
{
line.push_back(T(x, y));
g[x].push_back(line.size() - 1);
line.push_back(T(y, x));
g[y].push_back(line.size() - 1);
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF){
for(int i = 1; i <= m; i ++){
int x, y;
scanf("%d%d", &x, &y);
add_line(x, y);
}
memset(vis, 0, sizeof(vis));
memset(vl, 0, sizeof(vl));
int q = 0; //最少欧拉路径条数
for(int i = 1; i <= n; i ++){
if(!vis[i] && g[i].size()){
vis[i] = true;
dfs1(i); //搜索连通图
int s = i; //若有奇数度点,则起始点为奇数度点,否则可以为任意点
q += max(1, (int)que.size() / 2); //一个连通图中条数为奇数度顶点个数/2,若没用奇数顶点则为1
int last = -1;
//将奇数度点一对一对相连,留两个奇数度点作为起点和终点
while(!que.empty()){
int top = que.front();
que.pop();
if(que.size() < 2){
s = top;
continue;
}
if(last == -1){
last = top;
}
else {
add_line(last, top); //连边
last = -1;
}
}
dfs(s, INF);
}
}
printf("%d\n", q);
//处理得到的路径输出答案
queue ans;
while(!sta.empty()){
int top = sta.top();
sta.pop();
//注意此处取绝对值
if(abs(top) <= m){ //此边不是自己加上去的
ans.push(top);
}
else {
printf("%d", (int)ans.size());
while(!ans.empty()){
printf(" %d", ans.front());
ans.pop();
}
putchar('\n');
}
}
//清空
for(int i = 1; i <= n; i ++)
g[i].clear();
line.clear();
}
return 0;
}