CodeForces GYM 102012 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest

A. Rikka with Minimum Spanning Trees

Description:

Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a problem about the minimum spanning tree (MST). I promise you all that this should be the easiest problem for most people.
A minimum spanning tree, or minimum weight spanning tree, is a subset of edges from an edge-weighted undirected graph, which forms a tree with the minimum possible total edge weight that connects all the vertices together without any cycles.
In this problem, Rikka wants you to calculate the summation of total edge weights through all MSTs for a given graph, which obviously equals to the product of the total edge weight in an MST and the total number of different MSTs. Note that two spanning trees are different if the sets of their edges are different. In addition, a disconnected graph could have no MSTs, the number of whose different MSTs is zero.
To decrease the size of the input, Rikka provides an edge-weighted undirected graph via a random number generator with given random seeds, denoted by two integers $k_1$ and $k_2$. Supposing the number of vertices and edges in the graph are $n$ and $m$ respectively, the following code in C++ tells you how to generate the graph and store the $i$-th edge between the vertex $u[i]$ and $v[i]$ with weight $w[i]$ in corresponding arrays. You can use the code directly in your submissions.

img

Also, to decrease the size of the output, your code should output the answer modulo $(10^9 + 7)$.
If you have already learned how to handle that, start your show and omit all the rest of the statement.
To make sure everyone knows how to solve this problem, here Rikka would like to provide for you all an effective practice which can solve the problem and help you all get Accepted!
The first one you need to know is the Kirchhoff’s matrix tree theorem. Given an undirected graph $G$ with $n$ vertices excluding all loops, its Laplacian matrix $L_{n \times n}$ is defined as $(D - A)$, where $D$ is the degree matrix and $A$ is the adjacency matrix of the graph. More precisely, in the matrix $L$ the entry $l_{i, j}$ ($i \ne j$) equals to $-m$ where $m$ is the number of edges between the $i$-th vertex and the $j$-th vertex, and $L_{i, i}$ equals to the degree of the $i$-th vertex. Next, construct a matrix $L^{*}$ by deleting any row and any column from $L$, for example, deleting row $1$ and column $1$. The Kirchhoff’s matrix tree theorem shows that the number of spanning trees is exactly the determinant of $L^{*}$, which can be computed in polynomial time.
Now let me explain an algorithm that counts the number of MSTs. The algorithm breaks up the Kruskal’s algorithm for MST into a series of blocks, each of which consists of a sequence of operations about adding edges in a same weight into a multigraph (where a multigraph is a graph, two vertices of which may be connected by more than one edge) whose vertices are components that have been built through the previous block of operations.
Precisely speaking, let’s label the multigraph that has been built after the $i$-th block of operations as $G_i$. Without loss of generality, let’s consider the $0$-th block which has no operation and let $G_0$ be an empty graph with $n$ isolated vertices. The $i$-th block of operations squeezes vertices in $G_{i - 1}$ connected by edges in this block into a single vertex. The result is exactly the graph $G_i​$.
If you know the cardinal principle of Kruskal’s algorithm pretty well, you may find that the number of MSTs is the product of the numbers of spanning trees in every component of the graph for each block-defining weight. Actually, the number of edges for a certain weight is fixed in all MSTs, based on the greedy-choice strategy in Kruskal’s algorithm. Finally, the Kirchhoff’s matrix tree theorem helps you compute the numbers of spanning trees for graphs.

Input:

The input contains several test cases, and the first line contains a single integer $T$ ($1 \le T \le 100$), the number of test cases.
For each test case, the only line contains four integers $n$ $(1 \le n \le 10^5)$, $m$ ($m = 10^5$), $k_1$ and $k_2$ ($10^8 \le k_1, k_2 \le 10^{12}$), where $k_1$ and $k_2$ are chosen randomly except for the sample.

Output:

For each test case, output a single line with a single number, the answer modulo $(10^9 + 7)$.

Sample Input:

