神仙题。
Description
给定一个无向图,每条边带有一个长度为 的排列( 的排列)。定义路径的权值为:从初始排列 出发,可以任意次使用路径上任意边的排列进行复合(排列复合运算),能得到的不同排列的个数。
多组询问,每次问从 到 的所有路径(可重复经过点和边)中,权值的最小值。若不可达输出
No。
这题意真是太难概括了。
Solution
我们发现排列长度为 ,一共只有 种不同的排列,我们发现这个数字较小,于是得出猜想:最终的排列集合种数也比较小。
我们可以在本地暴力枚举初始排列,挂机等待最终排列。然后我们发现:最终的排列集合最多只有 种!
于是我们就可以给这 个集合按照元素个数排序,小的在前,大的在后。然后从最小的开始枚举,每次枚举时先建图,最后查询 和 是否连通,如果连通说明这个询问的答案就是当前枚举到的集合的大小。
其中建图可以用并查集,集合可以使用 bitset。总时间复杂度为 ,再乘上并查集的时间复杂度。
Code
#include<bits/stdc++.h>
#define For(a,b,c) for(int a=b,a##END=c; a<=a##END; ++a)
using namespace std; const int N=200004;
bitset<120>gr[156]; int ptoi[55555];
struct _EDGE_{int u,v,w;}e[N]; struct _ASK_{int u,v;}qu[N]; int fa[N],ans[N];
void init();
inline int find(int x){while(x^fa[x])x=fa[x]=fa[fa[x]]; return x;}
inline void combine(int u,int v){u=find(u),v=find(v); if(u^v) fa[u]=v;}
int main(){
init(); int n,m,q; cin>>n>>m>>q;
For(i,1,m){int u,v,A,B,C,D,E; cin>>u>>v>>A>>B>>C>>D>>E; e[i]={u,v,ptoi[A*10000+B*1000+C*100+D*10+E]};}
For(i,1,q) cin>>qu[i].u>>qu[i].v;
For(idx,0,155){
auto cur=gr[idx]; For(i,1,n) fa[i]=i;
For(i,1,m) if(cur[e[i].w-1]) combine(e[i].u,e[i].v);
For(i,1,q) if(!ans[i]&&find(qu[i].u)==find(qu[i].v)) ans[i]=cur.count();
}For(i,1,q) if(ans[i]) printf("%d\n",ans[i]); else puts("No");
}
void init(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
vector<int>p={1,2,3,4,5}; int cnt=0;
do ptoi[p[0]*10000+p[1]*1000+p[2]*100+p[3]*10+p[4]]=++cnt; while(next_permutation(p.begin(),p.end()));
stringstream s(R"(000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000001
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000001
000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011001
000000000000000100000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000001
000000000000000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000100000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000000001
000000000000000010000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000001000000000000000000000100000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001000000000000001
000000000000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000001
000000000000000000000000000000000000000000000100000000000000000000000000000000000100000000000000000000000000000000000001
000000001000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000001
000000000000100000000000000000000000010000000000000000000000000010000000000000000000000000000000000000000000000000000001
100000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000001
000000000000000000000000000000000100000000000000000000000001000000000000000000000000000000000100000000000000000000000001
000000000000000000000000010000000000000100000000000000000000000000000000000000000000000000000000001000000000000000000001
010000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000001
000000000000000000000000000000000000000000000000000010000000000001000000000000000000000000000000001000000000000000000001
100000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000001
000000000000000000000000000000000000000000000000000000000001000001000000000000000000000000000000000000000100000000000001
000000001000000000000000000000000000000000000000000010000000000000000000000000000000000000100000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000010000000010000001
000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101
000000000000000000000000000000000100000100000000000000000000000000000000000000000000000000000000000000000000000001000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000011
000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000000000000000100001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001000000000000000000100001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000011
000000000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000011
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000100001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000000000000000101
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000000000000000000101
000000000000010000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000001
000000000000000000000010000000000000000000000000000010000000000000000000000000000000100000000000000000000000000000000001
000000000000000000000000000000000100000000000000000000000000000000000100000000000001000000000000000000000000000000000001
100000000000000000000000000000000000000000001000000000000000000000000000000100000000000000000000000000000000000000000001
000000001000000000000000000000000000000000000000000000000000000000100000000010000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000001000000000001000000000000000000000000000100000000000000000000000000000001
000000000000000001000000000000000000010000000000000000000000001000000000000000000000000000000000000000000000000000000001
000000000000000000000100010000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000001
000000000000100000000000000000000000000010000000000000000000010000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000001000000000000000000000100000000000000000000000000000000000100000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000100000000010000001
000000000010000000000000000000000000000000000000001000000000000000000000000000000000000000100000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000010010000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000001000000001
000100000000000000000000000000100000000000000000000000000000000000001000000001000000000000000000000000000000000000000001
000000000000000000010000000000000010000000000000000100000000000000000000000000100000000000000000000000000000000000000001
000000010000000000000000001000000000000000000000000000000000000000010000000000000010000000000000000000000000000000000001
000000000001000000000000000000000000000000100000000000001000000000000000010000000000000000000000000000000000000000000001
000010000000000000000000000000000000000000000010100000000000000000000000000000000000010000000000000000000000000000000001
000000000000000000000001000001000000000000000000000000010000000000000000000000000000001000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000100001100001
000000000100000100000000000000001000001000000000000000000000000000000000000000000000000000000000000000000000000001000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011001000000000000000000011001
000000000000000000000000010000000000000001000000000010000000000100000000000000000000000000000000100000000000000000000001
000000000000000000000000000000000000000000010100000000000000000000000000000000010100000000000000000000000000000000100001
000000000000001010000000000000000000000000000000000000000000100001000000000000000000000000000000000000000000000000100001
000000000000100000001000000000000000000000000000000000000000000000000000001000000000000000000100100000000000000000000001
000000000000000000000000000000000000000000000000000000000000000001000001000000000000000001000001000000000000000001000001
000000000000000000000000000000000000000000000000000000000000000000000011000000000000000011000000000000000000000000000011
000000000000001000001000000000000000000000000000000000000000000000000000001000000000000000000001001000000000000000000001
100000000000000010000000000000000000000000000000000000000001100000000000000000000000000000000000000000010000000000000001
000000000000000000000000000000000000010000000100000000000000000000000000000000000100000000100000000000010000000000000001
000000000000000000000000000000000000000000000000000010000001000010000000000000000000000000000000000100001000000000000001
000000000000000000000000000000000000000000000000000000000000000010000001000000000000000001000010000000000000000010000001
000000001000000100000000000000000100001000000000000000000000000000000000000000000000000000000000000000000000000010000001
100000001000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000100000001
000000000000000000000000010000000100010000000000000000000000000000000000000000000000000000000000000010000000100000000001
000000000000000000000000000000000000000100000100000000000000000000000000000000000100000000000001000000000100000000000001
000000000000000000000000000100010000000100000000000000000000000000000000000000000000000000000000000010000000100000000001
000000000000000000101000000000000000000000000000000000000000000000000000101000000000000000000000000000000000000000000101
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100001100000000000011
000000000000001100000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000011
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100110000000000000000000011001
001000100000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000100000001
000000000000000000000000000000000000000000000000000001000010000001000000000000000000000000000000000100001000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000101000101
000001000000000010000000000000000000000000000000000000100000100000000000000000000000000000000000000000000100000000000001
000000000000000000000000000000000000000101000000000000000000000101000000000000000000000000000000000000000000000000000101
000000000000000000000000000010000000000001000000010000000000000100000000000000000000000000000000001000000000000000000001
000000000000101000000000000000000000010010000000000000000000010010000000000000000000000000000000000000000000000000000101
100000000000000000000100010000000000000100000000000000000000000000000000000000001000000000000010001000000000000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000110000000011000011
000000001000000000000010000000000000000000000000000010000000000001000000000000000000100000100000001000000000000000000001
000000000000000000000000000000000100000000000001000000000001000001000000000000000000000100000100000000000100000000000001
100000000000001000000000010000000000000000001000000000000000000000000000000100000000000000000010000000000100000000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100001010100001010100001
000000000000110000000000000000000000110000000000000000000000000011000000000000000000000000000000000000000000000000000011
110000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000011
000000000000000000000000000000000101000000000000000000000101000000000000000000000000000000000101000000000000000000000101
000000001010000000000000000000000000000000000000001010000000000000000000000000000000000000100001000000000000000000100001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000010010010010000101
000000000000000000000000000000000100000100000000000000000001000000000100000000000001000000000100000000000000000001000001
000000000000100001000000000000000000010100000000000000000000001010000000000000000000000000000000000000000000000000100001
000000001000001000000000000000000000000000000000000010000000000000100000000010000000000000100000000000000000000001000001
100000010000000000000000001000000000010000000000000010000000000000010000000000000010000000000100000000000000000010000001
000010001000000000000000010000000000000000000010100000000000000010000000000000000000010000000100000000010000000000000001
000000000001100000000000000000000100000000100000000010001000000000000000010000000000000000000010000000010000000000000001
000000001000000000010000000000000010010000000000000100000001000000000000000000100000000000000010100000000000000000000001
100000000000000000000001000001000100000000000000000000010000000010000000000000000000001000100000100000000000000000000001
000100000000100000000000010000100000000000000000000000000001000000001000000001000000000000100000000000000000000010000001
000000001000000010001000000000000000000000000000000010000000100000000001001000000000000001100000000010000000100000000001
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100110011001100110011001
000000000000000000000000000000000000000000000000000011000011000011000000000000000000000000000000001100001100000000000011
000000000000000000000000000000000100000001000100000000000001000100000001000000000100000001000100000000000001000100000001
000000000000000000000000000000000000000000000000000000000000000011000011000000000000000011000011000000000000000011000011
000000000000000000000000000000000000010100010100000000000000000000000000000000010100000000100001000000010100000000100001
000000000000100110000000000000000000011001000000000000000000100110000000000000000000000000000000000000000000000000011001
100001000000001010000000000000000000000000000000000000100001100001000000000000000000000000000000000000010100000000100001
100000000000000100001000010000000000001000000100000000000000000000000000001000000100000000000010000100001000000000000001
000000000000101000101000000000000000000000000000000000000000000000000000101000000000000000000101101000000000000000000101
000000001100001100000000000000001100001100000000000000000000000000000000000000000000000000000000000000000000000011000011
000000000000000000000000010010000000000101000000010010000000000101000000000000000000000000000000101000000000000000000101
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111000000000000000000111111
000000000000000000000000010100010100010100000000000000000000000000000000000000000000000000000000001010000000100001100001
101000101000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000101000101
000010001000010000000100010000000001100000000010100000000100000010100000000010001000010000000100000001010000001000000001
100000000010010000000001000001000100100000001000001000010000000010000100000100000001001000100000100000000010010000000001
010000001000000001010000100000000010010000000001000100000001001000100000000010100000000100000010100000000010010000000001
010000000001100000000010100000000100000010100000000010001000010000000100010000000001100000000010000001010000001000000001
100000010000000001000010001000000001010000001000000010000100001000010000000100000010100000000100010000100000000010000001
000100000010100000000100010000100000000010000001001000000001010000001000000001001000000100100000010000100000000010000001
110000000000001100001100110000000000001100001100000000000000000000000000001100001100000000000011001100001100000000000011
000000000000111111000000000000000000111111000000000000000000111111000000000000000000000000000000000000000000000000111111
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111
000000000000000000000000000000000101000101000101000000000101000101000101000000000101000101000101000000000101000101000101
000000001010001010001010000000000000000000000000001010000000100001100001001010000000100001100001001010000000100001100001
100110011001100110011001011001100110011001100110100110011001100110011001011001100110011001100110100110011001100110011001
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)");
For(i,0,155) s>>gr[i];
}