Problem King's Inspection

题目大意

  给一张n个点m条边的无向图,问是否存在一条欧拉回路。

  n<=10^5, 0<=m<=n+20。

解题分析

  注意到数据范围m<=n+20,可以想象若存在一条欧拉回路,那么图的形状必定是一条长链再加上20条边。

  将连续的一段入度和出度均为0的点缩成一个点,之后暴力跑欧拉回路就行了。

参考程序

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define V 1000008
#define E 2000008
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define bitcnt(x) __builtin_popcount(x)
#define rep(x,y,z) for (int x=y;x<=z;x++)
#define repd(x,y,z) for (int x=y;x>=z;x--)
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/ int lt[V],sum,indeg[V],outdeg[V],vis[V],cnt,nt[V],LT[V],SUM,fa[V],succ[V]; struct line{
int u,v,nt,flag;
}eg[E],EG[E]; void add(int u,int v){
eg[++sum]=(line){u,v,lt[u],};
lt[u]=sum;
indeg[v]++; outdeg[u]++;
}
void ADD(int u,int v){
EG[++SUM]=(line){u,v,LT[u],};
LT[u]=SUM;
}
int n,m;
bool check(int u){
return indeg[u]== && outdeg[u]==;
}
void dfs(int u){
vis[u]=; cnt++;
for (int i=lt[u];i;i=eg[i].nt){
int v=eg[i].v;
if (vis[v]) continue;
if (check(v) && check(u) && eg[lt[v]].v!=u){
nt[u]=v; fa[v]=u;
eg[i].flag=;
}
dfs(v);
}
} bool find(int u,int num){
if (u== && num==cnt) return ;
if (u== && num!=) return ;
for (int i=LT[u];i;i=EG[i].nt){
int v=EG[i].v;
if (fa[v]) continue;
fa[v]=u; succ[u]=v;
if (find(v,num+)) return ;
fa[v]=;
}
return ;
} int main(){
freopen("king.in","r",stdin);
freopen("king.out","w",stdout);
scanf("%d%d",&n,&m);
rep(i,,m){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
add(,);
clr(vis,); cnt=;
dfs();
if (cnt!=n) { printf("There is no route, Karl!\n"); return ; }
cnt=n;
clr(vis,);
rep(i,,sum)
if (eg[i].flag) ADD(eg[i].u,eg[i].v);
else
{
int l=eg[i].u,r=eg[i].v;
if (vis[l]||vis[r]) continue;
while (fa[l]) l=fa[l];
while (nt[r]) r=nt[r];
r=eg[lt[r]].v;
ADD(l,r);
int x=l;
while (nt[x])
{
x=nt[x];
cnt--;
vis[x]=;
}
}
clr(fa,);
if (!find(,)) { printf("There is no route, Karl!\n"); return ; }
int u=;
while (){
printf("%d ",u);
int uu=nt[u];
while (uu)
{
printf("%d ",uu);
uu=nt[uu];
}
u=succ[u];
if (u==) break;
}
printf("1\n");
}

Gym 100851K的更多相关文章

  1. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

随机推荐

  1. Intellij IDEA debug介绍

    先编译好要调试的程序. 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 点击红色箭头指向的小虫子,开始进入调试. IDE下方出现Debug视图,红色的箭头指 ...

  2. Ajax jsonp

    http://blog.csdn.net/superhosts/article/details/9057301

  3. hadoop2.0初识1.3

    1.配置分布式hadoop 1.1 准备三台测试机(虚拟机就可以) 1.1.1 将life-hadoop虚拟机克隆2个分别为life-hadoop02和life-hadoop03 1.1.2 查看ip ...

  4. 初学画布canvas的chapter1

    ——这篇读后感是我阅读<写给Web开发人员看的HTML5教程>一书中的第5章画布后的小小看法,由于编程实力有限,很多效果病没有一一去实现,所以只是停留在纸上谈兵的阶段. 画布(canvas ...

  5. Deep Learning Papers Reading Roadmap

    Deep Learning Papers Reading Roadmap https://github.com/songrotek/Deep-Learning-Papers-Reading-Roadm ...

  6. Scrum Meeting 8-20151210

    任务安排 姓名 今日任务 明日任务 困难 董元财 服务器入口更新 服务器发布记录接口 无 胡亚坤 请假(编译攻坚) 聊天界面优化 无 刘猛 请假(编译攻坚) 开新的分支存放服务器端或者,创建的仓库记录 ...

  7. 《K&R》里贯穿全书的代码

    个人阅读<K&R>的感觉就是:前后内容联系特别紧密,前面的代码没有理解好到了后面就看不下去. 1.getline(char s[], int lim) 调用结果:往参数数组中读入字 ...

  8. Action<>和Func<> 委托【代理】

    C#中的Action<>和Func<> 其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的 ...

  9. Redis设计思路学习与总结

    版权声明:本文由宋增宽原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/222 来源:腾云阁 https://www.qclo ...

  10. cxf的soap风格+spirng4+maven 客户端

    上篇博客介绍了,cxf的soap风格的服务端,现在我们写客户端来调用 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...