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. CentOS6.5 安装 tomcat

    1.首先下载tomcat.tar.gz包 http://tomcat.apache.org/download-70.cgi 2.移动到目标文件夹 mv tomcat.tar.gz /usr/tomca ...

  2. Criterion & DetachedCriteria

    今天笔记主要是记录笔记,以及代码: criteria: 创建查询接口:createCriteria(class) 查询条件接口:add(各种条件); Restrictions 提供了设置参数的各种接口 ...

  3. win7文件夹图标中多了一把小锁打不开文件夹怎么办?

    win7文件夹图标中多了一把小锁打不开文件夹怎么办?解决办法一:右击目录→取得管理员权限!该方法适用于win7旗舰版.解决办法二:右击目录→属性→安全→高级→选择everyone→更改权限→勾上完全访 ...

  4. DAS、SAN、NAS的区别

    在服务企业的过程中,经常会接触到存储,但是又容易和市面上的一些什么NAS等混淆. DAS 与主机直连的存储设备 NAS 存储设备自带文件系统 SAN 存储设备的互联依赖网络,比如IP-SAN, FC- ...

  5. LayoutInflater和inflate()

    LayoutInflater LayoutInflater抽象类是用来加载XML布局文件(UI界面)的. 作用: 1.对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater. ...

  6. python内置函数 2

    import__( name[, globals[, locals[, fromlist[, level]]]])被 import 语句调用的函数. 它的存在主要是为了你可以用另外一个有兼容接口的函数 ...

  7. 电脑的f5刷新不了

    新买的电脑,f5刷新不了页面,网上查了后发现是fn功能键的原因.同时fn+f5即可刷新.可是依然感觉好别扭... 按下fn+esc,再只按f5,就可以刷新页面了.神奇...

  8. java的concurrent用法详解

    我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常需要有程序员独立完成代码实现,当然也有一些开源的框架提供了这些功能,但是这些依然没有JDK自带的功能使用起来方便.而当针对高质量Java ...

  9. PHP判断变量是否为整型

    1.使用is_int() 2.使用is_numeric() 两个函数的区别,当变量为'1'时, is_int()的返回值为false, is_numeric()的返回值为true 当变量为1时, 两个 ...

  10. Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...