[poj2117 Electricity]求割点
来源:互联网 发布:网络综合布线投标书 编辑:程序博客网 时间:2024/05/21 09:00
Electricity
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 4055 Accepted: 1366
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 small area that surrounds it. This organization brings a lot of problems - it often happens that there is not enough power in one area, while there is a large surplus in the rest of the country.
ACM++ has therefore decided to connect the networks of some of the plants together. At least in the first stage, there is no need to connect all plants to a single network, but on the other hand it may pay up to create redundant connections on critical places - i.e. the network may contain cycles. Various plans for the connections were proposed, and the complicated phase of evaluation of them has begun.
One of the criteria that has to be taken into account is the reliability of the created network. To evaluate it, we assume that the worst event that can happen is a malfunction in one of the joining points at the power plants, which might cause the network to split into several parts. While each of these parts could still work, each of them would have to cope with the problems, so it is essential to minimize the number of parts into which the network will split due to removal of one of the joining points.
Your task is to write a software that would help evaluating this risk. Your program is given a description of the network, and it should determine the maximum number of non-connected parts from that the network may consist after removal of one of the joining points (not counting the removed joining point itself).
ACM++ has therefore decided to connect the networks of some of the plants together. At least in the first stage, there is no need to connect all plants to a single network, but on the other hand it may pay up to create redundant connections on critical places - i.e. the network may contain cycles. Various plans for the connections were proposed, and the complicated phase of evaluation of them has begun.
One of the criteria that has to be taken into account is the reliability of the created network. To evaluate it, we assume that the worst event that can happen is a malfunction in one of the joining points at the power plants, which might cause the network to split into several parts. While each of these parts could still work, each of them would have to cope with the problems, so it is essential to minimize the number of parts into which the network will split due to removal of one of the joining points.
Your task is to write a software that would help evaluating this risk. Your program is given a description of the network, and it should determine the maximum number of non-connected parts from that the network may consist after removal of one of the joining points (not counting the removed joining point itself).
Input
The input consists of several instances.
The first line of each instance contains two integers 1 <= P <= 10 000 and C >= 0 separated by a single space. P is the number of power plants. The power plants have assigned integers between 0 and P - 1. C is the number of connections. The following C lines of the instance describe the connections. Each of the lines contains two integers 0 <= p1, p2 < P separated by a single space, meaning that plants with numbers p1 and p2 are connected. Each connection is described exactly once and there is at most one connection between every two plants.
The instances follow each other immediately, without any separator. The input is terminated by a line containing two zeros.
The first line of each instance contains two integers 1 <= P <= 10 000 and C >= 0 separated by a single space. P is the number of power plants. The power plants have assigned integers between 0 and P - 1. C is the number of connections. The following C lines of the instance describe the connections. Each of the lines contains two integers 0 <= p1, p2 < P separated by a single space, meaning that plants with numbers p1 and p2 are connected. Each connection is described exactly once and there is at most one connection between every two plants.
The instances follow each other immediately, without any separator. The input is terminated by a line containing two zeros.
Output
The output consists of several lines. The i-th line of the output corresponds to the i-th input instance. Each line of the output consists of a single integer C. C is the maximum number of the connected parts of the network that can be obtained by removing one of the joining points at power plants in the instance.
Sample Input
3 30 10 22 14 20 12 33 11 00 0
Sample Output
122
Source
CTU Open 2004
题意给一个无向图(不一定是连通图),问你删除某一个结点至多可以把整个图分成多少个连通分量。
算法如下,对每一个连通块,跑一次tarjan,判断其中是否有割点,如果没有割点,那我删除其中任意一个点都只会发生:如果这个连通块只有一个点,那么就剩下0块,否则删除其中任意一个点剩下的点依然都是连通的,因为没有割点,答案为1.
然后假设整个图原来有block块,其中某一个连通块跑tarjan更新到的答案为sum,那么最后我们要得到最多的连通块,自然是其它连通块不动,把这个连通块可以删除某个点使得这一块被分成sum份的那个点删除,那么整个图就分成了(block-1)+(sum)块了,此即答案。
代码:
题意给一个无向图(不一定是连通图),问你删除某一个结点至多可以把整个图分成多少个连通分量。
算法如下,对每一个连通块,跑一次tarjan,判断其中是否有割点,如果没有割点,那我删除其中任意一个点都只会发生:如果这个连通块只有一个点,那么就剩下0块,否则删除其中任意一个点剩下的点依然都是连通的,因为没有割点,答案为1.
然后假设整个图原来有block块,其中某一个连通块跑tarjan更新到的答案为sum,那么最后我们要得到最多的连通块,自然是其它连通块不动,把这个连通块可以删除某个点使得这一块被分成sum份的那个点删除,那么整个图就分成了(block-1)+(sum)块了,此即答案。
代码:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int MAX = 10007;vector<int> G[MAX];bool vis[MAX];int dfn[MAX], low[MAX];int root, sonOfRoot;int cut, id;void dfs(int u) {vis[u] = true;dfn[u] = low[u] = ++id;int son = 0;for (int i = 0; i < G[u].size(); ++i) {int v = G[u][i];if (vis[v]) {low[u] = min(low[u], dfn[v]);} else {dfs(v);low[u] = min(low[u], low[v]);if (u == root) ++sonOfRoot;else if (low[v] >= dfn[u]) ++son;}}if (u != root) cut = max(cut, son + 1);}int tarjan(int u) {if (G[u].size() == 0) return 0;root = u;sonOfRoot = id = cut = 0;dfs(u);if (sonOfRoot >= 2) cut = max(cut, sonOfRoot);//printf("cut = %d\n", cut);return cut;}int read() {char ch;while ((ch = getchar()) < '0' || ch > '9');int x = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + ch - '0';}return x;}int main() {int p, c;while (~scanf(" %d %d", &p, &c) && p) {for (int i = 0; i < p; ++i) G[i].clear();int u, v;while (c--) {u = read();v = read();//scanf(" %d %d", &u, &v);G[u].push_back(v);G[v].push_back(u);}memset(vis, false, sizeof(vis));int block = 0;int sum = 0;for (int i = 0; i < p; ++i) {if (!vis[i]) {++block;sum = max(sum, tarjan(i));}}printf("%d\n", sum + block - 1);}return 0;}
0 0
- [poj2117 Electricity]求割点
- POJ2117--Electricity
- POJ2117-Electricity
- 【POJ2117】Electricity-点双连通分量
- POJ2117.Electricity——无向图的割点
- Electricity
- poj 2117 Electricity(Tarjan求割点)
- poj2117 tarjan()+割点
- ZOJ2697:Electricity
- poj2117求割点后最多的块。
- Electricity(简单)
- live achieve 4214 - Electricity
- POJ 2117 Electricity
- POJ 2117 Electricity
- poj 2117 Electricity
- Hoj 1789 Electricity
- poj 2117 Electricity(求割项)
- poj 2117 Electricity
- 在非Activity類下使用SharedPreferences
- fibonacci数列(二)
- iOS--UIProgressView的使用方法详细
- iOS--UIStepper的使用方法详细
- js事件列表
- [poj2117 Electricity]求割点
- iOS--UISegmentedControl的使用方法详细
- 柔性数组成员
- iOS--UIAlertView的使用方法详细
- Android 服务
- Unity与Android交互
- iOS--UIActivityIndicatorView的使用方法详细
- shell 中的 sed 命令
- Princeton Algorithms: Part 2 [week 3: Radix Sorts]