HDOJ 5348 MZL's endless loop 乱搞
2015-08-05 11:51
288 查看
对每个点DFS,根据入度和出度的情况选择标记入边或者出边进行DFS
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1037 Accepted Submission(s): 206
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.
T≤100, 1≤n≤105, 1≤m≤3∗105, ∑n≤2∗105, ∑m≤7∗105.
Output
For each test case, if there is no solution, print a single line with −1,
otherwise output m lines,.
In ith
line contains a integer 1 or 0, 1 for
direct the ith
edge to ui→vi, 0 for ui←vi.
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
Source
2015 Multi-University Training Contest 5
/* ***********************************************
Author :CKboss
Created Time :2015年08月05日 星期三 10时51分53秒
File Name :HDOJ5348.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn=100100;
int n,m;
struct Edge
{
int to,next;
}edge[maxn*6];
int Adj[maxn],Size;
void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
}
void Add_Edge(int u,int v)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
}
bool vis[maxn*6];
int ans[maxn*3],in[maxn],out[maxn],num[maxn];
void clear()
{
init();
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
}
/// u--->v
void dfs0(int u)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
if(vis[i]==true)
{
Adj[u]=edge[i].next; continue;
}
int v=edge[i].to;
if(v!=u&&in[v]>out[v]) continue;
vis[i]=vis[i^1]=true;
if(i%2==0) ans[i/2]=0;
else ans[i/2]=1;
out[u]++; in[v]++;
Adj[u]=edge[i].next;
dfs0(v);
return ;
}
}
/// u<----v
void dfs1(int u)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
if(vis[i]==true)
{
Adj[u]=edge[i].next;
continue;
}
int v=edge[i].to;
if(v!=u&&in[v]<out[v]) continue;
vis[i]=vis[i^1]=true;
if(i%2==0) ans[i/2]=1;
else ans[i/2]=0;
out[v]++; in[u]++;
Adj[u]=edge[i].next;
dfs1(v);
return ;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
clear();
scanf("%d%d",&n,&m);
for(int i=0,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
/// edgeid Size/2;
Add_Edge(u,v); /// Size%2==0 real edge
Add_Edge(v,u);
num[u]++; num[v]++;
}
for(int i=1;i<=n;i++)
{
while(in[i]+out[i]!=num[i])
{
if(in[i]>=out[i]) dfs0(i);
else dfs1(i);
}
}
for(int i=0;i<m;i++)
printf("%d\n",ans[i]);
}
return 0;
}
MZL's endless loop
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1037 Accepted Submission(s): 206
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.
T≤100, 1≤n≤105, 1≤m≤3∗105, ∑n≤2∗105, ∑m≤7∗105.
Output
For each test case, if there is no solution, print a single line with −1,
otherwise output m lines,.
In ith
line contains a integer 1 or 0, 1 for
direct the ith
edge to ui→vi, 0 for ui←vi.
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
Source
2015 Multi-University Training Contest 5
/* ***********************************************
Author :CKboss
Created Time :2015年08月05日 星期三 10时51分53秒
File Name :HDOJ5348.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn=100100;
int n,m;
struct Edge
{
int to,next;
}edge[maxn*6];
int Adj[maxn],Size;
void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
}
void Add_Edge(int u,int v)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
}
bool vis[maxn*6];
int ans[maxn*3],in[maxn],out[maxn],num[maxn];
void clear()
{
init();
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
}
/// u--->v
void dfs0(int u)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
if(vis[i]==true)
{
Adj[u]=edge[i].next; continue;
}
int v=edge[i].to;
if(v!=u&&in[v]>out[v]) continue;
vis[i]=vis[i^1]=true;
if(i%2==0) ans[i/2]=0;
else ans[i/2]=1;
out[u]++; in[v]++;
Adj[u]=edge[i].next;
dfs0(v);
return ;
}
}
/// u<----v
void dfs1(int u)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
if(vis[i]==true)
{
Adj[u]=edge[i].next;
continue;
}
int v=edge[i].to;
if(v!=u&&in[v]<out[v]) continue;
vis[i]=vis[i^1]=true;
if(i%2==0) ans[i/2]=1;
else ans[i/2]=0;
out[v]++; in[u]++;
Adj[u]=edge[i].next;
dfs1(v);
return ;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
clear();
scanf("%d%d",&n,&m);
for(int i=0,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
/// edgeid Size/2;
Add_Edge(u,v); /// Size%2==0 real edge
Add_Edge(v,u);
num[u]++; num[v]++;
}
for(int i=1;i<=n;i++)
{
while(in[i]+out[i]!=num[i])
{
if(in[i]>=out[i]) dfs0(i);
else dfs1(i);
}
}
for(int i=0;i<m;i++)
printf("%d\n",ans[i]);
}
return 0;
}
相关文章推荐
- 初学git:用git bash往github push代码
- 可伸缩系统的架构经验
- 初学git:用git bash往github push代码
- Tumblr:150亿月浏览量背后的架构挑战
- java 调用 powershell 遇到的 reader.readLine() 卡住问题
- [LinuxC]毫秒级时间间隔计算程序实例
- opencv学习(五)书本《学习Opencv》(中文版)第四章的作业答案第一题(opencv3.0.0+VS2012+win7)
- Apache与Nginx优缺点比较
- bash fifo管道使用测试例子
- Mac安装OpenCV下载ippicv_macosx_20141027.tgz失败解决方案
- linux下支持laravel和apache的php源码编译
- uwsgi部署到nginx出现invalid request block size: 4161 (max 4096)...skip问题
- Photoshop CS6学习地址
- 在Linux和Windows系统上安装Nginx服务器的教程
- TravelCMS旅游网站系统前台诞生记-2(后台框架篇)
- TravelCMS旅游网站系统前台诞生记-2(后台框架篇)
- Maven依赖的Scope去除部署不需要的jar 包
- Tomcat 性能监控及调优
- uC/OS-II内核架构解析(1)---嵌入式RTOS(转)
- nginx源码编辑带第三方模块lua