[POI2011]Śmieci

题目大意:

一个\(n(n\le10^5)\)个点\(m(m\le10^6)\)条边的无向图,每条边有边权\(0/1\),试找出若干个环,使得每次翻转环上所有边的权值,使得最后所有边权都是\(0\)。

思路:

权值为\(0\)的边都没有用,因为若方案存在,一定存在一种方案使得所有环只经过\(1\)边。

因此我们只留下\(1\)边,暴力DFS找环即可。注意要加上当前弧优化。

源代码:

#include<stack>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,M=2e6;
int deg[N],h[N];
bool mark[M],ins[N];
struct Edge {
int to,next;
};
Edge e[M];
inline void add_edge(const int &u,const int &v) {
e[++h[0]]=(Edge){v,h[u]};h[u]=h[0];deg[u]++;
e[++h[0]]=(Edge){u,h[v]};h[v]=h[0];deg[v]++;
}
std::stack<int> stk;
std::vector<std::vector<int> > ans;
void dfs(const int &x) {
if(ins[x]) {
const int k=ans.size();
ans.resize(k+1);
int y;
do {
y=stk.top();
stk.pop();
ins[y]=false;
ans[k].push_back(y);
} while(y!=x);
}
for(int &i=h[x];~i;i=e[i].next) {
const int &y=e[i].to;
if(mark[i]) continue;
mark[i]=mark[i^1]=true;
stk.push(x);
ins[x]=true;
dfs(y);
}
}
int main() {
memset(h,-1,sizeof h);
const int n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint();
if(getint()^getint()) {
add_edge(u,v);
}
}
for(register int i=1;i<=n;i++) {
if(deg[i]%2==1) {
puts("NIE");
return 0;
}
}
for(register int i=1;i<=n;i++) {
dfs(i);
}
printf("%lu\n",ans.size());
for(register unsigned i=0;i<ans.size();i++) {
printf("%lu ",ans[i].size());
for(register unsigned j=0;j<ans[i].size();j++) {
printf("%d ",ans[i][j]);
}
printf("%d\n",ans[i][0]);
}
return 0;
}

[POI2011]Śmieci的更多相关文章

  1. BZOJ2527: [Poi2011]Meteors

    补一发题解.. 整体二分这个东西,一开始感觉复杂度不是很靠谱的样子 问了po姐姐,说套主定理硬干.. #include<bits/stdc++.h> #define ll long lon ...

  2. BZOJ2276: [Poi2011]Temperature

    2276: [Poi2011]Temperature Time Limit: 20 Sec  Memory Limit: 32 MBSubmit: 293  Solved: 117[Submit][S ...

  3. BZOJ2213: [Poi2011]Difference

    2213: [Poi2011]Difference Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 343  Solved: 108[Submit][St ...

  4. BZOJ2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submi ...

  5. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

  6. bzoj 2217 [Poi2011]Lollipop 乱搞 贪心

    2217: [Poi2011]Lollipop Time Limit: 15 Sec  Memory Limit: 64 MBSec  Special JudgeSubmit: 383  Solved ...

  7. BZOJ_2529_[Poi2011]Sticks_贪心

    BZOJ_2529_[Poi2011]Sticks_贪心 Description Little Johnny was given a birthday present by his grandpare ...

  8. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

  9. BZOJ_2527_[Poi2011]Meteors_整体二分

    BZOJ_2527_[Poi2011]Meteors_整体二分 Description Byteotian Interstellar Union (BIU) has recently discover ...

随机推荐

  1. Linux驱动总结3- unlocked_ioctl和堵塞(waitqueue)读写函数的实现 【转】

    转自:http://blog.chinaunix.net/uid-20937170-id-3033633.html 学习了驱动程序的设计,感觉在学习驱动的同时学习linux内核,也是很不错的过程哦,做 ...

  2. centos系统设置通过windows代理上网

    网络环境说明: 物理机windows xp sp3系统 ip:192.168.29.21(通过路由上网,有权限设置proxy给其他机器代理上网) 虚拟机centos5.5系统 ip:192.168.2 ...

  3. ThinkPHP 3.1.3及之前的版本使用不当可造成SQLi

    Lib/Core/Model.class.php中解析SQL语句的函数parseSql没有对SQL语句进行过滤,使用不当可导致SQL注入.(哈哈,其实用再安全的框架使用不当都可能造成SQLi) 函数: ...

  4. PHP数组序列化和反序列化

    PHP序列化在我们实际项目运行过程中是一种非常常见的操作.比如当我们想要将数组值存储到数据库时,就可以对数组进行序列化操作,然后将序列化后的值存储到数据库中.其实PHP序列化数组就是将复杂的数组数据类 ...

  5. AC自动机(trie图版)

    AC自动机是一个多模字符串匹配的自动机(网上说的),主要作用是在一个长串中同时进行多个字符串的匹配 基础芝士: trie树(字典树) 烤馍片kmp单模字符串匹配 如果不会的建议去网上学一下(本篇讲解略 ...

  6. python+selenium九:ddt数据驱动

    第一种,测试数据放在Excel里面 test_Login: import unittestimport timeimport ddtimport osfrom selenium import webd ...

  7. DDD领域模型数据访问之对象(十一)

    在工程DDD.Domain中文件夹ModelPermission新建类:BAS_Object public partial class BAS_Obejct:AggreateRoot { //仓储接口 ...

  8. 192 Word Frequency

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  9. 【BZOJ4927】第一题 双指针+DP

    题解: 虽然是过了,不过做的十分智障 首先是有 2根 2 1 1 , 3根 1 1 1 这两种方法 然后考虑2 2 1 1 two-point-two没啥好说的 3 1 1 1 我很智障的以为数据范围 ...

  10. C#连接oracle连接字符串

    /// <summary> /// Oracle 的数据库连接字符串. /// </summary> private const String connString = @&q ...