2011年4月11日月曜日

Codeforces Beta Round #66 参戦記

今回はいまいちでした。Problem Dを解こうとして奮闘し、問題文を読み違えているのかうまくいかず…の繰り返しでした。特に書く事もない大会です。

問題はこちら


以下解答ソースです。
[ProblemD]

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct{
  4.   int from;
  5.   int to;
  6. }road;
  7. road *roads;
  8. void defprov(int *a,int num,int count,int r);
  9. int main(void){
  10.   int *cities;
  11.   int i,j,k,n,m,count=1;
  12.   int tun;
  13.   int *hist;
  14.   scanf("%d %d %d",&n,&m,&k);
  15.   roads=(road *)calloc(m,sizeof(road));
  16.   cities=(int *)calloc(n,sizeof(int));
  17.   for(i=0;i<m;i++){
  18.     scanf("%d %d",&((roads+i)->from),&((roads+i)->to));
  19.     ((roads+i)->from)--;
  20.     ((roads+i)->to)--;
  21.   }
  22.   for(i=0;i<n;i++){
  23.     if(*(cities+i)==0){
  24.       defprov(cities,i,count,m);
  25.       count++;
  26.     }
  27.   }
  28.   count--;
  29.   hist=(int *)calloc(count,sizeof(int));
  30.   for(i=0;i<n;i++) hist[cities[i]-1]++;
  31.   count--;
  32.   tun=0;
  33.   for(i=0;i<=count;i++){
  34.     tun+=(k>hist[i])?hist[i]:k;
  35.   }
  36.   tun=(tun>n/2)?n/2:tun;
  37.   free(hist);
  38.   if(count>=tun){
  39.     printf("%d\n",count-tun);
  40.   }else{
  41.     printf("0\n");
  42.   }
  43.   free(cities);
  44.   free(roads);
  45.   return 0;
  46. }
  47. void defprov(int *a,int num,int count,int r){
  48.   int i;
  49.   a[num]=count;
  50.   for(i=0;i<r;i++){
  51.     if((roads+i)->from==num){
  52.       if(a[(roads+i)->to]==count) continue;
  53.       defprov(a,(roads+i)->to,count,r);
  54.     }
  55.   }
  56.   return;
  57. }

0 件のコメント: