1118 Birds in Forest(25 分)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38677814/article/details/82082258

并查集入门,关键是想到并查集,我还想了其他操作好久发现没有什么想法
一个合并一个查找,就该想到并查集嘛
对于每一棵树上的鸟,可以将第一只作为已知在树上的鸟,把后面的鸟捉过来即可

#include <bits/stdc++.h>
using namespace std;
int fa[102000];
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
void Union(int a,int b){
    int fx=find(a);
    int fy=find(b);
    if(fx!=fy) fa[fy]=fx;
}
set<int>s;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=101000;i++) fa[i]=i;
    for(int i=1;i<=n;i++){
        int k;cin>>k;
        int boss;
        cin>>boss;
        s.insert(boss);
        for(int j=1;j<k;j++){
            int another_bird;
            cin>>another_bird;
            s.insert(another_bird); 
            Union(boss,another_bird);
        }
    }
    int ans=0;
    for(int i=1;i<=s.size();i++){
        if(find(i)==i) ans++;
    }
    printf("%d %d\n",ans,s.size());
    int q;
    cin>>q;
    while(q--){
        int x,y;
        cin>>x>>y;
        if(find(x)!=find(y)){
            printf("No\n");
        }
        else{
            printf("Yes\n");
        }
    }
    return 0;
}
展开阅读全文

Scaring the Birds

11-23

Problem DescriptionnIt’s harvest season now! nFarmer John plants a lot of corn. There are many birds living around his corn field. These birds keep stealing his corn all the time. John can't stand with that any more. He decides to put some scarecrows in the field to drive the birds away. nJohn's field can be considered as an N×N grid which has N×N intersections. John plants his corn on every intersection at first. But as time goes by, some corn were destroyed by rats or birds so some vacant intersections were left. Now John wants to put scarecrows on those vacant intersections and he can put at most one scarecrow on one intersection. Because of the landform and the different height of corn, every vacant intersections has a scaring range R meaning that if John put a scarecrow on it, the scarecrow can only scare the birds inside the range of manhattan distance R from the intersection.nn![](http://acm.hdu.edu.cn/data/images/4462-1.jpg)nnnThe figure above shows a 7×7 field. Assuming that the scaring range of vacant intersection (4,2) is 2, then the corn on the marked intersections can be protected by a scarecrow put on intersection (4,2). nNow John wants to figure out at least how many scarecrows he must buy to protect all his corn.n nnInputnThere are several test cases. nFor each test case: nThe first line is an integer N ( 2 <= N <= 50 ) meaning that John's field is an N×N grid. nThe second line is an integer K ( 0<= K <= 10) meaning that there are K vacant intersections on which John can put a scarecrow.nThe third line describes the position of K vacant intersections, in the format of r1,c1,r2,c2 …. rK,ck . (ri,ci) is the position of the i-th intersection and 1 <= r1,c1,r2,c2 …. rK,ck <= N. nThe forth line gives the scaring range of all vacant intersections, in the format of R1,R2…RK and 0 <= R1,R2…RK <= 2 × N. nThe input ends with N = 0.n nnOutputnFor each test case, print the minimum number of scarecrows farmer John must buy in a line. If John has no way to protect all the corn, print -1 instead.n nnSample Inputn4n2n2 2 3 3n1 3n4n2n2 2 3 3n1 4n0n nnSample Outputn-1n1 问答

100分求教浙大1118代码

12-06

BackgroundrnrnAn n-tersection is defined as a location in n-dimensional space, n being a positive integer, having all non-negative integer coordinates. For example, the location (1,2,3) represents an n-tersection in three dimensional space. Two n-tersections are said to be adjacent if they have the same number of dimensions and their coordinates differ by exactly 1 in a single dimension only. For example, (1,2,3) is adjacent to (0,2,3) and (2,2,3) and (1,2,4), but not to (2,3,3) or (3,2,3) or (1,2). An n-teresting space is defined as a collection of paths between adjacent n-tersections.rnrnFinally, an n-credible maze is defined as an n-teresting space combined with two specific n-tersections in that space, one of which is identified as the starting n-tersection and the other as the ending n-tersection.rnrnrnInputrnrnThe input file will consist of the descriptions of one or more n-credible mazes. The first line of the description will specify n, the dimension of the n-teresting space. (For this problem, n will not exceed 10, and all coordinate values will be less than 10.) The next line will contain 2n non-negative integers, the first n of which describe the starting n-tersection, least dimension first, and the next n of which describe the ending n-tersection. Next will be a nonnegative number of lines containing 2n non-negative integers each, identifying paths between adjacent n-tersections in the n-teresting space. The list is terminated by a line containing only the value �C1. Several such maze descriptions may be present in the file. The end of the input is signalled by space dimension of zero. No further data will follow this terminating zero.rnrnrnOutputrnrnFor each maze output it��s position in the input; e.g. the first maze is ��Maze #1��, the second is ��Maze #2��, etc. If it is possible to travel through the n-credible maze��s n-teresting space from the starting n-tersection to the ending n-tersection, also output ��can be travelled�� on the same line. If such travel is not possible, output ��cannot be travelled�� instead.rnrnrnExamplernrnInputrnrnrnrn2 rn0 0 2 2rn0 0 0 1rn0 1 0 2rn0 2 1 2rn1 2 2 2rn-1rn3rn1 1 1 1 2 3rn1 1 2 1 1 3rn1 1 3 1 2 3rn1 1 1 1 1 0rn1 1 0 1 0 0rn1 0 0 0 0 0rn-1rn0rnOutputrnrnMaze #1 can be travelledrnMaze #2 cannot be travelled rnrn 论坛

没有更多推荐了,返回首页