Warm up 3 [E] You Shall Not Pass!!
你为何这么叼!
[E] You Shall Not Pass!!
Ahmed Aly is the problem architect for this year’s regional contest, and he knows a lot about the participating teams and their coaches. More precisely, each participating university has a coach who coaches some seniors; each of them might be coaching some juniors where each junior might be coaching another junior, and so on. You can think of this as a hierarchy relation, where each team coaches those who are directly beneath it in the hierarchy. Ahmed, of course, knows these relations precisely and knows who coaches whom.
Ahmed also knows that, for each team, he can write a problem that he knows neither this team nor its trainees nor anyone beneath them in the hierarchy can solve and everyone else will solve it, but he has a limited number of problems he can put in the problem set of the contest. He wants to use all that for the sole purpose of maximizing the number of teams who will not solve at least one problem.
Ahmed needs your help, write a program that given the number of problems Ahmed can write in the problem set and the relations between the teams, prints the maximum number of those who will not solve at least one problem.
Note that a team can only be coached by one other team
Input Specification
The first line of input contains an integer T, the number of test cases. T test cases follow, the first line of each test case contains three integers A, B, C, the number of teams, the number of relations and the number of problems available in the problem set respectively. Then follows B lines, each in the format F T, where F and T are contestants and F coaches T where contestants are numbered from 0 to A-1.
0 < T ≤ 100 0 < A ≤ 10000 0 ≤ B < A 0 ≤ C ≤ A 0 ≤ F , T < A
Output Specification
For each test case, on a separate line, output the maximum number of those who will not solve at least one problem.
Sample Input
2
2 1 1
0 1
10 7 2
0 3
4 1
3 2
8 5
6 9
8 6
5 7
Sample Output
2 8
1 #include <cstdio> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 #define maxn 10005 8 #define mod 1000000007 9 #define ll long long 10 int n,m,k; 11 int sum[maxn]; 12 int b[maxn]; 13 int find(int x){ 14 if(x!=sum[x])sum[x]=find(sum[x]); 15 return sum[x]; 16 } 17 void search(int x,int y){ 18 int fx=find(x),fy=find(y); 19 if(fx!=fy){sum[fy]=fx;b[fx]+=b[fy];b[fy]=0;} 20 } 21 int main(){ 22 int t; 23 scanf("%d",&t); 24 while(t--){ 25 scanf("%d%d%d",&n,&m,&k); 26 for(int i=0;i<=n;i++){sum[i]=i;b[i]=1;} 27 for(int i=0;i<m;i++){ 28 int a,b; 29 scanf("%d%d",&a,&b); 30 search(a,b); 31 } 32 sort(b,b+n); 33 int ans=0; 34 for(int i=n-1;i>=n-k;i--)ans+=b[i]; 35 printf("%d\n",ans); 36 } 37 return 0; 38 }
浙公网安备 33010602011771号