惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
量子位
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
IT之家
IT之家
V
Visual Studio Blog
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
D
Docker
V
V2EX

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
Codeforces Round 606 E. Two Fairs——图论
Shiroha · 2020-01-08 · via Shiroha白羽的博客

题目链接

题意

给你一张无向图,求出有多少对点对(x, y)满足从点x到点y的所有路径必同时经过点a和点b

分析

单点

首先考虑假如点a和点b是同一个点的情况

我从任意的一点出发,把所有与点a/b相连的路视为不存在,通过bfs遍历所有可能到达的点。那么这些点之间可以满足不经过点a/b能联通。反之,如果能将其他所有的点均进行bfs,组成类似并查集的数据结构,那么我可以很快得到,所有非同一集合内的点之间必须通过点a/b。

下一个问题:如何保证所有点都完成了遍历(bfs)

我们可以不断的在vis数组中找没有被vis的点,然后不断的bfs,但是这样效率很低

换一种思路

我们可以直接从点a/b出发,设定bfs起点为点a/b,那么就可以一次性的完成整个图的bfs遍历,并且使用类似并查集的结构将他们保存下来。

两点

我们可以这样定义,如果存在点对(x,y),假设与点b联通的路均视为不连通,满足x与a联通,但是不与y联通,同时,假设与点a联通的路均视为不连通,满足y与b联通,但是不与x联通,那么我们可以得到这样点x的集合和点y的集合,那么这两个集合内各取一点即为一组合理的点对

AC code

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
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 201000;
const int MAXM = 1001000;

// 无权有向图
struct Graph {
struct Edge {
int to, next;
} edge[MAXM];
int head[MAXN];
int tot;

void init(int n) {
tot = 0;
memset(head, -1, sizeof(int) * (n + 1));
}

void add_edge(int from, int to) {
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
} graph;

void solve() {
int T;
cin >> T;
for (int ts = 0; ts < T; ++ts) {
int n, m, a, b;
cin >> n >> m >> a >> b;
graph.init(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
graph.add_edge(u, v);
graph.add_edge(v, u);
}
bool vis[MAXN];
queue<int> q;
int a_cnt = n - 2, b_cnt = n - 2;

memset(vis, false, sizeof(bool) * (n + 1));
q.push(a);
vis[a] = true;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = graph.head[cur]; i != -1; i = graph.edge[i].next) {
if (!vis[graph.edge[i].to] && graph.edge[i].to != b) {
vis[graph.edge[i].to] = true;
q.push(graph.edge[i].to);
a_cnt--;
}
}
}

memset(vis, false, sizeof(bool) * (n + 1));
q.push(b);
vis[b] = true;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = graph.head[cur]; i != -1; i = graph.edge[i].next) {
if (!vis[graph.edge[i].to] && graph.edge[i].to != a) {
vis[graph.edge[i].to] = true;
q.push(graph.edge[i].to);
b_cnt--;
}
}
}

cout << 1ll * a_cnt * b_cnt << endl;
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long long test_index_for_debug = 1;
char acm_local_for_debug;
while (cin >> acm_local_for_debug) {
cin.putback(acm_local_for_debug);
if (test_index_for_debug > 20) {
throw runtime_error("Check the stdin!!!");
}
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
}
#else
solve();
#endif
return 0;
}