TOJ-1319 Odd Loving Bakers

news/2025/2/5 22:42:07/

There is a group of n (1 ≤ n ≤ 100) bakers in the town of Utopia. These bakers hold a monthly celebration in which they award a prize to some of the luckier among themselves. These lucky guys are chosen as follows:

In the beginning there are some chalk marks on some of the bakers' houses. Each baker has a list of his/her favorite bakers. After each celebration each of the winners puts a chalk mark on the house of all the bakers in his/her favorite list. Before each celebration those bakers with an odd number of chalk marks on their house will be chosen as winners. As there will be a great prize for the winners of the tth (1 ≤ t ≤ 1,000,000,000) celebration, you are asked to find the number of its winners.

Input

The first line of the input file contains a single integer X (1 ≤ X ≤ 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows:

The first line of each instance contains two integers n (the number of bakers) and t (the number of the celebration we want the winners of).

The next n lines of the instance each describe a baker. In each of these lines first comes the name of the baker (names are lower case strings of no more than 20 characters without spaces), then comes the number of chalk marks initially on the baker's house, then comes the number of bakers in this baker's favorite list, and after that come the names of the bakers in this baker's list.

Output

There should be one line per test case. For each test case write a line containing a single integer, the number of the winners of the tth celebration.

Sample Input

2
3 2
bessie 2 3 bessie linda mary
mary 1 1 linda
linda 0 1 bessie
2 2	
siavosh 1 2 siavosh mohammad 
mohammad 1 0

Sample Output

2
0



Source: Tehran 2004 Iran Nationwide

 

所有baker的初始状态可用一个n*1矩阵表示,此矩阵乘一个单位矩阵不会改变,在单位矩阵的基础上,我们将第i行视为第i个baker在有权标记时对各个baker的动作。

以第一组样例为例,初始状态A:[2,0,1],动作矩阵 B:|2,1,1|,顺序为bessie,linda,mary。初始状态为偶数的无论怎样乘动作矩阵所得状态仍为偶数,则两矩阵相乘

                                     |1,1,0|

                            |0,1,1|

所得结果[x,y,z]中,x=2*2+0*1+1*0,表示此轮动作中mary对bessie无动作(动作矩阵3,1处为偶数),bessie和linda本轮不能动作(状态矩阵中前两位为偶数)。

结果中奇数为我们要计数的目标,而欲得奇数,唯有奇数乘奇数,奇数加偶数(这里体会两个矩阵相乘的意义)。根据题意,一轮动作结果为A,即AB^0,t轮结果即AB^(t-1).

#include<iostream>  
#include<memory.h>  
#include<string>  
#include<map>  
using namespace std;
struct matrix{short m[102][102];
}a,b;  
int x,n,t;
map<string,int> name;
void om(matrix a){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cout<<a.m[i][j]<<" ";}cout<<endl;}
}
matrix mm(matrix a,matrix b){matrix temp;memset(temp.m,0,sizeof(temp.m));//om(a);om(b);for(int i=1;i<=n;i++)  {  for(int j=1;j<=n;j++) {  for(int k=1;k<=n;k++)  temp.m[i][j] = (temp.m[i][j] + a.m[i][k] * b.m[k][j])%2;  }  }//om(temp);  return temp; 
}
matrix mp(int t)  
{  matrix temp;memset(temp.m,0,sizeof(temp.m));  for(int i = 1; i<=n; i++)  {  temp.m[i][i] = 1;  }  while(t)  {  if(t%2) temp = mm(temp,b);  t = t/2;  b = mm(b,b);  }  return temp;  
}
int main(){int i,j,k,e,g,f,d,h;string s;cin>>x;while(x--){cin>>n>>t;e = n;memset(a.m,0,sizeof(a.m));memset(b.m,0,sizeof(b.m));name.clear();k = 0;for(i=1;i<=n;i++)    b.m[i][i] = 1;while(e--){cin>>s>>h>>d;if(name.find(s)==name.end()){k++;name[s] = k;}g = name[s];a.m[1][g] = h;while(d--){cin>>s;if(name.find(s)==name.end()){k++;name[s] = k;}f = name[s];b.m[g][f]++;}}b = mp(t-1);a = mm(a,b);int ans = 0;  for(int i = 1; i<=n; i++)  {  if(a.m[1][i]%2) ans++;  }cout<<ans<<endl;}return 0;
}

