/*
思路:http://blog.csdn.net/string_yi/article/details/12686873
hdu 1814 输出字典序最小的2-sat
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 16100
#define NN 210000
struct node {
int v,w,next;
}bian[NN*2];
int head[N],cnt,yong,color[N],ans[N];
void init() {
yong=0;
memset(head,-1,sizeof(head));
yong=0;
memset(color,0,sizeof(color));
}
void addedge(int u,int v) {
bian[yong].v=v;
bian[yong].next=head[u];
head[u]=yong++;
}
int dfs(int v)
{
if(color[v]==2)
return 0;
if(color[v]==1)
return 1;
ans[cnt++]=v;
color[v]=1;
color[v^1]=2;
for(int i=head[v];i!=-1;i=bian[i].next)
if(!dfs(bian[i].v))
return 0;
return 1;
}
void slove(int n) {
int i,j;
for(i=0;i<2*n;i++) {
if(color[i])continue;
cnt=0;
if(!dfs(i)) {
for(j=0;j<cnt;j++) {
color[ans[j]]=0;
color[ans[j]^1]=0;
}
if(!dfs(i^1)) {
printf("NIE\n");
return ;
}
}
}
for(i=0;i<2*n;i+=2) {
if(color[i]==1)
printf("%d\n",i+1);
else
printf("%d\n",i+2);
}
}
int main() {
int n,aa,bb,m;
while(scanf("%d%d",&n,&m)!=EOF) {
init();
while(m--) {
scanf("%d%d",&aa,&bb);
aa--;bb--;
addedge(aa,bb^1);
addedge(bb,aa^1);
}
slove(n);
}
return 0;}
输出任意次序<pre name="code" class="cpp">/*
hit 1917输出任意次序的2-sat
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn=16005;
const int maxm=200005;
int head[maxn],next[maxm],to[maxm];
int dfn[maxn],low[maxn],stk[maxn],scc[maxn],ind[maxn],vis[maxn];
int color[maxn],f[maxn];
int tot,top,cnt,id;
vector<int> dag[maxn];
void addEdage(int u,int v)
{
next[tot]=head[u],to[tot]=v,head[u]=tot++;
}
void init()
{
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(ind,0,sizeof(ind));
memset(color,0,sizeof(color));
tot=top=cnt=id=0;
}
void tarjan(int v)
{
dfn[v]=low[v]=++cnt;
vis[v]=1;
stk[++top]=v;
for(int i=head[v];i!=-1;i=next[i])
{
int u=to[i];
if(!dfn[u])
{
tarjan(u);
low[v]=min(low[v],low[u]);
}
else if(vis[u])
low[v]=min(low[v],dfn[u]);
}
if(low[v]==dfn[v])
{
id++;
while(true)
{
int u=stk[top--];
vis[u]=0;
scc[u]=id;
if(u==v)break;
}
}
}
void buildDag(int n)
{
for(int u=0;u<2*n;u++)
for(int i=head[u];i!=-1;i=next[i])
{
int v=to[i];
if(scc[v]!=scc[u])
{
dag[scc[v]].push_back(scc[u]);
ind[scc[u]]++;
}
}
}
void topsort()
{
queue<int> q;
for(int i=1;i<=id;i++)
if(!ind[i])q.push(i);
while(!q.empty())
{
int u=q.front();
q.pop();
if(!color[u])
color[u]=1,color[f[u]]=2;
for(int i=0;i<(int)dag[u].size();i++)
{
int v=dag[u][i];
ind[v]--;
if(!ind[v])q.push(v);
}
}
}
void solve(int n)
{
for(int i=0;i<2*n;i++)
if(!dfn[i])tarjan(i);
for(int i=0;i<2*n;i+=2)
if(scc[i]==scc[i+1])
{
printf("NIE\n");
return;
}
else f[scc[i]]=scc[i+1],f[scc[i+1]]=scc[i];
for(int i=0;i<=id;i++)
dag[i].clear();
buildDag(n);
topsort();
for(int i=0;i<2*n;i+=2)
{
if(color[scc[i]]==1)
printf("%d\n",i+1);
else
printf("%d\n",i+2);
}
}
int main()
{
// freopen("in.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0,a,b;i<m;i++)
{
scanf("%d%d",&a,&b);
a--,b--;
addEdage(a,b^1);
addEdage(b,a^1);
}
solve(n);
}
return 0;
}
												

hdu 1814 2-sat 输出字典最小的和任意序列的 模板题的更多相关文章

  1. HDU 1814 Peaceful Commission

    2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...

  2. HDU 3264 区间内的最大最小之差

    题目链接:http://poj.org/problem?id=3264 题目大意:在给定一堆牛的数量以及其高度的时候,每次给定一段区间,求这个区间内最高的牛和最矮的牛的高度之差为多少. 可以直接利用R ...

  3. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  4. Peaceful Commission HDU - 1814(输出最小的一组解)

    Description 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: ...

  5. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】

    <题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...

  6. hdu 1814 字典序最小的2sat(暴力深搜)

    题意:      题意就是最基础的2sat,关系只有矛盾关系,然后二选一,关键是这个题目是输出字典序最小的那组解. 思路:      输出字典序最小,用强连通那个实现不了(起码没看到有人实现),其实我 ...

  7. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  8. 输入n个整数,输出其中最小的k个

    描述 输入n个整数,输出其中最小的k个. 详细描述: 接口说明 原型: bool GetMinK(unsignedint uiInputNum, int * pInputArray, unsigned ...

  9. 排序,求几个最值问题,输入n个整数,输出其中最小的k个元素。

    看完两个求最大值算法之后的一些感想. 如果想直接看算法的可以跳过.但是我觉得我这些想法还是比较有用的,至少对我将来的算法设计是这样的. 算法的功能越强大,必然意味着速度慢,因为根据丛林法则,那种慢又功 ...

随机推荐

  1. 对比度受限的自适应直方图均衡化(CLAHE)

    直方图均衡化(HE)是一种很常用的直方图类方法,基本思想是通过图像的灰度分布直方图确定一条映射曲线,用来对图像进行灰度变换,以达到提高图像 对比度的目的.该映射曲线其实就是图像的累计分布直方图(CDF ...

  2. SecureCRT 迁移到新环境,导出配置文件目录 转

    SecureCRT 打开SecureCRT,点击菜单栏的“选项”--“全局选项” 在打开的窗口中,选择“常规”,在右侧找到“配置文件夹”,这个就是SecureCRT的配置文件目录. 复制这个路径并且进 ...

  3. spring tool suite开发环境搭建

    先把是构建工具maven: maven里面有一个conf文件夹,然后里面有个setting.xml配置文件,先要把项目要的setting.xml覆盖这个原来的配置文件. 这个maven配置文件有一个作 ...

  4. 438 Find All Anagrams in a String 找出字符串中所有的变位词

    详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...

  5. poj1715Hexadecimal Numbers(数位dp)

    链接 好久没写这种逐位计数的了. 先统计出总的数 ,s-n+1,倒着计算的 ,感觉倒着比较符合计算方式,总数为15*A(15,i) (1=<i<=8) 也就是1-8长度所有的排列总数 然后 ...

  6. C# 控制台语音计算器

    记得上高中时,给人当会计,帮忙结算月度工资:用的就是带语音功能的计算器! 当时用起来倍儿爽,于是速度加倍,效率加速:结果让老板赔了不少钱! 就是因为这个,才对语音计算器有了深刻印象!可能是这货坑了我! ...

  7. C#特性的介绍及应用场景

    1.特性的任务:特性就是为了支持对象添加一些自我描述的信息,不影响类封装的前提添加额外信息.如果你用这个信息,那特性就有用:如果你不需要这个信息,那么这个特性就没用. 2.特性的基类:Attribut ...

  8. R in action读书笔记(19)第十四章 主成分和因子分析

    第十四章:主成分和因子分析 本章内容 主成分分析 探索性因子分析 其他潜变量模型 主成分分析(PCA)是一种数据降维技巧,它能将大量相关变量转化为一组很少的不相关变量,这些无关变量称为主成分.探索性因 ...

  9. php中字符与字节的区别

    字符: 字符是可使用多种不同字符方案或代码页来表示的抽象实体.例如,Unicode UTF-16 编码将字符表示为 16 位整数序列,而 Unicode UTF-8 编码则将相同的字符表示为 8 位字 ...

  10. iOS-UI控件之UIButton

    ---恢复内容开始--- UIButton 既可以显示图片,又可以显示文字,还能随时调整内部位置 系统自带尺寸 storyboard内部调整UIButton属性 状态 监听按钮点击事件 凡是继承自UI ...