D. Directed Roads(思维+组合数学+找基环)

30 篇文章 0 订阅
14 篇文章 0 订阅

https://codeforces.com/problemset/problem/711/D


思路:组合部分好想。然而我找环的时候,开始是用tarjan,然而死活不对,我去看了眼tarjan无向图找环的板子,发现没啥毛病。然后改成建有向图,用有向图强连通分量就好了。因为像2 1这样的数据会反复跳导致进入dfs回溯的else if里面多次判断。

所以用dfs直接深度找一个环或者tarjan都是可以的。


https://www.luogu.com.cn/blog/Starlight237/solution-cf711d

  1. 最后统计环的时候,若 rt[i] == i && siz[i] > 1,则构成一个环。其中 rt[i] 表示 i 所在强连通分量的代表节点。注意,不可以在 tarjan 函数中那个 if 语句里面更新答案,因为此时可能找到的并不是最终的环,而是一个非简单环的局部,甚至只有一个点。事实上,如果在 tarjan 函数中那个 if 语句里面更新答案,则样例 2 会输出 14
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL ksm(LL a,LL k){
   LL res=1;
   while(k>0){
      if(k&1) res=res*a%mod;
      k>>=1;
      a=a*a%mod;
   }return res%mod;
}
LL a[maxn];
vector<LL>g[maxn];
LL col[maxn],dfn[maxn],low[maxn],tot=0,cnt=0;
LL fa[maxn];
bool instack[maxn];
stack<LL>s;
void tarjan(LL u){
     dfn[u]=low[u]=++tot;
     s.push(u);
     instack[u]=true;
     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i];
         if(!dfn[v]){
           /// fa[v]=u;
            tarjan(v);
            low[u]=min(low[u],low[v]);
         }
         else if(instack[v]){
            low[u]=min(low[u],dfn[v]);
         }
     }
     if(dfn[u]==low[u]){
        cnt++;
        LL y;
        do{
           y=s.top();
           col[cnt]++;
           instack[y]=false;
           s.pop();
        }while(y!=u);
     }
     return;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
       LL to;cin>>to;
       g[i].push_back(to);
     ///  g[to].push_back(i);
   }
   for(LL i=1;i<=n;i++){
       if(!dfn[i]){
          tarjan(i);
       }
   }
   LL edge=0;
  /// debug(cnt);

   for(LL i=1;i<=cnt;i++){
       if(col[i]<2) continue;
       edge+=col[i];
   ///    cout<<"col["<<i<<"]="<<col[i]<<"\n";
       col[i]=(ksm(2,col[i]%mod)%mod-2+mod)%mod;
   }
   LL x=n-edge;
   LL ans=ksm(2,x)%mod;
   for(LL i=1;i<=cnt;i++){
      ans=(ans%mod*col[i]%mod)%mod;
   }
   cout<<ans<<"\n";
   return 0;
}
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL ksm(LL a,LL k){LL res=1;while(k>0){if(k&1) res=res*a%mod;k>>=1;a=a*a%mod;}return res%mod;}
vector<LL>g[maxn];
LL dep[maxn],vis[maxn];
LL col[maxn],cnt=0;
void dfs(LL u,LL d){
    dep[u]=d;vis[u]=1;
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(!vis[v]) dfs(v,d+1);
        else if(vis[v]==1) col[++cnt]=dep[u]-dep[v]+1;
    }
    vis[u]=2;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
       LL to;cin>>to;
       g[i].push_back(to);
       g[to].push_back(i);
   }
   for(LL i=1;i<=n;i++){
       if(!dep[i]) dfs(i,1);
   }
   debug(cnt);
   LL sum=0;LL res=1;
   for(LL i=1;i<=cnt;i++){
      sum+=col[i];
      res=(res%mod*(ksm(2,col[i]%mod)%mod-2+mod))%mod;
   }
   res=(res%mod*ksm(2,n-sum)%mod)%mod;
   cout<<res<<"\n";
   return 0;
}

关于无向图找环的trajan做法我实在调不出来了=.=链接里的那个佬码风太鬼畜了

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL ksm(LL a,LL k){LL res=1;while(k>0){if(k&1) res=res*a%mod;k>>=1;a=a*a%mod;}return res%mod;}
LL a[maxn],siz[maxn];
vector<LL>g[maxn];
LL col[maxn],dfn[maxn],low[maxn],tot=0,cnt=0;
LL fa[maxn];
bool instack[maxn];
stack<LL>s;
struct Edge{
    LL u,v;
}Edges[maxn];
inline bool operator<(const Edge&a, const Edge&b) {return a.u != b.u ? a.u < b.u : a.v < b.v;}
void tarjan(LL u){
     dfn[u]=low[u]=++tot;
     s.push(u);
     instack[u]=true;
     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i];
         if(v==fa[u]) continue;
         if(!dfn[v]){
            fa[v]=u;
            tarjan(v);
            low[u]=min(low[u],low[v]);
         }
         else if(instack[v]){
            low[u]=min(low[u],dfn[v]);
         }
     }
     if(dfn[u]==low[u]){
        cnt++;
        LL y;
        do{
           y=s.top();
           col[y]=cnt;
           instack[y]=false;
           siz[u]+=siz[y];
           s.pop();
        }while(y!=u);
     }
     return;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
        LL to;cin>>to;
        Edges[i]=Edge{min(i,to),max(i,to)};
   }
   sort(Edges+1,Edges+1+n);
   for(LL i=1;i<=n;i++){
       if(Edges[i].u!=Edges[i+1].u||Edges[i].v!=Edges[i+1].v){
            g[Edges[i].u].push_back(Edges[i].v);
  ///          g[Edges[i].v].push_back(Edges[i].u);
       }
   }
   for (int i = 1; i <= n; ++i) siz[i] = 1;
   for(LL i=1;i<=n;i++){
       if(!dfn[i]){
          tarjan(i);
       }
   }
   ///LL edge=0;
   LL m=n;
   LL ans=1;
   for(LL i=1;i<=n;i++){
       if(col[i]==i&&siz[i]>1){
         ans=(ans%mod*ksm(2,siz[i]%mod)%mod-2+mod)%mod;
         m-=siz[i];
       }
   }
   ans=(ans%mod*ksm(2,m)%mod)%mod;
   cout<<ans<<"\n";
   return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值