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 computeri computerj,
where computeri and computerj are
integers from 1 to . A pair of this form shows that computeri and computerj get
interconnected.
(b)
q computeri computerj,
where computeri and computerj are
integers from 1 to . A pair of this form stands for
the question: is computeri interconnectedwith computerj?

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;
}
时间: 2024-08-01 22:12:19

Network Connections UVA 793(并查集)的相关文章

Corporative Network(带权并查集)

这个题的题意是  当输入'E'是查找操作,查找从后面这个数到他的父亲这边的值,'I'代表把后面的数作为前面数的父亲 然后他们两个的差值代表这两个边的权值 水水的题 #include <stdio.h> #include <string.h> int par[20005]; int rank1[20005]; int abs(int hh) { return (hh>0)?hh:-hh; } void init() { for(int i=0;i<20005;i++) {

UVALive - 3027Corporative Network(带权并查集)

题目: UVALive - 3027Corporative Network(带权并查集) 题目大意:有n和节点,初始时每个节点的父节点都不存在,然后有下面两种操作:I 操作 I a,b 将a的父节点变成b.E操作 E a,查询a到它的父节点的距离. 解题思路:带权并查集.注意这里距离的变化是a -> b,那么a到根节点的距离就是a到b的距离的绝对值 % 1000 + b到它的根节点的距离. 代码: #include <cstdio> #include <cstring> #i

UVa 11987 并查集 Almost Union-Find

原文戳这 与以往的并查集不同,这次需要一个删除操作.如果是叶子节点还好,直接修改父亲指针就好. 但是如果要是移动根节点,指向它的所有子节点也会跟着变化. 所以要增加一个永远不会被修改的虚拟根节点,这样就可以把一个点从集合中删除而不影响其它的点了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std

UVa 10129 (并查集 + 欧拉路径) Play on Words

题意: 有n个由小写字母的单词,要求判断是否存在某种排列是的相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列就等价于该有向图中是否存在欧拉路径. 在判断之前,首先要确定这个图是连通的,代码中用并查集来实现. 回顾一下存在欧拉路径的条件,全都是偶点或者有且仅有两个奇点.我们用deg来记录每个点的度,出度为1,入度为-1. 程序中判断存在欧拉路径的条件就是:deg全为0 或者 有两个不为0的,其中一个为1一个

[POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来判断不知道群巨兹磁不兹磁……下面弱就给大家搞一发如何用floyd和并查集来缩点……大致的思路就是先floyd跑出所有距离,然后O(n^2)找两两都可达的点,把它们的关系用并查集来维护.接下来O(n)找并查集里的代表元素.这个时候应当特判一下连通块为1的时候.再O(n^2)找出所有单向边,然后更新所有

UVA 11987 并查集删点

并查集删点就是弄个id记录当前点的id,删除的时候将id设为新的id,忽略原来的id,当然还要注意去改变原来集合需要维护的性质比如元素个数等等. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) m

poj 1962 Corporative Network(带权并查集)

题意: 在n个站点间建电线:两种操作: I a b表示以a为中心站点建线: E a表示查询以a站点为中心,相连的电线总长度: 思路: 带权并查集:中心站点就是父亲,电线长度为权值: #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int fa[500010],w[500010]; int t,n,m; char ch[5];

HDU 2784 Connections between cities 并查集+Online_LCA

模板攒起来 #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <stack> #pragma comment(linker, "/STACK:1024000000"); #define LL long long

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,