The Suspects
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 31100 Accepted: 15110
Description
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
按照自己的思路重新做了下,1A。就是时间慢了点,map+vector的缘故吧,但是比较好理解。
思路有两种
1、将每个团队连成一条线,团队的中某一个人变成了自己团队的祖先(头头),然后进行合并的时候就会不停地找祖先,那么有传染的也被连成了一条线,一旦有一个人的中间祖先或者最后的祖先跟0号有关系,那么传染链会直接被并进去,并到最后就是全部被传染的人。时间16ms
代码:
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; typedef long long LL; #define INF 0x3f3f3f3f const int N=30010; int pre[N]; int rank[N]; int num[N]; inline int finder(int x) { if(x!=pre[x]) pre[x]=finder(pre[x]); return pre[x]; } inline void joint(int a,int b) { int fa=finder(a); int fb=finder(b); if(fa==fb) return ; else if(rank[fa]>rank[fb]) { pre[fb]=fa; num[fa]+=num[fb]; } else { pre[fa]=fb; if(rank[fa]==rank[fb]) rank[fb]++; num[fb]+=num[fa]; } } int main(void) { int n,m,i,j,mm,a,b; while (~scanf("%d%d",&n,&m)&&(n||m)) { for (i=0; i<=n; i++) { pre[i]=i; num[i]=1; rank[i]=0; } for (i=0; i<m; i++) { scanf("%d",&mm); if(mm!=0) { scanf("%d",&a); for (j=1; j<mm; j++) { scanf("%d",&b); joint(a,b); } } } int index=finder(0); printf("%d ",num[index]); } return 0; }
2、用一个map记录某人所呆的团队编号,用邻接表记录一个团队的人的集合,还有一个vis数组表示这个团队是否被访问过。这样就可以通过人来找团队,也可以通过团队来找人。刚接触这题的时候想过这么做,但是好像运行出错了,然后用前面的方法做的,现在用这个方法证明确实可以A而且比较好理解 时间235ms
首先把0这个团队压入一个队列,然后合并掉队列里的所有人,合并的时候看这个队列里的人是否也在其他团队呆过(这里就要用到map了)若有的话把那个团队也压入队列,然后再次进行上述操作,直到队列为空。
代码:
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> #define INF 0x3f3f3f3f #define MM(x) memset(x,0,sizeof(x)) using namespace std; typedef long long LL; const int N=30010; int pre[N],ran[N]; vector<int>team[510]; map<int,vector<int> >belong; int vis[510]; int find(int n) { if(n!=pre[n]) return pre[n]=find(pre[n]); return pre[n]; } inline void joint(int a,int b) { int fa=find(a),fb=find(b); if(fa!=fb) { if(ran[fa]>=ran[fb]) { ran[fa]+=ran[fb]; pre[fb]=fa; } else { ran[fb]+=ran[fa]; pre[fa]=fb; } } } inline void init() { for (int i=0; i<N; i++) { pre[i]=i; ran[i]=1; } for (int i=0; i<510; i++) team[i].clear(); MM(vis); belong.clear(); } int main(void) { int m,i,j,one,n,person; while (~scanf("%d%d",&n,&m)&&(n||m)) { init(); for (i=0; i<m; i++) { scanf("%d",&n); for (j=0; j<n; j++) { scanf("%d",&person); team[i].push_back(person); belong[person].push_back(i); } } queue<int>Q; for (i=0; i<belong[0].size(); i++) Q.push(belong[0][i]); while (!Q.empty()) { int now=Q.front(); Q.pop(); if(vis[now]) continue; vis[now]=1; for (i=0; i<team[now].size(); i++) { int v=team[now][i]; joint(0,v); for (j=0; j<belong[v].size(); j++) { if(!vis[belong[v][j]]) Q.push(belong[v][j]); } } } printf("%d ",ran[0]); } return 0; }