Network Connections UVA 793(并查集)



Network Connections

                                                                             Time Limit: 3000ms            Memory Limit: 131072KB

[PDF Link]


  Network Connections 

Bob, who is a network administrator, supervises a network of computers. He is keeping a log connections between the computers in the network. Each connection is bi-directional. Two computers are interconnected if they are directly connected or if they are interconnected with the same computer. Occasionally, Bob has to decide, quickly, whether two given computers are connected, directly or indirectly, according to the log information.


Write a program which based on information input from a text file counts the number of successful and the number of unsuccessful answers to the questions of the kind :


is computeri interconnected with computerj ?

Input and Output 

The first line of the input contains the number of dataset, and it's followed by a blank line. Each dataset is defined as follows:
1.
The number of computers in the network (a strictly positive integer);
2.
A list of pairs of the form:
(a)
c  computer i  computer j, where  computer i and  computer j are integers from 1 to  $no\_of\_computers$. A pair of this form shows that  computer i and  computer j  get interconnected.
(b)
q  computer i  computer j, where  computer i and  computer j are integers from 1 to  $no\_of\_computers$. A pair of this form stands for the question: is  computer i  interconnectedwith  computer j?

There's a blank line between datasets.

Each pair is on a separate line. Pairs can appear in any order, regardless of their type. The log is updated after each pair of type (a) and each pair of type (b) is processed according to the current network configuration.


For example, the input file illustrated in the sample below corresponds to a network of 10 computers and 7 pairs. There are N1 successfully answered questions and N2 unsuccessfully answered questions. The program prints these two numbers to the standard output on the same line, in the order: successful answers, unsuccessful answers, as shown in the sample output. Print a blank line between datasets.

Sample Input 

1

10
c 1 5
c 2 7
q 7 1
c 3 9
q 9 6
c 2 5
q 7 5

Sample Input 

1,2

题目大意:

每次q后计算失败和成功的次数,最后输出成功的和失败的次数。

解题思路:

并查集,死了很多次在输入上面,输入还是需要一些技巧的。


代码:

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;

const int maxn=1000000;//之前一直WA忘记了str0也可能随数字的增大而增长,

int t,n,parent[maxn],success,fail;
char str0[100];

void initial(){
    for(int i=0;i<=n;i++){parent[i]=i;}
    success=fail=0;
}

void outPut(){
    printf("%d,%d\n",success,fail);
    if(t) printf("\n");
}


int getRoot(int k){
    if(k!=parent[k]) parent[k]=getRoot(parent[k]);
    return parent[k];
}

void Union(int a,int b){
    parent[a]=b;
}

void solve(){
    int p,q,a,b;
    sscanf(str0+1,"%d%d",&a,&b);//从字符串里面截取数字的函数,以空格分数字.
    p=getRoot(a);
    q=getRoot(b);
    if(str0[0]=='c'){if(p!=q) Union(p,q);}
    if(str0[0]=='q'){
        if(p==q) success++;
        else fail++;
    }
}

int main(){
    scanf("%d",&t);
    while(t--){
        cin>>n;
        getchar();
        initial();
        while(gets(str0)){
            if(!str0[0]) break;//判断是否为空行
            solve();
        }
        outPut();
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值