【CodeForces】CF1327D Infinite Path

题目地址:

https://www.luogu.com.cn/problem/CF1327D

题面翻译:
T T T组数据。每次给一个长度为 n n n的置换 p p p和颜色数组 c c c,求最小的 k k k,使 p k p^k pk中存在一个“同色环”。即,存在 i i i,使 p i k p^k_i pik p p i k k p_{p^k_i}^k ppikk … \dots ,全部同色。

数据范围: 1 ≤ T ≤ 1 0 4 1\le T\le 10^4 1T104 1 ≤ n , ∑ n ≤ 2 × 1 0 5 1\le n,\sum n\le 2\times 10^5 1n,n2×105

题目描述:
You are given a colored permutation p 1 , p 2 , … , p n p_1, p_2, \dots, p_n p1,p2,,pn . The i i i -th element of the permutation has color c i c_i ci .

Let’s define an infinite path as infinite sequence i , p [ i ] , p [ p [ i ] ] , p [ p [ p [ i ] ] ] … i, p[i], p[p[i]], p[p[p[i]]] \dots i,p[i],p[p[i]],p[p[p[i]]] where all elements have same color ( c [ i ] = c [ p [ i ] ] = c [ p [ p [ i ] ] ] = … c[i] = c[p[i]] = c[p[p[i]]] = \dots c[i]=c[p[i]]=c[p[p[i]]]= ).

We can also define a multiplication of permutations a a a and b b b as permutation c = a × b c = a \times b c=a×b where c [ i ] = b [ a [ i ] ] c[i] = b[a[i]] c[i]=b[a[i]] . Moreover, we can define a power k k k of permutation p p p as p k = p × p × ⋯ × p ⏟ k  times p^k=\underbrace{p \times p \times \dots \times p}_{k \text{ times}} pk=k times p×p××p .

Find the minimum k > 0 k > 0 k>0 such that p k p^k pk has at least one infinite path (i.e. there is a position i i i in p k p^k pk such that the sequence starting from i i i is an infinite path).

It can be proved that the answer always exists.

输入格式:
The first line contains single integer T T T ( 1 ≤ T ≤ 1 0 4 1 \le T \le 10^4 1T104 ) — the number of test cases.

Next 3 T 3T 3T lines contain test cases — one per three lines. The first line contains single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105 ) — the size of the permutation.

The second line contains n n n integers p 1 , p 2 , … , p n p_1, p_2, \dots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin , p i ≠ p j p_i \neq p_j pi=pj for i ≠ j i \neq j i=j ) — the permutation p p p .

The third line contains n n n integers c 1 , c 2 , … , c n c_1, c_2, \dots, c_n c1,c2,,cn ( 1 ≤ c i ≤ n 1 \le c_i \le n 1cin ) — the colors of elements of the permutation.

It is guaranteed that the total sum of n n n doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 .

输出格式:
Print T T T integers — one per test case. For each test case print minimum k > 0 k > 0 k>0 such that p k p^k pk has at least one infinite path.

首先每个置换可以分解为若干轮换之乘积,每个轮换是一个封闭的集合,那么同色环一定在某个轮换里。由于循环群的子群也一定是循环群,并且子群的阶是群的阶的因子,从而可以从小到大枚举所有因子,看哪个因子对应的子群是个同色环;对于每个轮换都找到最小的同色环,求出整体的最小解即可。代码如下:

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 2e5 + 10, INF = N;
int n, a[N], col[N];
bool vis[N];
vector<int> v, fac;
int res;

// 看是否存在同色的k阶子群
bool check(int k) {
  // 所有的k阶子群其实都是群在某个k阶子群下的陪集分解,枚举所有的k阶子群
  for (int s = 0; s < k; s++) {
    int color = col[v[s]];
    bool flag = 1;
    // 看该子群是否同色
    for (int p = s + k; p < v.size(); p += k)
      if (col[v[p]] != color) {
        flag = 0; 
        break;
      }
    // 如果同色,说明存在k阶同色子群,返回true
    if (flag) return true;
  }
  return false;
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    // 初始化一下答案
    res = INF;
    memset(vis, 0, sizeof vis);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &col[i]);
    
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      // 求出i所在的轮换
      vis[i] = 1;
      v.clear();
      int p = a[i];
      v.push_back(i);
      for (; p != i; p = a[p]) v.push_back(p), vis[p] = 1;
      // 求出该轮换的阶的所有因子
      fac.clear();
      int s = v.size();
      for (int k = 1; k <= sqrt(s); k++) {
        if (s % k) continue;
        fac.push_back(k);
        if (k < s / k) fac.push_back(s / k);
      }
      // 对因子从小到大排序
      sort(fac.begin(), fac.end());
      // 枚举因子,求出最小的同色子群
      for (int x : fac) 
        if (check(x)) {
          // 找到最小的同色子群,用其阶更新答案
          res = min(res, x);
          break;
        }
    }

    printf("%d\n", res);
  }
}

每次询问时间复杂度 O ( ∑ i l i l i ) O(\sum_i l_i\sqrt {l_i}) O(ilili ) l i l_i li是轮换的长度,所以 ∑ l i = n \sum l_i=n li=n,空间 O ( n ) O(n) O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值