[POI2011]Garbage

链接

https://www.lydsy.com/JudgeOnline/problem.php?id=2278

https://loj.ac/problem/2162

https://www.luogu.org/problemnew/show/P3520

思路

求欧拉回路

求不到就GG

不过好像复杂度不对,一直TLE

luogu90TLE

loj85TLE

bzojwrong

不过本地只跑1e7,std跑2e7

代码

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N=1e5+7,M=1e6+7;
char buf[20000001],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,20000000,stdin),p1==p2)?EOF:*p1++)
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int out_tp,out_stk[25];
void opt(int x){
if (!x){ putchar('0'); return; }
int t; for (; x; x=t){ t=x/10; out_stk[++out_tp]=x-t*10; }
while (out_tp) putchar(out_stk[out_tp--]+'0');
putchar(' ');
}
int n,m;
struct node {
int v,nxt,q,ok;
}e[M<<1];
int head[N],tot=1;
vector<int> ans[N];
void add(int u,int v,int q) {
e[++tot].v=v;
e[tot].q=q;
e[tot].nxt=head[u];
head[u]=tot;
}
int stak[M],top,js;
bool dfs(int u,int f,int T) {
if(u==T&&f) return true;
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(e[i].ok||!e[i].q) continue;
e[i].q=e[i^1].q=0;
stak[++top]=i;
if(dfs(v,u,T)) return true;
while(stak[top]!=i) {
e[stak[top]^1].ok=e[stak[top]].q=1;
e[stak[top]^1].q=e[stak[top--]].q=1;
}
e[i].ok=e[i^1].ok=1;
e[stak[top]^1].q=e[stak[top--]].q=1;
}
return false;
}
int main() {
// freopen("smi10a.in","r",stdin);
n=read(),m=read();
for(int i=1;i<=m;++i) {
int u=read(),v=read();
int x=read(),y=read();
add(u,v,x^y),add(v,u,x^y);
}
for(int i=1;i<=n;++i) {
int flag=0;
for(int j=head[i];j;j=e[j].nxt) if(e[j].q) flag=1;
if(flag) {
if(!dfs(i,0,i)) {
puts("NIE");
return 0;
}
++js;
while(top) ans[js].push_back(e[stak[top--]].v);
ans[js].push_back(i);
}
}
printf("%d\n",js);
for(int i=1;i<=js;++i) {
opt(ans[i].size()-1);
for(int j=0;j<ans[i].size();++j)
opt(ans[i][j]);
printf("\n");
}
return 0;
}

[POI2011]Garbage 欧拉回路的更多相关文章

  1. BZOJ2278 [Poi2011]Garbage[欧拉回路求环]

    首先研究环上性质,发现如果状态不变的边就不需要动了,每次改的环上边肯定都是起末状态不同的边且仅改一次,因为如果有一条边在多个环上,相当于没有改,无视这条边之后,这几个环显然可以并成一个大环.所以,我们 ...

  2. BZOJ2278 : [Poi2011]Garbage

    如果两个环相交,那么相交的部分相当于没走. 因此一定存在一种方案,使得里面的环都不相交. 把不需要改变状态的边都去掉,剩下的图若存在奇点则无解. 否则,每找到一个环就将环上的边都删掉,时间复杂度$O( ...

  3. POI2011题解

    POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...

  4. POI做题笔记

    POI2011 Conspiracy (2-SAT) Description \(n\leq 5000\) Solution 发现可拆点然后使用2-SAT做,由于特殊的关系,可以证明每次只能交换两个集 ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. 【LOJ#2162】【POI2011】Garbage(欧拉回路)

    [LOJ#2162][POI2011]Garbage(欧拉回路) 题面 LOJ 题解 首先有一个比较显然的结论,对于不需要修改颜色的边可以直接删掉,对于需要修改的边保留.说白点就是每条边要被访问的次数 ...

  7. [LOJ #2162]「POI2011」Garbage

    题目大意:给一张$n$个点$m$条边的无向图,每条边是黑色的或白色的,要求变成一个目标颜色.可以从任意一个点开始,走一个简单环,回到开始的点,所经过的边颜色翻转.可以走无数次.问是否有一个方案完成目标 ...

  8. [POI2011]SMI-Garbage

    题目描述 http://main.edu.pl/en/archive/oi/18/smi The Byteotian Waste Management Company (BWMC) has drast ...

  9. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

随机推荐

  1. html5 手机端 通讯录 touch 效果

    不说那么多直接上代码. <html> <head> <meta http-equiv="Content-Type" content="tex ...

  2. python pynssql创建表,删除表,插入数据,查询

    import pymssql server='10.194.**.***:*****' user='sa' password='******' database='******' #连接 conn=p ...

  3. python 类似java的三目运算符

    python中没有其他语言中的三元表达式,不过有类似的实现方法 其他语言中,例如java的三元表达式是这样 int a = 1; String b = ""; b = a > ...

  4. python中__call__()方法的用法

    __call__()的用法 __call__()方法能够让类的实例对象,像函数一样被调用: >>> >>> class A(object): def __call_ ...

  5. SQL优化(转)

    1. 负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好习惯 可以优化为in查询: ...

  6. linux内核的双链表list_head、散列表hlist_head

    一.双链表list_head 1.基本概念 linux内核提供的标准链表可用于将任何类型的数据结构彼此链接起来. 不是数据内嵌到链表中,而是把链表内嵌到数据对象中. 即:加入链表的数据结构必须包含一个 ...

  7. P1012 拼数

    P1012 拼数 输入输出样例 输入样例 3 13 312 343 输出样例 34331213 注意 当你输入: 6321 32 407 135 13 217 应该输出: 40732321217135 ...

  8. 76 道 Oracle Goldengate 面试问题

    基础 12c新特性 性能 Troubleshoot 其它 1. Oracle Goldengate 支持部署到哪些拓扑? GoldenGate supports the following topol ...

  9. amoeba_mysql 读写分离

    环境 amoeba需要java环境,配置:略. MySQL主从配置:略. 基本架构 MySQL主:192.168.31.140 MySQL从:192.168.31.150 MySQL代理:192.16 ...

  10. Navicat使用ssh连接数据库

    1.主机名或ip地址必须填localhost 2.主机名或ip地址必须填外网ip地址