首页 > 其他分享 >CF662B Graph Coloring

CF662B Graph Coloring

时间:2023-09-15 19:23:30浏览次数:37  
标签:typedef Coloring int Graph CF662B include low now define

很一眼的题

考虑枚举最后所有边的颜色,然后每个点是否变化可以用一个bool变量表示,就是个很典的2-SAT问题,根据当前边和目标的颜色相同与否连边即可

但这题的难点在于要找一个操作次数最少的方案,乍一看很难搞

但如果你对图论和2-SAT那一套理解比较深的话就很容易发现,这道题中所有边都是双向的

这就意味着当选择了某个点后,它所在的联通块的所有点都要被选,那么直接统计下每个联通块内要操作的次数,每次选较少的那边即可

算是个比较有意思的trick,可以mark一下

#include<cstdio>
#include<iostream>
#include<utility>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<set>
#include<array>
#include<random>
#include<bitset>
#include<ctime>
#include<limits.h>
#include<assert.h>
#include<unordered_set>
#include<unordered_map>
#define RI register int
#define CI const int&
#define mp make_pair
#define fi first
#define se second
#define Tp template <typename T>
using namespace std;
typedef long long LL;
typedef long double LDB;
typedef unsigned long long u64;
typedef __int128 i128;
typedef pair <int,int> pi;
typedef vector <int> VI;
typedef array <int,3> tri;
const int N=200005;
int n,m,x[N],y[N],c[N],dfn[N],low[N],stk[N],col[N],vis[N],ext[N],top,idx,scc;
vector <int> v[N],id[N]; char ch[10];
inline void Tarjan(CI now)
{
	dfn[now]=low[now]=++idx; stk[++top]=now; vis[now]=1;
	for (int to:v[now]) if (!dfn[to]) Tarjan(to),low[now]=min(low[now],low[to]);
	else if (vis[to]) low[now]=min(low[now],dfn[to]);
	if (low[now]==dfn[now])
	{
		col[now]=++scc; vis[now]=0;
		if (now<=n) id[scc].push_back(now);
		while (stk[top]!=now)
		{
			col[stk[top]]=scc;
			if (stk[top]<=n) id[scc].push_back(stk[top]); vis[stk[top--]]=0;
		}
		--top;
	}
}
inline void addedge(CI x,CI y)
{
	v[x].push_back(y); v[y].push_back(x);
}
inline vector <int> solve(CI tar)
{
	RI i; for (i=1;i<=2*n;++i) v[i].clear(),id[i].clear(),dfn[i]=ext[i]=0;
	vector <int> ans; for (i=1;i<=m;++i)
	if (c[i]==tar) addedge(x[i],y[i]),addedge(x[i]+n,y[i]+n);
	else addedge(x[i],y[i]+n),addedge(x[i]+n,y[i]);
	for (idx=scc=0,i=1;i<=2*n;++i) if (!dfn[i]) Tarjan(i);
	for (i=1;i<=n;++i) if (col[i]==col[i+n]) return ans.resize(n),ans;
	for (i=1;i<=n;++i)
	{
		if (ext[col[i]]) continue; ext[col[i]]=ext[col[i+n]]=1;
		if (id[col[i]].size()<id[col[i+n]].size())
		for (auto it:id[col[i]]) ans.push_back(it); else
		for	(auto it:id[col[i+n]]) ans.push_back(it);
	}
	return ans;
}
inline void print(vector <int>& ans)
{
	if (ans.size()==n) return (void)(puts("-1"));
	printf("%d\n",ans.size());
	for (auto it:ans) printf("%d ",it);
}
int main()
{
	//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
	RI i; for (scanf("%d%d",&n,&m),i=1;i<=m;++i)
	scanf("%d%d%s",&x[i],&y[i],ch),c[i]=ch[0]=='B';
	auto A1=solve(1),A2=solve(0);
	if (A1.size()<A2.size()) print(A1); else print(A2);
	return 0;
}

标签:typedef,Coloring,int,Graph,CF662B,include,low,now,define
From: https://www.cnblogs.com/cjjsb/p/17705768.html

