http://codeforces.com/problemset/problem/405/E
Little Chris is participating in a graph cutting contest. He’s a pro. The time has come to test his skills to the fullest.

Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition all edges of the graph into pairs in such a way that the edges in a single pair are adjacent and each edge must be contained in exactly one pair.

For example, the figure shows a way Chris can cut a graph. The first sample test contains the description of this graph.

You are given a chance to compete with Chris. Find a way to cut the given graph or determine that it is impossible!

Input
The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 105), the number of vertices and the number of edges in the graph. The next m lines contain the description of the graph’s edges. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi), the numbers of the vertices connected by the i-th edge. It is guaranteed that the given graph is simple (without self-loops and multi-edges) and connected.

Note: since the size of the input and output could be very large, don’t use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output
If it is possible to cut the given graph into edge-distinct paths of length 2, output lines. In the i-th line print three space-separated integers xi, yi and zi, the description of the i-th path. The graph should contain this path, i.e., the graph should contain edges (xi, yi) and (yi, zi). Each edge should appear in exactly one path of length 2. If there are multiple solutions, output any of them.

If it is impossible to cut the given graph, print “No solution” (without quotes).

题意:给定n个点m条边的无向连通图,无重边/自环,求把m条边划分为m/2组,每组2条边必须相邻(含有公共点,比如u-v-w)。
思路:如果m为奇数,一定不存在划分;m为偶数,一定存在。
我们用类似于Tarjan的方法对图G进行dfs,得到一颗树,dfs顺序遍历的是树边,其余是回向边。自叶子向根考虑,对于结点u,如果u的子结点v个数为偶数,那么每两个v与u一起构成一组,如果v的个数为奇数个,先配好偶数个,剩下1个返回,与u的父亲fa一起,< fa-u,u-v >作为一组。按照这样的方法做,由于m是偶数,每次配对都是恰好配2条边,从叶子向根考虑,对于任意u,它的可用子结点(未被配对)v,均可以构造,故最终一定能够构造成功。

#include<bits/stdc++.h>
using namespace std;
#define maxn 100000+100

int n,m,d[maxn];
vector<int> G[maxn];

int dfs(int u,int fa)
{  
    d[u]=d[fa]+1;
    bool flag=0;int one;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(d[v]&&d[v]<d[u])continue;
        if(d[v]>d[u])
        {
            flag=!flag;
            if(flag)one=v;
            else printf("%d %d %d\n",one,u,v);
        }
        else
        {
            int w=dfs(v,u);
            if(w)printf("%d %d %d\n",u,v,w);
            else 
            {
                flag=!flag;
                if(flag)one=v;
                else printf("%d %d %d\n",one,u,v);
            }
        }        
    }
    if(flag)return one;
    else return 0;
}

int main()
{
    //freopen("input.in","r",stdin);
    int x,y;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    if(!(m&1))dfs(1,0);
    else puts("No solution");
    return 0;
}