https://www.patest.cn/contests/gplt/L2-005
题解:直接set的count函数
坑 :要用容斥原理算两个集合的并,否则超时。(我还以为要打表呢)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<list>
#define pb push_back
#define mp make_pair
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 50 + 5;
set<int> s[N];
int main() {
int n;
cin >> n;
_for(i, 1, n+1) {
int k;
cin >> k;
_for(j, 0, k) {
int x;
cin >> x;
s[i].insert(x);
}
}
int q;
/*_for(i,1,n)
_for(j, i+1, n + 1) {
for (auto t : s[i]) if (s[j].count(i)) tc[i][j]++;
tt[i][j] = s[i].size() + s[j].size() - 2*tc[i][j];
}*///写了一半感觉容斥定理可能能ac
cin >> q;
_for(i, 0, q) {//2000*4000 8e6 tle;
int x, y;
cin >> x >> y;
float nc=0, nt=0;
set<int> t;
for (auto t1 : s[x]) { if (s[y].count(t1)) nc++; }
//for (auto t2 : s[y]) {t.insert(t2);}
nt = s[x].size()+s[y].size()-nc;
printf("%.2lf%%
", nc / nt * 100);
}
system("pause");
}