0
点赞
收藏
分享

微信扫一扫

【集合数组】L2-005 集合相似度 (25 分)

1kesou 2022-04-13 阅读 29
c++
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;


int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin >> n;
	vector<unordered_map<int, int>> mp(n);
	for (int i = 0; i < n;++i){
		int m;
		cin >> m;
		while(m--){
			int x;
			cin >> x;
			mp[i][x] = 1;
		}
	}

	int k;
	cin >> k;
	while(k--){
		int a, b;
		cin >> a >> b;
		int nc = 0,nt=0;
		for(const auto x:mp[a-1]){
			if(mp[b-1].count(x.first))  //写成mp[b-1][x]会出错,程序会自动创建元素导致集合改变
				nc++;
			else
				nt++;
		}
		nt += mp[b - 1].size();
		printf("%.2f%%\n", 100.0 * nc/nt);
	}
}

举报

相关推荐

0 条评论