- DFS、BFS
C - Tour
- https://atcoder.jp/contests/abc204/editorial/1991
The republic of AtCoder has N cities numbered 1 through N and Mroads numbered 1 through M.
Road i leads from City A i _i i to City B i _i i, but you cannot use it to get from City B i _i i to City A i _i i.
Puma is planning her journey where she starts at some city, travels along zero or more roads, and finishes at some city.
How many pairs of cities can be the origin and destination of Puma’s journey? We distinguish pairs with the same set of cities in different orders.
Constraints- 2 ≤ leq ≤ N ≤ leq ≤ 2000
- 0 ≤ leq ≤ M ≤ leq ≤ min min min(2000,N(N-1))
- 1 ≤ leq ≤ A i _i i,B i _i i ≤ leq ≤ N
- A i _i i ≠ neq = B i _i i
- (A i _i i,B i _i i) are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A1 B1 ... AM BMOutput
Print the answer.
Sample Input 1
3 3 1 2 2 3 3 2Sample Output 1
7
We have seven pairs of cities that can be the origin and destination: (1,1),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3).
Sample Input 2
3 0Sample Output 2
3
We have three pairs of cities that can be the origin and destination: (1,1),(2,2),(3,3).
Sample Input 3
4 4 1 2 2 3 3 4 4 1Sample Output 3
16
Every pair of cities can be the origin and destination.
题意AtCoder王国有
- N个编号为1到N的城市
- M条编号为1到M的道路。
道路i从A i _i i市通往B i _i i市,但不能能用从B i _i i市通往A i _i i市。
彪马正在计划她的旅程,她从某个城市出发,沿着零条或多条道路旅行,然后在某个城市结束。
求有多少种起点和终点城市组合?
思路- 固定起始点,然后遍历到达可能的城市
- 可以考虑使用DFS、BFS算法遍历
int n; vector小码匠: BFSleaf(n); void dfs(int x, vector > &vvi) { if(leaf[x]) { return; } leaf[x] = true; for(auto a : vvi[x]) { dfs(a, vvi); } } void coder_solution() { // 提升cin、cout效率 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int m, temp = 0; cin >> n >> m; vector > vvi(n); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; vvi[a - 1].push_back(b - 1); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { leaf[j] = false; } dfs(i, vvi); for (int j = 0; j < n; j++) { if (leaf[j]) { temp++; } } } }
void best_solution() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int a, b;
vector> vvi(n);
for(int i = 0; i < m; i++) {
cin >> a >> b;
vvi[a - 1].push_back(b - 1);
}
vector dist(n, -1);
queue que;
dist[0] = 0;
int v;
int j = 0;
for(int i = 0; i < n; i++) {
que.push(i);
dist[i] = 0;
while(!que.empty()) {
v = que.front();
que.pop();
for(auto next_v : vvi[v]) {
if (dist[next_v] != -1) {
continue;
}
dist[next_v] = dist[v] + 1;
que.push(next_v);
}
}
j += n - count(dist.begin(), dist.end(), -1);
dist.assign(n, -1);
}
cout << j;
}
官方题解1:BFS
- 一些语法感觉有些诡异,例如第8行,第14行的循环语句,老码农建议不要学,不符合多数人习惯
#include官方题解2:DFSusing namespace std; int main(){ int n, m; cin >> n >> m; auto e = vector(n, vector ()); for(int i{}, a, b; i < (m); ++i){ cin >> a >> b; --a, --b; e[a].push_back(b); } ll ans{}; for(int s{}; s< (n); ++s){ vector vis(n); vis[s] = true; queue q; q.push(s); while(!q.empty()){ ++ans; int i = q.front(); q.pop(); for(auto&j:e[i]) if(!vis[j]){ vis[j] = true; q.push(j); } } } cout << ans << endl; }
#include复盘using namespace std; const int MAX_N = 2000; vector > G; bool temp[MAX_N]; void dfs(int v) { if (temp[v]) { return; } temp[v] = true; for (auto vv: G[v]){ dfs(vv); } } int main() { int N, M; cin >> N >> M; G.resize(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; G[a - 1].push_back(b - 1); } int ans = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { temp[j] = false; } dfs(i); for (int j = 0; j < N; j++) { if (temp[j]){ ans++; | } } cout << ans; }
-
动态数组初始化
dist.assign(n, -1);



