zoj3583Simple Path【并查集(思想很好)】

大意:

告诉你一个无向图

然后定义一个simple path是一条路径上面不包含重复的点

然后告诉你两个点s, t

问有多上个点是不在s到t的simple路径上

分析:

对于从s到t的simple path上  无论删除其他的任何一个 点   那么这个点一定是要么和s相连,要么和t相连

从这个角度出发的话

如果删除任何一点

如果有个点突然不和s,或t相连了

那么改点也就一定不是该simple path上的点

最后统计一下个数即可

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 105;
 7 int a[maxn * maxn], b[maxn * maxn];
 8 int vis[maxn];
 9 int fa[maxn];
10
11 int Find(int i) {
12     if(fa[i] == i) return i;
13     return fa[i] = Find(fa[i]);
14 }
15
16 void Union(int u, int v) {
17     int fu = Find(u); int fv = Find(v);
18     if(fu != fv) {
19         fa[fu] = fv;
20     }
21 }
22
23 int main() {
24     int n, m, s, t;
25     while(EOF != scanf("%d %d %d %d",&n, &m, &s, &t) ) {
26         for(int i = 1; i <= m; i++) {
27             scanf("%d %d",&a[i], &b[i]);
28         }
29         memset(vis,0, sizeof(vis));
30         for(int i = 0; i < n; i++) {
31             for(int j = 0; j <= n; j++) fa[j] = j;
32             for(int j = 1; j <= m; j++) {
33                 if(a[j] != i && b[j] != i) {
34                     Union(a[j], b[j]);
35                 }
36             }
37             for(int j = 0; j < n; j++) {
38                 if(j != i && Find(j) != Find(s) && Find(j) != Find(t)) {
39                     vis[j] = 1;
40                 }
41             }
42         }
43         int ans = 0;
44         for(int i = 0; i < n; i++) {
45             if(vis[i]) {
46                    ans++;
47             }
48         }
49         printf("%d\n",ans);
50     }
51     return 0;
52 }

时间: 2024-12-25 03:03:25

zoj3583Simple Path【并查集(思想很好)】的相关文章

DFS+并查集思想求被围绕的区域

class Solution { private int[][] dir= {{0,-1},{-1,0},{0,1},{1,0}}; private boolean[][] used; public boolean isMove(char[][] board,int x,int y) { if(x>=0&&x<board.length&&y>=0&&y<board[0].length) return true; return fals

hdu 2818 Building Block (带权并查集,很优美的题目)

Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 <= P <= 1000000). There are two kinds

CodeForces 828C String Reconstruction(并查集思想)

题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第一个没填涂的位置,那每次都能跳过涂过的地方.每次填完当前格就去填find(fa[i + 1]). ps:一定要合并,不然超时. 代码: #include<stack> #include<vector> #include<queue> #include<set>

hdu1863 畅通工程2 还是用并查集思想解决最小生成树问题

http://acm.hdu.edu.cn/showproblem.php?pid=1863 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的选拔 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19994    Accepted Submission(s): 8528

Java版的广度优先寻路(BFS+并查集思想)

import java.util.Deque; import java.util.LinkedList; class node{ int x; int y; } class Solution{ private int dir[][]=new int[][] {{0,-1},{-1,0},{0,1},{1,0}}; private node parentx[][]; private int Count[][]; private boolean used[][]; private node star

HDU 3081Marriage Match II(二分+并查集+网络流之最大流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3081 有一段时间没写最大流的题了,这题建图居然想了好长时间...刚开始是按着最终的最大流即是做多轮数去想建图,结果根本没思路,后来想了想,可以用二分答案的思想来找最终答案.然后很明显的并查集,但是并查集学的略渣,居然卡在并查集上了..= =. 但是也不是并查集的事..是我建图的思想太正了,稍微用点逆向思维并查集就可以很好利用了. 建图思路是:建立一个源点与汇点,将女孩与源点相连,男孩与汇点相连,权值

Is It A Tree?------HDOJ杭电1325(两种方法,可以用也可以不用并查集!!!!!!详解)

Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the

今天做题做到了并查集相关的内容~简单介绍一下关于并查集的东西

就例如一个非常简单的题~ 有一堆人 其中某些人是朋友 有如下的规则 如果A和B是朋友 B和C是朋友 那么A和C也是朋友~ 最后我们有n次的查询 每次查询问其中两个人是不是朋友? 这个题我们就可以用到集合的思想~ 例如A和B是朋友 我们可以把A和B放到一个集合里~ C和D是朋友 我们就把C和D放到一个集合里~ 如图 (原谅只会人工画图的笨比) 但如果我们假设A和C也是好朋友 我们就把这两个集合合并起来~ 就是这个样子~我们对所有的“朋友对”进行这样的操作 就会把他们分到一个个集合里 这时候查找他们

并查集水题 POJ 1611

题意:有n(n<=30000)个学生,每个学生属于一个团体(可以属于多个团体),共有m(<=500)个团体,如果一个学生是嫌疑人,则他所在的团体的所有人都是嫌疑人,初始时0号学生是嫌疑人.问总共有多少个嫌疑人. 很明显同一个团体的学生可以连一条边,即求0号点所在的连通块有多少个点,用并查集可以很方便的办到,如果两个点属于同一个连通块则把他们的代表元连接起来即可,始终把较小的那个节点作为父节点,所以最后p[0]的节点数就是答案. 代码:

【HDU1232】畅通工程(并查集基础题)

裸敲并查集,很水一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 #include <string> 1