[PYTHON] AtCoder Beginner Contest 049 Review of past questions

Time required

スクリーンショット 2020-02-29 16.55.33.png

Impressions

No, I couldn't solve D and slept unfaithfully. When I got stuck, I just thought about it on my iPad and told myself many times, but I gave up again soon. Not at all. The person who solved it today was thinking about it for two hours, but it didn't become AC at all, so today is a bad day ... (If I was fluttering while studying abroad, I postponed the review for about five days.)

Problem A

I misspelled it.

answerA.py


a=["a","e","i","o","u"]
print("vowel" if input() in a else "consonant")

B problem

Just output the input twice.

answerB.py


h,w=map(int,input().split())
x=[input() for i in range(h)]
for i in range(h):
    print(x[i])
    print(x[i])

C problem

I found it quite difficult. First, I usually tried to separate from the front. However, I found it quite difficult to determine where to cut when cutting. Here, I decided to ** conversely think from behind **. Then, since the cutting method is always fixed to one, it can be said that S = T can be set if S can be made an empty string by separating the last part in order.

answerC.py


s=input()
l=len(s)
while s!=0:
    l=len(s)
    if s.rfind("dream")==l-5:
        s=s[:l-5]
    elif s.rfind("dreamer")==l-7:
        s=s[:l-7]
    elif s.rfind("erase")==l-5:
        s=s[:l-5]
    elif s.rfind("eraser")==l-6:
        s=s[:l-6]
    else:
        break
if len(s)==0:
    print("YES")
else:
    print("NO")

D problem

I don't understand why I can't solve it. It's too sweet, as my thinking ability decreases in proportion to time. In this problem, we want to consider cities that are connected and belong to the same group, so we come to the idea that we want to divide them into groups by Union Find. Here, ** grouping of cities connected by roads and grouping of cities connected by railroads (✳︎) ** can be easily done by creating Union Find and updating each root node. I can. However, it is not really independent, and you need to find out what is in the same group for both (✳︎). At this point, I came up with the idea that it is difficult to find both at the same time, so I should search for one at a time. In this case, it is difficult to think of it (I couldn't get out of the discussion here ... ** Image of writing down the necessary and sufficient conditions **). Now note that UnionFind allows you to number each group. Then you can see that each city has ** two numbers **. In other words, if you rephrase the condition that both (✳︎) are in the same group, the condition that both of the two numbers are the same **. Therefore, it can be easily implemented by saving the pair of two numbers in the dictionary with each grouping and counting the number of each pair.

answerD.cc


//Reference: http://ehafib.hatenablog.com/entry/2015/12/23/164517
//Include
#include<algorithm>//sort,Binary search,Such
#include<bitset>//Fixed length bit set
#include<cmath>//pow,log etc.
#include<complex>//Complex number
#include<deque>//Queue for double-ended access
#include<functional>//sort greater
#include<iomanip>//setprecision(Floating point output error)
#include<iostream>//Input / output
#include<map>//map(dictionary)
#include<numeric>//iota(Generation of integer sequence),gcd and lcm(c++17)
#include<queue>//queue
#include<set>//set
#include<stack>//stack
#include<string>//String
#include<unordered_map>//Map with iterator but not keeping order
#include<unordered_set>//Set with iterator but not keeping order
#include<utility>//pair
#include<vector>//Variable length array

using namespace std;
typedef long long ll;

//macro
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define ALL(x) (x).begin(),(x).end() //I want to omit arguments such as sort
#define FORALL(i,x) for(auto i=x.begin();i!=x.end();i++)
#define SIZE(x) ((ll)(x).size()) //size to size_Change from t to ll
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second


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,k,l;cin >> n >> k >> l;

    UnionFind uf1(n);
    vector< pair<ll,ll> > pq(k);
    REP(i,k){cin >> pq[i].F >> pq[i].S;pq[i].F-=1;pq[i].S-=1;}
    REP(i,k){uf1.unite(pq[i].F,pq[i].S);}

    UnionFind uf2(n);
    vector< pair<ll,ll> > rs(l);
    REP(i,l){cin >> rs[i].F >> rs[i].S;rs[i].F-=1;rs[i].S-=1;}
    REP(i,l){uf2.unite(rs[i].F,rs[i].S);}

    vector< pair<ll,ll> > num(n);
    REP(i,n){num[i].F=uf1.root(i);num[i].S=uf2.root(i);}
    map< pair<ll,ll>, ll> group;
    REP(i,n){
        if(group.find(num[i])==group.end()){
            group[num[i]]=1;
        }else{
            group[num[i]]+=1;
        }
    }
    REP(i,n){
        if(i!=n-1){
            cout << group[num[i]] << " ";
        }else{
            cout << group[num[i]] << endl;
        }
    }
}

Recommended Posts

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 127 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 105 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 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 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 121 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 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 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 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 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