zoukankan      html  css  js  c++  java
  • csu1617]强连通分量

    题意:定义域属于一个集合S={0,1,...,n-1},求S的子集个数,满足以子集的元素为定义域的函数P(x)的值域等于子集本身。

    思路:以元素为点,x到P(x)连一条有向边,不难发现,如果有一个有向环,那么环上的元素构成的集合就满足要求。所以问题转化为求有向环的个数,由于有向环之间不可能有交点(同一个点有且仅有一条出边),所以答案就是2^有向环的个数(如果选了有向环上的一点,那么整个有向环必须全部选)。所以只要用tarjan算法统计点数大于等于2的强连通分量个数然后加上自环的,就得到了有向环的个数了。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2  
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <stack>
     16 #include <set>
     17 #include <bitset>
     18 #include <functional>
     19 #include <numeric>
     20 #include <stdexcept>
     21 #include <utility>
     22  
     23 using namespace std;
     24  
     25 #define mem0(a) memset(a, 0, sizeof(a))
     26 #define mem_1(a) memset(a, -1, sizeof(a))
     27 #define lson l, m, rt << 1
     28 #define rson m + 1, r, rt << 1 | 1
     29 #define define_m int m = (l + r) >> 1
     30 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     31 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     32 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     33 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     34 #define all(a) (a).begin(), (a).end()
     35 #define lowbit(x) ((x) & (-(x)))
     36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     39 #define pchr(a) putchar(a)
     40 #define pstr(a) printf("%s", a)
     41 #define sstr(a) scanf("%s", a)
     42 #define sint(a) scanf("%d", &a)
     43 #define sint2(a, b) scanf("%d%d", &a, &b)
     44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     45 #define pint(a) printf("%d
    ", a)
     46 #define test_print1(a) cout << "var1 = " << a << endl
     47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     49 #define mp(a, b) make_pair(a, b)
     50 #define pb(a) push_back(a)
     51  
     52 typedef long long LL;
     53 typedef pair<int, int> pii;
     54 typedef vector<int> vi;
     55  
     56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     58 const int maxn = 1e4 + 7;
     59 const int md = 1e9 + 7;
     60 const int inf = 1e9 + 7;
     61 const LL inf_L = 1e18 + 7;
     62 const double pi = acos(-1.0);
     63 const double eps = 1e-6;
     64  
     65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     68 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     70 int make_id(int x, int y, int n) { return x * n + y; }
     71  
     72 struct Graph {
     73     vector<vector<int> > G;
     74     void clear() { G.clear(); }
     75     void resize(int n) { G.resize(n + 2); }
     76     void add(int u, int v) { G[u].push_back(v); }
     77     vector<int> & operator [] (int u) { return G[u]; }
     78 };
     79 Graph G;
     80 int n, m;
     81 int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
     82 stack<int> S;
     83 int cnt[maxn], a[maxn];
     84  
     85 void dfs(int u) {
     86     pre[u] = lowlink[u] = ++ dfs_clock;
     87     S.push(u);
     88     rep_up0(i, G[u].size()) {
     89         int v = G[u][i];
     90         if (!pre[v]) {
     91             dfs(v);
     92             min_update(lowlink[u], lowlink[v]);
     93         }
     94         else if (!sccno[v]) {
     95             min_update(lowlink[u], pre[v]);
     96         }
     97     }
     98     if (lowlink[u] == pre[u]) {
     99         scc_cnt ++;
    100         for(;; ) {
    101             int x = S.top(); S.pop();
    102             sccno[x] = scc_cnt;
    103             if (x == u) break;
    104         }
    105     }
    106 }
    107 int find_scc(int n) {
    108     dfs_clock = scc_cnt = 0;
    109     mem0(sccno);
    110     mem0(pre);
    111     rep_up0(i, n) {
    112         if (!pre[i]) dfs(i);
    113     }
    114     mem0(cnt);
    115     int c = 0;
    116     rep_up0(i, n) {
    117         cnt[sccno[i]] ++;
    118     }
    119     rep_up1(i, scc_cnt) {
    120         if (cnt[i] >= 2) c ++;
    121     }
    122     rep_up0(i, n) if (G[i].size() == 0) c ++;
    123     int ans = 1;
    124     rep_up0(i, c) {
    125         ans = (ans << 1) % md;
    126     }
    127     return ans;
    128 }
    129  
    130 int P(int x) {
    131     int ans = 0;
    132     rep_up0(i, m + 1) {
    133         ans = (ans * x + a[m - i]) % n;
    134     }
    135     return ans;
    136 }
    137  
    138 int main() {
    139     //freopen("in.txt", "r", stdin);
    140     int T;
    141     cin >> T;
    142     while (T --) {
    143         cin >> n >> m;
    144         G.clear();
    145         G.resize(n);
    146         rep_up0(i, m + 1) sint(a[i]);
    147         rep_up0(i, n) {
    148             int x = P(i);
    149             if (x != i) G.add(i, x);
    150         }
    151         cout << find_scc(n) << endl;
    152     }
    153     return 0;
    154 }
    View Code
  • 相关阅读:
    Ubuntu 安装 NTP 服务
    Packer 如何将 JSON 的配置升级为 HCL2
    WinRM 如何设置 TrustedHosts
    Windows 10 如何设置网络属性为私有
    Windows 使用 PowerShell 来管理另外一台 Windows 机器
    Windows PowerShell ISE 是什么和 PowerShell 有什么区别
    Spring事务传播属性和隔离级别
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解作用
    杂文 | 如何在演讲中讲个好故事
    2.2 思考框架:什么样的代码才是高效的代码
  • 原文地址:https://www.cnblogs.com/jklongint/p/4496147.html
Copyright © 2011-2022 走看看