<p>An array is <strong>squareful</strong> if the sum of every pair of adjacent elements is a <strong>perfect square</strong>.</p>

<p>Given an integer array nums, return <em>the number of permutations of </em><code>nums</code><em> that are <strong>squareful</strong></em>.</p>

<p>Two permutations <code>perm1</code> and <code>perm2</code> are different if there is some index <code>i</code> such that <code>perm1[i] != perm2[i]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,17,8]
<strong>Output:</strong> 2
<strong>Explanation:</strong> [1,8,17] and [17,8,1] are the valid permutations.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2,2]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 12</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
private:
    unordered_set<string> squarefulPerms;
    string serialize(const vector<int>& nums, const vector<int>& perms) {
        string s;
        for(int ind:perms) {
            s.append(to_string(nums[ind]));
            s.push_back(',');
        }
        return s;
    }
    int numSquarefulPerms(const vector<int>& nums,vector<int>& perm,const vector<vector<int>>&edges, vector<int>& in_perm) {
        int last=perm.back();
        int ans=0;
        unordered_set<int> used_num;
        for(int next:edges[last]) {
            if (in_perm[next]==1) {
                continue;
            }
            if(used_num.contains(nums[next])) {
                continue;
            }
            used_num.insert(nums[next]);
            in_perm[next]=1;
            perm.push_back(next);
            if (perm.size()==nums.size()) {
                string s=serialize(nums,perm);
                perm.pop_back();
                in_perm[next]=0;
                if (squarefulPerms.contains(s)) {
                    return 0;
                }
                squarefulPerms.insert(s);
                return 1;
            }
            ans+=numSquarefulPerms(nums, perm,edges,in_perm);
            perm.pop_back();
            in_perm[next]=0;
        }
        return ans;
    }
public:
    int numSquarefulPerms(vector<int>& nums) {
        unordered_set<int> squared;
        for(int i=0;i<1000;i++) {
            squared.insert(i*i);
        }
        vector<vector<int>> edges(nums.size());
        for(int i=0;i<nums.size()-1;i++) {
            for(int j=i+1;j<nums.size();j++) {
                if(squared.contains(nums[i]+nums[j])) {
                    edges[i].push_back(j);
                    edges[j].push_back(i);
                }
            }
        }
        int ans=0;
        vector<int> perm;
        vector<int> in_perm(nums.size(),0);
        for(int i=0;i<nums.size();i++) {
            perm={i};
            in_perm[i]=1;
            ans+=numSquarefulPerms(nums, perm, edges,in_perm);
            in_perm[i]=0;
        }
        return ans;
    }
};
18100