noip模拟8
目录
T1
考场上想的是tarjan缩点,但是又忘了tarjan怎么打了。考完试发现tarjan也不是正解。所以tarjan:咕
正解是直接计算,统计自己连自己的和连别人的边数。如果边不联通就输出0
正解很短
CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
struct EDGE {
int to, next;
} edge[maxn];
int tote, head[maxn];
void addEdge(int from, int to) {
edge[++tote].next = head[from];
edge[tote].to = to;
head[from] = tote;
}
int m, n;
int visn;
bool vis[maxn];
void dfs(int id) {
vis[id] = true;
for (int i = head[id]; i; i = edge[i].next) {
int to = edge[i].to;
if (vis[to]) {
visn++;
continue;
}
dfs(to);
}
}
int SC, CT, in[maxn];
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int f, t;
cin >> f >> t;
if (t == f) {
SC++;
addEdge(f, t);
} else {
CT++;
addEdge(f, t);
addEdge(t, f);
in[t]++;
in[f]++;
}
}
for (int i = 1; i <= n; i++) {
dfs(i);
if (visn != 0) break;
vis[i] = 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i] && head[i] != 0) {
cout << 0 << endl;
return 0;
}
}
int ans = 0;
ans += SC * (SC - 1) / 2;
ans += SC * CT;
for (int i = 1; i <= n; i++) {
if (in[i] != 0) ans += in[i] * (in[i] - 1) / 2;
}cout << ans << endl;
return 0;
}
正解更短
注意 r-l 优化 否则会 TLE
彭师傅的解法LINK
|
|
T3
还不会
T4
大水题不提,快读+printf就能AC 写假了的码
|
|