[PYTHON] AtCoder Beginner Contest 120 Review of past questions

Time required

スクリーンショット 2020-01-27 22.00.48.png

Impressions

D was union-find, I tried to understand it because I tried to use it without much understanding. I want to acquire enough knowledge to be comfortable with ABC.

Problem A

Think about how many times you can listen for b yen without exceeding c.

answerA.py


a,b,c=map(int,input().split())
print(min(c,b//a))

B problem

The entire search is performed in descending order from the smallest of a and b.

answerB.py


a,b,k=map(int,input().split())
check=0
m=min(a,b)
for i in range(m,0,-1):
    if a%i==0 and b%i==0:
        check+=1
        if check==k:
            print(i)
            break

C problem

If you think that it is a problem of the pattern that considers the consistency of the number of blue and red using the stack, all of them will eventually fall, so only the cubes of the same color will remain at the end.

answerC.py


s=input()
c=0
for i in s:
    c+=(i=="0")
    c-=(i=="1")
print(len(s)-abs(c))

D problem

It was really difficult ... Or rather, the algorithm power is not enough. UnionFindTree I think it's too inexperienced to understand. For the time being, I implemented UnionFindTree by trial and error after the virtual console (I referred to the following two articles [1], [2]), [3]. First, UnionFindTree is a data structure that determines whether two elements belong to the same set and merges different sets at high speed (I will not explain how it works, so comment out in the code and See the reference article.). Also important here is that ** UnionFindTree cannot be split **. This problem requires division (because the bridge collapses) and requires some ingenuity (as mentioned in the answer, but in general ** it is difficult to delete the graph. ** ). Here, in the final state, none of the bridges are connected **, and if you perform the reverse operation, bridges will be added one by one **, so you can go back and forth when adding bridges. You can find the answer by recording the changes in the number of islands. Specifically, if you add a bridge connecting vertices a and b, the number of elements of the disjoint set that includes those vertices is $ N_a, N_b $, and $ (N_a + N_b) \ times (N_a + N_b- 1) \ div 2 -N_a \ times (N_a-1) \ div 2-N_b \ times (N_b-1) \ div 2 $ will increase the number of island pairs that can be visited. So, if you implement it with care that you want ** the number of pairs of islands that can't go back and forth ** (compared to the initial state):

answerD.cc


#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
typedef long long ll;

//Reference: https://pyteyon.hatenablog.com/entry/2019/03/11/200000
//Reference: https://qiita.com/ofutonfuton/items/c17dfd33fc542c222396

class UnionFind {
public:
    vector<ll> parent; //parent[i]Is the parent of i
    vector<ll> siz; //An array representing the size of the disjoint sets(Initialize with 1)

    //Of the constructor:Behind is initializing member variables
    UnionFind(ll n):parent(n),siz(n,1){ //Initialize as everything is root at first
        for(ll i=0;i<n;i++){parent[i]=i;}
    }

    ll root(ll x){ //Recursively get the root of the tree to which the data x belongs
        if(parent[x]==x) return x;
        //The value of the assignment expression will be the value of the assigned variable!
        //Path compression(Streamline calculations by connecting elements directly to the roots)
        return parent[x]=root(parent[x]);
        //Update the parent when recursion
    }

    void unite(ll x,ll y){ //Merge x and y trees
        ll rx=root(x);//The root of x is rx
        ll ry=root(y);//y root ry
        if(rx==ry) return; //When in the same tree
        //Merge a small set into a large set(Merged from ry to rx)
        if(siz[rx]<siz[ry]) swap(rx,ry);
        siz[rx]+=siz[ry];
        parent[ry]=rx; //If x and y are not in the same tree, add y root ry to x root rx
    }

    bool same(ll x,ll y){//Returns whether the tree to which x and y belong is the same
        ll rx=root(x);
        ll ry=root(y);
        return rx==ry;
    }

    ll size(ll x){ //Disjoint size
        return siz[root(x)];
    }
};

signed main(){
    ll n,m;cin >> n >> m;
    vector< pair<ll,ll> > ab(m);
    for(ll i=m-1;i>=0;i--){cin >> ab[i].first >> ab[i].second;}
    UnionFind uf(n);
    vector<ll> ans(m);ans[0]=(n*(n-1))/2;//cout << ans[0] << endl;
    for(ll i=0;i<m-1;i++){
        //Updates are happening here at the same time
        ll a,b;a=ab[i].first-1;b=ab[i].second-1;
        if(uf.same(a,b)){
        ans[i+1]=ans[i];
        }else{
        ll sa,sb;sa=uf.size(a);sb=uf.size(b);
        ans[i+1]=ans[i]+(sa*(sa-1))/2+(sb*(sb-1))/2;
        uf.unite(a,b);sa=uf.size(a);
        ans[i+1]-=(sa*(sa-1))/2;
        }
    }
    for(ll i=m-1;i>=0;i--){
        cout << ans[i] << endl;
    }
}

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 045 Review of past questions
AtCoder Beginner Contest 120 Review of past questions
AtCoder Beginner Contest 108 Review of past questions
AtCoder Beginner Contest 106 Review of past questions
AtCoder Beginner Contest 122 Review of past questions
AtCoder Beginner Contest 125 Review of past questions
AtCoder Beginner Contest 109 Review of past questions
AtCoder Beginner Contest 118 Review of past questions
AtCoder Beginner Contest 080 Review of past questions