codeforces Round#369 div2

编程入门 行业动态 更新时间:2024-10-10 03:33:40

<a href=https://www.elefans.com/category/jswz/34/1770097.html style=codeforces Round#369 div2"/>

codeforces Round#369 div2

题意:n个点n条有向边(无自环),求边的集合的方案数,使得翻转这些边的方向后无环。

题解:题目给出的图不会环套环(环套环的话,至少有一个点要有两个出度,但题目是每个点一个出度),所有直接求强连通分量,贡献分两种情况:

1.形成环的强连通:2^n-2(本身和所有边反向不合法,其余都合法);

2.单点的强连通:2(该点的出边对合法性无影响);

累乘即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>using namespace std;typedef long long LL;
const int M=1000000007;
const int N=400000+10;vector<int>V[N];
int n;
int Time,dfn[N],low[N],sta[N],instack[N],g[N];
int belong[N],h,Col;
LL RP(LL a,LL b)
{LL Ans=1;for (;b;b>>=1){if (b&1)Ans=Ans*a%M;a=a*a%M;}return Ans;
}
void tarjan(int u)
{Time++;dfn[u]=low[u]=Time;sta[++h]=u;instack[u]=1;for (int i=0;i<V[u].size();i++){int v=V[u][i];if (!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}else if (instack[v]==1)low[u]=min(low[u],low[v]);}if (dfn[u]==low[u]){Col++;while (sta[h]!=u){instack[sta[h]]=-1;belong[sta[h]]=Col;h--;g[Col]++;}instack[sta[h]]=-1;belong[sta[h]]=Col;h--;g[Col]++;}
}
int main()
{//freopen("1.txt","r",stdin);scanf("%d",&n);for (int i=1;i<=n;i++){int a;scanf("%d",&a);V[i].push_back(a);}memset(g,0,sizeof g);memset(dfn,0,sizeof dfn);memset(low,0,sizeof low);memset(instack,0,sizeof instack);h=Time=Col=0;for (int i=1;i<=n;i++)if (!dfn[i])tarjan(i);LL Ans=1;for (int i=1;i<=Col;i++)if (g[i]>1){Ans=Ans*(RP(2,g[i])-2+M)%M;}else Ans=Ans*2%M;cout<<Ans<<endl;return 0;
}


更多推荐

codeforces Round#369 div2

本文发布于:2024-03-13 00:48:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1732809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:codeforces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!