MZL's endless loop(欧拉回路,欧拉路径)

MZL's endless loop

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 751    Accepted Submission(s): 138
Special 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 n vertexs and m edges. Please direct all the edges so that for every vertex in the graph the inequation |out degree  in degree|1 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 T , indicating the number of testcases.
For each test case, the first line contains two integers n and m .
And the next m lines, each line contains two integers ui and vi , which describe an edge of the graph.
T100 , 1n105 , 1m3105 , n2105 , m7105 .
 

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

Sample Input
  
  
2 3 3 1 2 2 3 3 1 7 6 1 2 1 3 1 4 1 5 1 6 1 7
 

Sample Output
  
  
1 1 1 0 1 0 1 0 1 题意: 给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果.(输出m条边的方向) 题解: <1>定理: 一个图,肯定存在偶数个奇度点. 通过<1>,我们可以找出若干条欧拉路径,直接按遍历的路径,定一个方向,对于环来说,那就更简单了,随便定那个方向都能符合题目的条件. AC代码:
/* ***********************************************
Author        :xdlove
Created 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|1

typedef 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void Cin(Books *abs){ int i = 0; int g; cout<<"添加几个课程?"<<endl; xiuf(&g); if(abs->number + g > 5){ cout<<"太多了,进不去了!!!"<<endl; system("pause"); system("cls"); }else{ for(i=0;i<g;i++){ abs->number++; cout<<"输入课程名字"<<endl; string mzl; cin>>mzl; abs->ClassArray[abs->number].name = mzl; cout<<"输入课程编号"<<endl; string numberl; cin>>numberl; abs->ClassArray[abs->number].number = numberl; cout<<"输入课程性质"<<endl; string forml; cin>>forml; abs->ClassArray[abs->number].form = forml; cout<<"输入课程学分"<<endl; int gradel; xiuf(&gradel); abs->ClassArray[abs->number].grade = gradel; cout<<"输入课程学时"<<endl; int timel; xiuf(&timel); abs->ClassArray[abs->number].time = timel; cout<<"输入课程授课学时"<<endl; int finishtimel; xiuf(&finishtimel); abs->ClassArray[abs->number].finishtime = finishtimel; cout<<"输入课程实验学时"<<endl; int experiencel; xiuf(&experiencel); abs->ClassArray[abs->number].experience = experiencel; cout<<"添加成功"<<endl; abs->ClassArray[abs->number].people = abs->number *10; system("pause"); system("cls"); ofstream out("kc.txt",ios::app);//app表示每次操作前均定位到文件末尾 if(out.fail()){ cout<<"error\n"; } out<<"课程名字:"<<mzl<<endl; out<<"课程编号:"<<numberl<<endl; out<<"课程性质:"<<forml<<endl; out<<"课程学分:"<<gradel<<endl; out<<"课程学时:"<<timel<<endl; out<<"课程授课学时:"<<finishtimel<<endl; out<<"课程实验学时:"<<experiencel<<endl; out.close(); }} }
最新发布
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值