Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >UVA10765-Doves and bombs(BCC)

UVA10765-Doves and bombs(BCC)

作者头像
全栈程序员站长
发布于 2022-02-01 01:42:10
发布于 2022-02-01 01:42:10
27700
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是全栈君。

题意:给定一个n个点的连通的无向图。一个点的“鸽子值”定义为将它从图中删去后连通块的个数。

求“鸽子值”按降序排列的前m个。

思路:事实上题目就是要用来寻找割顶,我们仅仅需找出割顶。然后记录这个割顶属于几个不同连通分量的公共点,不是割点的,去掉之后。图的连通块数为1。

代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

const int MAXN = 10005;

struct node{
    int id, val;
}b[MAXN];

int n, m;
int pre[MAXN], low[MAXN], dfs_clock;
vector<int> g[MAXN];

void init() {
    for (int i = 0; i < n; i++) 
        g[i].clear();
    for (int i = 0; i < n; i++) {
        b[i].id = i; 
        b[i].val = 1;
    }     
}

bool cmp(node a, node b) {
    if (a.val == b.val)
        return a.id < b.id;
    return a.val > b.val;
}

int dfs(int u, int fa) {
    int lowu = pre[u] = ++dfs_clock;
    int child = 0;
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i]; 
        if (!pre[v]) {
            child++;
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
            if (lowv >= pre[u]) {
                b[u].val++;
            }
        } 
        else if (pre[v] < pre[u] && v != fa) {
            lowu = min(lowu, pre[v]);
        }
    }
    if (fa < 0 && child == 1) b[u].val = 1;
    low[u] = lowu;
    return lowu;
}

void find_bcc(int n) {
    memset(pre, 0, sizeof(pre));
    memset(low, 0, sizeof(low));
    dfs_clock = 0;
    for (int i = 0; i < n; i++)
        if (!pre[i])
            dfs(i, -1);
}

int main() {
    while (scanf("%d%d", &n, &m)) {
        if (n == 0 && m == 0)
            break;
        init();
        int u, v;
        while (scanf("%d%d", &u, &v)) {
            if (u == -1 && v == -1)
                break;
            g[u].push_back(v);
            g[v].push_back(u); 
        }

        find_bcc(n);
        sort(b, b + n, cmp);
        for (int i = 0; i < m; i++)
            printf("%d %d\n", b[i].id, b[i].val);
        puts("");
    }     
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115689.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
浅谈欧拉路径
一笔画玩过没有,其实就跟一笔画很相似,就是不重复走路径,走出来的就是欧拉路径,如果起点和终点是同一个点,那么就称为欧拉回路。\
Clare613
2025/07/24
300
P2015 二叉苹果树
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置。下面是一颗有4个树枝的树 2 5 \ / 3 4 \ / 1 现在这颗树枝条太多了,需要剪枝。但是一些树枝上长有苹果。 给定需要保留的树枝数量,求出最多能留住多少苹果。 输入输出格式 输入格式: 第1行2个数,N和Q(1<=Q<= N,1<N<=100)。 N表示树的结点数,Q表示要
attack
2018/04/13
9310
POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
题意:两题一样的。一份代码能交。给定一个连通无向图,问加几条边能使得图变成一个双连通图
全栈程序员站长
2022/07/06
1990
图论--2-SAT--HDOJ/HDU 1824 Let's go home
Problem Description 小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。                         —— 余光中
风骨散人Chiam
2020/10/28
3420
2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Angel_Kitty
2018/04/09
7190
2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2925  Solved: 1927 Description   城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市C的道 路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连 接。这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了。每条道路都有一个分值,分值越小表示
Angel_Kitty
2018/04/09
6740
BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】
最少联通代价(dfs+曼哈顿距离)
现在要把这 2 个连通块连通, 求最少需要把几个’.’转变成’X’。上图的例子中, 最少只需要把 3个’.’转变成’X’。下图用’*’表示转化为’X’的格点。 
Ch_Zaqdt
2019/01/10
9330
图论--2-SAT--POJ 3905 Perfect Election
Perfect Election Time Limit: 5000MS         Memory Limit: 65536K Total Submissions: 964         Accepted: 431 Description
风骨散人Chiam
2020/10/28
4200
图论--2-SAT--POJ Ikki's Story IV - Panda's Trick
liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.
风骨散人Chiam
2020/10/28
3980
BZOJ3083: 遥远的国度(树链剖分)
以下图片来自(https://blog.csdn.net/lcomyn/article/details/45718295)
attack
2018/07/27
3310
BZOJ3083: 遥远的国度(树链剖分)
图论--2-SAT--Tarjan连通分量+拓扑排序O(N+M)模板
#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <stack> #include <algorithm> #define MAXN 2000+10 #define MAXM 400000 #define INF 1000000 using namespace std; vector<int> G[MAXN]; int low[MAXN], dfn[MAXN]; int dfs_clock; in
风骨散人Chiam
2020/10/28
3410
BZOJ2125: 最短路(圆方树)
给一个N个点M条边的连通无向图,满足每条边最多属于一个环,有Q组询问,每次询问两点之间的最短路径。
attack
2019/01/30
3890
图论--双连通分量--点双连通模板
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; const int maxn=1000+10;   int n,m; int bcc_cnt; int dfs_clock;//bcc_cnt计数一共有多少个点-双连通分量 int pre[maxn]; bool iscut[maxn]; int bccno[m
风骨散人Chiam
2020/10/28
6070
图论--2-SAT--详解
现有一个由N个布尔值组成的序列A,给出一些限制关系,比如A[x]AND A[y]=0、A[x] OR A[y] OR A[z]=1等,要确定A[0..N-1]的值,使得其满足所有限制关系。这个称为SAT问题,特别的,若每种限制关系中最多只对两个元素进行限制,则称为2-SAT问题。
风骨散人Chiam
2020/10/28
7640
图论--2-SAT--详解
uva 11324 The Largest Clique(图论-tarjan,动态规划)
Given a directed graph G, consider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Create a directed edge between two vertices u and v in T(G) if and only if there is a path between u and v in G that follows the directed edges only in the forward direction. This graph T(G) is often called the transitive closure of G.
全栈程序员站长
2022/07/06
1820
2020 Multi-University Training Contest 2
给定一个 n 个点的图,每个点有一权值 b ,每次选择 k 个相互连通的点,使其中每个点权值减一,问最少需要多少操作可以使所有的点权值都为 0(每次选择的 k 必须最大)
wenzhuan
2022/08/15
2950
NOI 系列真题练习笔记
NOIP 前开始做做真题,虽然每年都风格迥异,不过感受一下 OI 风格的题目还是有一定意义的。
Clouder0
2022/09/23
9090
LeetCode 周赛上分之旅 #48 一道简单的树上动态规划问题
元素的集合,根据题意逆向遍历数组并从集合中移除元素,当集合为空时表示已经收集到所有元素,返回
用户9995743
2023/10/04
3070
LeetCode 周赛上分之旅 #48 一道简单的树上动态规划问题
J - Welcome Party ZOJ - 4109 【 并查集 + 优先队列 + BFS 】
J - Welcome Party ZOJ - 4109  &:这个就是看着题解搞得了,当时想到了优先队列和 BFS ,没有搞把所有的连起来,但是也没有尝试,当时在做字符串那个题,反正各种原因了。言归正传,题目的意思是,给你 n 个人,n 个人有 m 对关系,对于一个人,如果他上场了,发现没有他认识的人,会产生一个值,也就是 ans ++,否则不会产生,问安排上场顺序让 ans 最小,且让上场序列的字典序最小。 &:题解:将 n 个人的 m 对关系来用并查集连起来,合并的时候只往小了合并,让小的先上
Lokinli
2023/03/09
2320
图论--2-SAT--Ligthoj 1407 Explosion 三元关系枚举
Planet Krypton is about to explode. The inhabitants of this planet have to leave the planet immediately. But the problem is that, still some decisions have to be made - where to go, how to go etc. So, the council of Krypton has invited some of the people to meet in a large hall.
风骨散人Chiam
2020/10/28
3510
相关推荐
浅谈欧拉路径
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档