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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值