hash (codeforces 1394B Boboniu Walks on Graph )

Posted Kalzn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hash (codeforces 1394B Boboniu Walks on Graph )相关的知识,希望对你有一定的参考价值。

题目链接
题意:给你一个k长度序列c,和一个有向图,对于任意一个出度为s的点,下一步都会沿着权值第c[s]小的边走到下一节点。问:有多少符合要求的序列c,使得按照这个序列走,对于所有的点,都可以走回自己。
题解:
真的不会。一看题解,好家伙,hash(bitset也可)。因为如果想要每个点都会走回自己。那生成图就要满足是几个环的集合。满足此情况的充分必要条件是每个点的出度和入度都是1。题目保证了权值两两不同,所有每个点的出度一定为1。所以我们建立一个入度序列d。如果一个终点为v的边呗被选择了,则有:d[v]++。于是我们枚举所有方案(9!种方案),然后康康d序列是不是全为1。显然 O ( n k ! ) O(nk!) O(nk!)铁t。所以对d序列进行hash。对于某种方案即可 O ( 1 ) O(1) O(1)判。(好耶,cf竟然没卡单hash)
下面是ac代码:

// % everyone
#include <cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <cctype>
#include <time.h>
 
namespace IO 
    double start_time = 0.0;
    void ct()  start_time = clock(); return; 
    void fast_cin()  std::ios::sync_with_stdio(false); std::cin.tie(); 
    void read_f(int flag = 0)  freopen("0.in", "r", stdin); if(!flag) freopen("0.out", "w", stdout); 
    void run_time()  std::cout << "\\nESC in : " << ( clock() - start_time ) * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; 

using namespace IO;
template <typename T>
bool bacmp(const T & a, const T & b)  return a > b; 
template <typename T>
bool pecmp(const T & a, const T & b)  return a < b; 
 
#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr make_pair
#define pb push_back
using namespace std;
 
const int N = 2e5+5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const ull bs = 19491001;
ull _pow(ull a, ull b)

    ull res = 1;
    while(b)
    
        if (b & 1) res = res * a;
        a  = a * a;
        b >>= 1;
    
    return res;

ull st;
vector<pair<int, int> > g[N];
ull sum[16][16];
ull ans;
int n, m, k;
ll cnt;
void dfs(int x)

    if (x > k)
    
        cnt += (ans == st);
     //   cout << ans << endl;
        return;
    
    for (int i = 1; i <= x; i++)
    
        ans += sum[x][i];
        dfs(x+1);
        ans -= sum[x][i];
    

int main()

    
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        st = st * bs + 1;
   // cout << st <<"sd" << endl;
    while(m--)
    
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
        g[x].pb( pr(w, y) );
    
    for (int i = 1; i <= n; i++)
    
        sort(g[i].begin(), g[i].end());
        for (int j = 0; j < g[i].size(); j++)
        
     //       cout << g[i].size() << " " << j+1 << " :: " << g[i][j].second <<endl;
            sum[g[i].size()][j+1] += _pow(bs, g[i][j].second-1);
        
    
    dfs(1);
    printf("%lld\\n", cnt);
    return 0;

以上是关于hash (codeforces 1394B Boboniu Walks on Graph )的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces985F:Isomorphic Strings (字符串&hash)

CodeForces - 484BMaximum Value(hash优化)

D - DZY Loves Hash CodeForces - 447A

[Codeforces958A2]Death Stars (medium)(字符串+hash)

codeforces gym 101164 K Cutting 字符串hash

Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing