POJ——T2117 Electricity

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

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5459   Accepted: 1788

当m==0 时,特判输出 n-1~~~

先求出原始的连通分量,

然后只有删割点才会增加连通分量,枚举每个割点

最后加上原始的

  1 #include <algorithm>
  2 #include <cstring>
  3 #include <cstdio>
  4
  5 using namespace std;
  6
  7 const int N(100015);
  8 int n,m,u,v;
  9 int sumedge,head[N<<1];
 10 struct Edge
 11 {
 12     int to,next;
 13     Edge(int to=0,int next=0) :
 14         to(to),next(next) {}
 15 }edge[N*5];
 16
 17 void ins(int from,int to)
 18 {
 19     edge[++sumedge]=Edge(to,head[from]);
 20     head[from]=sumedge;
 21 }
 22
 23 int low[N<<1],dfn[N<<1],tim;
 24 int sumcol,num[N];
 25 int cutpoint[N];
 26
 27 void DFS(int now,int pre)
 28 {
 29     low[now]=dfn[now]=++tim;
 30     int sumtredge=0,if_cutpoint=0;
 31     for(int i=head[now];i!=-1;i=edge[i].next)
 32     {
 33         int go=edge[i].to;
 34         if((go^1)!=pre)
 35         {
 36             if(!dfn[go])
 37             {
 38                 sumtredge++;
 39                 DFS(go,i);
 40                 if(low[go]>=dfn[now])
 41                 {
 42                     if_cutpoint=1;
 43                     num[now]++;
 44                 }
 45
 46                 low[now]=min(low[now],low[go]);
 47             }
 48             else low[now]=min(low[now],dfn[go]);
 49         }
 50     }
 51     if(pre==-1)
 52     {
 53         if(sumtredge>1)  cutpoint[now]=1;
 54     }
 55     else if(if_cutpoint) cutpoint[now]=1;
 56 }
 57
 58 int ans,cnt,tmp,vis[N],root[N],cut[N];
 59
 60 void link(int x)
 61 {
 62     vis[x]=1;
 63     for(int i=head[x];i!=-1;i=edge[i].next)
 64     {
 65         v=edge[i].to;
 66         if(!vis[v]) link(v);
 67     }
 68 }
 69
 70 void cut_point(int pre,int fa)
 71 {
 72     vis[pre]=1;
 73     for(int i=head[pre];i!=-1;i=edge[i].next)
 74     {
 75         int go=edge[i].to;
 76         if(!vis[go])
 77         {
 78             if(pre==fa) cnt++;
 79             cut_point(go,fa);
 80         }
 81     }
 82 }
 83
 84 void init(int n)
 85 {
 86     sumedge=-1; ans=tim=tmp=cnt=0;
 87     memset(vis,0,sizeof(vis));
 88     memset(low,0,sizeof(low));
 89     memset(dfn,0,sizeof(dfn));
 90     memset(num,0,sizeof(num));
 91     memset(cut,0,sizeof(cut));
 92     memset(root,0,sizeof(root));
 93     memset(head,-1,sizeof(head));
 94     memset(cutpoint,0,sizeof(cutpoint));
 95 }
 96
 97 int main()
 98 {
 99 //    freopen("makerout.txt","r",stdin);
100 //    freopen("myout.txt","w",stdout);
101
102     while(~scanf("%d%d",&n,&m)&&n)
103     {
104         init(n);
105         if(m==0) printf("%d\n",n-1);
106         else
107         {
108             for(;m;m--)
109             {
110                 scanf("%d%d",&u,&v);
111                 ins(u,v); ins(v,u);
112             }
113             for(int i=0;i<n;i++)
114                 if(!vis[i]) ++tmp,link(i);
115             for(int i=0;i<n;i++)
116                 if(!dfn[i]) DFS(i,-1);
117             for(int i=0;i<n;i++)
118                 if(cutpoint[i])
119                 {
120                     memset(vis,0,sizeof(vis));
121                     cut_point(i,i);
122                     ans=max(ans,cnt-1);
123                     cnt=0;
124                 }
125             ans+=tmp;
126             printf("%d\n",ans);
127         }
128     }
129     return 0;
130 }
时间: 2024-07-30 10:20:55

POJ——T2117 Electricity的相关文章

poj 2117 Electricity 【无向图求割点】【求去掉一个点后 图中最多的BCC数目】

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4597   Accepted: 1515 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup

POJ 2117 Electricity

Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5573   Accepted: 1818 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them supplying a sma

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

题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根节点删掉之后++ #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 1e4 + 10; const int M = 1e6 + 10; st

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

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4727   Accepted: 1561 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup

无向图求割顶与桥

无向图求割顶与桥 对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶.对于连通图,割顶就是删除之后使图不再连通的点.如果删除边(u,v)一条边,就可以让连通图变成不连通的,那么边(u,v)是桥. 具体的概念和定义比较多,在刘汝佳<<训练指南>>P312-314页都有详细的介绍. 下面来写求无向图割顶和桥的DFS函数.我们令pre[i]表示第一次访问i点的时间戳,令low[i]表示i节点及其后代所能连回(通过反向边)的最早祖先的pre值. 下面的dfs函数返回

【POJ】2117 Electricity

无向图求割点和连通块. 1 /* POJ2117 */ 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 #include <cstdlib> 8 using namespace std; 9 10 #define MAXN 10005 11 12 vector<i

POJ2117 Electricity

Electricity poj上的一道割点的题. 题目大意就是求删去一点后,形成的联通块的最大值. 思路: 先求该图的割点. 如果删除的点是割点的话,需要分类讨论: 1.这个点是割点并且是根节点,那么增加的联通块就是树中的子节点数-1. 2.如果是割点但不是根节点,增加的联通块数就是搜索树中满足low[v]>=dfn[u]的子节点个数. 如果这个图没有割点,那就直接输出联通块的数量. 总结的话,就是 ans =( 没有去除顶点u时的联通分支 - 1 )  + 去除顶点后新增的分支数 . 代码:

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)