05-树8 File Transfer(25 分)
2017-10-28 20:33
525 查看
05-树8 File Transfer(25 分)
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?
Input Specification:
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:
I c1 c2
where
Istands
for inputting a connection between
c1and
c2;
or
C c1 c2
where
Cstands
for checking if it is possible to transfer files between
c1and
c2;
or
S
where
Sstands
for stopping this case.
Output Specification:
For each Ccase,
print in one line the word "yes" or "no" if it is possible or impossible to transfer files between
c1and
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
kcomponents."
where
k is the number of connected components
in this network.[code]
Sample Input 1:5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
Sample Output 1:
no no yes There are 2 components.
Sample Input 2:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S
Sample Output 2:
no no yes yes The network is connected.
一定要做到的是归并树时要按照大小,而且最好要进行路径压缩。
#include <cstdio> #include <cstring> //#define LOCAL #define MAXSIZE 10010 int father[MAXSIZE]; void init(int size) { for(int i=0; i<size; ++i) { father[i] = -1; } } int Find(int x) { if(father[x] < 0) return x; else return father[x] = Find(father[x]); } bool isSameSet(int a, int b) { int Fa = Find(a); int Fb = Find(b); return Fa==Fb; } void Union(int a, int b) { int Fa = Find(a); int Fb = Find(b); if(Fa!=Fb) { if(father[Fa] <= father[Fb]) { father[Fa] += father[Fb]; father[Fb] = Fa; } else { father[Fb] += father[Fa]; father[Fa] = Fb; } } } int main() { #ifdef LOCAL freopen("test.txt", "r", stdin); #endif int num; scanf("%d", &num); init(num); char ins; do { scanf("%c", &ins); if(ins=='C') { int a, b; scanf("%d %d", &a, &b); if(isSameSet(a, b)) printf("yes\n"); else printf("no\n"); } else if(ins=='I') { int a, b; scanf("%d %d", &a, &b); Union(a, b); } } while(ins != 'S'); int com = 0; for(int i=0; i<num; ++i) { if(father[i]<0) ++com; } if(com!=1) printf("There are %d components.", com); else printf("The network is connected."); return 0; }
提交代码时,遇到了一些问题,即:我用fgets处理的数据,问题在于不一定原始数据每一行都有换行符。所以,AC代码里getchar更好。
相关文章推荐
- 05-树8 File Transfer(25 分)
- 05-树8 File Transfer(25 分)
- 05-树7. File Transfer (25)
- 05-树8 File Transfer(25 分)
- 05-树7. File Transfer (25)
- PAT : 05-树7. File Transfer (25)
- 05-树8 File Transfer(25 point(s))
- PAT 05-树7. File Transfer (25)
- 05-树9 File Transfer
- 05-树8 File Transfer【并查集】
- 05-树9 File Transfer (25分)
- 05-树8 File Transfer
- 05-树8 File Transfer (25分)
- 05-树8 File Transfer
- File Transfer(25 分)
- 04-2. File Transfer (25)并查集
- 7-26 File Transfer(25 分)
- PAT 04-2. File Transfer (25) (并查集)
- SSH Secure File Transfer 新手小贴士
- [25]_arm-none-linux-gnueabi-gcc: No such file or directory