NBUT
来源:互联网 发布:如何优化搜索引擎 编辑:程序博客网 时间:2024/04/29 13:54
Eliseis the Spider Queen. She has a skill, Spider Form(蜘蛛形态) .
Input Output Sample Input Sample Output Hint
代码:
When she transformed to the spider, there will be some small spiders around her.
But she has a problem - the small spiders will have infighting because of less common interest. So Elise decided to train their interesting.
At least, they must have common interest no matter directly or indirectly.
How to train theirs interest in least cost? We assume that train a interest for a spider cost1 strength and there are at most 100 interests in total.
This problem contains several cases.
The first line of each case is an integer N (1 ≤ N ≤ 100), indicates the number of spiders.
Then N lines followed.
The ith line contains an integer Ti (0 ≤ Ti ≤ 100) that indicates the number of this spider's interest and Ti strings indicate the interests. You can assume there are only lowercase letters in the strings and no longer than 20.
The first line of each case is an integer N (1 ≤ N ≤ 100), indicates the number of spiders.
Then N lines followed.
The ith line contains an integer Ti (0 ≤ Ti ≤ 100) that indicates the number of this spider's interest and Ti strings indicate the interests. You can assume there are only lowercase letters in the strings and no longer than 20.
For each case, you should output the least cost.
51 kill1 kill2 sleep sing2 sing fart1 fart
1
In this case, the spider 1 or 2 just need to learn to sleep or sing or fart. So it's 1.
题意:有n个蜘蛛,蜘蛛之间如果没有相同的兴趣,就会打架,相同的兴趣可以直接或间接联系得到
看题用并查集,但是由元素中成分合并某些元素,先用map把字符串赋值,若两元素有成分的值相等则合并两元素。
#include<map>#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int n;int father[110];void intl(){ for(int i=1;i<=n;i++) father[i]=i;}int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x];}void unin(int x,int y){ int nx=find(x); int ny=find(y); if(nx!=ny) father[nx]=ny;}int main(){ while(~scanf("%d",&n)) { intl(); map<string,int>mat;//给每门兴趣编号 vector<int >G[110];//储存第i个蜘蛛的兴趣 mat.clear(); int cnt=0;int num=1; for(int i=1;i<=n;i++) { int m; scanf("%d",&m); if(m==0) cnt++; G[i].clear();//清空vector char s[25]; for(int j=1;j<=m;j++) { scanf("%s",s); if(!mat[s]) //编号 mat[s]=num++; G[i].push_back(mat[s]);//放入G[i]中 } } if(cnt==n) printf("%d\n",cnt); else {//遍历每个蜘蛛 有相同的成分则合并 for(int i=1;i<=n;i++) for(int j=0;j<G[i].size();j++) for(int k=i+1;k<=n;k++) for(int l=0;l<G[k].size();l++) if(G[i][j]==G[k][l]) unin(i,k); int ans=0; for(int i=1;i<=n;i++) if(father[i]==i) ans++; printf("%d\n",ans-1); } }}
阅读全文
0 0
- NBUT
- NBUT
- NBUT
- NBUT
- NBUT
- 【NBUT
- NBUT
- NBUT
- NBUT
- NBUT
- NBUT
- nbut 1475
- NBUT 1481
- NBUT 1552
- NBUT 1477
- NBUT 1450
- NBUT 1665
- NBUT 1647
- spring-boot学习笔记1
- Java学习 Number类 Charracter类
- 写个单例模式吧@liuyanm
- 菜鸟柳--23种设计模式初学记录笔记(二)装饰者设计模式
- 1分钟教你使用补丁破解IntelliJ IDEA 2017收费版(永久)
- NBUT
- java序列化
- mysql 安全模式
- Spring+Tomcat配置文件与项目分离
- C++创建类对象的三种方式区别
- 浅谈MVC和MVP
- 微信小程序模板消息无限制群发
- 史上最全互联网运维工作规划!十分钟找到职业方向!
- 芝麻信用倒逼银行改革,纷纷模仿支付宝推出借呗!