Skip to content

Instantly share code, notes, and snippets.

@paralleltree
Created April 2, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paralleltree/f0ed703d590fd9fc5913 to your computer and use it in GitHub Desktop.
Save paralleltree/f0ed703d590fd9fc5913 to your computer and use it in GitHub Desktop.
学内ミニプロコン(15/04/02)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
// ref: https://github.com/OCTPC/miniprocon/blob/master/miniprocon003/regulation.md
int n, m;
List<int>[] g;
bool[] visit;
int Solve(System.IO.StringReader stream)
{
string[] str = stream.ReadLine().Split(' ');
n = int.Parse(str[0]);
m = int.Parse(str[1]);
g = new List<int>[n];
for (int i = 0; i < n; i++)
g[i] = new List<int>();
for (int i = 0; i < m; i++)
{
string[] s = stream.ReadLine().Split(' ');
int a = int.Parse(s[0]) - 1;
int b = int.Parse(s[1]) - 1;
g[a].Add(b);
g[b].Add(a);
}
visit = new bool[n];
int sum = 0;
for (int i = 0; i < n; i++)
{
if (visit[i]) continue;
dfs(i);
sum++;
}
return sum - 1;
}
void dfs(int x)
{
visit[x] = true;
foreach (int i in g[x])
if (!visit[i]) dfs(i);
}
static void Main(string[] args)
{
Console.WriteLine(new Program().Solve(new System.IO.StringReader(System.IO.File.ReadAllText(args[0]))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment