题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348

题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1,如果无解输出-1

解:考虑奇数度的点一定会成对出现(因为所有度数和肯定是偶数个->一条边对应两度~),那么我们可以将奇数度的点两两一连消除掉(两奇数度点的出度入读差的绝对值都为1, 路径上的点的差绝对值为0)

然后偶数度的点可以成环,那么可以搜出所有的环

 /*
* Problem:
* Author: SHJWUDP
* Created Time: 2015/8/5 星期三 16:29:03
* File Name: 1006.cpp
* State:
* Memo:
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; struct Edge {
int u, v;
}; int n, m;
vector<Edge> edges;
vector<vector<int> > G;
vector<int> cur, dg, vis, ans;
void init(int N, int M) {
edges.clear();
G.assign(N, vector<int>());
cur.assign(N, );
vis.assign(M<<, );
ans.assign(M, -);
dg.assign(N, );
}
void addEdge(int u, int v) {
edges.push_back((Edge){u, v});
G[u].push_back(edges.size()-);
}
void euler(int u) {
while(cur[u]<(int)G[u].size()) {
int i=G[u][cur[u]];
if(vis[i]) {
cur[u]++; continue;
}
Edge & e=edges[i];
if(i & ) ans[i>>]=;
else ans[i>>]=;
vis[i]=vis[i^]=;
cur[u]++;
dg[e.u]--; dg[e.v]--;
u=e.v;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
init(n+, m+);
for(int i=; i<m; i++) {
int a, b;
scanf("%d%d", &a, &b);
addEdge(a, b);
addEdge(b, a);
dg[a]++; dg[b]++;
}
for(int i=; i<=n; i++) {
if(dg[i] & ) euler(i);
}
for(int i=; i<=n; i++) {
if(dg[i] > ) euler(i);
}
for(int i=; i<m; i++) {
printf("%d\n", ans[i]);
}
}
return ;
}

hdu5348

[2015hdu多校联赛补题]hdu5348 MZL's endless loop的更多相关文章

  1. [2015hdu多校联赛补题]hdu5384 Danganronpa

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...

  2. [2015hdu多校联赛补题]hdu5302 Connect the Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...

  3. [2015hdu多校联赛补题]hdu5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...

  4. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  5. [2015hdu多校联赛补题]hdu5372 Segment Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...

  6. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  7. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  8. [2015hdu多校联赛补题]hdu5299 Circles Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...

  9. [2015hdu多校联赛补题]hdu5324 Boring Class

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...

随机推荐

  1. python request属性及方法说明

    if  request.REQUEST.has_key('键值'): HttpRequest对象的属性 参考: 表 H-1. HttpRequest对象的属性 属      性 描述 path 表示提 ...

  2. sass&compass&grunt

    1. compass compile path/to/project//编译scss compass watch path/to/project//自动监视文件变化 2.mixin @include ...

  3. SQL总结(三)其他查询

    SQL总结(三)其他查询 其他常用的SQL,在这里集合. 1.SELECT INTO 从一个表中选取数据,然后把数据插入另一个表中.常用于创建表的备份或者用于对记录进行存档. 语法: SELECT c ...

  4. [Freemarker] - 使用struts的component调用freemarker的ftl模板方法

    struts中的component标签,可以用来调用freemarker的ftl模板文件,使用component标签传参可以这样写: 使用property方式写法: <s:component t ...

  5. 在ios8中做的屏幕旋转功能

    http://www.cnblogs.com/smileEvday/archive/2013/04/24/Rotate2.html 思路出自这篇博主的文章. 直接上代码 -(void)willAnim ...

  6. 文件大boss

    1.write()  写命令 f=open("a2.txt",'w',encoding='utf-8') f.write() f.close() 2.closed  判断是否是关闭 ...

  7. Python中利用LSTM模型进行时间序列预测分析

    时间序列模型 时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺 ...

  8. 113、ScrollView滑动到了最底部

    ScrollView使用 http://www.cnblogs.com/androidsj/archive/2012/03/04/2379387.html 滚动到顶部判断:getScrollY() = ...

  9. Bootstrap之BootstrapDialog

    Make use of Bootstrap's modal more monkey-friendly. 参考地址:http://nakupanda.github.io/bootstrap3-dialo ...

  10. csv文件批量导入数据到sqlite。

    csv文件批量导入数据到sqlite. 代码: f = web.input(bs_switch = {})  # bs_switch 为from表单file字段的namedata =[i.split( ...