UVaLive 7456 Least Crucial Node (并查集+暴力)

题意:求标号最小的最大割点.(删除该点后,指定点#sink能到达的点数减少最多).

析:由于不知道要去掉哪个结点,又因为只有100个结点,所以我们考虑用一个暴力,把所有的结点都去一次,然后用并查集去判断。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 100;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int x;
vector<P> v;
int p[105];
int Find(int x) {  return x == p[x] ? x : p[x] = Find(p[x]); }

int main(){
    while(scanf("%d", &n) == 1 && n){
        scanf("%d", &x);
        scanf("%d", &m);
        int u, vv;
        v.clear();
        for(int i = 0; i < m; ++i){
            scanf("%d %d", &u, &vv);
            v.push_back(P(u, vv));
        }
        int ans = 0, cnt = 0;
        for(int i = 1; i <= n; ++i){
            if(i == x)  continue;
            for(int j = 1; j <= n; ++j)  p[j] = j;
            for(int j = 0; j < v.size(); ++j){
                u = v[j].first;
                vv = v[j].second;
                if(u == i || vv == i)  continue;
                int x = Find(u);
                int y = Find(vv);
                if(x != y)  p[y] = x;
            }
            map<int, int> mp;
            map<int, int> :: iterator it;
            for(int j = 1; j <= n; ++j)
                if(i != j)  ++mp[Find(j)];
            if(mp.size() <= 1) continue;
            int y = Find(x);
            if(cnt < n-mp[y]-1){
                cnt = n-mp[y]-1;
                ans = i;
            }
        }

        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-05 06:57:28

UVaLive 7456 Least Crucial Node (并查集+暴力)的相关文章

UVALive 4730 Kingdom 线段树+并查集

题目链接:点击打开链接 题意见白书P248 思路: 先把读入的y值都扩大2倍变成整数 然后离散化一下 用线段树来维护y轴 区间上每个点的 城市数量和联通块数量, 然后用并查集维护每个联通块及联通块的最大最小y值,还要加并查集的秩来记录每个联通块的点数 然后就是模拟搞.. T^T绝杀失败题..似乎数组开小了一点就过了,== #include<stdio.h> #include<math.h> #include<vector> #include<string.h>

poj 2912 Rochambeau(带权并查集 + 暴力)

题目:poj 2912 Rochambeau(带权并查集 + 暴力) 题目大意:题目给出三个团队和一个裁判,这三个团队和裁判一起玩剪刀石头布,然后规定每个团队必须出一样的,只有裁判可以任意出.然后给出关系,x > y 代表 x 赢y , x < y代表 y 赢 x , 相等则出的一样.问这样的关系可以推出裁判是哪个吗?可以需要说明从第一条到第几条推出来的,不可以也要说明是不可能出现这样的关系,还是裁判不唯一. 解题思路:这题重点是裁判在里面会扰乱关系,并且n * m 才 100000,完全可以

UVALive - 6910 (离线逆序并查集)

题意:给处编号从1~n这n个节点的父节点,得到含有若干棵树的森林:然后再给出k个操作,分两种'C x'是将节点x与其父节点所连接的支剪短:'Q a b'是询问a和b是否在同一棵树中. 题解:一开始拿到题目绞尽脑汁咋都想不到哇该怎么做在线查询,,,除了用暴力外一脸懵逼(不过确实能用暴力水过...=_=); 正解就是将其逆序离线处理: 先根据k个查询去掉所有要剪去的边后将剩余的边进行并查集处理,然后将k个查询逆序地处理查询结果:如果是剪边操作,则是将该节点与父节点并起来.  另外要注意的是,同一条边

uvalive 4730王国kingdom(并查集+线段树)

 题意:有T组测试数据,每组数据的N表示有N个城市,接下来的N行里每行给出每个城市的坐标(0<=x,y<=1000000),然后有M(1<M<200000)个操作,操作有两类,(1)"road A B",表示将城市A和城市B通过一条道路连接,如果A和B原来属于不同的城市群,经过这个操作,A和B就在一个城市群里了,保证每条道路不会和其他道路相交(除了端点A和B).(2)"line C",表示查询当穿过y=C的直线,有多少个城市群.这几个城市

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor

UVALive 6910 Cutting Tree(并查集应用)

总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来. 我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得. 看到他们的做法后,我知道了这个题比较好的做法. 逆向思维的并查集,因为这里只有去边操作,完全可以离线计算,把删边当成加边,然后逆序输出答案即可. 但是,这个却有一个坑,很坑,只有第一次删除的时候,我们才对他进行操作,加边的时候也只能在第一次删除的时候加.当时因为这里,十分困惑-- 这是我无路径压

UVALive 3027 Corporative Network(并查集之四)

问题描述: 求出点到根的距离,带权并查集. 代码思路: 在 I 命令的执行过程中,并不路径压缩. 当执行 E 查询命令时在路径压缩的同时递归求解存储在dist数组里面. 代码: #include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #define MOD 1000 #define MAX 20000 int pre[MAX+7],dist[MAX+7]; int fi

UVALive 4035 - Undetectable Tour(并查集)

题意:给定一个 N * N(3 <= N <= 10000)的矩形区域,左下角为(0,0),右上角为(N,N),现在要从左下角走到右上角,但是有 k(k <= 100)个监视器,每个监视器的监视范围都是统一的,现给定监视范围可能出现的种类与概率,求能够逃出去的概率.(计算距离时均用曼哈顿距离) 1.若两个监视器的距离 <= 两个监视器的监视范围之和,则这两个监视器间的路就不可走: 2.若监视器距离墙的距离 <= 监视器的监视范围,则监视器与墙之间就不可走: 综上,对于每一个监

HDU2144Evolution(并查集+暴力或LCS)

Evolution Time Limit: 20000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 598    Accepted Submission(s): 143 Problem Description Every kind of living creatures has a kind of DNA. The nucleotide bases from whi