本来说好的每日一题解,因为下午睡了一觉醒来发现已经快十点了,然后才开始做题。。然后发现现在已经过了十二点了。。。

题目可以在这里看:传送门

然后这可以说是一道陈年老题了,我从高中的时候就打算要写,然后一直拖到现在-_-#,根据题意很容易想到可以用并查集来解决,我们可以添加一个节点0,然后有毒的水果都合并到0,然后最后统计一下有多少水果是有毒的就行了。

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

const int MN = 10010;

char a[30], b[30];
int n, m, k, p;
int f[MN];

string cv(string s) {
string ret = "";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < 26; j++)
if (s[i] == a[j]) {
ret += b[j];
break;
}
}
return ret;
}

int find(int v) {
return f[v] = f[v] == v ? v : find(f[v]);
}

int main() {
cin >> a;
cin >> b;
cin >> n >> m;
for (int i = 0; i <= n; i++)
f[i] = i;
string s;
for (int i = 0; i < m; i++) {
cin >> k >> s;
s = cv(s);
int t = s.find("poison");
if (t < s.length())
f[k] = 0;
}
cin >> p;
int x, y;
for (int i = 0; i < p; i++) {
cin >> x >> y;
int fx = find(x), fy = find(y);
if (fx == 0 || fy == 0) {
f[fx] = 0;
f[fy] = 0;
} else {
f[fx] = fy;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
f[i] = find(i);
if (f[i] == 0) ans++;
}
cout << n-ans << endl;
return 0;
}

ps:我这哪还是在写题解。。。这样下去真的可以吗T^T