全部评论 2

  • 
    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int MOD = 1e9+7;
    
    using ll = long long;
    
    #define f first
    #define s second
    
    struct mi {
     	int v; 
     	explicit operator int() const { return v; } 
    	mi() { v = 0; }
    	mi(ll _v):v(int(_v%MOD)) { v += (v<0)*MOD; }
    };
    mi& operator+=(mi& a, mi b) { 
    	if ((a.v += b.v) >= MOD) a.v -= MOD; 
    	return a; }
    mi& operator-=(mi& a, mi b) { 
    	if ((a.v -= b.v) < 0) a.v += MOD; 
    	return a; }
    mi operator+(mi a, mi b) { return a += b; }
    mi operator-(mi a, mi b) { return a -= b; }
    mi operator*(mi a, mi b) { return mi((ll)a.v*b.v); }
    mi& operator*=(mi& a, mi b) { return a = a*b; }
    mi pow(mi a, ll p) { assert(p >= 0); // asserts are important! 
    	return p==0?1:pow(a*a,p/2)*(p&1?a:1); }
    
    using vi = vector<int>;
    using vmi = vector<mi>;
    
    int N,M;
    vector<vi> adj;
     
    vector<array<int,2>> gen_dist() { // BFS
    	vector<array<int,2>> dist(N,{MOD,MOD});
    	queue<pair<int,int>> q;
    	auto ad = [&](int a, int b) {
    		if (dist[a][b%2] != MOD) return;
    		dist[a][b%2] = b; q.push({a,b});
    	};
    	ad(0,0);
    	while (!q.empty()) {
    		pair<int,int> p = q.front(); q.pop();
    		for (int t: adj[p.f]) ad(t,p.s+1);
    	}
    	return dist;
    }
    
    mi comb[105][105]; // comb[x][y] = (x choose y)
    vmi p2; // stores powers of 2
    
    void solve() {
    	cin >> N >> M;
    	adj = vector<vi>(N);
    	for (int i = 0; i < M; ++i) {
    		int a,b; cin >> a >> b; --a,--b;
    		adj[a].push_back(b), adj[b].push_back(a);
    	}
    	vector<array<int,2>> dists = gen_dist();
    	for (array<int,2>& t: dists) 
    		if (t[0] > t[1]) swap(t[0],t[1]);
    	if (dists[0][1] == MOD) { // bipartite
    		vi num_at_dist(N);
    		for (int i = 0; i < N; ++i) 
    			++num_at_dist[min(dists[i][0],dists[i][1])];
    		mi ans = 1;
    		for (int i = 1; i < N; ++i)
    			ans *= pow(p2[num_at_dist[i-1]]-1,num_at_dist[i]);
    		cout << (int)ans << "\n";
    		return;
    	}
    	vector<vi> S(4*N,vi(4*N));
    	for (array<int,2> t: dists) ++S[t[0]][t[1]];
    	mi ans = 1;
    	for (int sum = 1; sum < 4*N; sum += 2) {
    		vmi dp(S[0][sum]+1); dp.back() = 1;
    		for (int a = 1; a <= sum/2; ++a) { // solve in
    
    

    2024-10-14 来自 广东

    0
  • 666

    2023-09-05 来自 广东

    0
首页