(有任何问题欢迎留言或私聊

题目链接:CSU2104

题面:2017年南太平洋某区域赛题

  • The Suitably Protected Programming Contest (SPPC) is a multi-site contest in which contestants compete at specified sites, which are linked by the Notorious Broken Network (NBN) of two-way links between some pairs of individual sites. There is at most one link directly between any pair of sites and there is no link from a site to itself. Contestants submit their solutions to a judging server to get them tested. The event will use one or more judging servers. Each judging server is located at a contest site and may be accessed directly from that site or through a sequence of linked sites. At any time during the contest, each contestant must have access to at least one judging server. The links between sites are set up in such a way that if all links work properly, it would suffice to operate one judging server. At the other extreme, having each site operate its own judging server would guarantee access even if all of the links failed. The organisers wish to minimise the number of judging servers that they operate, while still maintaining the integrity of the contest. Examination of the NBN’s performance over time reveals some good news. The NBN is reliably unreliable! At any given time, exactly one link is broken (that is, communications cannot travel in either direction on that link). Given this fact, the organisers want you to work out the minimum number of judging servers that must operate so that no matter which link is broken, every contestant still has access to at least one judging server. As they know that they must operate at least one judging server, they ask you to tell them how many extra judging servers they must operate.

题意:

我把题意换一种说法解释:
 n个点看成n个矿洞,m条无向边链接两个矿洞。在某些矿洞设置安全出口,这些边中的某一条有毁坏的可能,为了保证矿洞里所有工人的安全,使得无论哪一条边毁坏所有的工人都能逃到安全出口。求设置的安全出口的最小数量cnt。答案输出cnt-1。

类似题目:

P3225 [HNOI2012]矿场搭建
P2746 [USACO5.3]校园网Network of Schools

思路:

1.显然只有桥毁坏才能改变图的连通性,所以只有断桥才有意义。
2.对于无向图而言,桥连接的是两个边双联通分量(不懂双联通分量的点这里)。将边双联通分量缩点后变成一个简单图。
(以下所说的点全部指的是缩点之后的点)
3.但是 是不是缩点后所有的点都要设置一个安全出口呢?
4.显然不是。记住,题目说的是只断一条边。
5.如果一个点的度大于1,它就没有设置出口的必要,原因如下:
6.因为它的度数大于1,而断一条边最多改变这个点和另外一个点的连通性。这时,它可以通过其他边走到出口。
7.所以度数大于1的点没有设置出口的必要。
8.综上:求出缩点后度数<=1的点的数量cnt,输出cnt-1即可。

注意:

 题目没有保证图一定联通,而一般网上有两种tarjan缩点的写法,我下面提供的第二种写法处理图不连通的情况会有问题。第一种写法可以处理图不连通的情况。
 然后,csuoj上的数据没有涉及图不连通的情况。比如单独一个点的情况。下面代码中有简单解释到。。。。
我提供一组数据:

4
0 0 0 0

(这题一直没过,后来才发现是把vis[v]写成了vis[i],难受啊,马飞

AC代码1:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<stack>
#include<cstring>
#include<cassert>
using namespace std;
const int N = 100005;
int head[N],vis[N];
int n,tot,inde;
int dfn[N],low[N];
int qltNum;
int qltMap[N];
int du[N];
int st[N*2],top;
struct lp{int u,to,pre;bool is;
}cw[N*2];
void addedge(int x,int y){cw[++tot].to=y;cw[tot].pre=head[x];head[x]=tot;cw[tot].is=0;cw[tot].u=x;
}
void dfs(int u,int Fa){dfn[u] = low[u] = ++inde;vis[u]=1;int v;st[++top]=u;for(int i=head[u];~i;i=cw[i].pre){v=cw[i].to;if(v==Fa)continue;if(!vis[v]){dfs(v,u);low[u]=min(low[u],low[v]);//if(low[v] > dfn[u]) bridge}else if(vis[v]==1){low[u]=min(low[u],dfn[v]);}}if(low[u]==dfn[u]){//就算只有单独一个点,联通块的个数还是无误的计算到了qltNum++;do{v=st[top--];vis[v]=2;qltMap[v]=qltNum;}while(v!=u);}
}
void tarjan(){for(int i=1;i<=n;++i){if(!vis[i]){dfs(i,-1);}}
}
void init(){qltNum=inde=top=0;tot=-1;memset(vis,0,sizeof(vis));memset(head,-1,sizeof(head));memset(qltMap,0,sizeof(qltMap));memset(du,0,sizeof(du));
}
int main(){while(~scanf("%d",&n)){init();for(int i=1;i<=n;++i){int x,y;scanf("%d",&x);while(x--){scanf("%d",&y);y++;addedge(i,y);addedge(y,i);}}tarjan();for(int i=0;i<=tot;i+=2){int a=cw[i].u,b=cw[i].to;if(qltMap[a]!=qltMap[b]){du[qltMap[a]]++;du[qltMap[b]]++;}}//printf("%d\n",qltNum);int cc=0;for(int i=1;i<=qltNum;++i){//printf("*%d\n",du[i] );if(du[i]<=1)cc++;}printf("%d\n",max(cc-1,0));}return 0;
}

AC代码2:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<stack>
#include<cstring>
using namespace std;
const int N = 100005;
int head[N],vis[N];
int n,tot,inde;
int dfn[N],low[N];
int qltNum;
int qltMap[N];
int du[N];
int st[N*2],top;
struct lp{int u,to,pre;bool is;
}cw[N*2];
void addedge(int x,int y){cw[++tot].to=y;cw[tot].pre=head[x];head[x]=tot;cw[tot].is=0;cw[tot].u=x;
}
void dfs(int u,int Fa){dfn[u] = low[u] = ++inde;vis[u]=1;int v;st[++top]=u;for(int i=head[u];~i;i=cw[i].pre){if(cw[i].is)continue;v=cw[i].to;if(v==Fa)continue;if(!vis[v]){cw[i].is=cw[i^1].is=1;dfs(v,u);low[u]=min(low[u],low[v]);if(low[v]>dfn[u]){//对于只有单独一个点的情况,这个for循环根本不会跑,qltNum就不会自加qltNum++;do{qltMap[st[top]]=qltNum;}while(st[top--]!=v);}}else if(vis[v]==1){low[u]=min(low[u],dfn[v]);}}
}
void tarjan(){for(int i=1;i<=n;++i){if(!vis[i]){dfs(i,-1);}}
}
void init(){qltNum=inde=top=0;tot=-1;memset(vis,0,sizeof(vis));memset(head,-1,sizeof(head));memset(qltMap,0,sizeof(qltMap));memset(du,0,sizeof(du));
}
int main(){while(~scanf("%d",&n)){init();for(int i=1;i<=n;++i){int x,y;scanf("%d",&x);while(x--){scanf("%d",&y);y++;addedge(i,y);addedge(y,i);}}tarjan();for(int i=0;i<=tot;i+=2){int a=cw[i].u,b=cw[i].to;if(qltMap[a]!=qltMap[b]){du[qltMap[a]]++;du[qltMap[b]]++;}}//printf("%d\n",qltNum);int cc=0;for(int i=0;i<=qltNum;++i){//printf("*%d\n",du[i] );if(du[i]<=1)cc++;}printf("%d\n",max(cc-1,0));}return 0;
}

CSU2104: Extra Judicial Operation-Tarjan边双联通分量缩点两种方法-难受的bug相关推荐

  1. POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)

    [题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...

  2. python3+matplotlib绘制双轴折线图(两种方法)

    Background 这里提供两种方法,一种是基于pandas,另一种是基于twinx. 1.先看最终效果图 pandas twinx 2.源码 import pandas as pd import ...

  3. 【BZOJ2730】【codevs1996】矿场建设,点双联通分量

    传送门1 传送门2 思路: 前段时间学习的tarjan求双联通分量 练习的时候碰到了这样一道蛋疼的题 基本思路还是显而易见的,考虑分割联通快及计算大小来求出答案 由于之前写的都是边双联通分量,可以把无 ...

  4. 点双联通分量,圆方树和广义圆方树

    点双联通分量 边双联通分量想必看这篇博客的同学就会,并且边双联通分量理解和打起来比较简单,就不再赘述了. 点双联通分量,类比边双的定义,它是原图的极大无向子图,满足删去子图中任意一个节点以及与其相邻的 ...

  5. 『Tarjan算法 无向图的双联通分量』

    无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...

  6. 【9.22校内测试】【可持久化并查集(主席树实现)】【DP】【点双联通分量/割点】...

    1 build 1.1 Description 从前有一个王国,里面有n 座城市,一开始两两不连通.现在国王将进行m 次命令,命令可 能有两种,一种是在u 和v 之间修建道路,另一种是询问在第u 次命 ...

  7. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

  8. POJ-3352-RoadConstruction(边双联通分量,缩点)

    链接:https://vjudge.net/problem/POJ-3352#author=0 题意: 给一个无向连通图,至少添加几条边使得去掉图中任意一条边不改变图的连通性(即使得它变为边双连通图) ...

  9. 10.31T2 点双联通分量+预处理前缀+二分答案

    2.逛公园I  (parka) [问题描述] 琥珀色黄昏像糖在很美的远方,思念跟影子在傍晚一起被拉长--       小 B 带着 GF 去逛公园,公园一共有 n 个景点,标号为 1 . . . n. ...

最新文章

  1. MATLAB_图形学_形态学课程II
  2. 世界各大天文台联合预警:今晚公布“引力波重要发现”
  3. 利用Matlab设计滤波器(FDAT)
  4. 玩转springboot:实现springboot自定义拦截器
  5. P1115 最大子段和
  6. LeetCode 多线程 1116. 打印零与奇偶数
  7. 利用 WebService实现远程服务器文件的上传和下载
  8. 如何离线安装chrome插件
  9. host 'xx' is not allowed to connect to this MySql server
  10. 经济学原理 下载 曼昆_2021南开经济学考研全年规划
  11. android 图片剪切组件,Android 图片裁剪库 uCrop
  12. mac建立sftp连接_【5分钟玩转Lighthouse】Win10远程连接同步代码
  13. Win7和Win10共享出现没有权限使用网络资源的解决步骤
  14. 数据科学Python库01:Pandas
  15. ORA-20011 ORA-29913 KUP-11024问题处理
  16. 森林中的兔子java
  17. 【EXLIBRIS】墙上的坏人
  18. 解决stm32下载错误 “Could not stop Cortex-M device.Please check the JTAG cable.“
  19. C语言入门(四):有关逻辑的运算符和表达式
  20. qt 设置背景图片方法

热门文章

  1. 物联网设备数据流转之数据何时存储:Spring事件及监听机制, 数据入库
  2. SPDK+NVMe SSD对接Virtio支撑红包场景性能
  3. android Stopwatch实例
  4. StopWatch秒表的使用
  5. 心理测试软件测评报告,心理测评软件的基本作用
  6. 九阴真经Ambari——1.熟悉Hortonworks官网结构并找到Ambari下载地址
  7. 实验室-关于老铁整一个社会语录api与网抑云热评api(并引入百度语音tts)
  8. 天融信防火墙重置配置_天融信防火墙认证配置
  9. 七个不可思议事件谜题
  10. 计算机应用基础第三版练习题答案,计算机应用基础练习题答案