1
2 100000 123456789 987654321

Sample Output:

575673759

题目链接

现场赛的时候题目根本就没有看懂,只知道题目上有说MST,然后随机代码也很明显的给出了u[i]、v[i]、w[i],所以队友就直接开敲Kruskal了,队友建边时没用unsigned long long,用的int,所以现场整整两个小时才写出来,写出来之后另一个队友偷听隔壁”拉格朗”说没判不存在生成树的情况WA了一发,就赶紧写上判断,提交(121min)1A(我比赛全程没碰键盘:stuck_out_tongue_winking_eye:)。

这道题目由于是随机数据,所以可以直接跑最小生成树(大佬实现题目算法也没问题),注意特判不存在生成树的情况并输出0、Kruskal并查集不压缩路径会TLE。

AC代码:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

struct Edge {
int u, v;
unsigned long long weight;

bool operator < (const Edge &b) const {
return weight < b.weight;
}
};

int pre[maxn];
bool vis[maxn];
Edge edges[maxn];

unsigned long long k1, k2;

unsigned long long xorShift128Plus() {
unsigned long long k3 = k1, k4 = k2;
k1 = k4;
k3 ^= k3 << 23;
k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
return k2 + k4;
}

int n, m, u[maxn], v[maxn];
unsigned long long w[maxn];

void init() {
for (int i = 0; i <= n; ++i) {
pre[i] = i;
vis[i] = false;
}
}

int find(int x) {
int r = x;
while (pre[r] != r) {
r = pre[r];
}
int i = x, j;
while (i != r) {
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}

void join(int x, int y) {
int xx = find(x);
int yy = find(y);
if (xx != yy) {
pre[xx] = yy;
}
}

unsigned long long kruskal() {
sort(edges + 1, edges + m + 1);
init();
unsigned long long ans = 0;
for (int i = 1; i <= m; ++i) {
if (find(edges[i].u) != find(edges[i].v)) {
join(edges[i].u, edges[i].v);
ans = (ans + edges[i].weight) % mod;
vis[edges[i].u] = true;
vis[edges[i].v] = true;
}
}
return ans;
}

void gen() {
scanf("%d%d%llu%llu", &n, &m, &k1, &k2);
for (int i = 1; i <= m; ++i) {
u[i] = xorShift128Plus() % n + 1;
v[i] = xorShift128Plus() % n + 1;
w[i] = xorShift128Plus();
edges[i] = Edge {u[i], v[i], w[i]};
}
}

bool check() {
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
return false;
}
}
return true;
}

int t;
unsigned long long ans;

int main(int argc, char *argv[]) {
scanf("%d", &t);
while (t--) {
gen();
ans = kruskal();
printf("%llu\n", check() ? ans : 0);
}
return 0;
}

C. Rikka with Consistency

Description:

On the way to the Moscow, Rikka knows someone will replace her. Who is the guy? A devil to get in touch with her dark side, or an angel to rinse the shadow off her mind? However, Rikka knows that her successor has a special name, whose meaning in Chinese is Consistency. The process of replacement is so wonderful and sentimental, which is what you all must know.
Now, the only road from Beijing to Moscow is described as a broken line with $n$ segments in the $X$-$H$ plane. The $i$-th segment connects the points $(i - 1, h_{i - 1})$ and $(i, h_i)$, and $h_0 = h_n = 0$ are known. This figure is a topographic map showing the whole trip from Beijing to Moscow and its $H$ axis indicates the altitude. The distance of a path between two points is the length of the broken line between their corresponding points in the map.
At the outset of the trip, Rikka is in Beijing whose location in the $X$-$H$ plane is $(0, 0)$; Consistency, the guy who will replace Rikka, is in Moscow which is located at $(n, 0)$. Consistency always maintains consistent academic standards, a consistent life level, a consistent height of perspective and the altitude as what Rikka owns. This is why their heights are the same yesterday, today and forever.
Now Rikka wants you to calculate the minimum total distance they need (which is the total length of paths that Rikka and Consistency travel along). By the time that Rikka arrives in Moscow and Consistency arrives in Beijing as well, their replacement will be finished (and this is the ending which is also a new beginning).

