当前位置:网站首页>第四讲:圆桌座位
第四讲:圆桌座位
2022-07-16 19:11:00 【奋斗吧!骚年!】
题目:AcWing 1491. 圆桌座位
N 个人围坐一圈,有 M 对朋友关系。
第 i 对朋友关系是指,编号是 ai 的人和编号是 bi 的人是朋友。
现在要给他们安排座位,要求所有相邻的人不能是朋友。
问共有多少种方案?
如果两个方案只有旋转角度不同,则我们将其视为一种方案。
输入格式
第一行包含两个整数 N,M。
接下来 M 行,每行包含一对 ai,bi。
输出格式
输出一个数,表示总方案数。
数据范围
输入样例1:
4 1
1 2
输出样例1:
2
输入样例2:
10 5
1 2
3 4
5 6
7 8
9 10
输出样例2:
112512
题目分析:
首先看到N的范围很小,所以一般会使用暴力搜索
因为旋转过后相等也视为同一种方案,所以我们固定第一个为1。(关键)
最后我们使用DFS进行搜索即可,记得回溯一下。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 15;
int g[N][N];
int st[N];
int choose[N];
int res=0;
int n,m;
void dfs(int k)
{
if(k==n)
{
if(g[choose[0]][choose[n-1]]==0)res++;
return;
}
for(int i=2;i<=n;i++)
{
if(!st[i]&&!g[choose[k-1]][i])
{
choose[k]=i;
st[i]=1;
dfs(k+1);
st[i]=0;
}
}
}
int main()
{
cin>>n>>m;
while(m--)
{
int a,b;
cin>>a>>b;
g[a][b]=g[b][a]=1;
}
st[1]=1;
choose[0]=1;
dfs(1);
cout<<res<<endl;
return 0;
}
边栏推荐
- 机器学习笔记 - 使用 GAN 进行数据增强以进行缺陷检测
- FreeSwitch的限流配置
- Layoffs are coming
- I rolled up a small and beautiful [markdown static blog program] at home
- Blog migration from cloudbase to virtual machine
- 如何制作选中项的下划线样式
- Detailed explanation of the use of documents
- Multithreaded operation list
- PyQt5-字体对话框(QFontDialog)
- 8大主流OA办公软件比拼,传统VS新秀你PICK谁?
猜你喜欢

鎳氫電池的特性和使用方法(FDK鎳氫電池充電機制)

How to deal with the security risks of the third party in the supply chain

What are the directions of voice signal processing in audio and video?

Application of Apache E8 industrial computer minipicecan card in Construction Robot
![Leetcode high frequency question: three unordered arrays a, B, C with length N, find the total number of combinations of (I, J, K) with a[i] + b[j] + c[k] = 64](/img/b0/8ed026a2fab2a3e6c95be2a6a424d9.png)
Leetcode high frequency question: three unordered arrays a, B, C with length N, find the total number of combinations of (I, J, K) with a[i] + b[j] + c[k] = 64

ES6-新增的数组方法之最常用的几种 map(),filter(),reduce(),forEach(),

AcWing 396. Solution to the problem of mine construction (tarjan cut point)

Win11怎么打开3D查看器

PyQt5-文件对话框(QFileDialog)

Layoffs are coming
随机推荐
Caractéristiques et mode d'utilisation de la batterie ni MH (mécanisme de charge de la batterie ni MH FDK)
MySQL -- string function
Topic 2-iis write permission vulnerability analysis and traceability
Linux服务器装mysql数据库(详细教程)
[wechat applet] progress (93/100)
一人用低代码开发平台搭建整个集团的数字化系统解决方案
PyQt5-字体对话框(QFontDialog)
One person builds the digital system solution of the whole group with a low code development platform
淺學js中的關系運算符
On learning relational operators in JS
Other new features of MySQL MySQL 8
Building a one-stop management system solution for student affairs on a low code platform
如何在自动化测试中使用MitmProxy获取数据返回?
Introduction to phabricator conduct API
阿普奇 E8 工控机——MinipiceCAN卡在建筑机器人的应用
2022年协同办公系统(OA系统)选型对比参考
低代码平台搭建学生事务一门式管理系统解决方案
Best practices for exclusive resource pool use -notebook and training task linkage
多线程操作List
Installing MySQL database on Linux server (detailed tutorial)