http://poj.org/problem?id=2117

题意:
求删除图中任意一个顶点后的最大连通分量数。

思路:

求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的。

这道题目我wa了很多发,主要是我忘了根结点的连通分量数得减1。

为什么呢?因为如果我们用cut[]来记录每个结点对应的连通分量数的话,最后的答案还需要加1。

比如2结点,我们计算所得的cut[2]=3,因为它只计算了它的子树的情况,但是父亲结点并没有计算进去,所以最后需要+1,这个1也就是父亲结点方向也会产生一个连通分量,这是肯定的,因为父亲结点方向就这么一条边。那根结点是没有父亲结点的,所以根结点需要-1。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int tot;
int sum;
int ans;
int dfs_clock;
int iscut[maxn];
int pre[maxn];
int head[maxn]; struct node
{
int v;
int next;
}e[*maxn]; void addEdge(int u, int v)
{
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
} int dfs(int u, int fa)
{
int lowu=pre[u]=++dfs_clock;
int child=;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(!pre[v])
{
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u]) iscut[u]++;
}
else if(pre[v]<pre[u] && v!=fa)
{
lowu=min(lowu,pre[v]);
}
}
if(fa< && child>=) iscut[u]--; //这儿很重要,根结点必须-1
return lowu;
} void solve()
{
sum=;
ans=;
memset(pre,,sizeof(pre));
memset(iscut,,sizeof(iscut));
for(int i=;i<n;i++)
{
if(!pre[i])
{
sum++;
dfs_clock=;
dfs(i,-);
}
ans=max(ans,iscut[i]);
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(n== && m==) break;
if(m==) {printf("%d\n",n-);continue;}
tot=;
memset(head,-,sizeof(head));
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
}
solve();
printf("%d\n",sum+ans);
}
return ;
}

POJ 2117 Electricity(割点求连通分量)的更多相关文章

  1. poj 2117 Electricity(tarjan求割点删掉之后的连通块数)

    题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根 ...

  2. POJ 1523 SPF (割点,连通分量)

    题意:给出一个网络(不一定连通),求所有的割点,以及割点可以切分出多少个连通分量. 思路:很多种情况. (1)如果给的图已经不是连通图,直接“  No SPF nodes”. (2)求所有割点应该不难 ...

  3. POJ 2117 Electricity 双联通分量 割点

    http://poj.org/problem?id=2117 这个妹妹我竟然到现在才见过,我真是太菜了~~~ 求去掉一个点后图中最多有多少个连通块.(原图可以本身就有多个连通块) 首先设点i去掉后它的 ...

  4. poj 2117 Electricity【点双连通求删除点后最多的bcc数】

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4727   Accepted: 1561 Descr ...

  5. POJ—— 2117 Electricity

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5620   Accepted: 1838 Descr ...

  6. poj 2117 Electricity

    /* Tarjan求割点 */ #include<iostream> #include<cstdio> #include<cstring> #include< ...

  7. poj 2117 去掉割点可以分得的联通图的个数模板

    #include<stdio.h> #include<string.h> #define N 11000 /* 去掉一个割点后,询问可以分得的联通图的个数 */ struct ...

  8. Electricity POJ - 2117 + SPF POJ - 1523 去除割点后求强连通分量个数问题

    Electricity POJ - 2117 题目描述 Blackouts and Dark Nights (also known as ACM++) is a company that provid ...

  9. POJ 2117 (割点+连通分量)

    题目链接: http://poj.org/problem?id=2117 题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大. 解题思路: 一个大陷阱,m可以等于0,这 ...

随机推荐

  1. OC convertRect

    举个例子: redView = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; redView.backgroundColor ...

  2. spring boot 中用@value给static变量赋值

    需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...

  3. PAT 1034 Head of a Gang[难][dfs]

    1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...

  4. mysql 简单主从

    主服务器master [root@localhost ~]# vim /etc/my.cnf [mysqld] log-bin=mysql-bin #必须开启log-bin server-id=129 ...

  5. [LeetCode] 286. Walls and Gates_Medium tag: BFS

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

  6. [LeetCode] 438. Find All Anagrams in a String_Easy

    438. Find All Anagrams in a String DescriptionHintsSubmissionsDiscussSolution   Pick One Given a str ...

  7. iOS 网易彩票-3常见设置

    Navigation导航设置 为了统一管理导航控制器,需要自定义导航控制器MJNavigationController,继承于UINavigationController.分别设置5个Navigati ...

  8. git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令)

    A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓库 3)打开终端输入命令 cd到你的本地项目根目录下,执行如下git命令 git in ...

  9. win10安装激活与关闭自动更新

    1.安装时即使选择了不保留任何文件,也不会删除掉非c盘里的东西 2.安装后需要执行KMS10_Crack2激活下 3.关闭自动更新. 在服务中禁用 https://zhidao.baidu.com/q ...

  10. Linux命令: 编辑模式移动光标

    敲命令按以下顺序 ①vim filename ②e ③i ④ESC 移动光标 0 (零):将光标移动到行的起始处. $:将光标移动到行的末尾处. H:将光标移到当前窗口(而非全文)的第一行起始处. M ...