Input:

The input contains several test cases, and the first line contains a single integer $T$ ($1 \le T \le 500$), the number of test cases.
For each test case, the first line contains a single integer $n$ ($1 \le n \le 50$), the number of segments.
The second line contains $(n + 1)$ integers $h_0, h_1, \cdots, h_n$ ($0 \le h_i \le 50$), satisfying $h_0 = h_n = 0$.
The input guarantees that the paths for each test case always exist.

Output:

For each test case, output a single line with a single number, the minimum total distance they need. Your answer is considered correct if its absolute or relative error does not exceed $10^{-9}$. Formally, let your answer be $a$, and Rikka’s answer be $b$. Your answer is considered correct if $\frac{|a - b|}{\max(1, |b|)} \le 10^{-9}$.

Sample Input:

2
4
0 1 1 2 0
4
0 2 1 3 0

Sample Output:

12.128990204491960
22.313624568639947

题目链接

这道题目和2010年Tokyo区域赛E题几乎一样(UVAlive5071Aizu1309),只是乘了二倍,具体请看

Aizu 1309 The Two Men of the Japanese Alps

AC代码:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e3 + 5;

struct Point {
double X, Y;

void Output() {
printf("(%lf,%lf)\n", X, Y);
}

Point operator - (const Point &B) const {
return Point {X - B.X, Y - B.Y};
}

double operator * (const Point &B) const {
return X * B.X + Y * B.Y;
}

double operator ^ (const Point &B) const {
return X * B.Y - Y * B.X;
}

bool operator < (const Point &B) const {
return X < B.X;
}
};

double Distance(Point A, Point B) {
return sqrt((B - A) * (B - A));
}

struct Line {
Point S, T;

Point operator & (const Line &B) const {
double Temp = ((S - B.S) ^ (B.S - B.T)) / ((S - T) ^ (B.S - B.T));
return Point {S.X + (T.X - S.X) * Temp, S.Y + (T.Y - S.Y) * Temp};
}
};

struct Status {
int Left, Right;
double Length;

bool operator < (const Status &B) const {
return Length > B.Length;
}
};

int T;
int N;
int Tot;
int Height[maxn];
Point points[maxn];

void Init() {
for (int i = 0; i <= N; ++i) {
Line SkyLine = Line {Point {-1.0, points[i].Y}, Point {55.0, points[i].Y}};
for (int j = 0; j < N; ++j) {
if ((points[i].Y - points[j].Y) * (points[i].Y - points[j + 1].Y) < 0) {
points[Tot++] = SkyLine & Line {points[j], points[j + 1]};
}
}
}
sort(points, points + Tot);
}

bool Vis[maxn][maxn];

double Bfs() {
memset(Vis, false, sizeof(Vis));
priority_queue<Status> Que;
Que.push(Status {0, Tot - 1, 0.0});
while (!Que.empty()) {
Status Cur = Que.top(); Que.pop();
if (Cur.Left == Cur.Right) {
return Cur.Length;
}
if (Vis[Cur.Left][Cur.Right]) {
continue;
}
Vis[Cur.Left][Cur.Right] = true;
for (int i = -1; i < 2; ++i) {
for (int j = -1; j < 2; ++j) {
if (i || j) {
int LeftIndex = Cur.Left + i, RightIndex = Cur.Right + j;
if (LeftIndex >= 0 && LeftIndex < Tot && RightIndex >= 0 && RightIndex < Tot) {
if (points[LeftIndex].Y == points[RightIndex].Y) {
Que.push(Status {LeftIndex, RightIndex, Cur.Length + Distance(points[Cur.Left], points[LeftIndex]) + Distance(points[Cur.Right], points[RightIndex])});
}
}
}
}
}
}
return 0.0;
}

