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. Tsung安装与使用

    Tsung安装与使用 Tsung安装与使用的详细说明,包括测试场景的脚本配置说明 Ray 2013/11/11   目录 安装tsung Tsung运行环境安装... Tsung安装... 使用Tsu ...

  2. Hadoop安装指引

    pre.ctl { font-family: "Liberation Mono", monospace } p { margin-bottom: 0.25cm; line-heig ...

  3. javascript 面向对象(转)

    1.使用[]调用对象的属性和方法 function User() { this.age = 21; this.sex = "男?"; } var user = new User() ...

  4. 深入对比数据科学工具箱:Python和R之争

    建议:如果只是处理(小)数据的,用R.结果更可靠,速度可以接受,上手方便,多有现成的命令.程序可以用.要自己搞个算法.处理大数据.计算量大的,用python.开发效率高,一切尽在掌握. 概述 在真实的 ...

  5. 从零开始学iPhone开发(2)——控件的使用

    这一节我们开始学习iOS中简单控件的使用. 在iOS编程中,简单的控件有很多,其中主要的用的多的有: UILabel,UIButton,UISegmentedControl, UITextField, ...

  6. CRM系统简析

    寄语: 简单阐述一下对CRM系统应用的理解,此内容参考网上资料所整理. CRM是Customer Relationship Management的缩写,简称客户关系管理. CRM系统可以从三个方面来分 ...

  7. GPIO相关知识

    参考资料: 1. 维基百科GPIO 2. GPIO博客资料(一) 3. MMIO和PMIO 知识点: ● GPIO是General-purpose input/output的缩写,是一个在集成电路上的 ...

  8. [Prodinner项目]学习分享_第四部分(完结篇)_Controller层(控制器)

    Controller作用: 数据从数据库查询出来后,通过一定的业务逻辑,筛选出来一个结果集,我们最终的目的是要将这个结果集在页面中显示的. Controller就是起到这个作用,将业务逻辑层的结果集调 ...

  9. poj2778DNA Sequence(AC自动机+矩阵乘法)

    链接 看此题前先看一下matrix67大神写的关于十个矩阵的题目中的一个,如下: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻 ...

  10. STM32学习笔记(四) RCC外设的学习和理解

    RCC时钟模块并不好理解,初次接触我也是一头雾水,而且我真正掌握它的时候也比较晚,是我在学习uC/os-II,需要分析时钟时才有了深刻认识.但在学习中我却一定要把放在了前列,因为这是整个嵌入式最重要的 ...