zoukankan      html  css  js  c++  java
  • 1112考试T1

    1112考试T1

    ​ 题目大意:

    ​ 给定一棵树, 每个节点可以规定权值0或1, 求(displaystyle sum_{i = 1}^{n} sum(i)),(sum(i))表示以节点(i)为根, 到每个叶子结点的路径的权值异或和都为0的方案数.(n <= 1e6)

    ​ 乱搞.

    ​ 假设这棵树是一条链, 那么可以发现, 以每个点为根的时候它到链两段的1的个数都为偶数个.我们又发现, 不管中间的节点怎么放, 都可以通过改变链两段的节点来使之合法.

    ​ 这个结论推广到树上.设(cnt)为非叶子节点, 那么这些点不管取何值, 都可以通过改变叶子结点使这棵树合法.所以当根为非叶子节点时, 它的方案数是(2 ^ {cnt}).以叶子节点为根时, 它的方案数是(2 ^ {cnt + 1}),因为这个叶子结点可以有两种取值.

    #include <bits/stdc++.h>
    
    using namespace std;
    
    inline long long read() {
    	long long s = 0, f = 1; char ch;
    	while(!isdigit(ch = getchar())) (ch == '-') && (f = -f);
    	for(s = ch ^ 48;isdigit(ch = getchar()); s = (s << 1) + (s << 3) + (ch ^ 48));
    	return s * f;
    }
    
    const int N = 1e6 + 5, mod = 1e9 + 7;
    int n, cnt, ans, deg[N];
    
    int ksm(int x, int y) {
    	int res = 1;
    	while(y) {
    		if(y & 1) res = 1ll * res * x % mod;
    		x = 1ll * x * x % mod; y >>= 1;
    	}
    	return res;
    }
    
    int main() {
    	
    	n = read();
    	for(int i = 1, x, y;i < n; i++) deg[read()] ++, deg[read()] ++;
    	for(int i = 1;i <= n; i++) if(deg[i] > 1) cnt ++;
    	int t = ksm(2, cnt);
    	ans = 1ll * t * cnt % mod;
    	t = 2ll * t % mod;
    	printf("%lld", (ans + 1ll * t * (n - cnt) % mod) % mod);
    
    	return 0;
    }
    
  • 相关阅读:
    每周进度条(第九周)
    团队项目最后更改版
    项目需求分析与建议 NABCD模型
    课堂练习找水王
    问题账户需求分析
    2016年秋季个人阅读计划
    学习进度条
    用户体验
    程序员修炼之道——从小工到专家阅读笔记03
    程序员修炼之道——从小工到专家阅读笔记02
  • 原文地址:https://www.cnblogs.com/czhui666/p/13967338.html
Copyright © 2011-2022 走看看