int main(int argc, char *argv[]) {
scanf("%d", &T);
for (int Case = 1; Case <= T; ++Case) {
scanf("%d", &N);
Tot = 0;
for (int i = 0; i <= N; ++i) {
scanf("%d", &Height[i]);
points[Tot++] = Point {double(i), double(Height[i])};
}
Init();
printf("%.15lf\n", Bfs() * 2);
}
return 0;
}

G. Rikka with Intersections of Paths

Description:

Rikka has a tree $T$ with $n$ vertices numbered from $1$ to $n$.
Meanwhile, Rikka has marked $m$ simple paths in $T$, the $i$-th of which is between the vertices $x_i$ and $y_i$, where some of them could be the same path.
Now, Rikka wants to know in how many different strategies she can select $k$ paths from the marked paths such that those selected paths share at least one common vertex.

Input:

The input contains several test cases, and the first line contains a single integer $T$ ($1 \le T \le 200$), the number of test cases.
For each test case, the first line contains three integers $n$ ($1 \le n \le 3 \times 10^5$), the size of the tree $T$, $m$ ($2 \le m \le 3 \times 10^5$), the number of marked paths, and $k$ ($2 \le k \le m$).
The following $(n - 1)$ lines describe the tree $T$. Each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \ne v$), representing an edge between the vertices $u$ and $v$.
The following $m$ lines describe all marked simple paths in the tree. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$).
The input guarantees that the sum of $n$ and the sum of $m$ in all test cases are at most $2 \times 10^6$ respectively.

Output:

For each test case, output a single line with a single integer, the number of different strategies meeting the requirement modulo $(10^9 + 7)$.

Sample Input:

1
3 6 2
1 2
1 3
1 1
2 2
3 3
1 2
1 3
2 3

Sample Output:

10

题目链接

这道题目和hihoCoder#1167 : 高等理论计算机科学有些类似,都是求的相交路径数量,只不过这道题目问的是k条路径相交的数量,求出LCA之后枚举顶点,用数学做法计算每个顶点的贡献即可。

$ans += C_{cnt[i]}^{k} - C_{cnt[i] - LCAcnt[i]}^{k}​$,其中$cnt[i]​$为经过顶点$i​$的路径数量(通过树上差分计算),$LCAcnt[i]​$为$LCA​$是$i​$顶点的路径数量。

AC代码:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;
const int maxn = 3e5 + 5;

struct Edge {
int V, Next;
};

Edge edges[maxn << 4];
int Head[maxn];
int Tot;

void AddEdge(int U, int V) {
edges[Tot] = Edge {V, Head[U]};
Head[U] = Tot++;
}

int Rmq[maxn << 1];

struct ST {
int Dp[maxn << 1][20];
void Init(int N) {
for (int i = 1; i <= N; ++i) {
Dp[i][0] = i;
}
for (int j = 1; (1 << j) <= N; ++j) {
for (int i = 1; i + (1 << j) - 1 <= N; ++i) {
Dp[i][j] = Rmq[Dp[i][j - 1]] < Rmq[Dp[i + (1 << (j - 1))][j - 1]] ? Dp[i][j - 1] : Dp[i + (1 << (j - 1))][j - 1];
}
}
}

int Query(int A, int B) {
if (A > B) {
swap(A, B);
}
int Len = (int)(log2(B - A + 1));
return Rmq[Dp[A][Len]] < Rmq[Dp[B - (1 << Len) + 1][Len]] ? Dp[A][Len] : Dp[B - (1 << Len) + 1][Len];
}
};

int Vertex[maxn << 1];
int First[maxn];
int Cnt;
ST St;
int Parent[maxn];

void LCADfs(int Cur, int Pre, int Depth) {
Vertex[++Cnt] = Cur;
First[Cur] = Cnt;
Rmq[Cnt] = Depth;
Parent[Cur] = Pre;
for (int i = Head[Cur]; ~i; i = edges[i].Next) {
if (edges[i].V == Pre) {
continue;
}
LCADfs(edges[i].V, Cur, Depth + 1);
Vertex[++Cnt] = Cur;
Rmq[Cnt] = Depth;
}
}

void LCAInit(int N) {
Cnt = 0;
LCADfs(1, 0, 0);
St.Init(2 * N - 1);
}

int QueryLCA(int U, int V) {
return Vertex[St.Query(First[U], First[V])];
}

long long Factorial[maxn];
long long InvFactorial[maxn];

long long QuickPow(long long A, long long B) {
long long Ans = 1;
while (B) {
if (B & 1) {
Ans = Ans * A % mod;
}
A = A * A % mod;
B >>= 1;
}
return Ans;
}

void FactorialInit(int N) {
Factorial[0] = 1;
for (long long i = 1; i <= N; ++i) {
Factorial[i] = (Factorial[i - 1] * i) % mod;
}
InvFactorial[N] = QuickPow(Factorial[N], mod - 2);
for (long long i = N - 1; ~i; --i) {
InvFactorial[i] = (InvFactorial[i + 1] * (i + 1)) % mod;
}
}

long long C(int N, int M) {
if (M > N || M < 0 || N < 0) {
return 0;
}
return Factorial[N] * InvFactorial[M] % mod * InvFactorial[N - M] % mod;
}

int Count[maxn];
int LCACount[maxn];

int T;
int N, M, K;
long long Ans;

int Dfs(int Cur, int Pre) {
for (int i = Head[Cur]; ~i; i = edges[i].Next) {
if (edges[i].V == Pre) {
continue;
}
Count[Cur] += Dfs(edges[i].V, Cur);
}
if (LCACount[Cur]) {
Ans += ((C(Count[Cur], K) - C(Count[Cur] - LCACount[Cur], K)) + mod) % mod;
Ans %= mod;
}
return Count[Cur];
}

void Init(int N) {
Tot = 0;
memset(Head, -1, sizeof(Head));
for (int i = 1; i <= N; ++i) {
Count[i] = 0;
LCACount[i] = 0;
}
}

int main(int argc, char *argv[]) {
FactorialInit(maxn - 5);
scanf("%d", &T);
for (int Case = 1; Case <= T; ++Case) {
scanf("%d%d%d", &N, &M, &K);
Init(N);
for (int i = 1, U, V; i < N; ++i) {
scanf("%d%d", &U, &V);
AddEdge(U, V);
AddEdge(V, U);
}
Parent[1] = 0;
LCAInit(N);
for (int i = 1, U, V; i <= M; ++i) {
scanf("%d%d", &U, &V);
int LCA = QueryLCA(U, V);
LCACount[LCA]++;
Count[U]++; Count[V]++;
Count[LCA]--; Count[Parent[LCA]]--;
}
Ans = 0;
Dfs(1, 0);
printf("%lld\n", Ans);
}
return 0;
}
文章目录
  1. 1. A. Rikka with Minimum Spanning Trees
    1. 1.1. Description:
    2. 1.2. Input:
    3. 1.3. Output:
    4. 1.4. Sample Input:
    5. 1.5. Sample Output:
      1. 1.5.1. 题目链接
    6. 1.6. AC代码:
  2. 2. C. Rikka with Consistency
    1. 2.1. Description:
    2. 2.2. Input:
    3. 2.3. Output:
    4. 2.4. Sample Input:
    5. 2.5. Sample Output:
      1. 2.5.1. 题目链接
      2. 2.5.2. Aizu 1309 The Two Men of the Japanese Alps
    6. 2.6. AC代码:
  3. 3. G. Rikka with Intersections of Paths
    1. 3.1. Description:
    2. 3.2. Input:
    3. 3.3. Output:
    4. 3.4. Sample Input:
    5. 3.5. Sample Output:
      1. 3.5.1. 题目链接
    6. 3.6. AC代码:
|