Submission #16319699


Source Code Expand

// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
    auto input = stdin.byLine.map!split.joiner;

    long N;
    N = input.front.to!long;
    input.popFront;

    long M;
    M = input.front.to!long;
    input.popFront;

    long[] A = new long[](cast(size_t)(M));
    long[] B = new long[](cast(size_t)(M));
    foreach (i; 0 .. cast(size_t)(M))
    {
        A[i] = input.front.to!long;
        input.popFront;
        B[i] = input.front.to!long;
        input.popFront;
    }

    solve(N, M, A, B);
}

void solve(long N, long M, long[] A, long[] B)
{
    auto uf = UnionFind(N);
    A[] -= 1;
    B[] -= 1;
    foreach (i; 0 .. M)
    {
        uf.unite(A[i], B[i]);
    }
    long ans;
    foreach (i; 0 .. N)
    {
        ans = ans.max(uf.size(i));
    }
    writeln(ans);
}

struct UnionFind
{
    alias T = long;
    T[] rank, parent, sizes;
    this(T n)
    {
        rank = new T[](n);
        sizes = new T[](n);
        sizes[] = 1;
        parent = iota!T(n).array();
    }

    T find(T x)
    {
        return (this.parent[x] == x) ? x : (this.parent[x] = this.find(this.parent[x]));
    }

    void unite(T x, T y)
    {
        T rx = this.find(x), ry = this.find(y);
        if (rx == ry)
            return;
        this.sizes[rx] = this.sizes[ry] = this.sizes[rx] + this.sizes[ry];
        if (this.rank[rx] == this.rank[ry])
            this.parent[rx] = ry, this.rank[ry]++;
        else if (this.rank[rx] < this.rank[ry])
            this.parent[rx] = ry;
        else
            this.parent[ry] = rx;
    }

    T size(T x)
    {
        return this.sizes[this.find(x)];
    }
}

Submission Info

Submission Time
Task D - Friends
User kotet
Language D (DMD 2.091.0)
Score 400
Code Size 2111 Byte
Status AC
Exec Time 178 ms
Memory 25704 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 29
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_11.txt, random_12.txt, random_13.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_31.txt, random_32.txt, random_33.txt, random_41.txt, random_42.txt, random_43.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 14 ms 8160 KiB
hand_02.txt AC 2 ms 3652 KiB
hand_03.txt AC 2 ms 3584 KiB
random_01.txt AC 172 ms 18624 KiB
random_02.txt AC 34 ms 9272 KiB
random_03.txt AC 130 ms 18168 KiB
random_04.txt AC 35 ms 9484 KiB
random_05.txt AC 133 ms 18220 KiB
random_06.txt AC 109 ms 17736 KiB
random_07.txt AC 116 ms 23084 KiB
random_08.txt AC 21 ms 7564 KiB
random_11.txt AC 119 ms 16784 KiB
random_12.txt AC 173 ms 19552 KiB
random_13.txt AC 120 ms 17168 KiB
random_21.txt AC 163 ms 19804 KiB
random_22.txt AC 160 ms 19760 KiB
random_23.txt AC 160 ms 19856 KiB
random_24.txt AC 162 ms 19828 KiB
random_25.txt AC 170 ms 19852 KiB
random_26.txt AC 159 ms 19804 KiB
random_31.txt AC 151 ms 25704 KiB
random_32.txt AC 148 ms 15108 KiB
random_33.txt AC 150 ms 15332 KiB
random_41.txt AC 177 ms 19808 KiB
random_42.txt AC 178 ms 19948 KiB
random_43.txt AC 177 ms 19712 KiB
sample_01.txt AC 3 ms 3692 KiB
sample_02.txt AC 2 ms 3592 KiB
sample_03.txt AC 2 ms 3692 KiB