用DFS做的。
一发ac,然而在本地调了蛮久。
刚开始直接删边(完全忘记了后面的k个是互相独立的,都是仅删去1个节点和连边
233333),在dfs和is_conn()做点小改变就好啦2333333
1013 Battle Over Cities
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1010;
int n, m, kk;
bool visited[N];
vector<int> graph[N];
void dfs(int curr, int del) {
visited[curr] = true;
for (int i = 0; i < graph[curr].size(); ++i) {
int neighbor = graph[curr][i];
if (!visited[neighbor] && neighbor != del)
dfs(neighbor,del);
}
}
int isConnected(int del) {
for (int i = 1; i <= n; ++i) {
if (i == del) continue;
else if (visited[i]) continue;
else return i;
}
return N;
}
int main() {
scanf("%d%d%d", &n, &m, &kk);
int u, v;
for (int i = 0; i < m; ++i) {
scanf("%d%d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
int delc;
vector<int>::iterator it;
for (int j = 0; j < kk; ++j) {
scanf("%d", &delc);
memset(visited, false, sizeof(visited));
if (n == 1) {
puts("0");
continue;
}
if (delc == 1) {
dfs(2,delc);
} else dfs(1,delc);
for (int k = 0; k <= n; k++) {
int not_conn = isConnected(delc);
if (not_conn > n) {
printf("%d\n", k);
break;
} else {
dfs(not_conn,delc);
}
}
}
return 0;
}
另外提一下,map、vector删除(改变size)用erase(iterator++)
vector remove() vs erase()
网友评论