某高斯消元和某2-sat

poj3678
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int dfn[2005],low[2005],mstack[2005],top;
bool instack[2005];
int head[2005],next[4000005],to[4000005],tot;
int dfsnum,cnt,color[2005],n,m,shu1,shu2,shu3;
char cha[10];
void tarjan(int x)
{
	dfn[x]=low[x]=++dfsnum;
	mstack[++top]=x;
	instack[x]=true;
	for(int i=head[x];i;i=next[i])
	{
		int y=to[i];
		if(!dfn[y])
		{
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(instack[y]) low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x])
	{
		cnt++;
		while(1)
		{
			int shu=mstack[top];
			top--;
			color[shu]=cnt;
			instack[shu]=false;
			if(shu==x)
			break;
		}
	}
}
void add(int x,int y)
{
	to[++tot]=y;
	next[tot]=head[x];
	head[x]=tot;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %d %s",&shu1,&shu2,&shu3,cha);
		shu1+=1;
		shu2+=1;
		if(cha[0]=='A')
		{
			if(shu3)
			{
				add(shu1*2,shu2*2);
				add(shu2*2,shu1*2);
				add(shu1*2-1,shu1*2);
				add(shu2*2-1,shu2*2);
			}
			else
			{
				add(shu1*2,shu2*2-1);
				add(shu2*2,shu1*2-1);
			}
		}
		else if(cha[0]=='O')
		{
			if(shu3)
			{
				add(shu2*2-1,shu1*2);
				add(shu1*2-1,shu2*2);
			}
			else
			{
				add(shu1*2-1,shu2*2-1);
				add(shu2*2-1,shu1*2-1);
				add(shu1*2,shu1*2-1);
				add(shu2*2,shu2*2-1);
			}
		}
		else if(cha[0]=='X')
		{
			if(shu3)
			{
				add(shu1*2,shu2*2-1);
				add(shu2*2,shu1*2-1);
				add(shu1*2-1,shu2*2);
				add(shu2*2-1,shu1*2);
			}
			else 
			{
				add(shu1*2,shu2*2);
				add(shu2*2,shu1*2);
				add(shu1*2-1,shu2*2-1);
				add(shu2*2-1,shu1*2-1);
			}
		}
	}
	for(int i=1;i<=2*n;i++)
	if(!dfn[i]) 
	tarjan(i);
	for(int i=1;i<=2*n;i+=2)
	{
		if(color[i]==color[i+1])
		{
			printf("NO");
			return 0;
		}
	}
	printf("YES");
	return 0;
}
/*
2 2
0 1 1 AND
0 1 1 XOR
*/
poj3207
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int dfn[2005],low[2005],mstack[2005],top;
bool instack[2005];
int head[2005],next[4000005],to[4000005],tot,a[1005],b[1005];
int dfsnum,cnt,color[2005],n,m,shu1,shu2,shu3;
void add(int x,int y)
{
	to[++tot]=y;
	next[tot]=head[x];
	head[x]=tot;
}
void tarjan(int x)
{
	dfn[x]=low[x]=++dfsnum;
	mstack[++top]=x;
	instack[x]=true;
	for(int i=head[x];i;i=next[i])
	{
		int y=to[i];
		if(!dfn[y])
		{
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(instack[y]) low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x])
	{
		cnt++;
		while(1)
		{
			int shu=mstack[top];
			top--;
			color[shu]=cnt;
			instack[shu]=false;
			if(shu==x)
			break;
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		if(a[i]>b[i]) 
		swap(a[i],b[i]);
	}
	for(int i=1;i<=m;i++)
	for(int j=i+1;j<=m;j++)
	{
		if((a[i]<a[j]&&a[j]<b[i]&&b[i]<b[j])||(a[j]<a[i]&&a[i]<b[j]&&b[j]<b[i]))
		{
			add(2*i,2*j-1);
			add(2*j,2*i-1);
			add(2*j-1,2*i);
			add(2*i-1,2*j);
		}
	}
	for(int i=1;i<=2*m;i++)
	if(!dfn[i])
	tarjan(i);
	for(int i=1;i<=m*2;i+=2)
	if(color[i]==color[i+1])
	{
		printf("the evil panda is lying again");
		return 0;
	}
	printf("panda is telling the truth...");
	return 0;
}

以及poj1201

需要注意的是要建一条反向边表示sum[i]-sum[i-1]<=1

也就是add(i+1,i,-1)

进行限制

昨天的圆桌骑士和那道题终于找到了错误原因

当前弧优化应该hh[x]=i而不是next[i]

当前这条边可能流量并没有流完

今天又欠了一道题

WA掉的poj2723

明天继续写

寒假作业怕是写不完了

凉凉

猜你喜欢

转载自blog.csdn.net/enesama/article/details/79365698
今日推荐