[OJ]1004:Xi and Bo

2019-1-11 写技术


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.


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.

#include <stdio.h>

int uset[100];
void u_init(){
        int i;
        for(i=0; i<100; i++){
                uset[i] = -1;

int u_find(int x){
        int i = x;
        while(uset[i] >= 0){
                i = uset[i];
        return i;

void u_cat(int r1, int r2){
        if(r1 == r2){
        uset[r1] += uset[r2];
        uset[r2] = r1;

void main(){
        int t,from,to,lines, vertices;
        int m,v,rm,rv;

        scanf(" %d", &t);
                scanf(" %d %d", &from, &to);
                scanf(" %d", &lines);
                        scanf(" %d", &vertices);
                        v = -1;
                                m = v;
                                scanf(" %d", &v);
                                if(m >= 0){
                                        rm = u_find(m);
                                        rv = u_find(v);
                                        u_cat(rm, rv);
                rm = u_find(from);
                rv = u_find(to);
                if(rm == rv){

标签: C


Powered by anycle 湘ICP备15001973号