编程上的问题,容器map的使用。

未解决的问题:函数中二维数组或多维数组的引用、返回等问题。没有弄明白,用了投机的方法结构体。

转载于:https://www.cnblogs.com/shenchuguimo/p/6358065.html


http://www.ppmy.cn/news/312736.html

相关文章

AcWing 1319. 移棋子游戏

sg函数是一张有向无环图 尼姆博弈对每一张图sg&#xff08;值&#xff09;进行游戏 就是加强版的集合尼姆博弈(集合尼姆博弈中拓展是根据集合可能的新状态)&#xff0c;这里是回归本质&#xff0c;sg操作是对每个状态拓展出边&#xff0c;并通过出边sg值集合进行mex操作&#x…

ZOJ 1319 Black Box

/*优先队列*/ #include<iostream> #include<queue> #include<algorithm> using namespace std; int main() {int caseN0;cin>>caseN;while(caseN--){int i0,j1,N0,M0;cin>>N>>M;int *anew int[N1];int *unew int[M1];for(i1;i<N;i)cin…

关于Windows 7 64位系统 HP M1319f 打印机无法扫描的解决办法

此办法主要针对Windows7 64位系统的用户&#xff0c;对于Xp系统或者Windows8系统没有验证。 笔者在将电脑重装成win7 64位系统后在安装hp打印机驱动的时候打印机自带的驱动盘提示不支持64位系统&#xff0c;笔者只能在HP官网上下载64位系统的驱动&#xff0c;但是这时候问题出现…

“HP LaserJet M1319f 激光一体机”在 Windows XP 下实现共享打印

一、“HP LaserJet M1319f 激光一体机”在 Windows XP 下实现共享打印 文章简介 在主机端上安装了激光一体机的驱动程序后&#xff0c;为了让网络中其他客户机使用该一体机进行打印&#xff0c;需要安装设置共享打印机。可以将 HP LaserJet M1319f 激光一体机设置为共享打印机。…

【Leetcode】DP | 打家劫舍,当一个机灵的小偷

198 打家劫舍 令 D [ i ] D[i] D[i]表示前 i i i间房子的最大收益&#xff1a; D [ i ] max ⁡ ( D [ i − 1 ] , D [ i − 2 ] n u m s [ i ] ) D [ 0 ] n u m s [ 0 ] D [ 1 ] max ⁡ ( n u m s [ 0 ] , n u m s [ 1 ] ) D[i] \max(D[i -1], D[i-2]nums[i]) \\ D[0] …

安卓大作业 书籍列表APP

系列文章 安卓大作业 书籍列表APP 文章目录 系列文章1&#xff0e;背景2&#xff0e;功能3. 源代码获取 1&#xff0e;背景 我做的项目是一个可以查看到书籍列表以及详情效果的内容&#xff0c;主要使用到的技术有Intent数据传递以及数据库存储的应用&#xff0c;其次使用的组…

wordpress二次元主题

几款开源的wordpress二次元主题&#xff0c;演示地址可到Github查看。 boxmoe Github&#xff1a;https://github.com/baomihuahua/boxmoe-dove- Kratos Github&#xff1a;https://github.com/seatonjiang/kratos Sakura Github&#xff1a;https://github.com/mashiroz…

几款二次元主题分享

文章原发布地址https://xiaoyou66.com/archives/454 本文章为个人博客的备份版本、作者&#xff1a;小游、作者博客&#xff1a;点击访问 目前大部分人搭建自己博客时都会选择wordpress&#xff0c;但是默认的主题实在很难符合我们中国人的审美&#xff08;吐槽一下&#xff09…