• 【图论】Codeforces 711D Directed Roads


    题目链接:

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

    题目大意:

      给一张N个点N条有向边的图,边可以逆向。问任意逆向若干条边使得这张图无环的方案数(mod 1e9+7)。

    题目思路:

      【图论】

      因为是N条边所以不会有复杂的环,最多只会有若干组一个环加一条链。

      推算得到,一个大小为k的环对答案的贡献是*(2k-2),而长度为k的链对答案的贡献是2k(链不包括环上的)

      用dfs找出每一组环的大小和链的长度,计算答案即可。

      1 //
      2 //by coolxxx
      3 //#include<bits/stdc++.h>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<string>
      7 #include<iomanip>
      8 #include<map>
      9 #include<stack>
     10 #include<queue>
     11 #include<set>
     12 #include<bitset>
     13 #include<memory.h>
     14 #include<time.h>
     15 #include<stdio.h>
     16 #include<stdlib.h>
     17 #include<string.h>
     18 //#include<stdbool.h>
     19 #include<math.h>
     20 #define min(a,b) ((a)<(b)?(a):(b))
     21 #define max(a,b) ((a)>(b)?(a):(b))
     22 #define abs(a) ((a)>0?(a):(-(a)))
     23 #define lowbit(a) (a&(-a))
     24 #define sqr(a) ((a)*(a))
     25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
     26 #define mem(a,b) memset(a,b,sizeof(a))
     27 #define eps (1e-8)
     28 #define J 10
     29 #define mod 1000000007
     30 #define MAX 0x7f7f7f7f
     31 #define PI 3.14159265358979323
     32 #define N 200014
     33 using namespace std;
     34 typedef long long LL;
     35 int cas,cass;
     36 int n,m,lll,ans;
     37 LL aans;
     38 LL e[N];
     39 int to[N];
     40 int t[N];
     41 bool mark[N];
     42 LL mi(int x,int y)
     43 {
     44     LL sum=1;
     45     while(y)
     46     {
     47         if(y&1)sum=(sum*x)%mod;
     48         x=(x*x)%mod;y>>=1;
     49     }
     50     return sum;
     51 }
     52 void dfs(int u)
     53 {
     54     while(!mark[u])
     55     {
     56         mark[u]=1;t[u]=++cas;
     57         u=to[u];
     58     }
     59     if(t[u]<=cass)
     60         aans=(aans*e[cas-cass])%mod;
     61     else
     62     {
     63         aans=(aans*((e[cas-t[u]+1]-2+mod)%mod))%mod;
     64         aans=(aans*e[t[u]-1-cass])%mod;
     65     }
     66     return;
     67 }
     68 void init()
     69 {
     70     int i;
     71     e[0]=1;
     72     for(i=1;i<N;i++)e[i]=(e[i-1]*2)%mod;
     73 }
     74 int main()
     75 {
     76     #ifndef ONLINE_JUDGE
     77 //    freopen("1.txt","r",stdin);
     78 //    freopen("2.txt","w",stdout);
     79     #endif
     80     int i,j,k;
     81     init();
     82 //    for(scanf("%d",&cass);cass;cass--)
     83 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
     84 //    while(~scanf("%s",s+1))
     85     while(~scanf("%d",&n))
     86     {
     87         mem(mark,0);
     88         for(i=1;i<=n;i++)scanf("%d",&to[i]);
     89         aans=1;cas=0;
     90         for(i=1;i<=n;i++)
     91         {
     92             if(mark[i])continue;
     93             cass=cas;
     94             dfs(i);
     95         }
     96         printf("%I64d
    ",aans);
     97     }
     98     return 0;
     99 }
    100 /*
    101 //
    102 
    103 //
    104 */
    View Code
  • 相关阅读:
    JSON开源库API【转载】https://nlohmann.github.io/json/index.html
    ZeroMQ示例(C/C++/PHP)详解三种模式
    Makefile精髓篇【转】
    JNI数组操作
    【摘要】malloc、calloc和realloc的用法
    容器内部安装scp,拷贝到外部物理机
    关于VMware虚拟机安装镜像时黑屏的解决办法
    linux下打压缩解压
    高效载入“大”图片
    后台执行Bitmap加载
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5822109.html
Copyright © 2020-2023  润新知