1013 Battle Over Cities
分数 25
作者 CHEN, Yue
单位 浙江大学
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 city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
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
* 在原图中删除某个点并不好真正的操作,我们可以在dfs函数中传入两个参数,
* 一个当前的访问节点编号,另外一个被删除的结点编号,当当前节点编号等于
* 删除节点编号时则结束递归;
* 不难而知:
* 当删除结点编号不是孤立的点时,被删除结点以后,当前图的连通块数量cnt
* 比原图必定多一,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
* 连通图;
*
* 当删除结点编号是孤立的点时,被删除结点以后,当前图的连通块数量cnt
* 与原图一样多,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
* 连通图;
*
* 综上所述,求出被删除节点以后连通块的数量cnt,cnt-2就是最终答案。
/**
* 在原图中删除某个点并不好真正的操作,我们可以在dfs函数中传入两个参数,
* 一个当前的访问节点编号,另外一个被删除的结点编号,当当前节点编号等于
* 删除节点编号时则结束递归;
* 不难而知:
* 当删除结点编号不是孤立的点时,被删除结点以后,当前图的连通块数量cnt
* 比原图必定多一,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
* 连通图;
*
* 当删除结点编号是孤立的点时,被删除结点以后,当前图的连通块数量cnt
* 与原图一样多,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
* 连通图;
*
* 综上所述,求出被删除节点以后连通块的数量cnt,cnt-2就是最终答案。
*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1010;
vector<int> Adj[N];
bool hs[N];
int chk[N];
int Nv, Ne, k;
void add(int u, int v)
{
Adj[u].push_back(v);
}
void Read()
{
cin >> Nv >> Ne >> k;
for(int i=0; i<Ne; ++i)
{
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
for(int i=0; i<k; ++i)
cin >> chk[i];
}
void FloodFill(int u, int cut)
{
//当前访问节点是被删除结点或者已经被放访问过
if(u == cut || hs[u])
return;
hs[u] = 1; //记录已被访问过
for(auto ele : Adj[u])
if(hs[ele] == 0) //ele未被访问过
FloodFill(ele, cut);
}
int main()
{
Read();
for(int i=0; i<k; ++i)
{
int cnt = 0;
fill(hs, hs+N, 0);
for(int j=1; j<=Nv; ++j)
if(hs[j] == 0)
{
++cnt;
FloodFill(j, chk[i]);
}
cout << cnt-2 << endl;
}
return 0;
}
2)这个是很久之前写的,倒是能看懂,不想写注释了,这个代码现在看来太复杂了。还是前面的DFS更甚一筹。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1010;
vector<int> Adj[N];
int low[N], num[N];
int stk[N];
bool hs[N];
int flag[N];
int id[N], Size[N];
int tim, top, cnt;
int n, m, k;
int root;
void add(int u, int v)
{
Adj[u].push_back(v);
}
void floodfill(int u)
{
hs[u] = 1;
stk[top++] = u;
for(auto v : Adj[u])
if(hs[v] == 0)
floodfill(v);
}
void targan(int u, int fath)
{
num[u] = low[u] = ++tim;
int child = 0;
for(auto v : Adj[u])
{
if(num[v] == 0)
{
++child;
targan(v, u);
low[u] = min(low[u], low[v]);
if(u != root && low[v] >= num[u])
flag[u] = 1;
if(u == root && child > 1)
flag[u] = 1;
}
else if(v != fath)
low[u] = min(low[u], low[v]);
}
}
int main()
{
cin >> n >> m >> k;
while(m--)
{
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
for(int i=1; i<=n; ++i)
if(hs[i] == 0)
{
floodfill(i);
++cnt;
int y;
do{
y = stk[--top];
id[y] = cnt;
Size[cnt]++;
}while(y != i);
}
for(int i=1; i<=n; ++i)
{
root = i;
if(num[i] == 0)
targan(i, -1);
}
while(k--)
{
int chk;
cin >> chk;
if(flag[chk]) cout << cnt << endl;
else if(Size[id[chk]] == 1) cout << cnt - 2 << endl;
else cout << cnt - 1 << endl;
}
return 0;
}