問題はこちら
以下解答ソースです。
[ProblemD]
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct{
- int from;
- int to;
- }road;
- road *roads;
- void defprov(int *a,int num,int count,int r);
- int main(void){
- int *cities;
- int i,j,k,n,m,count=1;
- int tun;
- int *hist;
- scanf("%d %d %d",&n,&m,&k);
- roads=(road *)calloc(m,sizeof(road));
- cities=(int *)calloc(n,sizeof(int));
- for(i=0;i<m;i++){
- scanf("%d %d",&((roads+i)->from),&((roads+i)->to));
- ((roads+i)->from)--;
- ((roads+i)->to)--;
- }
- for(i=0;i<n;i++){
- if(*(cities+i)==0){
- defprov(cities,i,count,m);
- count++;
- }
- }
- count--;
- hist=(int *)calloc(count,sizeof(int));
- for(i=0;i<n;i++) hist[cities[i]-1]++;
- count--;
- tun=0;
- for(i=0;i<=count;i++){
- tun+=(k>hist[i])?hist[i]:k;
- }
- tun=(tun>n/2)?n/2:tun;
- free(hist);
- if(count>=tun){
- printf("%d\n",count-tun);
- }else{
- printf("0\n");
- }
- free(cities);
- free(roads);
- return 0;
- }
- void defprov(int *a,int num,int count,int r){
- int i;
- a[num]=count;
- for(i=0;i<r;i++){
- if((roads+i)->from==num){
- if(a[(roads+i)->to]==count) continue;
- defprov(a,(roads+i)->to,count,r);
- }
- }
- return;
- }
0 件のコメント:
コメントを投稿