Acwing 1491. 圆桌座位

https://www.acwing.com/problem/content/1493/

输入样例1:
4 1
1 2
输出样例1:
2
输入样例2:
10 5
1 2
3 4
5 6
7 8
9 10
输出样例2:
112512
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const LL MAXN=1e18,MINN=-MAXN,INF=0x3f3f3f3f;
const LL N=200200,M=2020;
LL n,m;
LL st[N],g[M][M];
int res;
void dfs(int idx,int last)
{
    if(idx>=n)
    {
        if(g[last][1]==0)//首尾不相连
        {
            res++;
            return ;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(st[i]==0&&g[i][last]==0)//没有遍历过而且不是朋友
        {
            st[i]=1;
            dfs(idx+1,i);
            st[i]=0;//恢复现场
        }
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            g[x][y]=g[y][x]=1;
        }
        st[1]=true;//将第一个人固定在一号座位
        dfs(1,1);
        cout<<res<<endl;
    }
    return 0;
}
posted @ 2024-03-29 11:52  高尔赛凡尔娟  阅读(9)  评论(0编辑  收藏  举报