CodeForces Round 718D - Andrew and Chemistry
2016-11-16 11:33
141 查看
给出一个每个节点的度小于4的树,问有多少种增加叶子的方式(保持每个节点的度小于4)。增加之后的树同构的算一种
第二个树哈希
第二个树哈希
#include<bits/stdc++.h> using namespace std; #define LL long long const int maxn = 112345; vector<int> edge[maxn]; int dep[maxn],fa[maxn]; void getdep(int st,int Fa,int deep){ dep[st] = deep,fa[st] = Fa; for(auto x : edge[st]){ if(x == Fa) continue; getdep(x,st,deep+1); } } pair<int,int> getCtr(int n){ int rot; getdep(1,0,0); rot = max_element(dep+1,dep+1+n) - dep; getdep(rot,0,0); rot = max_element(dep+1,dep+1+n) - dep; int far = dep[rot]; for(int i = 0 ; i < far / 2 ; i ++) rot = fa[rot]; return make_pair(rot,(far & 1) ? fa[rot] : rot); } const int mod = 1e9 + 7,seed = 17; LL har[maxn]; LL getHar(int st,int fa){ har[st] = seed; for(auto x : edge[st]){ if(x == fa) continue; getHar(x,st); } sort(edge[st].begin(),edge[st].end(),[&](int x,int y){return har[x] < har[y];}); for(auto x : edge[st]){ if(x == fa) continue; har[st] = (har[st] + har[x]) ^ (har[st] * har[x]); har[st] %= mod; } (har[st] *= har[st]) %= mod; return har[st]; } int getans(int st,int fa){ int ret = edge[st].size() < 4; int bef = -1; for(auto x : edge[st]){ if(x == fa) continue; if(bef == -1 || har[bef] != har[x]) ret += getans(x,st); bef = x; } return ret; } int calans(int n){ pair<int,int> ctr = getCtr(n); getHar(ctr.first,ctr.second); getHar(ctr.second,ctr.first); int ret = getans(ctr.first,ctr.second)+ getans(ctr.second,ctr.first); return ret / (1+ (har[ctr.first] == har[ctr.second])); } int main(){ int n; scanf("%d",&n); int l,r; for(int i = 1; i < n;i++){ scanf("%d %d",&l,&r); edge[l].push_back(r); edge[r].push_back(l); } printf("%d\n",calans(n)); }
相关文章推荐
- Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry
- Codeforces Round #312 (Div. 2) Amr and Chemistry(位运算+暴搜)
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
- Codeforces Round #312 (Div. 2) C Amr and Chemistry
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job DFS序+树链剖分+线段树区间^
- Codeforces Round #253 Div2 D.Andrey and Problem 概率+贪心
- Codeforces Round #384 B. Chloe and the sequence
- Codecraft-18 and Codeforces Round#458 C. Travelling Salesman and Special Numbers(数位DP)
- Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)
- Mike and gcd problem Codeforces Round #410 (Div. 2)
- Codeforces Round #190 DIV.2 A. Ciel and Dancing
- 【Codeforces Round 272 (Div 2)C】【暴力】Dreamoon and Sums 所有除b结果是模b结果[1,a]倍数字之和
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #285 (Div. 2) C. Misha and Forest
- 【Codeforces Round 354 (Div 2)E】【数学 多项式除法 讨论】The Last Fight Between Human and AI 多项式除以x-k是否值整除
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #419 (Div. 2)-树形dp&依赖背包&-E. Karen and Supermarket
- Codeforces Round #169 (Div. 2):E - Little Girl and Problem on Trees
- Educational Codeforces Round 31 D. Boxes And Balls
- (DFS)Educational Codeforces Round 28 E. Chemistry in Berland