1013 Battle Over Cities (25 分)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city
1
-city
2
and city
1
-city
3
. Then if city
1
is occupied by the enemy, we must have 1 highway repaired, that is the highway city
2
-city
3
.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.
Output Specification:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input:
3 2 3
1 2
1 3
1 2 3
Sample Output:
1
0
0
一开始想用很朴素的想法,每读到一条边,将较小顶点的压到较大顶点为下标的数组里面。此时相当于将图存起来了。每遇到一个要检查的顶点,到其他顶点的下标数组里找。若该数组里面没有储存其他顶点,则连通分量加一,或者该数组里只有一个顶点,且该顶点恰好为要查找的这个顶点,连通分量也增加一。遍历所有顶点,即可获取当前总的连通分量。当前连通分量的总和减一即为需要修的路数。
提交结果如下:
测试点1 无法通过,经检查发现,这种方法无法解决顶点1 在环里的情形,暂时没有找到可以破解的方法。
#include <iostream>
#include <vector>
using namespace std;
class Graph {
public:
Graph(int);
~Graph();
void InsertEdge(int);
void Check(int);
private:
vector<vector<int>> AdjMatrix;
int VertexNum;
};
Graph::Graph(int VN)
{
VertexNum = VN + 1;
AdjMatrix.resize(VertexNum);
}
Graph::~Graph()
{
for (int i = 0; i < VertexNum; ++i)
AdjMatrix[i].clear();
AdjMatrix.clear();
VertexNum = 0;
}
void Graph::InsertEdge(int EN)
{
int V1, V2,Temp;
vector<bool> ConnectTo1(VertexNum, false);
ConnectTo1[1] = true;
for (int i = 0; i < EN; ++i) {
cin >> V1 >> V2;
if (V1 > V2) {
Temp = V1; V1 = V2; V2 = Temp;
}
AdjMatrix[V2].push_back(V1);
}
}
void Graph::Check(int ToCheckNum)
{
int Vertex,Count;
for (int k = 0; k < ToCheckNum; ++k) {
cin >> Vertex;
if (VertexNum == 2) {
cout << "0"; return;
}
Count = 0;
for (int i = 1; i < VertexNum; ++i) {
if (i == Vertex) continue;
if (AdjMatrix[i].size() == 0 ||\
(AdjMatrix[i].size() == 1 && AdjMatrix[i][0] == Vertex)) Count++;
}
cout << Count - 1 << endl;
}
}
int main(void)
{
// freopen("input5.txt", "r", stdin);
int VertexNum, EdgeNum, ToCheckNum;
cin >> VertexNum >> EdgeNum >> ToCheckNum;
Graph G(VertexNum);
G.InsertEdge(EdgeNum);
G.Check(ToCheckNum);
// fclose(stdin);
system("pause");
return 0;
}
以下是更改后使用BFS版本,算法最后一个节点会超时
void Graph :: BFS_Check(int ToCheckNum)
{
/*
-------------------------------------------------------------------
for(每一个要查找的节点 C){
初始化集合数组Belong[] = 0;
初始化已浏览数组Visited[] = false;
count =0;
for(每一个节点V){
若V == C 或者已经属于某个集合了或者节点已访问 continue;
V 入队列Q,
Belong[V] = ++count;
while(Q非空){
Q.pop();
Visited[V] = true;
V的每一个邻接点W入列;
Delong[W] = count;
}
}
}
-------------------------------------------------------------------
*/
int Count = 0,Vertex,i,j,k,Temp;
queue<int> Q;
for (i = 0; i < ToCheckNum; ++i) {
vector<bool> Visited(VertexNum, false);
vector<int> Belong(VertexNum, 0);
// cin >> Vertex;
scanf("%d", &Vertex);
Count = 0;
Visited[Vertex] = true;
for (j = 1; j < VertexNum; ++j) {
if (j == Vertex || Belong[j] || Visited[j]) continue;
Q.push(j);
Belong[j] = ++Count;
while (!Q.empty()) {
Temp = Q.front();
Q.pop();
Visited[Temp] = true;
for (k = 0; k < AdjMatrix[Temp].size(); ++k) {
if (Visited[AdjMatrix[Temp][k]]) continue;
Q.push(AdjMatrix[Temp][k]);
Belong[AdjMatrix[Temp][k]] = Count;
}
}
}
//cout << Count - 1 << endl;
printf("%d\n", Count - 1);
}
}