相关文章

  • TuGraph Analytics流图计算之行为路径归因
    GeaFlow(品牌名TuGraph-Analytics)已正式开源,欢迎大家关注!!!欢迎给我们Star哦!GitHub......
  • 如何实现一个数据库的 UDF?图数据库 NebulaGraph UDF 功能背后的设计与思考
    大家好,我是来自BOSS直聘的赵俊南,主要负责安全方面的图存储相关工作。作为一个从v1.x用到v3.x版本的忠实用户,在见证NebulaGraph发展的同时,也和它一起成长。BOSS直聘和NebulaGraph关于NebulaGraph在BOSS直聘的应用场景,大家可以看看之前文洲老师的文章(图数据库NebulaGr......
  • [VLDB 2012]Efficient Subgraph Matching on Billion Node Graphs
    [VLDB2012]EfficientSubgraphMatchingonBillionNodeGraphs重点了解实现star-join的具体过程。分解query和STwigs排序文中把star叫做STwigs,每一个STwigs查询为\(q=(r,L)\),其中r是跟节点标签,L是子节点标签合集。点的选择性:\(f(v)=deg(v)/freq(v.label)\)分解算法:每次......
  • 数据库重构之路,以 OrientDB 到 NebulaGraph 为例
    “本文由社区用户@阿七从第一视角讲述其团队重构图数据库的过程,首发于阿七公众号「浅谈架构」”原文出处:https://mp.weixin.qq.com/s/WIJNq-nuuAGtMjYo5rPLyg一、写在前面读过我公众号文章的同学都知道,我做过很多次重构,可以说是“重构钉子户”,但是这次,重构图数据库OrientDB......
  • Graph transduction via alternating minimization
    目录概符号说明GTAM交替优化求解WangJ.,JebaraT.andChangS.Graphtransductionviaalternatingminimization.ICML,2008.概一种对类别不均更鲁棒的半监督算法.符号说明\(\mathcal{X}_l=\{\mathbf{x}_1,\cdots,\mathbf{x}_l\}\),labeledinputs;\(\mathcal......
  • 3d-force-graph力导向图,如何让具有相同属性的子节点在一起
    前言3d-force-graph是一种基于力导向图的可视化工具,它可以帮助我们更直观地展示数据之间的关系。在使用3d-force-graph时,我们经常会遇到一种情况,即具有相同属性的子节点需要在一起展示,这时我们可以通过一些方法来实现这个目标。方法一:使用颜色区分我们可以通过为具有相同属性的......
  • JGraphT
    Introduction(简介)JGraphTisafreeJavaclasslibrarythatprovidesmathematicalgraph-theoryobjectsandalgorithms.ItrunsonJava2Platform(requiresJDK11orlaterstartingwithJGraphT1.5.0).GettingStarted(开始上手)TheJGraphT wiki providesvariou......
  • 什么是 GraphQL?
    作者:CatChen链接:https://www.zhihu.com/question/264629587/answer/949588861来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。GraphQL是一种针对Graph(图状数据)进行查询特别有优势的QueryLanguage(查询语言),所以叫做GraphQL。它跟SQL的关系......
  • Qt中QGraphics类坐标映射关系详解
    1、Item(图元)坐标:属于局部坐标,通常以图元中心为原点(中心对称),非中心对称类,比如dialog类,一般以左上角为原点,正方向x朝右,y朝下。2、setPos的坐标是父类坐标系的坐标,一般对于item位于scene中的应用场景。3、scene(场景)坐标:属于逻辑坐标logicalcoordinates(与QPainter相同),以场......
  • QGraphicsScene和QGraphicsView坐标系统
     GraphicsView中有三个坐标系统,即场景坐标、视图坐标、图形项坐标。场景坐标场景坐标等价于QPainter的逻辑坐标,一般以场景中心为原点;视图坐标与设备坐标相同,是物理坐标,默认为左上角为原点;图形项的坐标是局部逻辑坐标,一般以图形项的中心为原点。一个图形项的位置是其中心点在......