我是靠谱客的博主 大方溪流,这篇文章主要介绍2020ICPC济南站 J.Tree Constructer,现在分享给大家,希望可以做个参考。

题目大意:给定一棵N个顶点的树,顶点为1~N,对于一个序列A1,A2,…,An,若Ai | Aj == 2^60-1,则会连一条边(i,j)。要求求出一个序列,可以唯一确定所给定的树。

思路:考虑到树是一个二分图,可以考虑将二分图左侧顶点对应的值的二进制位后两位设置为01,而右部分对应设置为10,这样可以使得二分图同侧不会出现连边。接下来不妨把左侧顶点的其余二进制位都设为1,之后对于左侧的每个顶点,各从前面若干个1中选择一个不同的设为0,之后对于树中每个从左侧连接到右侧顶点的边,只要把右侧顶点二进制位中对应左侧顶点二进制位中0的那一位设置成1,就可以构造出答案了。

因为二分图中必然有一侧顶点数小于等于N/2,而选择顶点较少的一侧作为左侧,最多仅用到52位所以这个构造可行。

代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h> #include<unordered_map> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; //#define int LL #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #pragma warning(disable :4996) const int maxn = 110; const double eps = 1e-6; const LL MOD = 998244353; vector<int>G[maxn]; int V; vector<int>B[2]; LL ans[maxn]; LL key[maxn]; bool used[maxn]; void add_edge(int from, int to) { G[from].push_back(to); G[to].push_back(from); } void dfs(int v, int pcol) { used[v] = true; B[pcol ^ 1].push_back(v); for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; if (!used[u]) dfs(u, pcol ^ 1); } } void solve() { LL mx = 1LL; for (int i = 1; i <= 60; i++) mx <<= 1; mx -= 1; dfs(1, 1); LL base = 4LL; if (B[1].size() < B[0].size()) swap(B[1], B[0]); for (auto v : B[0]) { ans[v] = mx; ans[v] ^= base; key[v] = base; ans[v] -= 2LL; base <<= 1; } for (auto v : B[1]) ans[v] |= 2LL; for (auto v : B[0]) { for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; ans[u] |= key[v]; } } for (int i = 1; i <= V; i++) cout << ans[i] << ' '; cout << endl; } int main() { IOS; int u, v; cin >> V; for (int i = 1; i < V; i++) { cin >> u >> v; add_edge(u, v); } solve(); return 0; }

最后

以上就是大方溪流最近收集整理的关于2020ICPC济南站 J.Tree Constructer的全部内容,更多相关2020ICPC济南站内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部