ZOJ 3732 Graph Reconstruction
題目連結
Let there be a simple graph with
N
N
N vertices but we just know the degree of each vertex. Is it possible to reconstruct the graph only by these information?
A simple graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices. The degree of a vertex is the number of edges that connect to it.
Input
There are multiple cases. Each case contains two lines. The first line contains one integer
N
N
N
(
2
≤
N
≤
100
)
(2 ≤ N ≤ 100)
(2≤N≤100), the number of vertices in the graph. The second line conrains
N
N
N integers in which the ith item is the degree of ith vertex and each degree is between
0
0
0 and
N
−
1
N-1
N−1(inclusive).
Output
If the graph can be uniquely determined by the vertex degree information, output “UNIQUE” in the first line. Then output the graph.
If there are two or more different graphs can induce the same degree for all vertices, output “MULTIPLE” in the first line. Then output two different graphs in the following lines to proof.
If the vertex degree sequence cannot deduced any graph, just output “IMPOSSIBLE”.
The output format of graph is as follows:
N
E
N \ E
N E
u
1
u
2
…
u
E
u_1\ u_2\dots u_E
u1 u2…uE
v
1
v
2
…
v
E
v_1\ v_2 \dots v_E
v1 v2…vE
Where
N
N
N is the number of vertices and
E
E
E is the number of edges, and
{
u
i
,
v
i
}
\{u_i,v_i\}
{ui,vi} is the
i
t
h
i_{th}
ith edge the the graph. The order of edges and the order of vertices in the edge representation is not important since we would use special judge to verify your answer. The number of each vertex is labeled from
1
1
1 to
N
N
N. See sample output for more detail.
Sample Input
1
0
6
5 5 5 4 4 3
6
5 4 4 4 4 3
6
3 4 3 1 2 0
Sample Output
UNIQUE
1 0
UNIQUE
6 13
3 3 3 3 3 2 2 2 2 1 1 1 5
2 1 5 4 6 1 5 4 6 5 4 6 4
MULTIPLE
6 12
1 1 1 1 1 5 5 5 6 6 2 2
5 4 3 2 6 4 3 2 4 3 4 3
6 12
1 1 1 1 1 5 5 5 6 6 3 3
5 4 3 2 6 4 3 2 4 2 4 2
IMPOSSIBLE
Havel-Hakimi 定理:由非負整陣列成的不遞增序列
s
:
d
1
,
d
2
,
…
d
n
(
n
≥
2
,
d
1
≥
1
)
s: d_1, d_2, \ldots d_n(n \ge 2, d_1 \ge 1)
s:d1,d2,…dn(n≥2,d1≥1) 是可圖的,當且僅當序列
s
1
:
d
2
−
1
,
d
3
−
1
,
⋯
,
d
d
1
+
1
−
1
,
d
d
1
+
2
,
⋯
,
d
n
s_1: d_2-1, d_3-1, \cdots, d_{d_1+1}-1, d_{d_1+2}, \cdots, d_n
s1:d2−1,d3−1,⋯,dd1+1−1,dd1+2,⋯,dn
是可圖的。序列
s
1
s_1
s1 中有
n
−
1
n-1
n−1 個非負整數,
s
s
s 序列中
d
1
d_1
d1後的前
d
1
d_1
d1個度數(即
d
2
∼
d
d
1
+
1
d_2 \sim d_{d_1+1}
d2∼dd1+1)減
1
1
1 後構成
s
1
s_1
s1 中的前
d
1
d_1
d1 個數。對應到構造解的過程即為將節點
1
1
1 向節點
2
∼
d
1
+
1
2\sim d_1+1
2∼d1+1 各連一條邊。如果
d
d
1
+
1
=
d
d
1
+
2
d_{d_1+1}=d_{d_1+2}
dd1+1=dd1+2,那麼存在另一種建圖方案,其中
1
1
1連
d
1
+
2
d_1+2
d1+2而不是
d
1
+
1
d_1+1
d1+1,顯然兩種方案一定不相同。
至於不存在
d
d
1
+
1
=
d
d
1
+
2
d_{d_1+1}=d_{d_1+2}
dd1+1=dd1+2那麼解唯一,emm…不會證
#include<bits/stdc++.h>
#define pii pair<int,int>
#define ce(i, r) i==r?'\n':' '
#define fi first
#define se second
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 105, M = N * N;
pii p[2][N], e[2][M];
int n, t[2];
int main() {
while (~scanf("%d", &n)) {
t[0] = t[1] = 0;
for (int i = 1; i <= n; i++)p[0][i] = {qr(), i};
bool suc1 = true, suc2 = false;
int m = n;
sort(p[0] + 1, p[0] + 1 + m, greater<pii >());
memcpy(p[1], p[0], sizeof(pii) * (n + 1));
for (int i = 1; i <= m && p[0][i].fi; i++) {
while (!p[0][m].fi)m--;
if (i + p[0][i].fi > m) {
suc1 = false;
break;
}
if (i + p[0][i].fi < m && p[1][i + p[0][i].fi].fi == p[1][i + p[0][i].fi + 1].fi)
swap(p[1][i + p[0][i].fi], p[1][i + p[0][i].fi + 1]), suc2 = true;
for (int j = i + 1; j <= i + p[0][i].fi; j++)
p[0][j].fi--, e[0][++t[0]] = {p[0][i].se, p[0][j].se};
for (int j = i + 1; j <= i + p[1][i].fi; j++)
p[1][j].fi--, e[1][++t[1]] = {p[1][i].se, p[1][j].se};
sort(p[0] + i + 1, p[0] + 1 + m, greater<pii >());
sort(p[1] + i + 1, p[1] + 1 + m, greater<pii >());
}
if (!suc1)puts("IMPOSSIBLE");
else if (!suc2) {
puts("UNIQUE");
printf("%d %d\n", n, t[0]);
for (int i = 1; i <= t[0]; i++)printf("%d%c", e[0][i].fi, ce(i, t[0]));
for (int i = 1; i <= t[0]; i++)printf("%d%c", e[0][i].se, ce(i, t[0]));
if (!t[0])puts("\n");
} else {
puts("MULTIPLE");
printf("%d %d\n", n, t[0]);
for (int i = 1; i <= t[0]; i++)printf("%d%c", e[0][i].fi, ce(i, t[0]));
for (int i = 1; i <= t[0]; i++)printf("%d%c", e[0][i].se, ce(i, t[0]));
printf("%d %d\n", n, t[1]);
for (int i = 1; i <= t[1]; i++)printf("%d%c", e[1][i].fi, ce(i, t[1]));
for (int i = 1; i <= t[1]; i++)printf("%d%c", e[1][i].se, ce(i, t[1]));
}
}
return 0;
}
相關文章
- LeetCode 444 sequence reconstructionLeetCodeStruct
- Uva 10410 Tree ReconstructionStruct
- ZOJ First Digit(瞎搞)Git
- Diablo III ZOJ - 3769
- LeetCode 406 Queue Reconstruction by HeightLeetCodeStruct
- ZOJ 1048 Financial ManagementNaN
- ZOJ 3789 Gears(並查集)並查集
- ZOJ 1013 Great Equipment(DP)UI
- RTK(The Reconstruction ToolKit)在Windows系統下的安裝StructWindows
- ZOJ 3875 Lunch Time(結構體排序)結構體排序
- ZOJ 2745 01-K Code(DP)(轉)
- WPF draw graph
- Count BFS Graph
- 論文解讀(Graph-MLP)《Graph-MLP: Node Classification without Message Passing in Graph》
- ZOJ Problem Set - 1094 Matrix Chain MultiplicationAI
- ZOJ Problem Set - 1016 ParencodingsEncoding
- 動態規劃-ZOJ 1 163 THE STAIRCASES動態規劃AI
- ZOJ 4846 GCD Reduce (數學分析題)GC
- ZOJ Consecutive Blocks(左右指標移動)BloC指標
- zoj 1093 dp Monkey and BananaNaN
- 圖論(Graph Theory)圖論Graph Theory
- E - Reachability in Functional GraphFunction
- Graph Theory with ApplicationsGraph TheoryAPP
- Hidden Bipartite Graph
- 一文了解 Nebula Graph DBaaS 服務——Nebula Graph Cloud ServiceCloud
- ZOJ Problem Set - 3708 Density of Power Network
- llvm code call graphLVM
- ffmpeg graph2dot
- BundleFusion: Real-time GloballyConsistent 3D Reconstruction... 論文解析3DStruct
- 動態規劃-ZOJ 1025-WOODEN STICKS動態規劃
- ZOJ 3772 Calculate the Function(線段樹+矩陣)Function矩陣
- ZOJ 3868 GCD Expectation (容斥+莫比烏斯反演)GC
- ZOJ 3802 Easy 2048 Again(狀壓dp)AI
- 論文解讀(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
- 論文解讀(AGE)《Adaptive Graph Encoder for Attributed Graph Embedding》APT
- 論文解讀(GMT)《Accurate Learning of Graph Representations with Graph Multiset Pooling》
- Microsoft Graph 的 .NET 6 之旅ROS
- 資料結構:圖(Graph)資料結構