萌新刚学2-SAT,10pts求助
查看原帖
萌新刚学2-SAT,10pts求助
306954
芊枫Thomitics楼主2021/4/25 21:10

感觉改正了好多错误结果都不变啊

#include <bits/stdc++.h>
#define INF 999999999

using namespace std;

inline long long read()
{
	long long x = 0;
	int f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
void write(const long long &x)
{
	if (!x)
	{
		putchar('0');
		return;
	}
	char f[100];
	long long tmp = x;
	if (tmp < 0)
	{
		tmp = -tmp;
		putchar('-');
	}
	long long s = 0;
	while (tmp > 0)
	{
		f[s++] = tmp % 10 + '0';
		tmp /= 10;
	}
	while (s > 0)
	{
		putchar(f[--s]);
	}
}

long long totN;
long long totM;
struct Edge
{
	int nxt;
	int to;
}edges[4000090];
int head[2000090];
int cnt_edges;
int dfn[2000090];
int low[2000090];
int cntSCC;
int DFScnt;
int inSCC[2000090];
stack<int> Q;
bool inQ[2000090];
void add_edges(int x,int y)
{
	++cnt_edges;
	edges[cnt_edges].nxt=head[x];
	head[x]=cnt_edges;
	edges[cnt_edges].to=y;
}

void Tarjan(int nowX)
{
	++DFScnt;
	dfn[nowX] = low[nowX] = DFScnt;
	inQ[nowX] = true;
	Q.push(nowX);
	for (int i = head[nowX]; i; i = edges[i].nxt)
	{
		if (!dfn[edges[i].to])
		{
			Tarjan(edges[i].to);
			low[nowX] = min(low[edges[i].to], low[nowX]);
		} else if (inQ[nowX])
		{
			low[nowX] = min(dfn[edges[i].to], low[nowX]);
		}
	}
	if (dfn[nowX] == low[nowX])
	{
		++cntSCC;
		int y = -1;
		while (y != nowX && !Q.empty())
		{
			y = Q.top();
			inQ[y] = false;
			inSCC[y] = cntSCC;
			Q.pop();
		}
		inSCC[nowX] = cntSCC;
		if (!Q.empty())
		{
			Q.pop();
		}
	}
}

int main()
{
	totN = read();
	totM = read();
	for (int i = 1, x, y, s, t; i <= totM; ++i)
	{
		x = read();
		s = read();
		y = read();
		t = read();
		if ((!s) && (!t))
		{
			add_edges(x, y + totN);
			add_edges(y, x + totN);
		} else if (s && t)
		{
			add_edges(y + totN, x);
			add_edges(x + totN, y);
		} else if ((!s) && t)
		{
			add_edges(y + totN, x + totN);
			add_edges(x, y);
		} else if (s && (!t))
		{
			add_edges(y, x);
			add_edges(x + totN, y + totN);
		}
	}
	for (int i = 1; i <= totN * 2; ++i)
	{
		if (!dfn[i])
		{
			Tarjan(i);
		}
	}
	for (int i = 1; i <= totN; ++i)
	{
		if (inSCC[i] == inSCC[i + totN])
		{
			puts("IMPOSSIBLE");
			exit(0);
		}
	}
	puts("POSSIBLE\n");
	for (int i = 1; i <= totN; ++i)
	{
		if (inSCC[i] < inSCC[i + totN])
		{
			putchar('1');
			putchar(' ');
		} else
		{
			putchar('0');
			putchar(' ');
		}
	}
	return 0;
} //Thomitics Code
2021/4/25 21:10
加载中...