MZLs endless loop(欧拉回路,欧拉路径)

MZL’s endless loop

Time Limit: 3000/1500 MS (Java/Others)Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 751Accepted Submission(s): 138Special Judge

Problem Description

As we all kown, MZL hates the endless loop deeply, and he commands you to solve this problem to end the loop.You are given an undirected graph with vertexs and edges. Please direct all the edges so that for every vertex in the graph the inequation is satisified.The graph you are given maybe contains self loops or multiple edges.

Input

The first line of the input is a single integer , indicating the number of testcases.For each test case, the first line contains two integers and .And the next lines, each line contains two integers and , which describe an edge of the graph.,,,,.

Output

For each test case, if there is no solution, print a single line with, otherwise output lines,.In th line contains a integer or , for direct the th edge to , for .

Sample Input

23 31 22 33 17 61 21 31 41 51 61 7

Sample Output

111010101题意: 给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果.(输出m条边的方向)题解:<1>定理: 一个图,肯定存在偶数个奇度点.通过<1>,我们可以找出若干条欧拉路径,直接按遍历的路径,定一个方向,对于环来说,那就更简单了,随便定那个方向都能符合题目的条件.AC代码:/* ***********************************************Author:xdloveCreated Time :2015年07月31日 星期五 12时37分29秒File Name:a.cpp ************************************************ */#pragma comment(linker, "/STACK:1024000000,1024000000")#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;/**宏定义类 * **/#define FOR(i,s,t) for(int i = (s); i < (t); i++)#define FOR_REV(i,s,t) for(int i = (s – 1); i >= (t); i–)#define mid ((l + r) >> 1)#define clr(a) memset(a,0,sizeof(a))#define lson l,mid,u<<1#define rson mid+1,r,u<<1|1#define ls u<<1#define rs u<<1|1typedef long long ll;typedef unsigned long long ull;const int INF = 0x3f3f3f3f;const double pi = acos(-1.0);/**输入输出挂类模板 * **/class Fast{public:inline void rd(int &ret){char c;int sgn;if(c = getchar(),c == EOF) return;while(c != '-' && (c < '0' || c > '9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c – '0');while(c = getchar(),c >= '0' && c <= '9')ret = ret * 10 + c – '0';ret *= sgn;}public:inline void pt(int x){if(x < 0){putchar('-');x = -x;}if(x > 9) pt(x / 10);putchar(x % 10 + '0');}};Fast xd;const int M = 1e5 + 5;struct Edge{int to,next,id;}e[M * 6];int tot,head[M],pos;int del[M];void init(){tot = 0;memset(head,-1,sizeof(head));}void addedge(int u,int v){e[tot].to = v;e[tot].next = head[u];e[tot].id = 0;head[u] = tot++;}bool dfs(int u){int v,id;for(int &i = head[u]; ~i; i = e[i].next){v = e[i].to;id = e[i].id ^ e[i^1].id;if(id) continue;e[i].id = 1;if(del[v]){del[v] = 0;return true;}if(dfs(v)) return true;}return false;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int T;cin>>T;while(T–){int n,m;scanf("%d %d",&n,&m);init();FOR(i, 0, m){int u,v;scanf("%d %d",&u,&v);del[u] ^= 1;del[v] ^= 1;addedge(u,v);addedge(v,u);}for(int i = 1; i <= n; i++){if(del[i]){del[i] = 0;dfs(i);}}for(int i = 1; i <= n; i++)while(~head[i]) dfs(i);for(int i = 0; i < tot; i += 2){if(e[i].id) puts("1");else puts("0");}}return 0;}

版权声明:追逐心中的梦想,永不放弃! By-xdlove

,当你感到悲哀痛苦时,最好是去学些什么东西。

MZLs endless loop(欧拉回路,欧拉路径)

相关文章:

你感兴趣的文章:

标签云: