Div. 3难度图论树论题练手(持续)

Anna, Svyatoslav and Maps

题面翻译

以邻接矩阵的形式给定一个 \(n\) 个点的无自环的有向图 \(G\),和一个长为 \(m\) 的序列 \(p\)

求序列 \(p\) 的最短子序列 \(q\),使得 \(p\) 是按顺序连接顶点 \(q_1,q_2...\) 的最短路径之一。

题目描述

The main characters have been omitted to be short.

You are given a directed unweighted graph without loops with $ n $ vertexes and a path in it (that path is not necessary simple) given by a sequence $ p_1, p_2, , p_m $ of $ m $ vertexes; for each $ 1 i < m $ there is an arc from $ p_i $ to $ p_{i+1} $ .

Define the sequence $ v_1, v_2, , v_k $ of $ k $ vertexes as good, if $ v $ is a subsequence of $ p $ , $ v_1 = p_1 $ , $ v_k = p_m $ , and $ p $ is one of the shortest paths passing through the vertexes $ v_1 $ , $ $ , $ v_k $ in that order.

A sequence $ a $ is a subsequence of a sequence $ b $ if $ a $ can be obtained from $ b $ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $ p $ is good but your task is to find the shortest good subsequence.

If there are multiple shortest good subsequences, output any of them.

输入格式

The first line contains a single integer $ n $ ( $ 2 n $ ) — the number of vertexes in a graph.

The next $ n $ lines define the graph by an adjacency matrix: the $ j $ -th character in the $ i $ -st line is equal to $ 1 $ if there is an arc from vertex $ i $ to the vertex $ j $ else it is equal to $ 0 $ . It is guaranteed that the graph doesn't contain loops.

The next line contains a single integer $ m $ ( $ 2 m ^6 $ ) — the number of vertexes in the path.

The next line contains $ m $ integers $ p_1, p_2, , p_m $ ( $ 1 p_i n $ ) — the sequence of vertexes in the path. It is guaranteed that for any $ 1 i < m $ there is an arc from $ p_i $ to $ p_{i+1} $ .

输出格式

In the first line output a single integer $ k $ ( $ 2 k m $ ) — the length of the shortest good subsequence. In the second line output $ k $ integers $ v_1 $ , $ $ , $ v_k $ ( $ 1 v_i n $ ) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.

样例 #1

样例输入 #1

1
2
3
4
5
6
7
4
0110
0010
0001
1000
4
1 2 3 4

样例输出 #1

1
2
3
1 2 4

样例 #2

样例输入 #2

1
2
3
4
5
6
7
4
0110
0010
1001
1000
20
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

样例输出 #2

1
2
11
1 2 4 2 4 2 4 2 4 2 4

样例 #3

样例输入 #3

1
2
3
4
5
6
3
011
101
110
7
1 2 3 1 3 2 1

样例输出 #3

1
2
7
1 2 3 1 3 2 1

样例 #4

样例输入 #4

1
2
3
4
5
6
7
4
0110
0001
0001
1000
3
1 2 4

样例输出 #4

1
2
2
1 4

提示

Below you can see the graph from the first example:

The given path is passing through vertexes $ 1 $ , $ 2 $ , $ 3 $ , $ 4 $ . The sequence $ 1-2-4 $ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $ 1 $ , $ 2 $ and $ 4 $ in that order is $ 1-2-3-4 $ . Note that subsequences $ 1-4 $ and $ 1-3-4 $ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $ 1-3-4 $ .

In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes.

In the fourth example, the paths $ 1-2-4 $ and $ 1-3-4 $ are the shortest paths passing through the vertexes $ 1 $ and $ 4 $ .

题意大概是找到题目所给路径的子路径,从而使得必经一些节点走完一张图等于原来的最短路。

这里不删除的原则就是1-3<1-2-3,因此2不能删除,不然就是1-3-4,并不符合最短路。

预处理出floyd,并且记录判断即可。

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
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define frep(i,a,n) for(int i=a;i>=n;i--)
#define int long long
#define PII pair<int,int>
#define lowbit(x) (x&(-x))
const int mod=1e9+7;
const double pai=acos(-1.0);
#define ios ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
#define LF(x) fixed<<setprecision(x)
const int N=1e2+100;
int e[N][N];
int n;
const int M=1e6+100;
int a[M];
int m;
signed main()
{
cin>>n;
//输入边权
rep(i,1,n)
{
rep(j,1,n)
{
char c;
cin>>c;
e[i][j]=c-'0';
if(e[i][j]==0)
{
e[i][j]=0x7f7f7f;
}
if(i==j)
{
e[i][j]=0;
}
}
}
//floyd
rep(k,1,n)
{
rep(i,1,n)
{
rep(j,1,n)
{
e[i][j]=min(e[i][j],e[i][k]+e[k][j]);
}
}
}
cin>>m;
rep(i,1,m)
{
cin>>a[i];
}
vector<int>ans;
ans.push_back(a[1]);
int pre=a[1];
int now=0;
rep(i,2,m)
{
now+=e[a[i-1]][a[i]];
if(e[pre][a[i]]<now)
{
pre=a[i-1];
ans.push_back(pre);
now=e[pre][a[i]];
}
}
ans.push_back(a[m]);
cout<<ans.size()<<'\n';
for(auto x:ans)
{
cout<<x<<' ';
}
return 0;
}

Cow and Snacks

题面翻译

\(n\) 种花,\(k\) 个客人,每个人喜欢两种编号不同的花。但是每种花在花店里只有一束。

客人按顺序进入花店,会买走所有她喜欢且仍在店铺里的花。如果一个客人买不到任何一束花,那么她就会十分沮丧导致变成肥宅。现在你可以自己安排这 \(k\) 个人的顺序,使得肥宅的数量最小。

翻译 by @皎月半洒花

题目描述

The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!

There are $ n $ snacks flavors, numbered with integers $ 1, 2, , n $ . Bessie has $ n $ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:

  • First, Bessie will line up the guests in some way.
  • Then in this order, guests will approach the snacks one by one.
  • Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.

Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.

输入格式

The first line contains integers $ n $ and $ k $ ( $ 2 n ^5 $ , $ 1 k ^5 $ ), the number of snacks and the number of guests.

The $ i $ -th of the following $ k $ lines contains two integers $ x_i $ and $ y_i $ ( $ 1 x_i, y_i n $ , $ x_i y_i $ ), favorite snack flavors of the $ i $ -th guest.

输出格式

Output one integer, the smallest possible number of sad guests.

样例 #1

样例输入 #1

1
2
3
4
5
5 4
1 2
4 3
1 4
3 4

样例输出 #1

1
1

样例 #2

样例输入 #2

1
2
3
4
5
6
6 5
2 3
2 1
3 4
6 5
4 5

样例输出 #2

1
0

提示

In the first example, Bessie can order the guests like this: $ 3, 1, 2, 4 $ . Guest $ 3 $ goes first and eats snacks $ 1 $ and $ 4 $ . Then the guest $ 1 $ goes and eats the snack $ 2 $ only, because the snack $ 1 $ has already been eaten. Similarly, the guest $ 2 $ goes up and eats the snack $ 3 $ only. All the snacks are gone, so the guest $ 4 $ will be sad.

In the second example, one optimal ordering is $ 2, 1, 3, 5, 4 $ . All the guests will be satisfied.

解题思路: 本题直接并查集查询即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<cstdio>
const int N=1e5+1;
int n,k,x,y,f[N],ans;
inline int find(int x){//基本操作:找爸爸
return f[x]^x?f[x]=find(f[x]):x;
}
int main(){
cin>>n>>k;
for(register int i=1;i<=n;++i)f[i]=i;//并查集基本操作
for(register int i=0;i<k;++i){//k个客人
cin>>x>>y;
x=find(x),y=find(y),f[x]=y;//连边
}
for(register int i=1;i<=n;++i)
if(f[i]==i)++ans;//统计连通块个数
cout<<k-n+ans;
return 0;
}