[Educational Codeforces Round 17 F (762F)] Tree nesting
来源:互联网 发布:linux dd 备份u盘 编辑:程序博客网 时间:2024/05/16 17:51
题意
我把educational round 理解为 eazy round真是too young too simple,明明是 be educated round
给定两棵树
询问
题解
树的同构问题一般都是牵扯到最小表示法的。
官方题解给出了一个trick,同构的树总有一个点或一条边位置不变,也就是树的中心,或者两个中心之间的边。
按照题解的说法,求以中心为树根的最小表示,然后在
不太会写。
于是去膜了一发毛爷爷。
首先,求出
再然后就是dfs一遍
另外,原来c++11用着这么爽,编译命令加个-std=c++11
就行了(似乎需要gcc4.8.x以上?我是gcc4.9.2)。
代码
/// by ztx/// blog.csdn.net/hzoi_ztx/// learnt from myy (matthew99:http://codeforces.com/contest/762/submission/24128833)#define Rep(i,l,r) for(i=(l);i<=(r);i++)#define rep(i,l,r) for(i=(l);i< (r);i++)#define r(x) read(x)typedef long long ll ;int CH , NEG ;template <typename TP>inline void read(TP& ret) { ret = NEG = 0 ; while (CH=getchar() , CH<'!') ; if (CH == '-') NEG = true , CH = getchar() ; while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ; if (NEG) ret = -ret ;}using namespace std; #define kN 1000LL#define kM 12LL#define pb push_back#define kMod 1000000007LLint n, m, ANS;int now[(1<<kM)+5], nxt[(1<<kM)+5];vector<int> G[kN+5], g[kM+5];map<int,vector<int> > STA;map<int,int> ans[kN+5];inline int blen(int x) { return 32 - __builtin_clz(x); } // __builtin_clz() count leading zerosinline int combine(int x, int y) { return x << blen(y) | y; }inline void P(int x) { for (int i = 30; ~i; i -- ) { printf("%d",int(bool(x&(1<<i)))); } puts("");}int dfs0(int u,int fa = 0) { int h = 1; vector<int> H; for (auto v : g[u]) if (v != fa) H.pb(dfs0(v,u)); sort(H.begin(), H.end()); // minimum representation of tree for (auto s : H) h = combine(h,s); h <<= 1;//printf("h[%d] = ",u);//P(h); if (!STA.count(h)) STA[h] = H; return h;}void dfs(int u,int fa = 0) { vector<int> chd; for (auto v : G[u]) if (v != fa) chd.pb(v), dfs(v,u); for (const auto &sta : STA) { auto &H = sta.second; int h = sta.first, N = H.size(); memset(now, 0, sizeof(int)*(1<<N)); now[0] = 1; for (auto ch : chd) { memcpy(nxt, now, sizeof(int)*(1<<N)); for (int i = 0; i < N; i ++ ) { int num = ans[ch][H[i]]; if (!num) continue; for (int j = (1<<N)-1; j >= 0; j -- ) if (now[j] && !(j>>i & 1) && !(i && H[i]==H[i-1] && !(j>>(i-1) & 1))) (nxt[j|(1<<i)] += (ll)now[j]*num%kMod) %= kMod; } memcpy(now, nxt, sizeof(int)*(1<<N)); } if (now[(1 << N) - 1]) { ans[u][h] = now[(1 << N) - 1]; if (blen(h) == (m<<1)) (ANS += now[(1<<N)-1]) %= kMod; } }}int main() { int i, u, v; r(n); rep (i,1,n) r(u), r(v), G[u].pb(v), G[v].pb(u); r(m); rep (i,1,m) r(u), r(v), g[u].pb(v), g[v].pb(u); Rep (i,1,m) dfs0(i); dfs(1); (ANS += kMod) %= kMod; printf("%d\n", ANS); END: getchar(), getchar(); return 0;}
0 0
- [Educational Codeforces Round 17 F (762F)] Tree nesting
- CodeForces 762F. Tree nesting
- CodeForces 762F. Tree nesting
- Educational Codeforces Round 5 [补F]
- Educational Codeforces Round 23 A-F
- Educational Codeforces Round 32 F. Connecting Vertices
- Educational Codeforces Round 31 F. Anti-Palindromize
- Educational Codeforces Round 1 F Cut Length(计算几何)
- Educational Codeforces Round 9 A B C D F
- Educational Codeforces Round 20 F. Coprime Subsequences(容斥)
- cf Educational Codeforces Round 20 F. Coprime Subsequences
- 解题报告:Educational Codeforces Round 24 D,E,F
- Educational Codeforces Round 19 F(dp+队列优化)
- Educational Codeforces Round 25 F. String Compression(kmp+dp)
- cf Educational Codeforces Round 25 F. String Compression
- Educational Codeforces Round 33 F. Subtree Minimum Query
- Educational Codeforces Round 33 (Rated for Div. 2) F
- Codeforces 888F. Connecting Vertices (Educational Codeforces Round 32 F. Connecting Vertices)
- tidyr之gather、separate、spread…
- 博为峰Java技术文章 ——JavaSE Swing JTabbedPane选项卡面板I
- 图论
- dplyr包介绍
- stringr包介绍
- [Educational Codeforces Round 17 F (762F)] Tree nesting
- poj 2965 枚举+DFS
- windows使用sublime text3编译lua
- java设计模式之命令模式
- HDU 1513 Palindrome (动态规划 & LCS)
- Android Studio导入项目问题
- codevs 高低位交换 5641
- List comprehension
- 3D游戏引擎系列九