Submission #311859


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class Myon
{
    public Myon() { }
    public static int Main()
    {

        rnd = new XRand();
        new Myon().calc();
        return 0;
    }
    static XRand rnd;


    void calc()
    {
        Scanner cin = new Scanner();
        int N = cin.nextInt();
        int M = cin.nextInt();
        int[] a = new int[M];
        int[] b = new int[M];

        for (int i = 0; i < M; i++)
        {
            a[i] = cin.nextInt() - 1;
            b[i] = cin.nextInt() - 1;
        }

        init(N);
        for (int i = 0; i < M; i++)
        {
            connect(a[i], b[i]);
        }

        int ret = 0;
        for (int i = 0; i < N; i++)
        {
            if (getuni(i) == i) ret++;
        }
        Console.WriteLine(ret - 1);
    }

    int[] uni;

    void init(int N)
    {
        uni = new int[N];
        for (int i = 0; i < N; i++)
        {
            uni[i] = -1;
        }
    }

    int getuni(int a)
    {
        if (uni[a] < 0) return a;
        else return uni[a] = getuni(uni[a]);
    }

    bool connect(int a, int b)
    {
        a = getuni(a);
        b = getuni(b);
        if (a == b) return false;
        uni[a] = b;
        return true;
    }

}




class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        do
        {
            s = Console.ReadLine().Split(cs, StringSplitOptions.RemoveEmptyEntries);
        } while ((s.Length == 1 && s[0] == "") || s.Length == 0);
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

}

class XRand
{
    uint x, y, z, w;


    public XRand()
    {
        init();
    }

    public XRand(uint s)
    {
        init();
        init_xor128(s);
    }

    void init()
    {
        x = 314159265; y = 358979323; z = 846264338; w = 327950288;

    }

    public void init_xor128(uint s)
    {
        z ^= s;
        z ^= z >> 21; z ^= z << 35; z ^= z >> 4;
        z *= 736338717;
    }

    uint next()
    {
        uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
    }

    public long nextLong(long m)
    {
        return (long)((((ulong)next() << 32) + next()) % (ulong)m);
    }

    public int nextInt(int m)
    {
        return (int)(next() % m);
    }

    public long nextLong(long min, long max)
    {
        return min + nextLong(max - min + 1);
    }

    public int nextInt(int min, int max)
    {
        return min + nextInt(max - min + 1);
    }

    public int nextIntP(int a)
    {
        return (int)Math.Pow(a, nextDouble());
    }

    public int nextIntP(int min, int max)
    {
        int diff = max - min;
        return min + nextIntP(diff + 2) - 1;
    }

    public long nextLongP(long a)
    {
        return (long)Math.Pow(a, nextDouble());
    }

    public long nextLongP(long min, long max)
    {
        long diff = max - min;
        return min + nextLongP(diff + 2) - 1;
    }


    public double nextDouble()
    {
        return (double)next() / uint.MaxValue;
    }

    public double nextDoubleP(double a)
    {
        return Math.Pow(a, nextDouble());
    }
}

Submission Info

Submission Time
Task B - 道路工事
User chokudai
Language C# (Mono 2.10.8.1)
Score 100
Code Size 3740 Byte
Status AC
Exec Time 220 ms
Memory 9496 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 20
Set Name Test Cases
Sample sample1.txt, sample2.txt
All 0.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt
Case Name Status Exec Time Memory
0.txt AC 116 ms 7748 KB
1.txt AC 107 ms 7708 KB
10.txt AC 109 ms 7736 KB
11.txt AC 107 ms 7780 KB
12.txt AC 107 ms 7740 KB
13.txt AC 105 ms 7744 KB
14.txt AC 108 ms 7736 KB
15.txt AC 193 ms 9020 KB
16.txt AC 107 ms 8172 KB
17.txt AC 107 ms 8128 KB
18.txt AC 106 ms 8100 KB
19.txt AC 220 ms 9496 KB
2.txt AC 106 ms 7748 KB
3.txt AC 119 ms 7740 KB
4.txt AC 105 ms 7748 KB
5.txt AC 108 ms 7708 KB
6.txt AC 105 ms 7748 KB
7.txt AC 106 ms 7748 KB
8.txt AC 107 ms 7748 KB
9.txt AC 115 ms 7744 KB
sample1.txt AC 104 ms 7704 KB
sample2.txt AC 105 ms 7748 KB