列表

详情


NC213953. RikkawithGameTheory

描述

Game theory is an interesting subject in computer science.
SG function is an important concept in game theory. Given a directed acyclic graph G_1 with vertex set V_1 and directed edge set E_1, for each vertex , its SG function sg(u) is defined as:

where given a set S of non-negative integers, is defined as the smallest non-negative integer which is not in S.
Today, Rikka wants to generalize SG function to undirected graphs. Given an undirected graph G with vertex set V and undirected edge set E, a function f over V is a valid SG function on G if and only if:
  • For each vertex , f(u) is a non-negative integer;
  • For each vertex , .
Under this definition, there may be many valid SG functions for a graph. Therefore, Rikka wants to further figure out whether there is a connection between these valid SG functions. As the first step, your task is to calculate the number of valid SG functions for a given undirected graph G.

输入描述

The first line contains two integers , representing the number of vertices and edges in the graph.
Then m lines follow. Each line contains two integers , representing an edge in the graph.
The input guarantees that there are no loops and duplicate edges in the graph.

输出描述

Output a single line with a single integer, representing the number of valid SG functions.

示例1

输入:

5 4
1 2
2 3
3 4
4 5

输出:

6

说明:

For simplicity, we use list [f(1), \dots, f(n)] to represent a function f.
For the sample input, there are 6 valid SG functions: [0,1,0,1,0], [0,1,2,0,1], [0,2,1,0,1], [1,0,1,0,1], [1,0,1,2,0] and [1,0,2,1,0].

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 224ms, 内存消耗: 1944K, 提交时间: 2020-11-25 23:26:32

#include<bits/stdc++.h>
using namespace std;
const int N=17;
int n,m,g[N],w[1<<N];
int64_t dp[1<<N];
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;++i)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		--u,--v;
		g[u]|=1<<v;
		g[v]|=1<<u;
	}
	for(int i=0;i<(1<<n);++i)
	{
		for(int j=0;j<n;++j)
		if(i>>j&1)
		w[i]|=g[j];
	}
	dp[0]=1;
	for(int i=1;i<(1<<n);++i)
	{
		for(int j=i;j;--j&=i)
		{
			if((w[j]&j)==0&&(w[j]&(i^j))==(i^j))
			dp[i]+=dp[i^j];
		}
	}
	printf("%lld\n",dp[(1<<n)-1]);
}

上一题