Meaning of the questions:
Novel coronavirus pneumonia (Corona Virus Disease 2019, COVID- 19), referred to as "the new crown pneumonia" refers to the 2019 novel coronavirus infection caused by pneumonia.
If an infected person into a group, then this group need to be isolated!
A small student was diagnosed with the new crown infection, and does not wear a mask! ! ! ! ! !
Risk! ! !
Time is running out! ! ! !
Need to find as quickly as possible to all students and small A direct or indirect contact through the students, they will be isolated to prevent the spread of a wider range.
As we all know, students' communication may be in small groups, a student may also participate in multiple small groups.
Please write a program to solve! wear mask! !
Input
A plurality of sets of data, for each set of test data:
the first two integers n and Behavior m (n = m = 0 indicates the end of input, does not need to process), n is the number of students, m is the number of groups of students. 0 <n <= 3e4, 0 <= m <= 5e2
student number of 0 ~ n-1
A small number 0.
Then, m lines, each person has a number of small groups i.e. integer num. Followed by a num integer representing students in small groups.
Output
The number of output to be isolated, the answer to the output of each data per 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
My Solution:
Disjoint-set : to "category" element, to a certain element as a representative element, form a set of different categories.
Charles: Find the corresponding representatives yuan to determine a property
And: by checking whether the same kind of element is determined, and then determines whether or not combined into one earth element.
Set: The several elements are bundled together to form a set of representatives of dollars.
This question need only count the number of groups working with the source of infection associated with a simple disjoint-set problems.
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<string.h> 5 using namespace std; 6 7 const int maxn=100005; 8 int n,maxL,maxlen,f1,f2; 9 int L1[maxn],L2[maxn]; 10 int u,w; 11 typedef struct node{ 12 int u,v,w,nxt; 13 }Edge; 14 Edge Edges[maxn]; 15 int head[maxn],tot,vis[maxn]; 16 17 void addEdge(int u,int v,int w){ 18 Edges[tot].u=u; 19 Edges[tot].v=v; 20 Edges[tot].w=w; 21 Edges[tot].nxt=head[u]; 22 head[u]=tot; 23 tot++; 24 } 25 26 void dfs(int u,int *a,int L){ 27 a[u]=L; 28 if(maxL<L) { 29 maxL=L;maxlen=u; 30 } 31 for(int i=head[u];i!=-1;i=Edges[i].nxt){ 32 if(!vis[Edges[i].v]){ 33 vis[Edges[i].v]=true; 34 dfs(Edges[i].v, a, L + Edges[i].w); 35 } 36 } 37 } 38 int main(){ 39 while(cin>>n){ 40 41 tot=0; 42 for(int i=0;i<n+2;++i) head[i]=-1; 43 44 for(int i=2;i<=n;++i){ 45 cin>>u>>w; 46 addEdge(i,u,w); 47 addEdge(u,i,w); //加入双向边 48 } 49 50 memset(vis,0,sizeof(vis)); 51 maxL=-1;vis[1]=1; 52 53 dfs(1 , L 1, 0 ); 54 F1 = maxlen; 55 memset (view, 0 , sizeof (show)); 56 maxL = - 1 ; 57 show [f1] = 1 ; 58 59 DFS (F1, L1, 0 ); 60 f2 = maxlen; 61 memset (view, 0 , sizeof (show)); 62 show [f 2] = 1 ; 63 DFS (F2, L2, 0 ); 64 65 for ( int= I . 1 ; I <= n-; I ++ ) { 66 COUT << max (Ll [I], L2 of [I]) << endl; 67 // greater is the output 68 } 69 } 70 return 0 ; 71 is }