题目链接:传送门
思路:直接用map记录数字出现次数模拟即可。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
vector <int> vt[maxn];
map <int , int> mp[maxn];
int main() {
int n;
ios::sync_with_stdio(0);
cin >> n;
for(int i = 1 ; i <= n ; i++) {
int t;
cin >> t;
while(t--) {
int x;
cin >> x;
if(!mp[i].count(x)) {
vt[i].push_back(x);
mp[i][x] = 1;
}
}
}
int m;
cin >> m;
for(int i = 1 ; i <= m ; i++) {
int a , b;
cin >> a >> b;
double nc = 0 , nt = 0;
for(int j = 0 ; j < vt[a].size() ; j++) {
int t = vt[a][j];
if(mp[a].count(t) && mp[b].count(t)) {
nc++;
}
nt++;
}
for(int j = 0 ; j < vt[b].size() ; j++) {
int t = vt[b][j];
if(!mp[a].count(t))nt++;
}
cout << fixed << setprecision(1) <<nc / nt * 100.0 << "%\n";
}
return 0;
}