- Quiz
- AC
- 解説
- 強連結成分分解する
- 始点の強連結グループからトポロジカル順にDPして最大距離を求める
- 最大距離のうちの最大値=強連結成分をつなぐ辺数ならYes
- ACL
#include <atcoder/scc>
using namespace atcoder;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll N,M;
cin>>N>>M;
VI A(N);
rep(i, N) cin >> A[i];
auto g = scc_graph(M);
FOR(i,1,M){
for(ll a : A){
ll to = (i*a)%M;
if(i==to)continue;
g.add_edge(i,to);
}
}
auto G = g.scc();
ll sz = SZ(G);
VI Group(M);
rep(i,sz){
for(ll a : G[i]) Group[a] = i;
}
VI dist(sz, 0);
ll ma = 0;
rep(i,sz){
for(ll from : G[i]){
for(ll a : A){
ll to = (from*a)%M;
if(Group[from]==Group[to])continue;
chmax(dist[Group[to]], dist[Group[from]]+1);
chmax(ma, dist[Group[to]]);
}
}
}
if(ma==sz-1) yes();
no();
return 0;
}