前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >图论--2-SAT--Tarjan连通分量+拓扑排序O(N+M)模板

图论--2-SAT--Tarjan连通分量+拓扑排序O(N+M)模板

作者头像
风骨散人Chiam
发布于 2020-10-28 03:49:38
发布于 2020-10-28 03:49:38
30400
代码可运行
举报
文章被收录于专栏:CSDN旧文CSDN旧文
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#define MAXN 2000+10
#define MAXM 400000
#define INF 1000000
using namespace std;
vector<int> G[MAXN];
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
bool Instack[MAXN];
int N, M;
void init()
{
	for(int i = 0; i < 2*N; i++) G[i].clear();
}
void getMap()
{
	int a, b, c;
	char op[10];
	while(M--)
	{
		scanf("%d%d%d%s", &a, &b, &c, op);
		if(op[0]=='A') //表示and
        {
            if(c==1) //表示上述关系为真,即AB为真
            {
                G[N+a].push_back(a);
                G[N+b].push_back(b);
            }
            else
            {
                G[a].push_back(b+N);
                G[b].push_back(a+N);
            }
        }
        else if(op[0]=='O')
        {
            if(c==1)
            {
                G[b + N].push_back(a);
				G[a + N].push_back(b);
            }
            else
            {
                G[a].push_back(a+N);
                G[b].push_back(b+N);
            }
        }
        else if(op[0]=='X')
        {
            if(c==1)
            {
                G[a].push_back(b+N);
                G[a+N].push_back(b);
                G[b+N].push_back(a);
                G[b].push_back(a+N);
            }
            else
            {
                G[a].push_back(b);
				G[b].push_back(a);
				G[a + N].push_back(b + N);
				G[b + N].push_back(a + N);
            }
        }
	}
}
void tarjan(int u, int fa)
{
	int v;
	low[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	Instack[u] = true;
	for(int i = 0; i < G[u].size(); i++)
	{
		v = G[u][i];
		if(!dfn[v])
		{
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
		}
		else if(Instack[v])
		low[u] = min(low[u], dfn[v]);
	}
	if(low[u] == dfn[u])
	{
		scc_cnt++;
		for(;;)
		{
			v = S.top(); S.pop();
			Instack[v] = false;
			sccno[v] = scc_cnt;
			if(v == u) break;
		}
	}
}
void find_cut(int l, int r)
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(sccno, 0, sizeof(sccno));
	memset(Instack, false, sizeof(Instack));
	dfs_clock = scc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1);
}
void solve()
{
	for(int i = 0; i < N; i++)
	{
		if(sccno[i] == sccno[i + N])
		{
			printf("NO\n");
			return ;
		}
	}
	printf("YES\n");
}
int main()
{
	while(scanf("%d%d", &N, &M) != EOF)
	{
		init();
		getMap();
		find_cut(0, 2*N-1);
		solve();
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/11/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
图论--2-SAT--HDOJ/HDU 1824 Let's go home
Problem Description 小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。                         —— 余光中
风骨散人Chiam
2020/10/28
3210
强连通专题
POJ 2762 Going from u to v or from v to u? 题意:判断该图的任意两点是否可达 分析:tarjan后进行缩点,缩点后再建图,判断该图是否为单链式图形(只有一个叶
用户1624346
2018/04/17
9420
acwing-395. 冗余路径(Tarjan双连通分量)
为了从 F 个草场中的一个走到另一个,奶牛们有时不得不路过一些她们讨厌的可怕的树。
全栈程序员站长
2022/09/22
2020
2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Angel_Kitty
2018/04/09
6020
2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
uva 11324 The Largest Clique(图论-tarjan,动态规划)
Given a directed graph G, consider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Create a directed edge between two vertices u and v in T(G) if and only if there is a path between u and v in G that follows the directed edges only in the forward direction. This graph T(G) is often called the transitive closure of G.
全栈程序员站长
2022/07/06
1650
BZOJ2707: [SDOI2012]走迷宫(期望 tarjan 高斯消元)
显然有\(f[x] = \sum_{y} \frac{f[y]}{deg[x]} + 1\)
attack
2019/01/30
4680
BZOJ 1823: [JSOI2010]满汉全席(2-SAT)
Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中。由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而能够烹饪出经过专家认证的满汉全席,也是中国厨师最大的荣誉之一。 世界满汉全席协会是由能够料理满汉全席的专家厨师们所组成,而他们之间还细分为许多不同等级的厨师。为了招收新进的厨师进入世界满汉全席协会,将于近日举办满汉全席大赛,协会派遣许多会员当作评审员,为的就是要在參赛的厨师之中,找到满汉料理界的明日之星。 大会
attack
2018/04/10
7120
图论--2-SAT--详解
现有一个由N个布尔值组成的序列A,给出一些限制关系,比如A[x]AND A[y]=0、A[x] OR A[y] OR A[z]=1等,要确定A[0..N-1]的值,使得其满足所有限制关系。这个称为SAT问题,特别的,若每种限制关系中最多只对两个元素进行限制,则称为2-SAT问题。
风骨散人Chiam
2020/10/28
7350
图论--2-SAT--详解
图论--2-SAT--POJ 3905 Perfect Election
Perfect Election Time Limit: 5000MS         Memory Limit: 65536K Total Submissions: 964         Accepted: 431 Description
风骨散人Chiam
2020/10/28
4040
图论--2-SAT--POJ Ikki's Story IV - Panda's Trick
liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.
风骨散人Chiam
2020/10/28
3850
图论--2-SAT--Ligthoj 1407 Explosion 三元关系枚举
Planet Krypton is about to explode. The inhabitants of this planet have to leave the planet immediately. But the problem is that, still some decisions have to be made - where to go, how to go etc. So, the council of Krypton has invited some of the people to meet in a large hall.
风骨散人Chiam
2020/10/28
3380
图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict
Problem Description Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated. Edward contributes hi
风骨散人Chiam
2020/10/28
4740
TarJan 算法求解有向连通图强连通分量
在有向图G中,如果两个 顶点间至少存在一条路径,称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components)。
RainMark
2019/09/10
1.9K0
TarJan 算法求解有向连通图强连通分量
367. 学校网络(Tarjan强连通分量)[通俗易懂]
一些学校连接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(学校 A 支援学校 B,并不表示学校 B 一定要支援学校 A)。
全栈程序员站长
2022/09/22
3770
Tarjan算法
SCC即强连通分量,即一个图的子图,其中的点能相互到达,全称是strongly connected component。
饶文津
2020/06/02
5840
图论--SCC强连通缩点--Tarjan
强连通缩点与双连通缩点大同小异,也就是说将强连通分支缩成一个点之后,没有强连通,成为有向无环图,在对图进行题目的操作。
风骨散人Chiam
2020/10/28
5780
POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Every cow’s dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
全栈程序员站长
2022/07/08
1870
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
意甲冠军:给定一个有向图有m单向边缘。免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求
全栈程序员站长
2022/07/05
1730
c3pool网页挖矿_actin
于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖煤点坍塌之后,其他挖煤点的工人都有一条道路通向救援出口。
全栈程序员站长
2022/09/22
6450
【POJ 3062】Party(2-SAT、tarjan)
a,a',b,b'分别表示两对夫妇,如果a,b有矛盾,那么a要来,就只能来b',b要来,就只能来a'。于是建了两条边(a,b'),(b,a')。
饶文津
2020/06/02
3790
相关推荐
图论--2-SAT--HDOJ/HDU 1824 Let's go home
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验