CF #611 H, New Year And Forgotten Tree
来源:互联网 发布:sql 查询多条结果合并 编辑:程序博客网 时间:2024/06/01 10:45
题意:给出一棵树,但是点的编号只知道是几位数,不知道确切的数,要求还原出一棵合法的树。
我们将位数相同的称作是同种颜色,考虑任意一棵合法的树,假定任意一个根,对于同一个颜色的所有联通块,我们先取出所有联通块中深度最浅的点作为关键点,那么对于这个颜色的联通块的儿子都可以将父亲重新接为这个关键点而不改变约束。进而除了关键点以外的所有联通块大小都为1。因此我们可以想象出任何一棵合法的树都可以改造成一棵所有节点颜色都不同的树并往这棵树上加叶子的形态。
考虑一棵关键点形成的树,现在我们要将额外的叶子给加上去。那么对于边
那么我们只需要枚举所有
枚举树的形态可以枚举prufer序列然后还原回来。
其实这题还有个故事。(雾)
那是将近两年前,一个夜嘿黑风高的夜晚,我和whx和dwj在宾馆里嘿嘿嘿开黑打cf goodbye 2015。
愉快地切完水之后。。。就停了,后面好像都有点难难啊。。?
于是我们分工合作一人一题愉快地肝了起来。。。
期间一位成七小哥找dwj要了某题的代码,愉快地交给了他。。
还剩40min的时候大概还是一题都没肝动,遂弃疗看榜。
突然发现刚才那个小哥PP掉了H啊???似乎还是first blood?
通过一番愉快地py交易,我们get到了PP代码。。。
最后的0.5h就颓在了读代码上。。然而却并不能看懂他的爆搜里的某个蜜汁if是在干什么的。。。
最后的最后。。。。他FST了。。。【手动再见】
讲起来非常简单,事实上也非常好写,30min就写完了。
然而我盯着代码超过了4h,对拍了一晚上,后来突然灵光一现才在CF上AC。
至于发生了什么。。。。这又是另一个悲惨的故事了QAQ。。。。代码里注释部分就是了。。。
怪自己太sb吧。。
#include <bits/stdc++.h>using namespace std;#define rep(i,a,b) for (int i = a, _ = b;i <= _; i ++)#define fore(i,u) for (int i = head[u]; i; i = nxt[i])const int N = 61;const int M = 201;const int inf = 0x7fffffff;int n, m, tot, S, T, id[10][10], g[10][10], e[10][10];int flow[M], cap[M], nxt[M], to[M], head[N], dis[N];int ett = 1;queue<int> Q;inline void ins(int u, int v, int w) { to[++ ett] = v, nxt[ett] = head[u], head[u] = ett, cap[ett] = w; to[++ ett] = u, nxt[ett] = head[v], head[v] = ett;}bool bfs() { rep (i , 1 , tot) dis[i] = 0; dis[S] = 1 , Q.push(S); while (!Q.empty()) { int u = Q.front(); Q.pop(); fore (i , u) if (cap[i] > flow[i]) { int v = to[i]; if (!dis[v]) dis[v] = dis[u] + 1, Q.push(v); } } return dis[T] > 0;}int dfs(int u, int a) { if (u == T || !a) return a; int ret = 0, f; fore (i, u) { int v = to[i]; if (dis[v] == dis[u] + 1 && (f = dfs(v, min(a, cap[i] - flow[i])))) { ret += f, a -= f, flow[i] += f, flow[i ^ 1] -= f; if (!a) break; } } return ret;}pair<int, int> pr[11];int org[11], cur[11], vis[11], cnt[11], pru[11], lg[200007];int tg[11][11];void gao() { rep (i , 1 , ett) flow[i] = 0; rep (i , 1 , m) rep (j , i , m) cap[e[i][j]] = g[i][j]; int ret = 0; while (bfs()) ret += dfs(S, inf); if (ret != n - m) return; rep (i , 1 , m) rep (j , i , m) { int a = 0, x = 0, b = 0, y = 0; fore (k , id[i][j]) if (to[k] > tot - m) { if (!a) a = to[k] - (tot - m), x = flow[k]; else b = to[k] - (tot - m), y = flow[k]; } if (i == j) b = a; while (x --) printf("%d %d\n", ++ cur[a], org[b]), ret --, assert(cur[a] < org[a+1]); if (i != j) { assert((a == i && b == j) || (a == j && b == i)); while (y --) printf("%d %d\n", ++ cur[b], org[a]), ret --, assert(cur[b] < org[b + 1]); } } assert(ret == 0); rep (i , 1 , m - 1) printf("%d %d\n", org[pr[i].first], org[pr[i].second]); exit(0);}int mark[11];void dfs(int p) { if (p == m - 1) { rep (i , 1 , m) vis[i] = 0, mark[i] = 0; rep (i , 1 , m - 2) vis[pru[i]] ++; rep (i , 1 , m - 2) { int k = 0; rep (j , 1 , m) if (!vis[j] && !mark[j]) { k = j; break; } assert(k); pr[i] = make_pair(pru[i], k); vis[pru[i]] --; mark[k] = 1; } int x = 0, y = 0; rep (i , 1 , m) if (!mark[i]) { if (!x) x = i; else assert(!y), y = i; } pr[m - 1] = make_pair(x, y); int flag = 1; rep (i , 1 , m - 1) { int x = pr[i].first, y = pr[i].second; if (x > y) swap(x, y); if (!g[x][y]) flag = 0; g[x][y] --; } if (flag) gao(); rep (i , 1 , m - 1) { int x = pr[i].first, y = pr[i].second; if (x > y) swap(x, y); g[x][y] ++; } return; } rep (i , 1 , m) { pru[p] = i; dfs(p + 1); }}int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif ios::sync_with_stdio(0); cin >> n; lg[0] = 0; rep (i , 1 , n) lg[i] = lg[i / 10] + 1; m = lg[n]; //rep (i , 1 , 6) org[i] = cur[i] = pow(10, i - 1), cout << org[i] << endl; org[1] = 1, org[2] = 10, org[3] = 100, org[4] = 1000, org[5] = 10000, org[6] = 100000; rep (i , 1 , m) cur[i] = org[i]; rep (i , 2 , n) { char a[7], b[7]; cin >> a >> b; int l1 = strlen(a), l2 = strlen(b); if (l1 > l2) swap(l1, l2); g[l1][l2] ++; if (l2 > m) return puts("-1"), 0; } rep (i , 1 , n) cnt[lg[i]] ++; S = 1, T = 2, tot = 2; rep (i , 1 , m) rep (j , i , m) { id[i][j] = ++ tot; e[i][j] = ett + 1; ins(S, id[i][j], 0); } rep (i , 1 , m) rep (j , i , m) { ins(id[i][j], tot + i, n); if (i != j) ins(id[i][j], tot + j, n); } rep (i , 1 , m) ins(tot + i, T, cnt[i] - 1); tot += m; if (m == 1) { rep (i , 2 , n) printf("%d %d\n", i - 1, i); return 0; } dfs(1); puts("-1"); return 0;}
- CF #611 H, New Year And Forgotten Tree
- DP - CF 611D New Year and Ancient Prophecy
- CF 379F: New Year Tree
- CF 750C New Year and Rating
- [CF750G]New Year and Binary Tree Paths
- [cf]New Year Candles
- CF Good Bye 2015 A- New Year and Days(CF611A)
- CF Good Bye 2015 C- New Year and Domino(CF611C)
- cf#gb2015-B - New Year and Old Property-暴力
- cf#gb2015-C. New Year and Domino-dp
- CF 653E(Bear and Forgotten Tree 2-链表优化搜索)
- CodeForces 750H. New Year and Snowy Grid
- CF620E - New Year Tree
- New Year Tree CodeForces
- New Year Tree CodeForces
- New Year Tree CodeForces
- CF Good Bye 2015 C. New Year and Domino && D. New Year and Ancient Prophecy (DP)
- CodeForces 750G. New Year and Binary Tree Paths
- Apache Kafka
- echarts中series中的数据如何如何循环显示数据
- java解析excel
- LightOJ
- 积分与导数原理
- CF #611 H, New Year And Forgotten Tree
- KeyPoint&DMatch类型简介
- 真 · windows环境下php7.0以上开启curl方法
- 257. Binary Tree Paths打印二叉树路径
- 数量金融学(6):TIPP策略
- TCP 的超时与重传
- TreeSet保证元素唯一性
- dedecms模板教程:dedecms模板如何使用?
- android中常用adb命令