/*
思路: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;
}
												

最新文章

  1. 【JavaScript 插件】实现图片倒影效果 - reflex.js
  2. 基于 BinaryReader 的高效切割TXT文件
  3. matlab函数大全
  4. Drupal 7.23版本升级笔记
  5. VS2010提示error TRK0002: Failed to execute command解决方法
  6. UML类图几种关系的总结,泛化 = 实现 &gt; 组合 &gt; 聚合 &gt; 关联 &gt; 依赖
  7. Cocos2d-x——CocosBuilder官方帮助文档翻译2 多分辨率支持
  8. 让 Putty 保存密码,自动登陆的四种方法
  9. /Users/XX/Library/Developer/Xcode/DerivedData/XX.app/xxsdk.bundle Directory not empty
  10. webform的三级联动
  11. poj 1080
  12. Openjudge-计算概论(A)-第二个重复出现的数
  13. 7.21.01 if语句
  14. LCD 显示异常定位分析方法
  15. web中spring框架启动流程第一发
  16. 如何使用Keras的Model visualization功能
  17. springmvc后台接前端的参数,数组,集合,复杂对象等
  18. MySQL笔记(6)---锁
  19. hdu1584
  20. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

热门文章

  1. TRUNCATE TABLE 与 DELETE (转)
  2. 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字
  3. Java实现求二叉树的路径和
  4. Java多线程——线程的优先级和生命周期
  5. Spring &amp;&amp; 实验IOC
  6. Selenium--Python环境部署
  7. TNS-00511: 无监听程序
  8. hibernate对象状态 的小问题
  9. C/S模型:TCP,UDP构建客户端和服务器端(BIO实现
  10. COMMENT - 定义或者改变一个对象的评注