1 solutions

  • 0
    @ 2025-11-5 17:17:24

    C++ :

    #include<iostream>
    using namespace std;
    const int N = 25;
    const int INF = 9999999;
    int e[N][N],n,k,m,color[N],tot;
    bool check(int k){
    	for(int i=1;i<=n;i++)
    		if(e[k][i]==1&&color[k]==color[i])
    				return false;
    	return true;
    }
    void dfs(int step)
    {
    	if(step==n+1){
    		tot++;
    		return ;
    	}
    	for(int i=1;i<=m;i++){
    		color[step]=i;
    		if(check(step))
    			dfs(step+1);
    		color[step]=0;
    	}
    	return ;
    }
    int main()
    {
    	cin>>n>>k>>m;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    			if(i==j) e[i][j]=0;
    			else e[i][j]=INF;
    	for(int i=1;i<=k;i++){
    		int a,b;
    		cin>>a>>b;
    		e[a][b]=1;
    		e[b][a]=1;
    	}
    	dfs(1);
    	cout<<tot<<endl;
    	return 0;
    }
    
    • 1

    Information

    ID
    18256
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By