Gym 100851K
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的更多相关文章
- 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 ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
随机推荐
- End Routine
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- win7 用户目录
robocopy "C:\Users" "D:\Users" /E /COPYALL /XJ /XD "C:\Users\Administrator& ...
- AngularJS 特性—SinglePage、template、Controller
单页Web应用(SinglePage) 顾名思义,只使用一个页面的Web应用程序.单页面应用是指用户通过浏览器加载独立的HTML页面,Ajax加载数据页面无刷新,实现操作各种操作. 模板(templa ...
- HDU4511 AC自动机+dijkstra
题意: Description 终于放寒假了,小明要和女朋友一起去看电影.这天,女朋友想给小明一个考验,在小明正准备出发的时候,女朋友告诉他,她在电影院等他,小明过来的路线必须满足给定的规则: 1. ...
- python 中date datetime time 与str的互转
以下全部引入 form datetime import datetime, timedelta import time 一.time 转str 二.datetime 转 str str_date = ...
- JVM学习——编译OpenJDK
最近在学习<深入理解java虚拟机 第二版>这本书.书中第一部分建议大家自己编译OpenJDK.抱着学习态度也来编译个玩一玩.下面进入正题. 1.编译环境介绍 操作系统 CentOS Li ...
- nginx虚拟主机配置
nginx虚拟主机配置 虚拟主机的概念虚拟主机,就是把一台物理服务器划分成多个"虚拟"的服务器,每一个虚拟主机都可以有独立的域名和独立的目录nginx虚拟主机的配置nginx的 ...
- react native get started run 模拟机报错解决
参照 http://reactnative.cn/docs/0.30/getting-started.html#content 1)当执行 react-native run-android 这个环节的 ...
- python核心编程第六章练习6-12
6-12.字符串.(a)创建一个名字为findchr()的函数,函数声明如下.def findchr(string, char)findchr()要在字符串string中查找字符char,找到就返回该 ...
- SSM框架学习之高并发秒杀业务--笔记2-- DAO层
上节中利用Maven创建了项目,并导入了所有的依赖,这节来进行DAO层的设计与开发 第一步,创建数据库和表. 首先分析业务,这个SSM匡济整合案例是做一个商品的秒杀系统,要存储的有:1.待秒杀的商品的 ...