2017 Multi-University Training Contest 1 && HDOJ 6038 Function 【强连通找环】


Function

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit:131072/131072 K (Java/Others)
Total Submission(s): 348    Accepted Submission(s): 136

Problem Description

You are given a permutation a from 0 ton−1 and apermutationb from 0 tom−1.

Define that the domain of function
f is the set of integers from 0 ton−1, and the range of it is the set of integers from 0 tom−1.

Please calculate the quantity of different functions
f satisfying thatf(i)=bf(ai) for each i from 0 ton−1.

Two functions are different if and only if there exists at least one integer from 0 to
n−1 mapped into different integers in these two functions.

The answer may be too large, so please output it in modulo 109+7.

 

 

Input

The input contains multiple test cases.

For each case:

The first line contains two numbers
n,m. (1≤n≤100000,1≤m≤100000)

The second line contains
n numbers,ranged from 0 ton−1, thei -th number of which representsai−1.

The third line contains
m numbers,ranged from 0 tom−1, thei -th number of which representsbi−1.

It is guaranteed that ∑
n≤106,∑m≤106.

 

 

Output

For each test case, output "Case #x:y"in one line (without quotes), wherex indicates the case number starting from 1 andy denotes the answer of corresponding case.

 

 

Sample Input

3 2

1 0 2

0 1

3 4

2 0 1

0 2 3 1

 

 

Sample Output

Case #1: 4

Case #2: 4



【题意】给出两个数组a,b,a数组是[0~n-1] 的排列,b数组是[0~m-1]的排列,问有多少种赋值,使得F(i)=b[F(a[i])]。

【思路】举个例子,对于样例二,,我们可以得到以下等式:

(1) F(0)=b[F(2)]

(2) F(1)=b[F(0)]

(3) F(2)=b[f(1)]

可以发现,对于这个样例,F(0),F(1),F(2)形成了一个环,我们可以先设定F(2)的值,进而可以根据(1),(2)得到F(0),F(1)的值,然后利用(3)又回到了代回F(2)。要使这个环成立,必须在值域(即b)中也要有一个长度相等或为其因子的环.


那么这道题就转化为用强连通找环了,我们分别找出数组a和数组b中环的个数及每个环的长度,并求出数组b对应的长度为某个值的所有环包含元素的个数,然后枚举数组a中每个环的长度X,如果数组b中有一个环长度为d,且X%d==0,那么这个环的方案数加上该长度的所有环包含的元素num(因为第一个元素的取值有num种可能),最后数组a的所有环的方案数累乘即可。

#include <cstdio>
#include <cmath>
#include <vector>
#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 100005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f;
const double eps = 1e-9;

int n,m;
int cnt,tot,id;
int num_huan;
int a[maxn];
int b[maxn];
int low[maxn],dfn[maxn];
int vis[maxn];
int stack[maxn];
int color[maxn];
int num1[maxn],num2[maxn];  //每个环的元素个数
ll ans[maxn];
vector<int>vec[maxn];
map<int,ll>mp;             //某一长度的环所有元素的个数

void init()
{
    cnt=1,id=0,tot=-1;
    mst(stack,0),mst(vis,0);
    mst(low,0),mst(dfn,0);
    mst(color,0);
}

void tarjan(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stack[++tot]=u;
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        if(vis[v]==0) tarjan(v);
        if(vis[v]==1) low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        id++;
        do
        {
            color[stack[tot]]=id;
            vis[stack[tot]]=-1;
        }
        while(stack[tot--]!=u);
    }
}

void solve1()
{
    init();
    mst(num1,0);
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
            tarjan(i);
    }
    for(int i=1;i<=n;i++)
    {
        num1[color[i]]++;
    }
    num_huan=id;
}

void solve2()
{
    init();
    mst(num2,0);
    for(int i=1;i<=m;i++)
    {
        if(vis[i]==0)
            tarjan(i);
    }
    for(int i=1;i<=m;i++)
    {
        num2[color[i]]++;
    }
    for(int i=1;i<=id;i++)
    {
        mp[num2[i]]+=num2[i];
    }
}

int main()
{
    int cas=1;
    while(~scanf("%d%d",&n,&m))
    {
        mp.clear();
        mst(ans,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]++;
            vec[i].clear();
        }
        for(int i=1;i<=n;i++)
        {
            vec[a[i]].push_back(i);
        }
        solve1();
        //printf("***%d\n",num_huan);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&b[i]);
            b[i]++;
            vec[i].clear();
        }
        for(int i=1;i<=m;i++)
        {
            vec[b[i]].push_back(i);
        }
        solve2();
        for(int i=1;i<=num_huan;i++)
        {
            for(int j=1;j*j<=num1[i];j++)
            {
                if(num1[i]%j==0)
                {
                    ans[i]+=mp[j];
                    if(j*j!=num1[i])
                        ans[i]+=mp[num1[i]/j];

                }
            }
        }
        ll output=1;
        for(int i=1;i<=num_huan;i++)
        {
            output=(output*ans[i])%mod;
        }
        printf("Case #%d: %I64d\n",cas++,output);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值