zoukankan      html  css  js  c++  java
  • PAT 05-树7 File Transfer

    这次的题让我对选择不同数据结构所产生的结果惊呆了,一开始用的是结构来存储集合,课件上有现成的,而且我也是实在不太会,150ms的时间限制过不去,不得已,看到这题刚好可以用数组,结果7ms最多,有意思!什么是时间复杂度,终于有了一次感性的认识。这题还有个check的要求,我用了一个链表来存储,感觉挺麻烦的,不知是否有更好的方法,题设要求及代码实现如下

      1 /*
      2     Name: 
      3     Copyright: 
      4     Author: 
      5     Date: 06/04/15 09:46
      6     Description: 
      7 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
      8 
      9 Input Specification:
     10 
     11 Each input file contains one test case. For each test case, the first line contains N (2<=N<=104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:
     12 
     13 I c1 c2  
     14 where I stands for inputting a connection between c1 and c2; or
     15 
     16 C c1 c2    
     17 where C stands for checking if it is possible to transfer files between c1 and c2; or
     18 
     19 S
     20 where S stands for stopping this case.
     21 
     22 Output Specification:
     23 
     24 For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." where k is the number of connected components in this network.
     25 
     26 Sample Input 1:
     27 5
     28 C 3 2
     29 I 3 2
     30 C 1 5
     31 I 4 5
     32 I 2 4
     33 C 3 5
     34 S
     35 Sample Output 1:
     36 no
     37 no
     38 yes
     39 There are 2 components.
     40 Sample Input 2:
     41 5
     42 C 3 2
     43 I 3 2
     44 C 1 5
     45 I 4 5
     46 I 2 4
     47 C 3 5
     48 I 1 3
     49 C 1 5
     50 S
     51 Sample Output 2:
     52 no
     53 no
     54 yes
     55 yes
     56 The network is connected.
     57 */
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <stdbool.h>
     61 
     62 typedef struct Flag
     63 {
     64     bool flag;
     65     struct Flag * next;
     66 }Flag, * pFlag;
     67 
     68 int Find(int S[], int X);
     69 void Union(int S[], int X1, int X2);
     70 
     71 int MaxSize;
     72 
     73 int main()
     74 {
     75 //    freopen("in.txt", "r", stdin); // for test
     76     int i, c1, c2, k;
     77     char ch;
     78     
     79     scanf("%d
    ", &MaxSize);
     80     
     81     int S[MaxSize];
     82     for(i = 0; i < MaxSize; i++)
     83         S[i] = -1;
     84     
     85     pFlag head = (pFlag)malloc(sizeof(Flag));
     86     pFlag p, tmp;
     87     
     88     p = head;
     89     scanf("%c", &ch);
     90     while(ch != 'S')
     91     {
     92         if(ch == 'C')
     93         {
     94             pFlag f = (pFlag)malloc(sizeof(Flag));
     95             f->next = NULL;
     96             scanf("%d%d
    ", &c1, &c2);
     97             if(Find(S, c1) == Find(S, c2))
     98                 f->flag = true;
     99             else
    100                 f->flag = false;
    101             p->next = f;
    102             p = p->next;
    103         }
    104         else
    105         {
    106             scanf("%d%d", &c1, &c2);
    107             Union(S, c1, c2);
    108         }
    109         scanf("%c", &ch);
    110     }
    111     
    112     p = head->next;
    113     tmp = head;
    114     free(tmp);
    115     while(p)
    116     {
    117         if(p->flag)
    118             printf("yes
    ");
    119         else
    120             printf("no
    ");
    121         tmp = p;
    122         p = p->next;
    123         free(tmp);
    124     }
    125     k = 0;
    126     do
    127     {
    128         if(S[--MaxSize] < 0)
    129             k++;
    130     }while(MaxSize);    
    131     if(k > 1)
    132         printf("There are %d components.
    ", k);
    133     else
    134         printf("The network is connected.
    ");
    135 //    fclose(stdin); // for test
    136     return 0;
    137 }
    138 
    139 int Find(int S[], int X)
    140 {
    141     X--;
    142     for(; S[X] >= 0; X = S[X]);
    143     
    144     return X;
    145 }
    146 
    147 void Union(int S[], int X1, int X2)
    148 {
    149     int Root1, Root2;
    150     
    151     Root1 = Find(S, X1);
    152     Root2 = Find(S, X2);
    153     if(Root1 != Root2)
    154     {
    155         if(S[Root1] <= S[Root2])
    156         {
    157             S[Root1] += S[Root2];
    158             S[Root2] = Root1;
    159         }
    160         else
    161         {
    162             S[Root2] += S[Root1];
    163             S[Root1] = Root2;
    164         }
    165     }
    166 }
  • 相关阅读:
    如何使用 Pylint 来规范 Python 代码风格
    linux中强大且常用命令:find、grep
    linux tar 命令
    linux xargs 命令详解
    Python3.x和Python2.x的区别-转
    linux下svn命令大全
    vim---打造Python IDE
    iPhone图片拉伸:resizableImageWithCapInsets
    OC语言构造方法
    OC语言@property @synthesize和id
  • 原文地址:https://www.cnblogs.com/qingkai/p/4395994.html
Copyright © 2011-2022 走看看