csuoj-1004-Xi and Bo

题目:

Description

Bo has been in Changsha for four years. However he spends most of his time staying his small dormitory. One day he decides to get out of the dormitory and see the beautiful city. So he asks to Xi to know whether he can get to another bus station from a bus station. Xi is not a good man because he doesn‘t tell Bo directly. He tells to Bo about some buses‘ routes. Now Bo turns to you and he hopes you to tell him whether he can get to another bus station from a bus station directly according to the Xi‘s information.

Input

The first line of the input contains a single integer T (0<T<30) which is the number of test cases. For each test case, the first contains two different numbers representing the starting station and the ending station that Bo asks. The second line is the number n (0<n<=50) of buses‘ routes which Xi tells. For each of the following n lines, the first number m (2<=m<= 100) which stands for the number of bus station in the bus‘ route. The remaining m numbers represents the m bus station. All of the bus stations are represented by a number, which is between 0 and 100.So you can think that there are only 100 bus stations in Changsha.

Output

For each test case, output the "Yes" if Bo can get to the ending station from the starting station by taking some of buses which Xi tells. Otherwise output "No". One line per each case. Quotes should not be included.

Sample Input

3
0 3
3
3 1 2 3
3 4 5 6
3 1 5 6
0 4
2
3 0 2 3
2 2 4
3 2
1
4 2 1 0 3

Sample Output

No
Yes
Yes
分析:1,初始化,每个节点单独作为一个集合,集合的代表元素为该节点;2,求出每条公交路线第一个站点的祖父,即其所在集合的代表元;3,求出该路线上的其他节点的祖父,将其父节点设置为步骤2中的代表元,即合并原本不相交的两个集合;4,根据祖父节点即所在集合的代表元是否相同来判断查询的两个公交站是否属于同一个集合。

代码:
#include<iostream>
using namespace std;
int node[101];
int findRoot(int temp){
    int root = node[temp];
    while(root != temp){
        temp = root;
        root = node[temp];
    }//while
    return root;
}
int main(){
    int t;
    cin >> t;
    while(t--){
        for(int i = 0;i <= 100;i++){
            node[i] = i;
        }//for
        int begin,end;
        cin >>begin >>end;
        int cnt;
        cin >>cnt;
        for(int i = 0;i < cnt;i++){
            int num;
            cin >>num;
            int first;
            cin >>first;
            int root = findRoot(first);
            for(int j = 1;j < num;j++){
                int temp;
                cin >>temp;
                int tempRoot = findRoot(temp);
                node[tempRoot] = root;
            }//for
        }//for
        int beginRoot = findRoot(begin);
        int endRoot = findRoot(end);
        if(beginRoot == endRoot) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
				
时间: 2024-10-14 00:16:44

csuoj-1004-Xi and Bo的相关文章

CSU 1004

1004: Xi and Bo Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 273  Solved: 93[Submit][Status][Web Board] Description Bo has been in Changsha for four years. However he spends most of his time staying his small dormitory. One day he decides to get ou

HDU 4864(多校)1004 Task

Problem Description Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task's level yi cannot complete this task. If the company comp

BZOJ 1004 Cards

Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给出了答案.进一步,小春要求染出Sr张红色,Sb张蓝色,Sg张绝色.他又询问有多少种方案,Sun想了一下,又给出了正确答案. 最后小春发明了M种不同的洗牌法,这里他又问Sun有多少种不同的染色方案.两种染色方法相同当且仅当其中一种可以通过任意的洗牌法(即可以使用多种洗牌法,而每种方法可以使用多次)洗成另一种.Sun发现这个问题有点难度,决

bzoj 1004 1004: [HNOI2008]Cards burnside定理

1004: [HNOI2008]Cards Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1668  Solved: 978[Submit][Status] Description 小 春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给出了答 案.进一步,小春要求染出Sr张红色,Sb张蓝色,Sg张绝色.他又询问有多少种方案,Sun想了一下,又给出了正确答案. 最

hdu 5762 Teacher Bo 曼哈顿路径

Teacher Bo Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1014    Accepted Submission(s): 561 Problem Description Teacher BoBo is a geography teacher in the school.One day in his class,he mar

Ural 1004 Sightseeing Trip

Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 100464-bit integer IO format: %lld      Java class name: (Any) There is a travel agency in Adelton town on Zanzibar island. It has decided to

【BZOJ】1004: [HNOI2008]Cards(置换群+polya+burnside)

http://www.lydsy.com/JudgeOnline/problem.php?id=1004 学习了下polya计数和burnside引理,最好的资料就是:<Pólya 计数法的应用> --陈瑜希 burnside: $$等价类的个数=\frac{1}{|G|}\sum_{i=1}^{s}D(a_i), a_i \in G$$其中$D(a_i)=a_i置换中染色后不变的方案$ 而polya: $$D(a_i)=k^{C(a_i)},其中C(a_i)是a_i的循环节个数$$证明很简单

【BZOJ】【1004】【HNOI2008】Cards

Burnside/Polya+背包DP 这道题目是等价类计数裸题吧……>_> 题解:http://m.blog.csdn.net/blog/njlcazl_11109/8316340 啊其实重点还是:找出每个置换下的不动点数目 这道题比较特殊,牌的数量是限定的,所以只能DP来搞……(dp[R][G][B]表示的是R张红牌,G张绿牌,B张蓝牌在当前这个置换下,有多少种方案是会置换回自身的) 恒等置换单独处理一下即可(其实就是总染色数,多重集排列数吧……$\frac{N!}{R!G!B!}$) 最

【BO】为WEBI报表添加自定义字体font

本篇主要讲解如何为sap business objects 的web intelligence报表组件新增字体.因为系统默认预设的字体对中文而言实在是太丑了,有的字体特喵的直接把中文变成方框框了! 一.系统环境: SAP BusinessObjects Business Intelligence (BI) 4.x Windows Server2008R2 示例字体:微软雅黑 二.操作步骤 1.首先要确保[微软雅黑]字体已经安装在Windows系统里了(“安装”指的是:ttf文件存放在这个目录下: