图论练习4

内容:染色划分,带权并查集,扩展并查集

Arpa’s overnight party and Mehrdad’s silent entering

题目链接

题目大意

  • 2n个点围成一圈,分为n对,对内两点不同染色
  • 同时,相邻3个点之间必须有两个点不同染色
  • 问构造出一种染色方案

解题思路 

  •  将每对进行的连边看作一类边
  • 将为满足相邻3个点必须有两个点不同染色的边,看作二类边
  • 满足构造方案,即将2n个点形成一个二分图,无奇环
  • 对于只有一类边,形不成环,端点不同
  • 对于二类边与一类边组合才能形成环
  • 将二类边定义为2_i\leftrightarrow 2_i+1
  • 成环的情况只能,如下图
  • 由于一类边不可能相连,中间必然是二类边,一类边交换,所以环上的点数为二类边的总点数,一定为偶数,只能形成偶环,构造成立
  • 利用带权并查集实现

import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.Vector;public class Main{static int find(int x,int[] fa,int[] relation) {if(x==fa[x])return x;else {int f=fa[x];fa[x]=find(fa[x], fa, relation);relation[x]=(relation[x]+relation[f])%2;return fa[x];}}public static void main(String[] args) throws IOException{AReader input=new AReader();PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));int n=input.nextInt();int[] fa=new int[2*n+1];int[] relation=new int[2*n+1];for(int i=1;i<=2*n;++i)fa[i]=i;int[] a=new int[n+1];int[] b=new int[n+1];for(int i=1;i<=n;++i) {int x=input.nextInt();int y=input.nextInt();a[i]=x;b[i]=y;int fx=find(x, fa, relation);int fy=find(y, fa, relation);if(fx==fy)continue;fa[fx]=fy;relation[fx]=(relation[y]+1-relation[x]+2)%2;}for(int i=1;i<=n;++i) {//一圈int x=2*i;int y=i==n?1:2*i+1;int fx=find(x, fa, relation);int fy=find(y, fa, relation);if(fx==fy)continue;fa[fx]=fy;relation[fx]=(relation[y]+1-relation[x]+2)%2;}for(int i=1;i<=n;++i) {find(a[i], fa, relation);find(b[i], fa, relation);int x=relation[a[i]]+1;int y=relation[b[i]]+1;out.println(x+" "+y);}out.flush();out.close();}staticclass AReader{BufferedReader bf;StringTokenizer st;BufferedWriter bw;public AReader(){bf=new BufferedReader(new InputStreamReader(System.in));st=new StringTokenizer("");bw=new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException{return bf.readLine();}public String next() throws IOException{while(!st.hasMoreTokens()){st=new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException{//确定下一个token只有一个字符的时候再用return next().charAt(0);}public int nextInt() throws IOException{return Integer.parseInt(next());}public long nextLong() throws IOException{return Long.parseLong(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public float nextFloat() throws IOException{return Float.parseFloat(next());}public byte nextByte() throws IOException{return Byte.parseByte(next());}public short nextShort() throws IOException{return Short.parseShort(next());}public BigInteger nextBigInteger() throws IOException{return new BigInteger(next());}public void println() throws IOException {bw.newLine();}public void println(int[] arr) throws IOException{for (int value : arr) {bw.write(value + " ");}println();}public void println(int l, int r, int[] arr) throws IOException{for (int i = l; i <= r; i ++) {bw.write(arr[i] + " ");}println();}public void println(int a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{bw.write(a);bw.newLine();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}}
}

食物链 

题目链接 

题目大意

  • n个点,k对关系
  • 每个点可能为A/B/C类,它们之间满足ABBCCA
  • k对关系中,1,x,y表示x,y同类,2,x,y表示xy
  • 依次处理k对关系,若当前关系与之前的关系矛盾,则视为错误
  • x>n,y>n,(2,x,y)x=y(自己吃自己),均视为错误
  • 问有多少对错误

解题思路

  • n个点进行3种颜色的染色划分
  • 类似黑白染色
  • 利用带权并查集或扩展并查集实现
  • 详见图论笔记 

