zoukankan      html  css  js  c++  java
  • File Transfer

    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≤10​4), 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 I stands for inputting a connection between c1 and c2; or
    C c1 c2
    where C stands for checking if it is possible to transfer files between c1 and c2; or
    S
    where S stands for stopping this case.

    Output Specification:

    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.

    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.

    我的代码:C++(g++ 6.5.0)

    #include <iostream>
    
    #define MaxSize 10001
    typedef int elementType; /*默认元素可以用非负整数表示*/
    typedef int setName; /*默认根节点的下标作为集合名称*/
    //typedef elementType setType[MaxSize];
    typedef setName * setType;
    
    /*S[0]空间不用,计算机序号与数组下标一一映射*/
    setType Initialization(int N) {
    	setType S = (setName *)malloc((N+1)*sizeof(elementType));
    	for (int i = 1; i <= N; i++) S[i] = -1;
    	return S;
    }
    void Union(setType S,setName root1,setName root2) {
    	if (S[root1] < S[root2] ) {
    		S[root1] += S[root2];
    		S[root2] = root1;
    	}else {
    		S[root2] += S[root1];
    		S[root1] = root2;
    	}
    }
    setName Find(setType S, elementType X) {
    	if (S[X] < 0) return X;
    	else return S[X] = Find(S,S[X]); //路径压缩	
    }
    void Input_connection(setType S) {
    	elementType c1, c2;
    	scanf("%d %d
    ",&c1,&c2);
    	setName root1 = Find(S, c1);
    	setName root2 = Find(S, c2);
    	if (root1 != root2) Union(S, root1, root2);
    }
    void Check_connection(setType S) {
    	elementType c1, c2;
    	scanf("%d %d
    ", &c1, &c2);
    	setName root1 = Find(S, c1);
    	setName root2 = Find(S, c2);
    	if (root1 == root2) printf("yes
    ");
    	else printf("no
    ");
    }
    void Check_network(setType S,int N) {
    	int cnt = 0;
    	for (int i = 1; i <= N; i++) {
    		if (S[i] < 0) ++cnt;
    	}
    	if (cnt > 1) printf("There are %d components.
    ",cnt);
    	else printf("The network is connected.
    ");
    }
    
    int main()
    {
    	//setType S;
    	int N;
    	char in;
    	scanf("%d",&N);
    	//Initialization(S,N);
    	setType S = Initialization(N);
    	rewind(stdin); // 键盘文件当前位置指针指向文件头(起到清空键盘缓冲区中无用字符的作用)
    	do {
    		scanf("%c",&in);
    		switch (in) {
    		case 'I':Input_connection(S); break;
    		case 'C':Check_connection(S); break;
    		case 'S':Check_network(S, N); break;
    		}
    	} while (in != 'S');
    	return 0;
    }
    
  • 相关阅读:
    AndroidManifest.xml文件详解(uses-feature) (转载)
    两分钟彻底让你明白Android Activity生命周期(图文)! (转载)
    android studio学习----添加项目依赖包补充---添加github上的开源项目为库
    android studio学习----添加项目依赖包总结
    android studio学习----如何创建一个库项目
    android studio学习----添加项目库
    android studio学习----通过libs来导入jar包
    android studio学习----通过gradle来导入jar包
    android studio学习----gradle配置
    android studio学习----构建(gradle )依赖时使用动态依赖的问题
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/13624316.html
Copyright © 2011-2022 走看看