1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include <iostream> #include <cstring> #include <cstdio> #include <cctype>
using namespace std;
inline int read() { int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x*f; }
int buf[30];
inline void write(int x) { if (x<0) putchar('-'),x=-x; for (;x;x/=10) buf[++buf[0]]=x%10; if (!buf[0]) buf[++buf[0]]=0; for (;buf[0];putchar('0'+buf[buf[0]--])); }
const int N=250005; const int M=250005; const int V=N<<1; const int E=M+N<<1;
struct edge { int x,y; }edg[M];
int tov[E],nxt[E],euler_path[E]; int matchpath[M]; bool is_fwd[M]; bool used[E]; bool vis[V]; int last[V]; int deg[N]; int n,m,tot,el,len;
inline void insert(int x,int y){tov[++tot]=y,nxt[tot]=last[x],last[x]=tot;} inline void addedge(int x,int y){insert(x,y),insert(y,x);}
void clearedge(){memset(last,0,sizeof last),memset(nxt,0,sizeof nxt),memset(tov,0,sizeof tov),tot=0;}
void determine(int x,int e=0) { bool out_degree=0; vis[x]=1; for (int i=last[x],y;i;i=nxt[i]) if (e!=i+1>>1) { if (vis[y=tov[i]]) is_fwd[i+1>>1]=0; else determine(y,i+1>>1); out_degree^=is_fwd[i+1>>1]^(edg[i+1>>1].y==x); } if (e) if (out_degree^(deg[x]&1)) out_degree^=1,is_fwd[e]=edg[e].x==x; else is_fwd[e]=edg[e].y==x; }
void Hierholzer(int x,int e=0) { for (;last[x]&&used[last[x]];last[x]=nxt[last[x]]); if (last[x]) { int i=last[x],y=tov[i]; used[i]=used[((i-1)^1)+1]=1,Hierholzer(y,i); } if (e) euler_path[++el]=e; }
void process() { for (int i=el,e,st=0,en=0;i>=1;--i) { e=euler_path[i]; if (tov[e]) if (!st) st=en=tov[e],len=0; else en=tov[e],matchpath[++len]=e+1>>1; else { write(st+1>>1),putchar(' '),write(en+1>>1),putchar(' '),write(len),putchar('\n'); for (int j=1;j<=len;++j) write(matchpath[j]),putchar(j<len?' ':'\n'); st=0; } } }
int main() { freopen("kro.in","r",stdin),freopen("kro.out","w",stdout); n=read(),m=read(); for (int i=1,x,y;i<=m;++i) ++deg[edg[i].x=x=read()],++deg[edg[i].y=y=read()],addedge(x,y); determine(1),clearedge(); for (int i=1;i<=m;++i) if (is_fwd[i]) addedge(edg[i].x*2-1,edg[i].y*2); else addedge(edg[i].x*2,edg[i].y*2-1); for (int i=1;i<=n;++i) if (deg[i]&1) addedge(0,i*2-1); Hierholzer(0),process(); fclose(stdin),fclose(stdout); return 0; }
|