扩展并查集 

 
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.Vector;public class Main{static int find(int x,int[] fa) {if(x==fa[x])return x;else return fa[x]=find(fa[x], fa);}public static void main(String[] args) throws IOException{AReader input=new AReader();PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));int n=input.nextInt();int k=input.nextInt();int[] fa=new int[3*n+1];for(int i=1;i<=3*n;++i)fa[i]=i;int ans=0;for(int i=1;i<=k;++i) {int op=input.nextInt();int x=input.nextInt();int y=input.nextInt();if(x>n||y>n) {ans++;continue;}if(op==1) {if(find(x, fa)==find(y+n, fa)||find(y, fa)==find(x+n, fa)) {//x吃y或y吃x//x吃y=>xA->yB,xB->yC,xC->yA//y吃x=>yA->xB,yB->xC->yC->xAans++;continue;}fa[find(x, fa)]=find(y, fa);fa[find(x+n, fa)]=find(y+n, fa);fa[find(x+2*n, fa)]=find(y+2*n, fa);}else {if(x==y) {ans++;continue;}if(find(x, fa)==find(y, fa)||find(y, fa)==find(x+n, fa)) {//x和y是同类,y吃xans++;continue;}fa[find(x, fa)]=find(y+n, fa);fa[find(x+n, fa)]=find(y+2*n, fa);fa[find(x+2*n, fa)]=find(y, fa);}}out.print(ans);out.flush();out.close();}staticclass AReader{BufferedReader bf;StringTokenizer st;BufferedWriter bw;public AReader(){bf=new BufferedReader(new InputStreamReader(System.in));st=new StringTokenizer("");bw=new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException{return bf.readLine();}public String next() throws IOException{while(!st.hasMoreTokens()){st=new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException{//确定下一个token只有一个字符的时候再用return next().charAt(0);}public int nextInt() throws IOException{return Integer.parseInt(next());}public long nextLong() throws IOException{return Long.parseLong(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public float nextFloat() throws IOException{return Float.parseFloat(next());}public byte nextByte() throws IOException{return Byte.parseByte(next());}public short nextShort() throws IOException{return Short.parseShort(next());}public BigInteger nextBigInteger() throws IOException{return new BigInteger(next());}public void println() throws IOException {bw.newLine();}public void println(int[] arr) throws IOException{for (int value : arr) {bw.write(value + " ");}println();}public void println(int l, int r, int[] arr) throws IOException{for (int i = l; i <= r; i ++) {bw.write(arr[i] + " ");}println();}public void println(int a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{bw.write(a);bw.newLine();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}}
}

 带权并查集

 
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.Vector;public class Main{static int find(int x,int[] fa,int[] relation) {if(x==fa[x])return x;else {int f=fa[x];fa[x]=find(fa[x], fa, relation);relation[x]=(relation[x]+relation[f])%3;return fa[x];}}public static void main(String[] args) throws IOException{AReader input=new AReader();PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));int n=input.nextInt();int k=input.nextInt();int ans=0;int[] fa=new int[n+1];int[] relation=new int[n+1];for(int i=1;i<=n;++i) fa[i]=i;for(int i=1;i<=k;++i) {int op=input.nextInt();int x=input.nextInt();int y=input.nextInt();if(x>n||y>n) {ans++;continue;}if(op==1) {int fx=find(x, fa, relation);int fy=find(y, fa, relation);if(fx==fy) {if(relation[x]!=relation[y]) {ans++;continue;}}else {fa[fx]=fy;relation[fx]=(relation[y]+0-relation[x]+3)%3;}}else {if(x==y) {ans++;continue;}int fx=find(x, fa, relation);int fy=find(y, fa, relation);if(fx==fy) {//0吃1,1吃2,2吃0if(relation[x]==0&&relation[y]==1||relation[x]==1&&relation[y]==2||relation[x]==2&&relation[y]==0)continue;ans++;continue;}else {fa[fx]=fy;relation[fx]=(relation[y]+2-relation[x]+3)%3;}}}out.println(ans);out.flush();out.close();}staticclass AReader{BufferedReader bf;StringTokenizer st;BufferedWriter bw;public AReader(){bf=new BufferedReader(new InputStreamReader(System.in));st=new StringTokenizer("");bw=new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException{return bf.readLine();}public String next() throws IOException{while(!st.hasMoreTokens()){st=new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException{//确定下一个token只有一个字符的时候再用return next().charAt(0);}public int nextInt() throws IOException{return Integer.parseInt(next());}public long nextLong() throws IOException{return Long.parseLong(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public float nextFloat() throws IOException{return Float.parseFloat(next());}public byte nextByte() throws IOException{return Byte.parseByte(next());}public short nextShort() throws IOException{return Short.parseShort(next());}public BigInteger nextBigInteger() throws IOException{return new BigInteger(next());}public void println() throws IOException {bw.newLine();}public void println(int[] arr) throws IOException{for (int value : arr) {bw.write(value + " ");}println();}public void println(int l, int r, int[] arr) throws IOException{for (int i = l; i <= r; i ++) {bw.write(arr[i] + " ");}println();}public void println(int a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{bw.write(a);bw.newLine();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{bw.write(String.valueOf(a));bw.newLine();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/451180.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

24.云原生ArgoCD高级之钩子

云原生专栏大纲 文章目录 Argo CD钩子如何定义钩子钩子删除策略 Argo CD钩子 Argo CD 是一个用于部署和管理 Kubernetes 应用程序的工具&#xff0c;它提供了一种声明式的方式来定义和自动化应用程序的部署过程。Argo CD 钩子&#xff08;Hooks&#xff09;是一种机制&#x…

远程手机搭建Termux环境,并通过ssh连接Termux

背景 Termux只能通过鼠标点击&#xff0c;无法使用电脑键盘&#xff0c;输入速度很慢&#xff0c;你想通过ssh 连接Termux&#xff0c;获得友好体验搞了个云手机&#xff0c;想像普通手机那样充当服务器想把自己的手机公开到局域网中供同事调试想把自己的模拟器公开到局域网中…

【MySQL】——数据定义

&#x1f383;个人专栏&#xff1a; &#x1f42c; 算法设计与分析&#xff1a;算法设计与分析_IT闫的博客-CSDN博客 &#x1f433;Java基础&#xff1a;Java基础_IT闫的博客-CSDN博客 &#x1f40b;c语言&#xff1a;c语言_IT闫的博客-CSDN博客 &#x1f41f;MySQL&#xff1a…

【考研408】操作系统笔记

文章目录 [toc] 计算机系统概述操作系统的基本概念操作系统的概念和特征操作系统的目标和功能&#xff08;**处理器管理、存储器管理、设备管理、文件管理、向用户提供接口、扩充机器**&#xff09; 操作系统的发展与分类操作系统的运行环境操作系统的运行机制 操作系统的体系结…

从MySQL到TiDB:兼容性全解析

MySQL 在高并发和大数据量场景下&#xff0c;单个实例的扩展性有限。而 TiDB 作为一款分布式NewSQL数据库&#xff0c;设计之初就支持水平扩展&#xff08;Scale-Out&#xff09;&#xff0c;通过增加节点来线性提升处理能力和存储容量&#xff0c;能够很好地应对大规模数据和高…

C系列-柔性数组

&#x1f308;个人主页: 会编程的果子君 ​&#x1f4ab;个人格言:“成为自己未来的主人~” 目录 ​编辑 柔性数组 柔性数组的特点 柔性数组的使用 柔性数组的优势 柔性数组 也许你从来没有听说过柔性数组这个概念&#xff0c;但是它确实是存在的&#xff0c;C99中&#…

大带宽服务器托管的特点和考虑因素

很多公司和企业对于使用大带宽服务器的需求和存储不一样&#xff0c;为了满足不同的用户需求&#xff0c;大带宽服务器托管是个不错的选择&#xff0c;小编为您整理发布大带宽服务器托管的特点和要考虑的因素。 大带宽服务器托管是一种服务器托管服务&#xff0c;其主要特点是…

DS:经典算法OJ题(2)

创作不易&#xff0c;友友们给个三连吧&#xff01;&#xff01; 一、旋转数组&#xff08;力扣&#xff09; 经典算法OJ题&#xff1a;旋转数组 思路1&#xff1a;每次挪动1位&#xff0c;右旋k次 时间复杂度&#xff1a;o(N^2) 右旋最好情况&#xff1a;k是n的倍数…

2017年苏州大学837复试机试C/C++

2017年苏州大学复试机试 要求 要求用C/C编程&#xff1b;对程序中必要的地方进行注释。上机规则 请在电脑桌面上新建一个文件夹文件夹名为考试姓名&#xff08;中文&#xff09;&#xff1b;考试完毕后&#xff0c;将所编写的文件放在上述文件中。 第一题&#xff08;20分&…

Linux 多线程 | 线程的概念

线程的概念 线程是一个执行分支&#xff0c;执行粒度比进程更细&#xff0c;调度成本更低&#xff1b; 线程是进程内部的一个执行流&#xff1b; 线程是CPU调度的基本单位&#xff0c;进程是承担分配系统资源的基本实体。 之前我们学习过虚拟地址空间的知识&#xff0c;知道…

倾斜摄影轻量化,这样才规范!

自然资源部发布了《倾斜数字摄影测量技术规程》等17项行业标准报批稿公示&#xff0c;其中高频出现的倾斜摄影技术&#xff0c;重回大众视野。 倾斜摄影技术是国际测绘领域近些年发展起来的一项高新技术&#xff0c;它颠覆了以往正射影像只能从垂直角度拍摄的局限&#xff0c;通…

69.请描述Spring MVC的工作流程?描述一下 DispatcherServlet 的工作流程?

69.请描述Spring MVC的工作流程&#xff1f;描述一下 DispatcherServlet 的工作流程&#xff1f; 核心架构的具体流程步骤如下&#xff1a; 首先用户发送请求——>DispatcherServlet&#xff0c;前端控制器收到请求后自己不进行处理&#xff0c;而是委托给其他的解析器进行…