제출 #1271157

#제출 시각아이디문제언어결과실행 시간메모리
1271157BigBadBully9월 (APIO24_september)C++20
45 / 100
551 ms14932 KiB
#include "september.h"

#include <bits/stdc++.h>
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;

pii p = {109,59};
pii mod = {1e9+7,1e9+9};

int exp(int x,int a,int mod)
{
    int ans = 1;
    for (;a>0;a/=2,x=x*x%mod)
        if(a%2) ans=ans*x%mod;
    return ans;
}

int solve(int N, int M, std::vector<int> F, std::vector<std::vector<int>> S) {
	
    int n=N,m=M;
    auto par = F;
    auto s = S;
    vector<vector<int>> del(m,vector<int>(n,0));
    vector<vector<int>> graph(n);
    for (int i = 1; i < n; i++)
        graph[par[i]].push_back(i);
    
    vector<int> cnt(m,0);
    function<void(int,int)> dfs = [&](int cur,int typ)
    {
        if (del[typ][cur])return;
        cnt[typ]++;
        del[typ][cur]  =1;
        for(int a : graph[cur])
            dfs(a,typ);
    };
    vector<pii> hash(m);
    map<pii,int> here;
    here[{0,0}] = m;
    int ans = 0;
    for (int i = 0; i < n-1; i++)
    {
        
        for (int j = 0; j < m; j++)
        {
            dfs(s[0][i],j);
            here[hash[j]]--;
            hash[j].ff+=exp(p.ff,s[j][i],mod.ff);
            hash[j].ff%=mod.ff;
            hash[j].ss+=exp(p.ss,s[j][i],mod.ss);
            hash[j].ss%=mod.ss;
            here[hash[j]]++;
        }
        bool good = 1;
        for (int j = 0; j < m; j++)
            if(cnt[j]!=i+1)good=0;
        if(here[hash[0]]==m && good)
            ans++;
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...