You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.5 KiB

#include <bits/stdc++.h>
using namespace std;
/*
输入:
5 4
1 2
2 3
3 4
4 5
输出:
1
4
5
-------
输入:
7 5
1 2
2 3
3 4
4 5
6 7
输出:
2
4
7
*/
const int N = 10005;
const int M = 100005;
int st[N], belong[N];
int edgeCnt, pointCnt;
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void dfs(int u, int id) { // 哪个节点,属于哪个连通块
st[u] = 1; // 此节点已访问过
belong[u] = id; // 记录从属关系
pointCnt++; // 多访问了一个点
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
edgeCnt++; // 多了一条边
if (!st[v]) dfs(v, id); // 如果此点没有访问过,走之
}
}
int main() {
freopen("LianTongKuai.in", "r", stdin);
// freopen(".out", "w", stdout);
int n, m;
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
int id = 0;
for (int i = 1; i <= n; i++) {
if (!st[i]) {
id++; // 连通块数量
dfs(i, id); // 遍历这个连通块
}
}
cout << id << endl; // 连通块个数
cout << edgeCnt / 2 << endl; // 边的数量
cout << pointCnt << endl; // 点的数量
return 0;
}