
C - Tour

Input is given from Standard Input in the following format:
N M
A1 B1
...
AM BM
Print the answer.
3 3
1 2
2 3
3 2
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).
3 0
3
We have three pairs of cities that can be the origin and destination: (1,1),(2,2),(3,3).
4 4
1 2
2 3
3 4
4 1
16
Every pair of cities can be the origin and destination.
AtCoder王国有
道路i从A_i 市通往B_i 市,但不能能用从B_i 市通往A_i 市。
彪马正在计划她的旅程,她从某个城市出发,沿着零条或多条道路旅行,然后在某个城市结束。
求有多少种起点和终点城市组合?
int n;
vector<bool>leaf(n);
void dfs(int x, vector<vector<int>> &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<vector<int>> 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<vector<int>> vvi(n);
for(int i = 0; i < m; i++) {
cin >> a >> b;
vvi[a - 1].push_back(b - 1);
}
vector<int> dist(n, -1);
queue<int> 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;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
auto e = vector(n, vector<int>());
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<bool> vis(n);
vis[s] = true;
queue<int> 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<bits/stdc++.h>
using namespace std;
const int MAX_N = 2000;
vector<vector<int>> 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);